Browse Source

cleaned up kernels, better memory managment (with exceptions)

master
Joshua Moerman 15 years ago
parent
commit
c89f4bb8c9
  1. 47
      AttractorKernel.cpp
  2. 2
      AttractorKernel.hpp
  3. 25
      kernels/Logistic.cpp
  4. 22
      kernels/Lorenz3D.cpp
  5. 38
      kernels/Polynomial.cpp
  6. 2
      kernels/Polynomial.hpp
  7. 13
      kernels/PolynomialA3D.cpp
  8. 2
      kernels/PolynomialA3D.hpp
  9. 12
      kernels/Unravel3D.cpp

47
AttractorKernel.cpp

@ -14,18 +14,15 @@
#pragma mark - #pragma mark -
#pragma mark memory #pragma mark memory
AttractorKernel::AttractorKernel(const unsigned int dimension, const unsigned int numberOfParameters): AttractorKernel::AttractorKernel(const unsigned int dimension, const unsigned int numberOfParameters) :
numberOfParameters(numberOfParameters), dimension(dimension) { numberOfParameters(numberOfParameters), dimension(dimension){
try { try {
parameters = new double[numberOfParameters]; allocate();
}
vectorNew = new double[dimension]; catch (std::exception& e) {
vectorOld = new double[dimension]; std::cout << "Couldn't construct AttractorKernel: " << e.what() << std::endl;
} catch (std::bad_alloc& e) { dealloc();
std::cerr << "Exception: " << e.what() << std::endl;
std::cerr << "Quiting because nothing to do... " << std::endl;
exit(1);
} }
std::fill_n(parameters, numberOfParameters, 0.0); std::fill_n(parameters, numberOfParameters, 0.0);
@ -35,26 +32,22 @@ numberOfParameters(numberOfParameters), dimension(dimension) {
} }
AttractorKernel::~AttractorKernel(){ AttractorKernel::~AttractorKernel(){
delete[] vectorOld; dealloc();
delete[] vectorNew;
delete[] parameters;
} }
void AttractorKernel::reallocParameters(const unsigned int newNumberOfParameters){ 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; delete[] parameters;
parameters = NULL;
numberOfParameters = newNumberOfParameters;
try {
parameters = new double[numberOfParameters];
}
catch (std::bad_alloc& e) {
std::cerr << "Exception: " << e.what() << std::endl;
std::cerr << "Quiting because nothing to do... " << std::endl;
exit(1);
}
std::fill_n(parameters, numberOfParameters, 0.0);
} }
#pragma mark - #pragma mark -

2
AttractorKernel.hpp

