My old project for strange attractors
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
This repo is archived. You can view files and clone it, but cannot push or open issues/pull-requests.
 
 
 

161 lines
4.2 KiB

/*
* AttractorKernel.cpp
* AwesomeAttractor
*
* Created by Joshua Moerman on 07-08-10.
* Copyright 2010 Rodo. All rights reserved.
*
*/
#include "AttractorKernel.hpp"
#include <algorithm>
#include <iostream>
#pragma mark -
#pragma mark memory
AttractorKernel::AttractorKernel(const unsigned int dimension, const unsigned int numberOfParameters) :
numberOfParameters(numberOfParameters), dimension(dimension){
try {
allocate();
}
catch (std::exception& e) {
std::cout << "Couldn't construct AttractorKernel: " << e.what() << std::endl;
dealloc();
}
std::fill_n(parameters, numberOfParameters, 0.0);
std::fill_n(vectorNew, dimension, 0.0);
std::fill_n(vectorOld, dimension, 0.0);
}
AttractorKernel::~AttractorKernel(){
dealloc();
}
void AttractorKernel::allocate(){
parameters = new double[numberOfParameters];
vectorNew = new double[dimension];
vectorOld = new double[dimension];
}
void AttractorKernel::dealloc(){
delete[] vectorOld;
vectorOld = NULL;
delete[] vectorNew;
vectorNew = NULL;
delete[] parameters;
parameters = NULL;
}
#pragma mark -
#pragma mark parameters
// NOTE: inlining these functions (with the keyword inline) improves performance by at most 1% (tested)
double & AttractorKernel::operator[](const unsigned int index){
return parameters[index];
}
double const & AttractorKernel::operator[](const unsigned int index) const{
return parameters[index];
}
unsigned int AttractorKernel::getNumberOfParameters() const{
return numberOfParameters;
}
#pragma mark -
#pragma mark vector
double const * AttractorKernel::vector() const{
return vectorNew;
}
double const * AttractorKernel::previousVector() const{
return vectorOld;
}
unsigned int AttractorKernel::getDimension() const{
return dimension;
}
#pragma mark -
#pragma mark factory function
#include "AttractorKernel.hpp"
#include "kernels/Logistic.hpp"
#include "kernels/Lorenz3D.hpp"
#include "kernels/Polynomial.hpp"
#include "kernels/PolynomialA3D.hpp"
#include "kernels/Unravel3D.hpp"
AttractorKernel * AttractorKernel::createAttractorKernel(stfu::node& attractor){
AttractorKernel * myAttractor = NULL;
// reading basic stuff
const std::string attractorType = attractor.getValue("type");
const std::string attractorDimension = attractor.getValue("dimensions");
// for ( unsigned int i = 0; attractorType[i] != '\0'; i++ ) {
// attractorType[i] = tolower(attractorType[i]);
// }
const unsigned int dimension = atoi(attractorDimension.c_str());
std::cout << " Formula: " << attractorType << std::endl;
std::cout << " Dimensions: " << dimension << std::endl;
// depending on type, make the formula object
if ( attractorType == "lorenz" ){
if ( dimension == 3 ) {
myAttractor = new Lorenz3D();
} else {
std::cerr << "something wrong";
exit(37);
}
} else if ( attractorType == "polynomial" ) {
const std::string attractorOrde = attractor.getValue("orde");
const unsigned int orde = atoi(attractorOrde.c_str());
std::cout << " Orde: " << orde << std::endl;
myAttractor = new Polynomial(dimension, orde);
} else if ( attractorType == "polynomial a" ) {
if ( dimension == 3 ) {
myAttractor = new PolynomialA3D();
} else {
std::cerr << "something wrong";
exit(37);
}
} else if ( attractorType == "logistic" ) {
myAttractor = new Logistic(dimension);
} else if ( attractorType == "unravel" ) {
if ( dimension == 3 ) {
myAttractor = new Unravel3D();
} else {
std::cerr << "something wrong";
exit(37);
}
} else {
std::cout << "'" << attractorType << "' not recognized" << std::endl;
exit(3);
}
// read parameters
const unsigned int numberOfParameters = myAttractor->getNumberOfParameters();
for ( unsigned int i = 0; i < numberOfParameters; i++ ) {
stfu::node attractorParameters = attractor.getChild("parameters");
(*myAttractor)[i] = atof(attractorParameters.getValue(i).c_str());
std::cout << " Parameter " << i << " set to " << (*myAttractor)[i] << ", ";
}
std::cout << std::endl << " Reading file complete" << std::endl;
return myAttractor;
}