Browse Source

removed old files, added command line arguments

master
Joshua Moerman 14 years ago
parent
commit
cc018547f0
  1. 6
      Attractor.cpp
  2. 3
      Attractor.hpp
  3. 110
      Parameters.cpp
  4. 34
      Parameters.hpp
  5. 126
      Vector.cpp
  6. 31
      Vector.hpp
  7. 2
      attractors/testAttractor.stf
  8. 2
      attractors/testPolynomial.stf
  9. 2
      attractors/testUnravel.stf
  10. 16
      defines.hpp
  11. 116
      main.cpp

6
Attractor.cpp

@ -10,12 +10,12 @@
#include "Projector.hpp" #include "Projector.hpp"
Attractor::Attractor(const char* const fileName) { Attractor::Attractor(const std::string& filename) {
// opening file // opening file
std::cout << "Reading file " << fileName << "..." << std::endl; std::cout << "Reading file " << filename << "..." << std::endl;
stfu::node system; stfu::node system;
system.read(fileName); system.read(filename.c_str());
stfu::node attractor = system.getChild("AttractorKernel"); stfu::node attractor = system.getChild("AttractorKernel");
myAttractor = AttractorKernel::createAttractorKernel(attractor); myAttractor = AttractorKernel::createAttractorKernel(attractor);

3
Attractor.hpp

@ -1,6 +1,7 @@
#ifndef ATTRACTOR_HPP #ifndef ATTRACTOR_HPP
#define ATTRACTOR_HPP #define ATTRACTOR_HPP
#include <string>
#include <vector> #include <vector>
class Projector; class Projector;
@ -16,7 +17,7 @@ public:
// should be private really // should be private really
std::vector<Projector *> projectors; std::vector<Projector *> projectors;
Attractor(const char* const filename); Attractor(const std::string& filename);
~Attractor(); ~Attractor();
void init_range(); void init_range();

110
Parameters.cpp

@ -1,110 +0,0 @@
#include <iostream>
using namespace std;
#include "Parameters.hpp"
Parameters::Parameters(unsigned int num_parameters, float default_val):
num_parameters(num_parameters) {
begin = new (nothrow) Vector(num_parameters, default_val);
eind = new (nothrow) Vector(num_parameters, default_val);
interpolated = new (nothrow) Vector(num_parameters, default_val);
// *interpolated = *begin
check_pointers();
#ifdef HARDDEBUG
cout << "New Parameters with one default val:" << endl << *this << endl;
#endif
}
/*Parameters::Parameters(unsigned int num_parameters, float default_val1, float default_val2):
num_parameters(num_parameters) {
begin = new (nothrow) Vector(num_parameters, default_val1);
eind = new (nothrow) Vector(num_parameters, default_val2);
interpolated = new (nothrow) Vector(num_parameters, default_val1);
// *interpolated = *begin
check_pointers();
#ifdef HARDDEBUG
cout << "New Parameters with two default vals:" << endl << *this << endl;
#endif
}*/
Parameters::~Parameters() {
delete begin;
delete eind;
delete interpolated;
#ifdef HARDDEBUG
cout << "Parameters deleted" << endl;
#endif
}
void Parameters::check_pointers() {
assert(begin != NULL);
assert(eind != NULL);
assert(interpolated != NULL);
}
void Parameters::set(unsigned int parameter, float val1, float val2) {
assert(parameter < num_parameters);
begin->coordinates[parameter] = val1;
eind->coordinates[parameter] = val2;
interpolated->coordinates[parameter] = val1;
#ifdef HARDDEBUG
cout << "Parameter " << parameter << " set to: " << val1 << " - " << val2 << endl;
#endif
}
void Parameters::set(unsigned int parameter, float val) {
assert(parameter < num_parameters);
begin->coordinates[parameter] = val;
eind->coordinates[parameter] = val;
interpolated->coordinates[parameter] = val;
#ifdef HARDDEBUG
cout << "Parameter " << parameter << " set to: " << val << endl;
#endif
}
float Parameters::get(unsigned int parameter) {
assert(parameter < num_parameters);
#ifdef HARDDEBUG
cout << "Asked for parameter " << parameter << " with value:" << interpolated->coordinates[parameter] << endl;
#endif
return interpolated->coordinates[parameter];
}
void Parameters::interpolate(float time) {
/*
Dit is mogelijk met vector rekenen:
(*interpolated) = (*begin) * ( 1.0 - time ) + (*eind) * time;
Maar we doen het per element, zodat we simpelere code hebben,
geen vectoren hoeven te returnen en makkelijker kunnen optimaliseren
*/
const float invtime = 1.0 - time;
for ( unsigned int i = 0; i < num_parameters; i++ ) {
interpolated->coordinates[i] = invtime * begin->coordinates[i] + time * eind->coordinates[i];
}
#ifdef HARDDEBUG
cout << "interpolate() result" << endl << *interpolated << endl;
#endif
}
ostream& operator<<(ostream& os, const Parameters& param) {
os << param.num_parameters << endl;
os << "Begin:" << endl << *param.begin << endl;
os << "Eind:" << endl << *param.eind << endl;
os << "Interpolated:" << endl << *param.interpolated << endl;
os <<endl;
return os;
}

34
Parameters.hpp

@ -1,34 +0,0 @@
#ifndef PARAMETER_HPP
#define PARAMETER_HPP
#include "Vector.hpp"
class Parameters {
Vector * begin;
Vector * eind;
Vector * interpolated;
void check_pointers();
public:
// for checks and assertions
unsigned int num_parameters;
Parameters(unsigned int num_parameters, float default_val = 0.0);
//Parameters(unsigned int num_parameters, float default_val1, float default_val2);
~Parameters();
void set(unsigned int parameter, float val1, float val2);
void set(unsigned int parameter, float val);
float get(unsigned int parameter);
void interpolate(float time);
// output operator
friend ostream& operator<<(ostream& os, const Parameters& param);
};
#endif // PARAMETER_HPP

126
Vector.cpp

@ -1,126 +0,0 @@
#include <iostream>
using namespace std;
#include "Vector.hpp"
Vector::Vector():
dimension(0) {
coordinates = new (nothrow) float[0];
assert(coordinates != NULL);
#ifdef HARDDEBUG
cout << "New vector (without elements)" << endl;
#endif
}
Vector::Vector(unsigned int d):
dimension(d) {
coordinates = new (nothrow) float[dimension];
assert(coordinates != NULL);
#ifdef HARDDEBUG
cout << "New vector:" << endl << *this << endl;
#endif
}
Vector::Vector(unsigned int d, float default_val):
dimension(d) {
coordinates = new (nothrow) float[dimension];
assert(coordinates != NULL);
for (unsigned int i = 0; i < dimension; i++) {
coordinates[i] = default_val;
}
#ifdef HARDDEBUG
cout << "New vector with default values:" << endl << *this << endl;
#endif
}
Vector::~Vector() {
delete[] coordinates;
#ifdef HARDDEBUG
cout << "coordinates deleted" << endl;
#endif
}
Vector& Vector::operator=(const Vector& a) {
if ( dimension != a.dimension ) {
dimension = a.dimension;
delete[] coordinates;
coordinates = new float[dimension];
#ifdef HARDDEBUG
cout << "Dimensions were not equal, made new vector" << endl;
#endif
}
for ( unsigned int i = 0; i < dimension; i++ ) {
coordinates[i] = a.coordinates[i];
}
#ifdef HARDDEBUG
cout << "operator= result" << endl << *this << endl;
#endif
return *this;
}
ostream& operator<<(ostream& os, const Vector& a) {
os << a.dimension << endl;
for ( unsigned int i = 0; i < a.dimension; i++ ) {
os << a.coordinates[i] << " ";
}
os << endl;
return os;
}
float& Vector::operator[](const unsigned int index) {
assert(index < dimension);
return coordinates[index];
}
// matig werkende optelling en scalaire vermenigvuldiging van vectoren
/*
Vector Vector::operator+(const Vector a) const {
if ( dimension != a.dimension ) {
cout << "WARNING: dimensions not equal in vector addition" << endl;
exit(1);
} else {
static Vector ret(dimension);
for ( unsigned int i = 0; i < dimension; i++ ) {
ret.coordinates[i] = coordinates[i] + a.coordinates[i];
}
#ifdef HARDDEBUG
cout << "operator+ result" << endl << ret << endl;
#endif
return ret;
}
}
Vector Vector::operator*(const float a) const {
static Vector ret(dimension);
for ( unsigned int i = 0; i < dimension; i++ ) {
ret.coordinates[i] = coordinates[i] * a;
}
#ifdef HARDDEBUG
cout << "operator* result" << endl << ret << endl;
#endif
return ret;
}
*/

31
Vector.hpp

@ -1,31 +0,0 @@
#ifndef VECTOR_HPP
#define VECTOR_HPP
class Vector {
public:
unsigned int dimension;
float * coordinates;
// const, dest
Vector();
Vector(unsigned int d);
Vector(unsigned int d, float default_val);
~Vector();
// output operator
friend ostream& operator<<(ostream& os, const Vector& a);
// easy access
float& Vector::operator[](const unsigned int index);
// vector rekenen
Vector& operator=(const Vector& a);
// faaloperatoren
// Vector operator+(const Vector a) const;
// Vector operator*(const float a) const;
};
#endif // VECTOR_HPP

2
attractors/testAttractor.stf

@ -2,7 +2,7 @@
input: "attractor" input: "attractor"
output: "png" output: "png"
attractor: { AttractorKernel: {
type: lorenz/unravel/polynomial/polynomial a/logistic type: lorenz/unravel/polynomial/polynomial a/logistic
=> "polynomial" => "polynomial"
dimensions: most types only support 3D dimensions: most types only support 3D

2
attractors/testPolynomial.stf

@ -2,7 +2,7 @@
input: "attractor" input: "attractor"
output: "png" output: "png"
attractor: { AttractorKernel: {
type: lorenz/unravel/polynomial/polynomial a/logistic type: lorenz/unravel/polynomial/polynomial a/logistic
=> "polynomial" => "polynomial"
dimensions: most types only support 3D dimensions: most types only support 3D

2
attractors/testUnravel.stf

@ -2,7 +2,7 @@
input: "attractor" input: "attractor"
output: "png" output: "png"
attractor: { AttractorKernel: {
type: lorenz/unravel/polynomial/polynomial a/logistic type: lorenz/unravel/polynomial/polynomial a/logistic
=> "unravel" => "unravel"
dimensions: most types only support 3D dimensions: most types only support 3D

16
defines.hpp

@ -1,15 +1,13 @@
#define DEFAULT_ATTRACTOR_FILE "attractors/testUnravel.stf"
//TODO: do this with files
#define ATTRACTOR_FILE "attractors/testUnravel.stf"
#ifdef UNI_BUILD #ifdef UNI_BUILD
#warning Building for the RU, are you sure? #warning Building for the RU, are you sure?
#define WIDTH 8000 #define DEFAULT_WIDTH 8000
#define HEIGHT 8000 #define DEFAULT_HEIGHT 8000
#define ITERATIONS 4200000000 #define DEFAULT_ITERATIONS 4200000000
#else #else
#define WIDTH 800 #define DEFAULT_WIDTH 800
#define HEIGHT 800 #define DEFAULT_HEIGHT 800
#define ITERATIONS 1000000 #define DEFAULT_ITERATIONS 1000000
#endif #endif

116
main.cpp

@ -1,6 +1,7 @@
#include <iostream> #include <iostream>
#include <ctime> #include <ctime>
#include <valarray> #include <valarray>
#include <cstring>
#include "Attractor.hpp" #include "Attractor.hpp"
#include "Canvas.hpp" #include "Canvas.hpp"
@ -8,59 +9,68 @@
#include "defines.hpp" #include "defines.hpp"
/*
I compared the performance of C-style arrays with valarray. My conclusion is that accessing and setting elements makes no difference (sometimes C-syle array's were just 2% faster, but sometimes valarray was 2% faster (5000000 samples)). But the allocation and initialisation of the valrray was like 30% faster, so it might be a good idea to change my C-style array to valarray. It is also nicer in use, one can use functions like sum, etc. How valarray manages memory may also be more efficient, but all i know is that a valarray<double> is bigger than a double* (in means of sizeof()), but that is not really an issue. So valarray!!!
*/
int main(int argc, char *argv[]) { int main(int argc, char *argv[]) {
std::cout << "Awesome Attractor, version " << __DATE__ << std::endl;
clock_t start, end;
double totalTime, totalIterations; bool verbose = false;
std::string attractorFile = DEFAULT_ATTRACTOR_FILE;
bool verbose = false; unsigned int iterations = DEFAULT_ITERATIONS;
unsigned int width = DEFAULT_WIDTH;
// initialising stuff unsigned int height = DEFAULT_HEIGHT;
Attractor myAttractor(ATTRACTOR_FILE);
for ( int i = 1; i < argc; ++i ) {
Projector projection; if ( strcmp(argv[i], "-v") == 0 ) {
Canvas canvas(WIDTH, HEIGHT, 3); verbose = true;
projection.canvas = &canvas; } else if ( strcmp(argv[i], "-w") == 0 ) {
width = atoi(argv[++i]);
myAttractor.projectors.push_back(&projection); } else if ( strcmp(argv[i], "-h") == 0 ) {
myAttractor.init_range(); height = atoi(argv[++i]);
} else if ( strcmp(argv[i], "-i") == 0 ) {
projection.output(); iterations = atoi(argv[++i]);
} else {
unsigned int iterations = ITERATIONS; attractorFile = argv[i];
start = clock(); }
for ( unsigned int j = 1; j <= 100; j++ ) { }
for ( unsigned int i = 0; i <= iterations; i++ ) {
myAttractor.iterate(); // initialising stuff
myAttractor.plot(); Attractor myAttractor(attractorFile);
}
if (verbose) { Projector projection;
myAttractor.output(); Canvas canvas(width, height, 3);
std::cout << j << "% done" << std::endl; projection.canvas = &canvas;
}
} myAttractor.projectors.push_back(&projection);
end = clock(); myAttractor.init_range();
totalIterations = 100.0*iterations; projection.output();
totalTime = ((double)(end-start)/(double)(CLOCKS_PER_SEC));
std::cout << std::endl << "total clock time: " << totalTime << std::endl; clock_t start, end;
std::cout << "average iterations per second: " << totalIterations/((double)(end-start)/(double)(CLOCKS_PER_SEC)) << std::endl << std::endl; start = clock();
for ( unsigned int j = 1; j <= 100; ++j ) {
// saving output for ( unsigned int i = 0; i <= iterations; i++ ) {
start = clock(); myAttractor.iterate();
canvas.output_file(); myAttractor.plot();
end = clock(); }
std::cout << "\r" << j << "% done" << std::flush;
totalTime = ((double)(end-start)/(double)(CLOCKS_PER_SEC)); }
end = clock();
std::cout << "total clock time for writing png: " << totalTime << std::endl;
std::cout << std::endl << "Awesome Attractor, version " << __DATE__ << std::endl; double totalIterations = 100.0*iterations;
double totalTime = ((double)(end-start)/(double)(CLOCKS_PER_SEC));
std::cout << std::endl << "total clock time: " << totalTime << std::endl;
return 0; std::cout << "average iterations per second: " << totalIterations/totalTime << std::endl << std::endl;
// saving output
start = clock();
canvas.output_file();
end = clock();
totalTime = ((double)(end-start)/(double)(CLOCKS_PER_SEC));
std::cout << "total clock time for writing png: " << totalTime << std::endl;
return 0;
} }