Skip to content
Snippets Groups Projects
Commit a4e3e1af authored by Axel Modave's avatar Axel Modave
Browse files

pp

parent f849a9c3
Branches
Tags
No related merge requests found
...@@ -11,6 +11,7 @@ ...@@ -11,6 +11,7 @@
#include "Bindings.h" #include "Bindings.h"
struct functionReplaceCache { struct functionReplaceCache {
dataCacheMap *map; dataCacheMap *map;
std::vector <dataCacheDouble*> toReplace; std::vector <dataCacheDouble*> toReplace;
...@@ -18,15 +19,26 @@ struct functionReplaceCache { ...@@ -18,15 +19,26 @@ struct functionReplaceCache {
}; };
/*==============================================================================
* class Function (with additionnal class)
*============================================================================*/
// Constructor and destructor
function::function(int nbCol, bool invalidatedOnElement) : _nbCol(nbCol), _invalidatedOnElement(invalidatedOnElement) {
}
function::~function() { function::~function() {
} }
function::function(int nbCol, bool invalidatedOnElement):_nbCol(nbCol), _invalidatedOnElement(invalidatedOnElement){} // Set
void function::addFunctionReplace(functionReplace &fr) { void function::addFunctionReplace(functionReplace &fr) {
fr._master = this; fr._master = this;
_functionReplaces.push_back(&fr); _functionReplaces.push_back(&fr);
} }
void function::setArgument(fullMatrix<double> &v, const function *f, int iMap) { void function::setArgument(fullMatrix<double> &v, const function *f, int iMap) {
if (f==NULL) if (f==NULL)
throw; throw;
...@@ -47,6 +59,158 @@ void function::setArgument(fullMatrix<double> &v, const function *f, int iMap) { ...@@ -47,6 +59,158 @@ void function::setArgument(fullMatrix<double> &v, const function *f, int iMap) {
} }
} }
// Time and DT
functionConstant *function::_timeFunction = NULL;
functionConstant *function::_dtFunction = NULL;
functionConstant *function::getTime() {
if (! _timeFunction)
_timeFunction = functionConstantNew(0.);
return _timeFunction;
}
functionConstant *function::getDT() {
if (! _dtFunction)
_dtFunction = functionConstantNew(0.);
return _dtFunction;
}
// Get informations
functionSolution *functionSolution::_instance = NULL;
function *function::getSolution() {
return functionSolution::get();
}
//------------------------------------
// Get Solution Gradient + Additionnal class
//------------------------------------
class functionSolutionGradient : public function {
static functionSolutionGradient *_instance;
functionSolutionGradient():function(0){} // constructor is private only 1 instance can exists, call get to access the instance
public:
void call(dataCacheMap *m, fullMatrix<double> &sol) {
Msg::Error("a function requires the gradient of the solution but this algorithm does not provide the gradient of the solution");
throw;
}
static function *get() {
if(!_instance)
_instance = new functionSolutionGradient();
return _instance;
}
};
functionSolutionGradient *functionSolutionGradient::_instance = NULL;
function *function::getSolutionGradient() {
return functionSolutionGradient::get();
}
//------------------------------------
// Get Parametric Coordinates + Additionnal class
//------------------------------------
class functionParametricCoordinates : public function {
static functionParametricCoordinates *_instance;
functionParametricCoordinates():function(0, false){} // constructor is private only 1 instance can exists, call get to access the instance
public:
void call(dataCacheMap *m, fullMatrix<double> &sol) {
Msg::Error("a function requires the parametric coordinates but this algorithm does not provide the parametric coordinates");
throw;
}
static function *get() {
if(!_instance)
_instance = new functionParametricCoordinates();
return _instance;
}
};
functionParametricCoordinates *functionParametricCoordinates::_instance = NULL;
function *function::getParametricCoordinates() {
return functionParametricCoordinates::get();
}
//------------------------------------
// Get Normals + Additionnal class
//------------------------------------
class functionNormals : public function {
static functionNormals *_instance;
functionNormals():function(0){} // constructor is private only 1 instance can exists, call get to access the instance
public:
void call(dataCacheMap *m, fullMatrix<double> &sol) {
Msg::Error("a function requires the normals coordinates but this algorithm does not provide the normals");
throw;
}
static function *get() {
if(!_instance)
_instance = new functionNormals();
return _instance;
}
};
functionNormals *functionNormals::_instance = NULL;
function *function::getNormals() {
return functionNormals::get();
}
/*==============================================================================
* class functionReplace
*============================================================================*/
functionReplace::functionReplace() {
_nChildren=0;
}
void functionReplace::get(fullMatrix<double> &v, const function *f, int iMap) {
bool allDepFromParent = true;
for (std::set<function::dependency>::const_iterator itDep = f->dependencies.begin(); itDep != f->dependencies.end(); itDep++){
bool depFromParent = (_replaced.count(*itDep)==0);
for (std::set<function::dependency>::const_iterator itDep2 = itDep->f->dependencies.begin(); itDep2 != itDep->f->dependencies.end() && depFromParent; itDep2++)
depFromParent &= (_replaced.count(*itDep2)==0);
if(depFromParent)
_master->dependencies.insert(*itDep);
else
allDepFromParent = false;
}
function::dependency asDep(iMap, f);
if (allDepFromParent && _replaced.count(asDep)==0)
_master->dependencies.insert(asDep);
_toCompute.push_back(function::argument(v, iMap, f));
}
void functionReplace::replace(fullMatrix<double> &v, const function *f, int iMap) {
_replaced.insert(function::dependency(iMap,f));
_toReplace.push_back(function::argument(v, iMap, f));
}
void functionReplace::addChild() {
_nChildren++;
}
void functionReplace::compute() {
for (unsigned i = 0; i < _toReplace.size(); i++)
currentCache->toReplace[i]->set();
for (unsigned i = 0; i < _toCompute.size(); i++)
_toCompute[i].val->setAsProxy(currentCache->toCompute[i]->get());
};
/*==============================================================================
* class dataCacheDouble
*============================================================================*/
dataCacheDouble::dataCacheDouble(dataCacheMap *m, function *f): dataCacheDouble::dataCacheDouble(dataCacheMap *m, function *f):
_cacheMap(*m),_value(m->getNbEvaluationPoints(),f->getNbCol()), _function(f) _cacheMap(*m),_value(m->getNbEvaluationPoints(),f->getNbCol()), _function(f)
{ {
...@@ -72,8 +236,6 @@ void dataCacheDouble::_eval() { ...@@ -72,8 +236,6 @@ void dataCacheDouble::_eval() {
_valid = true; _valid = true;
} }
dataCacheDouble &dataCacheMap::get(const function *f, dataCacheDouble *caller) { dataCacheDouble &dataCacheMap::get(const function *f, dataCacheDouble *caller) {
// do I have a cache for this function ? // do I have a cache for this function ?
dataCacheDouble *&r = _cacheDoubleMap[f]; dataCacheDouble *&r = _cacheDoubleMap[f];
...@@ -134,6 +296,12 @@ dataCacheDouble &dataCacheMap::get(const function *f, dataCacheDouble *caller) { ...@@ -134,6 +296,12 @@ dataCacheDouble &dataCacheMap::get(const function *f, dataCacheDouble *caller) {
return *r; return *r;
} }
/*==============================================================================
* class dataCacheMap
*============================================================================*/
dataCacheMap::~dataCacheMap() dataCacheMap::~dataCacheMap()
{ {
for (std::set<dataCacheDouble*>::iterator it = _allDataCaches.begin(); for (std::set<dataCacheDouble*>::iterator it = _allDataCaches.begin();
...@@ -145,48 +313,32 @@ dataCacheMap::~dataCacheMap() ...@@ -145,48 +313,32 @@ dataCacheMap::~dataCacheMap()
} }
} }
void functionReplace::replace(fullMatrix<double> &v, const function *f, int iMap) {
_replaced.insert(function::dependency(iMap,f));
_toReplace.push_back(function::argument(v, iMap, f));
}
void functionReplace::get(fullMatrix<double> &v, const function *f, int iMap) {
bool allDepFromParent = true;
for (std::set<function::dependency>::const_iterator itDep = f->dependencies.begin(); itDep != f->dependencies.end(); itDep++){
bool depFromParent = (_replaced.count(*itDep)==0);
for (std::set<function::dependency>::const_iterator itDep2 = itDep->f->dependencies.begin(); itDep2 != itDep->f->dependencies.end() && depFromParent; itDep2++)
depFromParent &= (_replaced.count(*itDep2)==0);
if(depFromParent)
_master->dependencies.insert(*itDep);
else
allDepFromParent = false;
}
function::dependency asDep(iMap, f);
if (allDepFromParent && _replaced.count(asDep)==0)
_master->dependencies.insert(asDep);
_toCompute.push_back(function::argument(v, iMap, f)); void dataCacheMap::setNbEvaluationPoints(int nbEvaluationPoints) {
_nbEvaluationPoints = nbEvaluationPoints;
for(std::set<dataCacheDouble*>::iterator it = _allDataCaches.begin(); it!= _allDataCaches.end(); it++){
(*it)->resize(nbEvaluationPoints);
} }
void functionReplace::addChild() { for(std::list<dataCacheMap*>::iterator it = _children.begin(); it!= _children.end(); it++) {
_nChildren++; (*it)->setNbEvaluationPoints(nbEvaluationPoints);
} }
functionReplace::functionReplace(){
_nChildren=0;
} }
void functionReplace::compute(){
for (unsigned i = 0; i < _toReplace.size(); i++){
currentCache->toReplace[i]->set();
}
for (unsigned i = 0; i < _toCompute.size(); i++) {
_toCompute[i].val->setAsProxy(currentCache->toCompute[i]->get());
}
};
// now some example of functions
// constant values copied over each line /*==============================================================================
* Some examples of functions
*============================================================================*/
// functionConstant (constant values copied over each line)
void functionConstant::set(double val) {
if(getNbCol() != 1)
Msg::Error ("set scalar value on a vectorial constant function");
_source(0,0) = val;
}
functionConstant *functionConstantNew(double v) { functionConstant *functionConstantNew(double v) {
std::vector<double> vec(1); std::vector<double> vec(1);
...@@ -198,24 +350,17 @@ functionConstant *functionConstantNew(const std::vector<double> &v) { ...@@ -198,24 +350,17 @@ functionConstant *functionConstantNew(const std::vector<double> &v) {
return new functionConstant(v); return new functionConstant(v);
} }
void functionConstant::set(double val) {
if(getNbCol() != 1) {
Msg::Error ("set scalar value on a vectorial constant function");
}
_source(0,0) = val;
}
// functionSum
class functionSum : public function { class functionSum : public function {
public: public:
fullMatrix<double> _f0, _f1; fullMatrix<double> _f0, _f1;
void call(dataCacheMap *m, fullMatrix<double> &val) { void call(dataCacheMap *m, fullMatrix<double> &val) {
for (int i=0; i<val.size1(); i++) for (int i=0; i<val.size1(); i++)
for(int j=0;j<val.size2();j++){ for (int j=0; j<val.size2(); j++)
val(i,j)= _f0(i,j) + _f1(i,j); val(i,j)= _f0(i,j) + _f1(i,j);
} }
}
functionSum(const function *f0, const function *f1) : function(f0->getNbCol()) { functionSum(const function *f0, const function *f1) : function(f0->getNbCol()) {
if (f0->getNbCol() != f1->getNbCol()) { if (f0->getNbCol() != f1->getNbCol()) {
Msg::Error("trying to sum 2 functions of different sizes\n"); Msg::Error("trying to sum 2 functions of different sizes\n");
...@@ -230,15 +375,17 @@ function *functionSumNew(const function *f0, const function *f1) { ...@@ -230,15 +375,17 @@ function *functionSumNew(const function *f0, const function *f1) {
return new functionSum (f0, f1); return new functionSum (f0, f1);
} }
// functionProd
class functionProd : public function { class functionProd : public function {
public: public:
fullMatrix<double> _f0, _f1; fullMatrix<double> _f0, _f1;
void call(dataCacheMap *m, fullMatrix<double> &val) { void call(dataCacheMap *m, fullMatrix<double> &val) {
for (int i=0; i<val.size1(); i++) for (int i=0; i<val.size1(); i++)
for(int j=0;j<val.size2();j++){ for (int j=0; j<val.size2(); j++)
val(i,j)= _f0(i,j)*_f1(i,j); val(i,j)= _f0(i,j)*_f1(i,j);
} }
}
functionProd(const function *f0, const function *f1) : function(f0->getNbCol()) { functionProd(const function *f0, const function *f1) : function(f0->getNbCol()) {
if (f0->getNbCol() != f1->getNbCol()) { if (f0->getNbCol() != f1->getNbCol()) {
Msg::Error("trying to compute product of 2 functions of different sizes\n"); Msg::Error("trying to compute product of 2 functions of different sizes\n");
...@@ -253,6 +400,9 @@ function *functionProdNew(const function *f0, const function *f1) { ...@@ -253,6 +400,9 @@ function *functionProdNew(const function *f0, const function *f1) {
return new functionProd (f0, f1); return new functionProd (f0, f1);
} }
// functionExtractComp
class functionExtractComp : public function { class functionExtractComp : public function {
public: public:
fullMatrix<double> _f0; fullMatrix<double> _f0;
...@@ -272,16 +422,17 @@ function *functionExtractCompNew(const function *f0, const int iComp) { ...@@ -272,16 +422,17 @@ function *functionExtractCompNew(const function *f0, const int iComp) {
} }
// functionScale
class functionScale : public function { class functionScale : public function {
public: public:
fullMatrix<double> _f0; fullMatrix<double> _f0;
double _s; double _s;
void call(dataCacheMap *m, fullMatrix<double> &val) { void call(dataCacheMap *m, fullMatrix<double> &val) {
for(int i=0; i<val.size1(); i++) for(int i=0; i<val.size1(); i++)
for(int j=0;j<val.size2();j++){ for(int j=0; j<val.size2(); j++)
val(i,j)= _f0(i,j)*_s; val(i,j)= _f0(i,j)*_s;
} }
}
functionScale(const function *f0, const double s) : function(f0->getNbCol()) { functionScale(const function *f0, const double s) : function(f0->getNbCol()) {
setArgument (_f0, f0); setArgument (_f0, f0);
_s = s; _s = s;
...@@ -292,7 +443,9 @@ function *functionScaleNew(const function *f0, const double s) { ...@@ -292,7 +443,9 @@ function *functionScaleNew(const function *f0, const double s) {
return new functionScale (f0, s); return new functionScale (f0, s);
} }
// get XYZ coordinates
// functionCoordinates (get XYZ coordinates)
class functionCoordinates : public function { class functionCoordinates : public function {
static functionCoordinates *_instance; static functionCoordinates *_instance;
fullMatrix<double> uvw; fullMatrix<double> uvw;
...@@ -305,10 +458,9 @@ class functionCoordinates : public function { ...@@ -305,10 +458,9 @@ class functionCoordinates : public function {
xyz(i, 2) = p.z(); xyz(i, 2) = p.z();
} }
} }
functionCoordinates():function(3) functionCoordinates():function(3) { // constructor is private only 1 instance can exists, call get to access the instance
{
setArgument(uvw, function::getParametricCoordinates()); setArgument(uvw, function::getParametricCoordinates());
};// constructor is private only 1 instance can exists, call get to access the instance };
public: public:
static function *get() { static function *get() {
if(!_instance) if(!_instance)
...@@ -316,69 +468,15 @@ class functionCoordinates : public function { ...@@ -316,69 +468,15 @@ class functionCoordinates : public function {
return _instance; return _instance;
} }
}; };
functionCoordinates *functionCoordinates::_instance = NULL;
functionSolution *functionSolution::_instance = NULL; functionCoordinates *functionCoordinates::_instance = NULL;
function *function::getSolution() {
return functionSolution::get();
}
class functionSolutionGradient : public function { function *getFunctionCoordinates() {
static functionSolutionGradient *_instance; return functionCoordinates::get();
functionSolutionGradient():function(0){} // constructor is private only 1 instance can exists, call get to access the instance
public:
void call(dataCacheMap *m, fullMatrix<double> &sol) {
Msg::Error("a function requires the gradient of the solution but this algorithm does not provide the gradient of the solution");
throw;
}
static function *get() {
if(!_instance)
_instance = new functionSolutionGradient();
return _instance;
}
};
functionSolutionGradient *functionSolutionGradient::_instance = NULL;
function *function::getSolutionGradient() {
return functionSolutionGradient::get();
} }
class functionParametricCoordinates : public function {
static functionParametricCoordinates *_instance;
functionParametricCoordinates():function(0, false){} // constructor is private only 1 instance can exists, call get to access the instance
public:
void call(dataCacheMap *m, fullMatrix<double> &sol) {
Msg::Error("a function requires the parametric coordinates but this algorithm does not provide the parametric coordinates");
throw;
}
static function *get() {
if(!_instance)
_instance = new functionParametricCoordinates();
return _instance;
}
};
functionParametricCoordinates *functionParametricCoordinates::_instance = NULL;
function *function::getParametricCoordinates() {
return functionParametricCoordinates::get();
}
class functionNormals : public function { // functionStructuredGridFile
static functionNormals *_instance;
functionNormals():function(0){} // constructor is private only 1 instance can exists, call get to access the instance
public:
void call(dataCacheMap *m, fullMatrix<double> &sol) {
Msg::Error("a function requires the normals coordinates but this algorithm does not provide the normals");
throw;
}
static function *get() {
if(!_instance)
_instance = new functionNormals();
return _instance;
}
};
functionNormals *functionNormals::_instance = NULL;
function *function::getNormals() {
return functionNormals::get();
}
class functionStructuredGridFile : public function { class functionStructuredGridFile : public function {
fullMatrix<double> coord; fullMatrix<double> coord;
...@@ -420,9 +518,8 @@ class functionStructuredGridFile : public function { ...@@ -420,9 +518,8 @@ class functionStructuredGridFile : public function {
input>>o[0]>>o[1]>>o[2]>>d[0]>>d[1]>>d[2]>>n[0]>>n[1]>>n[2]; input>>o[0]>>o[1]>>o[2]>>d[0]>>d[1]>>d[2]>>n[0]>>n[1]>>n[2];
int nt = n[0]*n[1]*n[2]; int nt = n[0]*n[1]*n[2];
v = new double [nt]; v = new double [nt];
for (int i=0; i<nt; i++){ for (int i=0; i<nt; i++)
input>>v[i]; input>>v[i];
}
} else { } else {
input.read((char *)o, 3 * sizeof(double)); input.read((char *)o, 3 * sizeof(double));
input.read((char *)d, 3 * sizeof(double)); input.read((char *)d, 3 * sizeof(double));
...@@ -437,6 +534,9 @@ class functionStructuredGridFile : public function { ...@@ -437,6 +534,9 @@ class functionStructuredGridFile : public function {
} }
}; };
// functionLua
#ifdef HAVE_LUA #ifdef HAVE_LUA
class functionLua : public function { class functionLua : public function {
lua_State *_L; lua_State *_L;
...@@ -454,25 +554,15 @@ class functionLua : public function { ...@@ -454,25 +554,15 @@ class functionLua : public function {
: function(nbCol), _luaFunctionName(luaFunctionName), _L(L) : function(nbCol), _luaFunctionName(luaFunctionName), _L(L)
{ {
args.resize(dependencies.size()); args.resize(dependencies.size());
for (int i = 0; i < dependencies.size(); i++) { setArgument(args[i], dependencies[i]); for (int i = 0; i < dependencies.size(); i++)
} setArgument(args[i], dependencies[i]);
} }
}; };
#endif #endif
void dataCacheMap::setNbEvaluationPoints(int nbEvaluationPoints) {
_nbEvaluationPoints = nbEvaluationPoints;
for(std::set<dataCacheDouble*>::iterator it = _allDataCaches.begin(); it!= _allDataCaches.end(); it++){
(*it)->resize(nbEvaluationPoints);
}
for(std::list<dataCacheMap*>::iterator it = _children.begin(); it!= _children.end(); it++) {
(*it)->setNbEvaluationPoints(nbEvaluationPoints);
}
}
// functionC // functionC
class functionC : public function { class functionC : public function {
std::vector<fullMatrix<double> > args; std::vector<fullMatrix<double> > args;
void (*callback)(void); void (*callback)(void);
...@@ -551,6 +641,12 @@ class functionC : public function { ...@@ -551,6 +641,12 @@ class functionC : public function {
} }
}; };
/*==============================================================================
* Bindings
*============================================================================*/
void function::registerBindings(binding *b){ void function::registerBindings(binding *b){
classBinding *cb = b->addClass<function>("Function"); classBinding *cb = b->addClass<function>("Function");
cb->setDescription("A generic function that can be evaluated on a set of points. Functions can call other functions and their values are cached so that if two different functions call the same function f, f is only evaluated once."); cb->setDescription("A generic function that can be evaluated on a set of points. Functions can call other functions and their values are cached so that if two different functions call the same function f, f is only evaluated once.");
...@@ -619,18 +715,3 @@ void function::registerBindings(binding *b){ ...@@ -619,18 +715,3 @@ void function::registerBindings(binding *b){
#endif #endif
} }
functionConstant *function::_timeFunction = NULL;
functionConstant *function::getTime() {
if (! _timeFunction)
_timeFunction = functionConstantNew(0.);
return _timeFunction;
}
functionConstant *function::_dtFunction = NULL;
functionConstant *function::getDT() {
if (! _dtFunction)
_dtFunction = functionConstantNew(0.);
return _dtFunction;
}
function *getFunctionCoordinates(){
return functionCoordinates::get();
}
...@@ -7,71 +7,162 @@ ...@@ -7,71 +7,162 @@
#include <list> #include <list>
#include <string> #include <string>
#include <vector> #include <vector>
class dataCacheMap;
class MElement;
class binding;
class function;
class dataCacheDouble; class dataCacheDouble;
class dataCacheMap;
class dgDataCacheMap;
class function;
class functionConstant; class functionConstant;
class functionReplace; class functionReplace;
struct functionReplaceCache; struct functionReplaceCache;
class MElement;
class binding;
/*==============================================================================
* class function : An abstract interface to functions
*============================================================================*/
// An abstract interface to functions
// more explanation at the head of this file
class function { class function {
public: public:
int _nbCol;
bool _invalidatedOnElement; // Additionnal types
std::vector<functionReplace*> _functionReplaces;
class dependency { class dependency {
public : unsigned iMap; const function *f; public:
dependency(int iMap_, const function *f_){iMap = iMap_; f = f_; } unsigned iMap;
const function *f;
dependency(int iMap_, const function *f_) {
iMap = iMap_;
f = f_;
}
bool operator < (const dependency &d) const { bool operator < (const dependency &d) const {
return (d.iMap <iMap || d.f < f); return (d.iMap <iMap || d.f < f);
} }
}; };
void printDep() {
for(std::set<dependency>::iterator it = dependencies.begin(); it != dependencies.end(); it++)
printf("%i %p\n", it->iMap, it->f);
}
class argument { class argument {
//iMap is the id of the dataCacheMap, e.g. on interfaces
public: public:
int iMap; const function *f; fullMatrix<double> *val; int iMap; // id of the dataCacheMap, e.g. on interfaces
argument(fullMatrix<double> &v, int iMap_, const function *f_){ val = &v; iMap = iMap_; f = f_; } const function *f;
fullMatrix<double> *val;
argument(fullMatrix<double> &v, int iMap_, const function *f_) {
val = &v;
iMap = iMap_;
f = f_;
}
}; };
// Data
int _nbCol;
bool _invalidatedOnElement;
std::vector<functionReplace*> _functionReplaces;
std::vector<argument> arguments; std::vector<argument> arguments;
std::set<dependency> dependencies; std::set<dependency> dependencies;
void setArgument(fullMatrix<double> &v, const function *f, int iMap = 0);
void addFunctionReplace(functionReplace &fr); private:
static functionConstant *_timeFunction;
static functionConstant *_dtFunction;
public:
function(int nbCol, bool invalidatedOnElement = true); function(int nbCol, bool invalidatedOnElement = true);
virtual ~function(); virtual ~function();
void addFunctionReplace(functionReplace &fr);
void setArgument(fullMatrix<double> &v, const function *f, int iMap = 0);
virtual void call(dataCacheMap *m, fullMatrix<double> &res) = 0; virtual void call(dataCacheMap *m, fullMatrix<double> &res) = 0;
virtual void registerInDataCacheMap(dataCacheMap *m, dataCacheDouble *d) {} virtual void registerInDataCacheMap(dataCacheMap *m, dataCacheDouble *d) {}
// Get or print information
inline bool isInvalitedOnElement() {return _invalidatedOnElement;} inline bool isInvalitedOnElement() {return _invalidatedOnElement;}
inline int getNbCol() const {return _nbCol;} inline int getNbCol() const {return _nbCol;}
static void registerBindings(binding *b); static functionConstant *getTime();
static functionConstant *getDT();
private:
static functionConstant *_timeFunction;
static functionConstant *_dtFunction;
public:
static function *getSolution(); static function *getSolution();
static function *getSolutionGradient(); static function *getSolutionGradient();
static function *getParametricCoordinates(); static function *getParametricCoordinates();
static function *getNormals(); static function *getNormals();
static functionConstant *getTime();
static functionConstant *getDT(); void printDep() {
for (std::set<dependency>::iterator it = dependencies.begin(); it != dependencies.end(); it++)
printf("%i %p\n", it->iMap, it->f);
}
// Bindings
static void registerBindings(binding *b);
};
//------------------------------------
// Additionnal class to get solution
//------------------------------------
class functionSolution : public function {
static functionSolution *_instance;
functionSolution() : function(0) {}; // constructor is private only 1 instance can exists, call get to access the instance
public:
void call(dataCacheMap *m, fullMatrix<double> &sol) {
Msg::Error("a function requires the solution but this algorithm does not provide the solution");
throw;
}
void setNbFields( int nbFields) {
_nbCol = nbFields;
}
static functionSolution *get() {
if(!_instance)
_instance = new functionSolution();
return _instance;
}
};
/*==============================================================================
* class functionReplace
*============================================================================*/
class functionReplace {
friend class dataCacheMap;
friend class dataCacheDouble;
public:
int _nChildren;
function *_master;
functionReplaceCache *currentCache;
std::set <function::dependency> _replaced;
std::set <function::dependency> _fromParent;
std::vector <function::argument> _toReplace;
std::vector <function::argument> _toCompute;
functionReplace();
void get(fullMatrix<double> &v, const function *, int iMap = 0);
void replace(fullMatrix<double> &v, const function *, int iMap = 0);
void compute();
void addChild();
}; };
// dataCache when the value is a matrix of double
// the user should provide the number of rows by evaluating points and the number of columns
// then the size of the matrix is automatically adjusted /*==============================================================================
* class dataCacheDouble :
* dataCache when the value is a matrix of double
* the user should provide the number of rows by evaluating points and the number of columns
* then the size of the matrix is automatically adjusted
*============================================================================*/
class dataCacheDouble { class dataCacheDouble {
friend class dataCacheMap; friend class dataCacheMap;
friend class dgDataCacheMap; friend class dgDataCacheMap;
...@@ -87,7 +178,6 @@ class dataCacheDouble { ...@@ -87,7 +178,6 @@ class dataCacheDouble {
fullMatrix<double> _value; fullMatrix<double> _value;
bool _valid; bool _valid;
dataCacheDouble(dataCacheMap *,function *f); dataCacheDouble(dataCacheMap *,function *f);
// do the actual computation and put the result into _value // do the actual computation and put the result into _value
void _eval(); void _eval();
void resize(int nrow); void resize(int nrow);
...@@ -123,8 +213,11 @@ class dataCacheDouble { ...@@ -123,8 +213,11 @@ class dataCacheDouble {
}; };
class dgDataCacheMap;
// more explanation at the head of this file /*==============================================================================
* class dataCacheMap
*============================================================================*/
class dataCacheMap { class dataCacheMap {
public: public:
dataCacheMap *_parent; dataCacheMap *_parent;
...@@ -134,9 +227,12 @@ class dataCacheMap { ...@@ -134,9 +227,12 @@ class dataCacheMap {
std::map<const function*, dataCacheDouble*> _cacheDoubleMap; std::map<const function*, dataCacheDouble*> _cacheDoubleMap;
std::set<dataCacheDouble*> _allDataCaches; std::set<dataCacheDouble*> _allDataCaches;
std::set<dataCacheDouble*> _toInvalidateOnElement; std::set<dataCacheDouble*> _toInvalidateOnElement;
MElement *_element; MElement *_element;
dataCacheMap() {
_nbEvaluationPoints = 0;
_parent=NULL;
}
~dataCacheMap();
void addDataCacheDouble(dataCacheDouble *data, bool invalidatedOnElement) { void addDataCacheDouble(dataCacheDouble *data, bool invalidatedOnElement) {
_allDataCaches.insert(data); _allDataCaches.insert(data);
if(invalidatedOnElement) if(invalidatedOnElement)
...@@ -165,10 +261,6 @@ class dataCacheMap { ...@@ -165,10 +261,6 @@ class dataCacheMap {
} }
} }
inline MElement *getElement() {return _element;} inline MElement *getElement() {return _element;}
dataCacheMap() {
_nbEvaluationPoints = 0;
_parent=NULL;
}
virtual dataCacheMap *newChild() { virtual dataCacheMap *newChild() {
dataCacheMap *m = new dataCacheMap(); dataCacheMap *m = new dataCacheMap();
m->_parent = this; m->_parent = this;
...@@ -177,70 +269,43 @@ class dataCacheMap { ...@@ -177,70 +269,43 @@ class dataCacheMap {
return m; return m;
} }
void setNbEvaluationPoints(int nbEvaluationPoints); void setNbEvaluationPoints(int nbEvaluationPoints);
inline int getNbEvaluationPoints() {return _nbEvaluationPoints;} inline int getNbEvaluationPoints() {return _nbEvaluationPoints;}
~dataCacheMap();
};
class functionReplace {
friend class dataCacheMap;
friend class dataCacheDouble;
public :
function *_master;
int _nChildren;
functionReplaceCache *currentCache;
std::set <function::dependency> _replaced;
std::set <function::dependency> _fromParent;
std::vector <function::argument> _toReplace;
std::vector <function::argument> _toCompute;
void get(fullMatrix<double> &v, const function *, int iMap = 0);
void replace(fullMatrix<double> &v, const function *, int iMap = 0);
void compute ();
functionReplace();
void addChild();
}; };
functionConstant *functionConstantNew(const std::vector<double>&);
functionConstant *functionConstantNew(double);
function *functionSumNew (const function *f0, const function *f1);
function *functionProdNew (const function *f0, const function *f1);
function *functionScaleNew (const function *f0, const double s);
function *functionExtractCompNew (const function *f0, const int iComp);
class functionSolution : public function { /*==============================================================================
static functionSolution *_instance; * Some example of functions
functionSolution():function(0){};// constructor is private only 1 instance can exists, call get to access the instance *============================================================================*/
public:
void call(dataCacheMap *m, fullMatrix<double> &sol) { // functionConstant (constant values copied over each line)
Msg::Error("a function requires the solution but this algorithm does not provide the solution");
throw;
}
void setNbFields( int nbFields) {
_nbCol = nbFields;
}
static functionSolution *get() {
if(!_instance)
_instance = new functionSolution();
return _instance;
}
};
class functionConstant : public function { class functionConstant : public function {
public: public:
fullMatrix<double> _source; fullMatrix<double> _source;
void call(dataCacheMap *m, fullMatrix<double> &val) { void call(dataCacheMap *m, fullMatrix<double> &val) {
for (int i=0; i<val.size1(); i++) for (int i=0; i<val.size1(); i++)
for(int j=0;j<_source.size1();j++){ for (int j=0; j<_source.size1(); j++)
val(i,j)=_source(j,0); val(i,j)=_source(j,0);
} }
}
functionConstant(std::vector<double> source) : function(source.size()) { functionConstant(std::vector<double> source) : function(source.size()) {
_source = fullMatrix<double>(source.size(),1); _source = fullMatrix<double>(source.size(),1);
for(size_t i=0; i<source.size(); i++){ for (size_t i=0; i<source.size(); i++)
_source(i,0) = source[i]; _source(i,0) = source[i];
} }
}
void set(double val); void set(double val);
}; };
functionConstant *functionConstantNew(const std::vector<double>&);
functionConstant *functionConstantNew(double);
function *functionSumNew (const function *f0, const function *f1);
function *functionProdNew (const function *f0, const function *f1);
function *functionScaleNew (const function *f0, const double s);
function *functionExtractCompNew (const function *f0, const int iComp);
function *getFunctionCoordinates(); function *getFunctionCoordinates();
#endif #endif
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment