1
Fork 0

added basic stf support, made attractor class as it should (not fully implemented yet)

This commit is contained in:
Joshua 2010-04-02 17:03:58 +02:00
parent f754b83642
commit b056e48cff
18 changed files with 174 additions and 134 deletions

View file

@ -1,98 +1,91 @@
#include <cmath>
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <iomanip>
#include <cassert>
#include <string>
using namespace std;
#include "Attractor.hpp"
using namespace std;
/*
Constructors & initialisers
*/
Attractor::Attractor():
dim(3), par(4), formula(LORENZ), param(NULL), point(NULL), new_point(NULL) {
// Default attractor: 3D Lorenz attrractor
Attractor::Attractor() {
param[0] = 0.001; //dt
param[1] = 10; //sigma
param[2] = 28; //rho
param[3] = 8.0/3.0; //beta
myAttractor = new Lorenz3D();
}
Attractor::Attractor(const char* const filename) {
ifstream file(filename);
Attractor::Attractor(const char* const fileName) {
ifstream file(fileName);
cout << "Reading file " << filename << "..." << endl;
cout << "Reading file " << fileName << "..." << endl;
if ( !file ) {
cout << " Error reading file '" << filename << "' dying now..." << endl;
cerr << " Error reading file '" << fileName << "' dying now..." << endl;
exit(2);
}
// TODO : Use stfu
/*
file.attr:
lorenz
3
0
stfu::node system;
system.read(file);
stfu::node attractor = system.getChild("attractor");
3.24454
1.25
....
*/
string attractorType = attractor.getValue("type");
const string attractorDimension = attractor.getValue("dimensions");
string fileFormula;
file >> fileFormula;
for ( unsigned int i = 0; fileFormula[i] != '\0'; i++ ) {
fileFormula[i] = tolower(fileFormula[i]);
for ( unsigned int i = 0; attractorType[i] != '\0'; i++ ) {
attractorType[i] = tolower(attractorType[i]);
}
const unsigned int dimension = atoi(attractorDimension.c_str());
unsigned int fileDim;
file >> fileDim;
cout << " Formula: " << attractorType << endl;
cout << " Dimensions: " << dimension << endl;
unsigned int fileOrde;
file >> fileOrde;
if ( attractorType == "lorenz" ){
if ( dimension == 3 ) {
myAttractor = new Lorenz3D();
} else {
cerr << "something wrong";
exit(37);
}
} else if ( attractorType == "polynomial" ) {
const string attractorOrde = attractor.getValue("orde");
const unsigned int orde = atoi(attractorOrde.c_str());
cout << " Orde: " << orde << endl;
myAttractor = new Polynomial(dimension, orde);
cout << " Formula: " << fileFormula << endl;
cout << " Dimensions: " << fileDim << endl;
cout << " Orde: " << fileOrde << endl;
} else if ( attractorType == "polynomial a" ) {
if ( dimension == 3 ) {
myAttractor = new PolynomialA3D();
} else {
cerr << "something wrong";
exit(37);
}
} else if ( attractorType == "logistic" ) {
myAttractor = new Logistic(dimension);
if ( fileFormula == "lorenz" )
init(fileDim, LORENZ, fileOrde);
else if ( fileFormula == "poly_n" )
init(fileDim, POLY_N, fileOrde);
else if ( fileFormula == "poly_a" )
init(fileDim, POLY_A, fileOrde);
else if ( fileFormula == "logistic" )
init(fileDim, LOGISTIC, fileOrde);
else if ( fileFormula == "unravel" )
init(fileDim, UNRAVEL, fileOrde);
else {
cout << " Formula not (yet) supported" << endl;
} else if ( attractorType == "unravel" ) {
if ( dimension == 3 ) {
myAttractor = new Unravel3D();
} else {
cerr << "somtheing wrong";
exit(37);
}
} else {
cout << "'" << attractorType << "' not recognized" << endl;
exit(3);
}
for ( unsigned int i = 0; i < par; i++ ) {
file >> param[i];
cout << " Parameter " << i << " set to " << param[i] << ", ";
unsigned int numberOfParameters = myAttractor->getNumberOfParameters();
double * & parameters = myAttractor->parameters();
for ( unsigned int i = 0; i < numberOfParameters; i++ ) {
stfu::node attractorParameters = attractor.getChild("parameters");
parameters[i] = atof(attractorParameters.getValue(i).c_str());
cout << " Parameter " << i << " set to " << parameters[i] << ", ";
}
cout << endl << " Reading file complete" << endl;
}
void Attractor::init(unsigned int dim_in, FormulaChoice formula_in, unsigned int orde_in) {
}
void Attractor::init_range() {
/*
// stabilize attractor
for ( unsigned int i = 0; i < 100000; i++ ) {
iterate();
@ -125,6 +118,7 @@ void Attractor::init_range() {
for ( vector<Projector*>::iterator it = projectors.begin(); it != projectors.end(); it++ ) {
(*it)->finish_range();
}
*/
}
bool Attractor::is_chaos() {
@ -134,10 +128,10 @@ bool Attractor::is_chaos() {
Single point attractor
Lyapunov exponent
*/
/*
double sum = 0;
for ( unsigned int i = 0; i < dim; i++ ) {
const double dist = new_point[i] - point[i];
const double dist = 0; //new_point[i] - point[i];
sum += dist*dist;
}
if ( sum >= 1.0e7 ) {
@ -149,17 +143,17 @@ bool Attractor::is_chaos() {
return false;
}
return true;
*/
return true;
}
/*
Iteration & Math
*/
void Attractor::iterate() {
// attractorKernel->iterate()
myAttractor->iterate();
}
void Attractor::plot() {
for ( vector<Projector *>::iterator it = projectors.begin(); it != projectors.end(); it++ ) {
const double * point = myAttractor->vector();
(*it)->plot(point);
}
}
@ -169,8 +163,13 @@ void Attractor::plot() {
IO & control
*/
void Attractor::output() {
for ( unsigned int i = 0; i < dim; i++ ) {
const unsigned int* dim = (unsigned int*)myAttractor->getProperty("dimension");
const double * point = myAttractor->vector();
for ( unsigned int i = 0; i < *dim; i++ ) {
cout << point[i] << " ";
}
cout << endl;
delete dim;
}

View file

@ -1,16 +1,21 @@
#ifndef ATTRACTOR_HPP
#define ATTRACTOR_HPP
#include <iostream>
#include <fstream>
#include <cstdlib>
#include <string>
#include <vector>
//#include "Vector.hpp"
//#include "Parameters.hpp"
#include "stfu/stf.hpp"
#include "Projector.hpp"
#include "AttractorKernel.hpp"
enum FormulaChoice {
POLY_N, LORENZ, POLY_A, POLY_2, LOGISTIC, UNRAVEL
};
#include "kernels/Logistic.hpp"
#include "kernels/Lorenz3D.hpp"
#include "kernels/Polynomial.hpp"
#include "kernels/PolynomialA3D.hpp"
#include "kernels/Unravel3D.hpp"
class Projector;
@ -18,23 +23,16 @@ class Projector;
class Attractor {
public:
unsigned int dim;
unsigned int par;
unsigned int orde;
FormulaChoice formula;
double * param;
double * point;
double * new_point;
AttractorKernel * myAttractor;
vector<Projector *> projectors;
std::vector<Projector *> projectors;
public:
Attractor();
Attractor(const char* const filename);
void init(unsigned int dimensions, FormulaChoice formula, unsigned int orde);
//void init(unsigned int dimensions, FormulaChoice formula, unsigned int orde);
void init_range();
@ -42,8 +40,6 @@ public:
// TODO : lyapunov exponent uit rekenen
bool is_chaos();
// TODO : optimaliseren voor lage graads veeltermen
void iterate();
void plot();

View file

@ -14,6 +14,8 @@ class AttractorKernel {
virtual double& parameter(const unsigned int index) = 0;
virtual double*& parameters() = 0;
virtual unsigned int getNumberOfParameters() = 0;
// get properties of the attractor
// such as the dimension
// you should delete the void pointer if you used it

View file

@ -43,9 +43,8 @@
<Unit filename="Projector.hpp">
<Option compilerVar="CC" />
</Unit>
<Unit filename="attr/test.attr" />
<Unit filename="attr/test2.attr" />
<Unit filename="attr/waardes.txt" />
<Unit filename="kernels/Logistic.cpp" />
<Unit filename="kernels/Logistic.hpp" />
<Unit filename="kernels/Lorenz3D.cpp" />
<Unit filename="kernels/Lorenz3D.hpp" />
<Unit filename="kernels/Polynomial.cpp" />
@ -58,6 +57,8 @@
<Unit filename="myMath.hpp" />
<Unit filename="pngwriter/pngwriter.cc" />
<Unit filename="pngwriter/pngwriter.h" />
<Unit filename="stfu/stf.cpp" />
<Unit filename="stfu/stf.hpp" />
<Extensions>
<code_completion />
<envvars />

View file

@ -78,14 +78,14 @@ void Projector::finish_range() {
}
void Projector::project(double * point) {
void Projector::project(const double * point) {
assert(extern_dim >= 2);
project_point[0] = point[0];
project_point[1] = point[1];
}
void Projector::plot(double * point) {
void Projector::plot(const double * point) {
project(point);
const double x = project_point[0]*factor + offset[0];

View file

@ -35,8 +35,8 @@ class Projector{
-genormalizeerde coordinaten als kleurintensiteit (gebruikt fp canvas)
-kleurbanden, dus met een periodieke functie (gebruikt int canvas)
*/
void project(double * point);
void plot(double * point);
void project(const double * point);
void plot(const double * point);
void output();
};

View file

@ -83,6 +83,10 @@ double & Logistic::parameter(const unsigned int index) {
return myParameters[index];
}
unsigned int Logistic::getNumberOfParameters() {
return dimension;
}
double * & Logistic::vector() {
return vectorNew;
}

View file

@ -5,7 +5,7 @@
#include "../AttractorKernel.hpp"
class Logistic : AttractorKernel {
class Logistic : public AttractorKernel {
double * myParameters;
double * vectorNew;
@ -25,6 +25,8 @@ class Logistic : AttractorKernel {
virtual double& parameter(const unsigned int index);
virtual double*& parameters();
virtual unsigned int getNumberOfParameters();
// get properties of the attractor
// such as the dimension
// you should delete the void pointer if you used it

View file

@ -1,29 +1,32 @@
#include "Lorenz3D.hpp"
Lorenz::Lorenz() {
const static unsigned int dimension = 3;
const static unsigned int numberOfParameters = 4;
Lorenz3D::Lorenz3D() {
init();
}
void Lorenz::init() {
void Lorenz3D::init() {
// allocation
myParameters = new double[4];
vectorNew = new double[3];
vectorOld = new double[3];
myParameters = new double[numberOfParameters];
vectorNew = new double[dimension];
vectorOld = new double[dimension];
// initialisation
assert(myParameters != NULL);
assert(vectorNew != NULL);
assert(vectorOld != NULL);
for ( unsigned int i = 0; i < 4; i++ ) {
for ( unsigned int i = 0; i < numberOfParameters; i++ ) {
myParameters[i] = 0.0;
}
for ( unsigned int i = 0; i < 3; i++ ) {
for ( unsigned int i = 0; i < dimension; i++ ) {
vectorNew[i] = vectorOld[i] = 1.0;
}
}
// the main function
void Lorenz::iterate() {
void Lorenz3D::iterate() {
swap(vectorNew, vectorOld);
vectorNew[0] = vectorOld[0] + myParameters[0] * myParameters[1] * (vectorOld[1] - vectorOld[0]);
@ -41,32 +44,36 @@ void Lorenz::iterate() {
*/
// setters, getters, all i/o to other classes/objects
void * Lorenz::getProperty(const string identifier) {
void * Lorenz3D::getProperty(const string identifier) {
if ( identifier == "dimension" ) {
unsigned int * _return = new unsigned int;
*_return = 3;
*_return = dimension;
return _return;
}
return NULL;
}
void Lorenz::setProperty(const string identifier, const void * value) {
void Lorenz3D::setProperty(const string identifier, const void * value) {
return;
}
double * & Lorenz::parameters() {
double * & Lorenz3D::parameters() {
return myParameters;
}
double & Lorenz::parameter(const unsigned int index) {
double & Lorenz3D::parameter(const unsigned int index) {
return myParameters[index];
}
double * & Lorenz::vector() {
unsigned int Lorenz3D::getNumberOfParameters() {
return numberOfParameters;
}
double * & Lorenz3D::vector() {
return vectorNew;
}
double * & Lorenz::previousVector() {
double * & Lorenz3D::previousVector() {
return vectorOld;
}

View file

@ -9,7 +9,7 @@ using namespace std;
#include "../AttractorKernel.hpp"
class Lorenz : public AttractorKernel {
class Lorenz3D : public AttractorKernel {
double * myParameters;
@ -20,14 +20,16 @@ class Lorenz : public AttractorKernel {
public:
Lorenz();
Lorenz(const unsigned int dimensions);
Lorenz3D();
Lorenz3D(const unsigned int dimensions);
// parameters are stored in a array of doubles
// if you want to use other types, use the properties
virtual double& parameter(const unsigned int index);
virtual double*& parameters();
virtual unsigned int getNumberOfParameters();
// get properties of the attractor
// such as the dimension
// you should delete the void pointer if you used it

View file

@ -128,6 +128,10 @@ double & Polynomial::parameter(const unsigned int index) {
return myParameters[index];
}
unsigned int Polynomial::getNumberOfParameters() {
return numberOfParameters;
}
double * & Polynomial::vector() {
return vectorNew;
}

View file

@ -30,6 +30,8 @@ class Polynomial : public AttractorKernel {
virtual double& parameter(const unsigned int index);
virtual double*& parameters();
virtual unsigned int getNumberOfParameters();
// get properties of the attractor
// such as the dimension
// you should delete the void pointer if you used it

View file

@ -68,6 +68,10 @@ double & PolynomialA3D::parameter(const unsigned int index) {
return myParameters[index];
}
unsigned int PolynomialA3D::getNumberOfParameters() {
return numberOfParameters;
}
double * & PolynomialA3D::vector() {
return vectorNew;
}

View file

@ -24,6 +24,8 @@ class PolynomialA3D : public AttractorKernel {
virtual double& parameter(const unsigned int index);
virtual double*& parameters();
virtual unsigned int getNumberOfParameters();
// get properties of the attractor
// such as the dimension
// you should delete the void pointer if you used it

View file

@ -1,28 +1,31 @@
#include "Unravel3D.hpp"
Unravel::Unravel(){
const static unsigned int dimension = 3;
const static unsigned int numberOfParameters = 7;
Unravel3D::Unravel3D(){
init();
}
void Unravel::init() {
void Unravel3D::init() {
// allocation
myParameters = new double[4];
vectorNew = new double[3];
vectorOld = new double[3];
myParameters = new double[numberOfParameters];
vectorNew = new double[dimension];
vectorOld = new double[dimension];
// initialisation
assert(myParameters != NULL);
assert(vectorNew != NULL);
assert(vectorOld != NULL);
for ( unsigned int i = 0; i < 7; i++ ) {
for ( unsigned int i = 0; i < numberOfParameters; i++ ) {
myParameters[i] = 0.0;
}
for ( unsigned int i = 0; i < 3; i++ ) {
for ( unsigned int i = 0; i < dimension; i++ ) {
vectorNew[i] = vectorOld[i] = 0.0;
}
}
void Unravel::iterate() {
void Unravel3D::iterate() {
swap(vectorNew, vectorOld);
vectorNew[0] = myParameters[0]*(vectorOld[2] + myParameters[1]);
@ -41,31 +44,35 @@ void Unravel::iterate() {
}
// setters, getters, all i/o to other classes/objects
void * Unravel::getProperty(const string identifier) {
void * Unravel3D::getProperty(const string identifier) {
if ( identifier == "dimension" ) {
unsigned int * _return = new unsigned int;
*_return = 3;
*_return = dimension;
return _return;
}
return NULL;
}
void Unravel::setProperty(const string identifier, const void * value) {
void Unravel3D::setProperty(const string identifier, const void * value) {
return;
}
double * & Unravel::parameters() {
double * & Unravel3D::parameters() {
return myParameters;
}
double & Unravel::parameter(const unsigned int index) {
double & Unravel3D::parameter(const unsigned int index) {
return myParameters[index];
}
double * & Unravel::vector() {
unsigned int Unravel3D::getNumberOfParameters() {
return numberOfParameters;
}
double * & Unravel3D::vector() {
return vectorNew;
}
double * & Unravel::previousVector() {
double * & Unravel3D::previousVector() {
return vectorOld;
}

View file

@ -10,7 +10,7 @@ using namespace std;
#include "../AttractorKernel.hpp"
class Unravel : public AttractorKernel {
class Unravel3D : public AttractorKernel {
double * myParameters;
@ -21,14 +21,16 @@ class Unravel : public AttractorKernel {
public:
Unravel();
Unravel(const unsigned int dimensions);
Unravel3D();
Unravel3D(const unsigned int dimensions);
// parameters are stored in a array of doubles
// if you want to use other types, use the properties
virtual double& parameter(const unsigned int index);
virtual double*& parameters();
virtual unsigned int getNumberOfParameters();
// get properties of the attractor
// such as the dimension
// you should delete the void pointer if you used it

View file

@ -11,9 +11,9 @@ using namespace std;
// TODO : Allemaal files inlezen, voor makkelijker gebruik
int main(int argc, char *argv[]) {
/*
AttractorKernel * mijnAttractortje;
mijnAttractortje = new Lorenz();
mijnAttractortje = new Lorenz3D();
AttractorKernel &attractor = *mijnAttractortje;
attractor.parameter(0) = 1.0;
@ -35,7 +35,13 @@ int main(int argc, char *argv[]) {
}
cout << endl;
}
*/
Attractor myAttractor("attractors/testAttractor.stf");
for ( unsigned int i = 0; i < 20; i++ ) {
myAttractor.iterate();
myAttractor.output();
}
/*if ( argc <= 2 ) {
cout << endl << "nothing to do..." << endl;

View file

@ -280,9 +280,9 @@ namespace stfu {
bool read(istream &in);
/**
Reads the STF from a file
Reads the STF from a file
\return Returns whether it was succesful
\return Returns whether it was succesful
*/
bool read(const char *filename);