@ -8,6 +8,8 @@
class AttractorKernel { class AttractorKernel {
private: private:
void allocate();
void dealloc();
protected: protected:

25
kernels/Logistic.cpp

@ -9,36 +9,35 @@
#include "Logistic.hpp" #include "Logistic.hpp"
#include <algorithm> #include <algorithm>
Logistic::Logistic():
AttractorKernel(3, 3) {
#pragma mark -
#pragma mark ctors
Logistic::Logistic():
AttractorKernel(3, 3) {
init(); init();
} }
Logistic::Logistic(const unsigned int dimension): Logistic::Logistic(const unsigned int dimension):
AttractorKernel(dimension, dimension) { AttractorKernel(dimension, dimension) {
init(); init();
} }
void Logistic::init() { void Logistic::init() {
// setting some starting values // setting some starting values
for ( unsigned int i = 0; i < dimension; i++ ) { std::fill_n(vectorOld, dimension, 0.5);
vectorNew[i] = vectorOld[i] = 0.5; std::fill_n(vectorNew, dimension, 0.5);
}
} }
#pragma mark -
#pragma mark attractor
void Logistic::operator()() { void Logistic::operator()() {
std::swap(vectorNew, vectorOld); std::swap(vectorNew, vectorOld);
for ( unsigned int i = 0; i < dimension; i++ ) { for ( unsigned int i = 0; i < dimension; i++ ) {
vectorNew[i] = parameters[i]*vectorOld[i]*(1.0 - vectorOld[i]); vectorNew[i] = parameters[i]*vectorOld[i]*(1.0 - vectorOld[i]);
} }
} }

22
kernels/Lorenz3D.cpp

@ -1,31 +1,31 @@
#include "Lorenz3D.hpp" #include "Lorenz3D.hpp"
#include <algorithm> #include <algorithm>
#pragma mark -
#pragma mark ctors
Lorenz3D::Lorenz3D(): Lorenz3D::Lorenz3D():
AttractorKernel(3, 4){ AttractorKernel(3, 4){
init(); init();
} }
void Lorenz3D::init() { void Lorenz3D::init() {
// setting some starting values // setting some starting values
for ( unsigned int i = 0; i < dimension; i++ ) { std::fill_n(vectorOld, dimension, 1.0);
vectorNew[i] = vectorOld[i] = 1.0; std::fill_n(vectorNew, dimension, 1.0);
}
} }
// the main function
#pragma mark -
#pragma mark attractor
void Lorenz3D::operator()() { void Lorenz3D::operator()() {
std::swap(vectorNew, vectorOld); std::swap(vectorNew, vectorOld);
vectorNew[0] = vectorOld[0] + parameters[0] * parameters[1] * (vectorOld[1] - vectorOld[0]); vectorNew[0] = vectorOld[0] + parameters[0] * parameters[1] * (vectorOld[1] - vectorOld[0]);
vectorNew[1] = vectorOld[1] + parameters[0] * (vectorOld[0] * (parameters[2] - vectorOld[2]) - vectorOld[1]); vectorNew[1] = vectorOld[1] + parameters[0] * (vectorOld[0] * (parameters[2] - vectorOld[2]) - vectorOld[1]);
vectorNew[2] = vectorOld[2] + parameters[0] * (vectorOld[0] * vectorOld[1] - parameters[3] * vectorOld[2]); vectorNew[2] = vectorOld[2] + parameters[0] * (vectorOld[0] * vectorOld[1] - parameters[3] * vectorOld[2]);
} }
/* /*

38
kernels/Polynomial.cpp

@ -8,37 +8,32 @@
#include "Polynomial.hpp" #include "Polynomial.hpp"
#include <algorithm> #include <algorithm>
//#include <iostream>
Polynomial::Polynomial():
AttractorKernel(3, 0),
orde(2){
init();
}
Polynomial::Polynomial(const unsigned int dimension, const unsigned int orde): unsigned int calculateNumberOfParameters(const unsigned int dimension, const unsigned int orde) {
AttractorKernel(dimension, 0),
orde(orde) {
init();
}
void Polynomial::init() {
const unsigned int number = calculateNumberOfParameters();
reallocParameters(number);
}
// i want to has constexpr, so the ctor can be clean :]
unsigned int Polynomial::calculateNumberOfParameters() {
double n_coef = orde + 1; double n_coef = orde + 1;
for (unsigned int i = 2; i <= dimension; i++) { for (unsigned int i = 2; i <= dimension; i++) {
n_coef = n_coef*(orde + i)/(i - 1); n_coef = n_coef*(orde + i)/(i - 1);
} }
const unsigned int output = (unsigned int) n_coef; const unsigned int output = (unsigned int) n_coef;
return output; return output;
} }
#pragma mark -
#pragma mark ctors
Polynomial::Polynomial():
AttractorKernel(3, calculateNumberOfParameters(3, 2)), orde(2){}
Polynomial::Polynomial(const unsigned int dimension, const unsigned int orde):
AttractorKernel(dimension, calculateNumberOfParameters(dimension, orde)), orde(orde){}
#pragma mark -
#pragma mark attractor
void Polynomial::operator()() { void Polynomial::operator()() {
std::swap(vectorNew, vectorOld); std::swap(vectorNew, vectorOld);
@ -49,7 +44,6 @@ void Polynomial::operator()() {
m++; m++;
recur(i, 0, 1, m); recur(i, 0, 1, m);
} }
} }
void Polynomial::recur(unsigned int curr_dimension, unsigned int prev_i, unsigned int n, unsigned int& m, double prev_product) { void Polynomial::recur(unsigned int curr_dimension, unsigned int prev_i, unsigned int n, unsigned int& m, double prev_product) {

2
kernels/Polynomial.hpp

@ -8,8 +8,6 @@ private:
unsigned int orde; unsigned int orde;
void init();
unsigned int calculateNumberOfParameters();
void recur(unsigned int curr_dimension, unsigned int prev_i, unsigned int n, unsigned int& m, double prev_product=1.0); void recur(unsigned int curr_dimension, unsigned int prev_i, unsigned int n, unsigned int& m, double prev_product=1.0);
public: public:

13
kernels/PolynomialA3D.cpp

@ -9,14 +9,16 @@
#include "PolynomialA3D.hpp" #include "PolynomialA3D.hpp"
#include <algorithm> #include <algorithm>
#pragma mark -
#pragma mark ctors
PolynomialA3D::PolynomialA3D(): PolynomialA3D::PolynomialA3D():
AttractorKernel(3, 3){ AttractorKernel(3, 3){}
init();
}
void PolynomialA3D::init() {
} #pragma mark -
#pragma mark attractor
void PolynomialA3D::operator()() { void PolynomialA3D::operator()() {
std::swap(vectorNew, vectorOld); std::swap(vectorNew, vectorOld);
@ -25,6 +27,7 @@ void PolynomialA3D::operator()() {
vectorNew[1] = parameters[1] + vectorOld[2] - vectorOld[2]*vectorOld[0]; vectorNew[1] = parameters[1] + vectorOld[2] - vectorOld[2]*vectorOld[0];
vectorNew[2] = parameters[2] + vectorOld[0] - vectorOld[0]*vectorOld[1]; vectorNew[2] = parameters[2] + vectorOld[0] - vectorOld[0]*vectorOld[1];
} }
/* /*
N dimensional: N dimensional:
for ( unsigned int i = 0; i < dim; i++ ) { for ( unsigned int i = 0; i < dim; i++ ) {

2
kernels/PolynomialA3D.hpp

@ -6,8 +6,6 @@
class PolynomialA3D : public AttractorKernel { class PolynomialA3D : public AttractorKernel {
private: private:
void init();
public: public:
PolynomialA3D(); PolynomialA3D();

12
kernels/Unravel3D.cpp

@ -2,10 +2,16 @@
#include <algorithm> #include <algorithm>
#include <cmath> #include <cmath>
#pragma mark -
#pragma mark ctors
Unravel3D::Unravel3D(): Unravel3D::Unravel3D():
AttractorKernel(3, 7){ AttractorKernel(3, 7){}
}
#pragma mark -
#pragma mark attractor
void Unravel3D::operator()() { void Unravel3D::operator()() {
std::swap(vectorNew, vectorOld); std::swap(vectorNew, vectorOld);
@ -17,7 +23,7 @@ void Unravel3D::operator()() {
const double dist = vectorNew[0]*vectorNew[0] + vectorNew[1]*vectorNew[1] + vectorNew[2]*vectorNew[2]; const double dist = vectorNew[0]*vectorNew[0] + vectorNew[1]*vectorNew[1] + vectorNew[2]*vectorNew[2];
if ( dist > parameters[6]*parameters[6] ) { if ( dist > parameters[6]*parameters[6] ) {
const double sqrtDist = sqrt(dist); const double sqrtDist = std::sqrt(dist);
const double p = 1.0 - parameters[6] * ( static_cast<int> ( sqrtDist / parameters[6] ) + 1.0 ) / sqrtDist; const double p = 1.0 - parameters[6] * ( static_cast<int> ( sqrtDist / parameters[6] ) + 1.0 ) / sqrtDist;
vectorNew[0] *= p; vectorNew[0] *= p;
vectorNew[1] *= p; vectorNew[1] *= p;