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

2
AttractorKernel.hpp

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

25
kernels/Logistic.cpp

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

22
kernels/Lorenz3D.cpp

@ -1,31 +1,31 @@
#include "Lorenz3D.hpp"
#include <algorithm>
#pragma mark -
#pragma mark ctors
Lorenz3D::Lorenz3D():
AttractorKernel(3, 4){
AttractorKernel(3, 4){
init();
}
void Lorenz3D::init() {
// setting some starting values
for ( unsigned int i = 0; i < dimension; i++ ) {
vectorNew[i] = vectorOld[i] = 1.0;
}
std::fill_n(vectorOld, dimension, 1.0);
std::fill_n(vectorNew, dimension, 1.0);
}
// the main function
#pragma mark -
#pragma mark attractor
void Lorenz3D::operator()() {
std::swap(vectorNew, vectorOld);
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[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 <algorithm>
//#include <iostream>
Polynomial::Polynomial():
AttractorKernel(3, 0),
orde(2){
init();
}
Polynomial::Polynomial(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() {
unsigned int calculateNumberOfParameters(const unsigned int dimension, const unsigned int orde) {
double n_coef = orde + 1;
for (unsigned int i = 2; i <= dimension; i++) {
n_coef = n_coef*(orde + i)/(i - 1);
}
const unsigned int output = (unsigned int) n_coef;
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()() {
std::swap(vectorNew, vectorOld);
@ -49,7 +44,6 @@ void Polynomial::operator()() {
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) {

2
kernels/Polynomial.hpp

@ -8,8 +8,6 @@ private:
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);
public:

13
kernels/PolynomialA3D.cpp

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

2
kernels/PolynomialA3D.hpp

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

12
kernels/Unravel3D.cpp

@ -2,10 +2,16 @@
#include <algorithm>
#include <cmath>
#pragma mark -
#pragma mark ctors
Unravel3D::Unravel3D():
AttractorKernel(3, 7){
AttractorKernel(3, 7){}
}
#pragma mark -
#pragma mark attractor
void Unravel3D::operator()() {
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];
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;
vectorNew[0] *= p;
vectorNew[1] *= p;