Skip to content
Snippets Groups Projects
Commit f6ded005 authored by Jonathan Lambrechts's avatar Jonathan Lambrechts
Browse files

dg : terms are functions #1

parent a6de7544
No related branches found
No related tags found
No related merge requests found
...@@ -10,16 +10,9 @@ ...@@ -10,16 +10,9 @@
#endif #endif
#include "Bindings.h" #include "Bindings.h"
void function::call (dataCacheMap *m, fullMatrix<double> &res, std::vector<const fullMatrix<double>*> &depM) { function::~function() {
switch (arguments.size()) { for (int i=0; i<arguments.size(); i++) {
case 0 : call(m, res); break; delete arguments[i];
case 1 : call(m, *depM[0], res); break;
case 2 : call(m, *depM[0], *depM[1], res); break;
case 3 : call(m, *depM[0], *depM[1], *depM[2], res); break;
case 4 : call(m, *depM[0], *depM[1], *depM[2], *depM[3], res); break;
case 5 : call(m, *depM[0], *depM[1], *depM[2], *depM[3], *depM[4], res); break;
case 6 : call(m, *depM[0], *depM[1], *depM[2], *depM[3], *depM[4], *depM[5], res); break;
default : Msg::Error("function are not implemented for %i arguments\n", arguments.size());
} }
} }
function::function(int nbCol, bool invalidatedOnElement):_nbCol(nbCol), _invalidatedOnElement(invalidatedOnElement){}; function::function(int nbCol, bool invalidatedOnElement):_nbCol(nbCol), _invalidatedOnElement(invalidatedOnElement){};
...@@ -50,15 +43,23 @@ dataCacheDouble::dataCacheDouble(dataCacheMap *m, function *f): ...@@ -50,15 +43,23 @@ dataCacheDouble::dataCacheDouble(dataCacheMap *m, function *f):
m->addDataCacheDouble(this, f->isInvalitedOnElement()); m->addDataCacheDouble(this, f->isInvalitedOnElement());
_function = f; _function = f;
_dependencies.resize ( _function->arguments.size()); _dependencies.resize ( _function->arguments.size());
_depM.resize (_function->arguments.size()); for (unsigned int i=0;i<_function->arguments.size();i++) {
for (unsigned int i=0;i<_function->arguments.size();i++) int iCache = _function->arguments[i]->iMap;
_dependencies[i] = &m[_function->arguments[i].first].get(_function->arguments[i].second,this); const function *f = _function->arguments[i]->f;
_dependencies[i] = &m->getSecondaryCache(iCache)->get(f,this);
}
} }
void dataCacheDouble::resize() { void dataCacheDouble::resize() {
_value = fullMatrix<double>(_nRowByPoint==0?1:_nRowByPoint*_cacheMap.getNbEvaluationPoints(),_value.size2()); _value = fullMatrix<double>(_nRowByPoint==0?1:_nRowByPoint*_cacheMap.getNbEvaluationPoints(),_value.size2());
} }
void dataCacheDouble::_eval() {
for(unsigned int i=0;i<_dependencies.size(); i++){
_function->arguments[i]->val.setAsProxy((*_dependencies[i])());
}
_function->call(&_cacheMap, _value);
}
//dataCacheMap members //dataCacheMap members
dataCacheDouble &dataCacheMap::get(const function *f, dataCacheDouble *caller) dataCacheDouble &dataCacheMap::get(const function *f, dataCacheDouble *caller)
...@@ -106,9 +107,10 @@ class functionConstant : public function { ...@@ -106,9 +107,10 @@ class functionConstant : public function {
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++){
...@@ -130,7 +132,8 @@ function *functionConstantNew(const std::vector<double> &v) { ...@@ -130,7 +132,8 @@ function *functionConstantNew(const std::vector<double> &v) {
// get XYZ coordinates // get XYZ coordinates
class functionCoordinates : public function { class functionCoordinates : public function {
static functionCoordinates *_instance; static functionCoordinates *_instance;
void call (dataCacheMap *m, const fullMatrix<double> &uvw, fullMatrix<double> &xyz){ const fullMatrix<double> &uvw;
void call (dataCacheMap *m, fullMatrix<double> &xyz){
for(int i = 0; i < uvw.size1(); i++){ for(int i = 0; i < uvw.size1(); i++){
SPoint3 p; SPoint3 p;
m->getElement()->pnt(uvw(i, 0), uvw(i, 1), uvw(i, 2), p); m->getElement()->pnt(uvw(i, 0), uvw(i, 1), uvw(i, 2), p);
...@@ -139,8 +142,8 @@ class functionCoordinates : public function { ...@@ -139,8 +142,8 @@ class functionCoordinates : public function {
xyz(i, 2) = p.z(); xyz(i, 2) = p.z();
} }
} }
functionCoordinates():function(3){ functionCoordinates():function(3),
addArgument(function::getParametricCoordinates()); uvw(addArgument(function::getParametricCoordinates())){
};// constructor is private only 1 instance can exists, call get to access the instance };// constructor is private only 1 instance can exists, call get to access the instance
public: public:
static function *get() { static function *get() {
...@@ -228,6 +231,7 @@ function *function::getNormals() { ...@@ -228,6 +231,7 @@ function *function::getNormals() {
} }
class functionStructuredGridFile : public function { class functionStructuredGridFile : public function {
const fullMatrix<double> &coord;
public: public:
int n[3]; int n[3];
double d[3],o[3]; double d[3],o[3];
...@@ -235,7 +239,7 @@ class functionStructuredGridFile : public function { ...@@ -235,7 +239,7 @@ class functionStructuredGridFile : public function {
return v[(i*n[1]+j)*n[2]+k]; return v[(i*n[1]+j)*n[2]+k];
} }
double *v; double *v;
void call(dataCacheMap *m, const fullMatrix<double> &coord, fullMatrix<double> &val){ void call(dataCacheMap *m, fullMatrix<double> &val){
for(int pt=0;pt<val.size1();pt++){ for(int pt=0;pt<val.size1();pt++){
double xi[3]; double xi[3];
int id[3]; int id[3];
...@@ -257,9 +261,8 @@ class functionStructuredGridFile : public function { ...@@ -257,9 +261,8 @@ class functionStructuredGridFile : public function {
+get(id[0]+1 ,id[1]+1 ,id[2]+1 )*( xi[0])*( xi[1])*( xi[2]); +get(id[0]+1 ,id[1]+1 ,id[2]+1 )*( xi[0])*( xi[1])*( xi[2]);
} }
} }
functionStructuredGridFile(const std::string filename, const function *coordFunction): function(1){ functionStructuredGridFile(const std::string filename, const function *coordFunction): function(1), coord(addArgument(coordFunction)){
std::ifstream input(filename.c_str()); std::ifstream input(filename.c_str());
addArgument(coordFunction);
if(!input) if(!input)
Msg::Error("cannot open file : %s",filename.c_str()); Msg::Error("cannot open file : %s",filename.c_str());
if(filename.substr(filename.size()-4,4)!=".bin") { if(filename.substr(filename.size()-4,4)!=".bin") {
...@@ -288,12 +291,12 @@ class functionLua : public function { ...@@ -288,12 +291,12 @@ class functionLua : public function {
lua_State *_L; lua_State *_L;
std::string _luaFunctionName; std::string _luaFunctionName;
public: public:
void call (dataCacheMap *m, fullMatrix<double> &res, std::vector<const fullMatrix<double>*> &depM) { void call (dataCacheMap *m, fullMatrix<double> &res) {
lua_getfield(_L, LUA_GLOBALSINDEX, _luaFunctionName.c_str()); lua_getfield(_L, LUA_GLOBALSINDEX, _luaFunctionName.c_str());
for (int i=0;i< depM.size(); i++) for (int i=0;i< arguments.size(); i++)
luaStack<const fullMatrix<double>*>::push(_L, depM[i]); luaStack<const fullMatrix<double>*>::push(_L, &arguments[i]->val);
luaStack<const fullMatrix<double>*>::push(_L, &res); luaStack<const fullMatrix<double>*>::push(_L, &res);
lua_call(_L, depM.size()+1, 0); lua_call(_L, arguments.size()+1, 0);
} }
functionLua (int nbCol, std::string luaFunctionName, std::vector<const function*> dependencies, lua_State *L) functionLua (int nbCol, std::string luaFunctionName, std::vector<const function*> dependencies, lua_State *L)
: function(nbCol), _luaFunctionName(luaFunctionName), _L(L) : function(nbCol), _luaFunctionName(luaFunctionName), _L(L)
...@@ -322,40 +325,40 @@ void dataCacheMap::setNbEvaluationPoints(int nbEvaluationPoints) { ...@@ -322,40 +325,40 @@ void dataCacheMap::setNbEvaluationPoints(int nbEvaluationPoints) {
class functionC : public function { class functionC : public function {
void (*callback)(void); void (*callback)(void);
public: public:
void call (dataCacheMap *m, fullMatrix<double> &val, std::vector<const fullMatrix<double>*> &depM) { void call (dataCacheMap *m, fullMatrix<double> &val) {
switch (depM.size()) { switch (arguments.size()) {
case 0 : case 0 :
((void (*)(fullMatrix<double> &))(callback))(val); ((void (*)(fullMatrix<double> &))(callback))(val);
break; break;
case 1 : case 1 :
((void (*)(fullMatrix<double> &, const fullMatrix<double>&)) ((void (*)(fullMatrix<double> &, const fullMatrix<double>&))
(callback)) (val, *depM[0]); (callback)) (val, arguments[0]->val);
break; break;
case 2 : case 2 :
((void (*)(fullMatrix<double> &, const fullMatrix<double>&, const fullMatrix<double> &)) ((void (*)(fullMatrix<double> &, const fullMatrix<double>&, const fullMatrix<double> &))
(callback)) (val, *depM[0], *depM[1]); (callback)) (val, arguments[0]->val, arguments[1]->val);
break; break;
case 3 : case 3 :
((void (*)(fullMatrix<double> &, const fullMatrix<double>&, const fullMatrix<double>&, const fullMatrix<double>&)) ((void (*)(fullMatrix<double> &, const fullMatrix<double>&, const fullMatrix<double>&, const fullMatrix<double>&))
(callback)) (val, *depM[0], *depM[1], *depM[2]); (callback)) (val, arguments[0]->val, arguments[1]->val, arguments[2]->val);
break; break;
case 4 : case 4 :
((void (*)(fullMatrix<double> &, const fullMatrix<double>&, const fullMatrix<double>&, const fullMatrix<double>&, ((void (*)(fullMatrix<double> &, const fullMatrix<double>&, const fullMatrix<double>&, const fullMatrix<double>&,
const fullMatrix<double>&)) const fullMatrix<double>&))
(callback)) (val, *depM[0], *depM[1], *depM[2], *depM[3]); (callback)) (val, arguments[0]->val, arguments[1]->val, arguments[2]->val, arguments[3]->val);
break; break;
case 5 : case 5 :
((void (*)(fullMatrix<double> &, const fullMatrix<double>&, const fullMatrix<double>&, const fullMatrix<double>&, ((void (*)(fullMatrix<double> &, const fullMatrix<double>&, const fullMatrix<double>&, const fullMatrix<double>&,
const fullMatrix<double>&, const fullMatrix<double>&)) const fullMatrix<double>&, const fullMatrix<double>&))
(callback)) (val, *depM[0], *depM[1], *depM[2], *depM[3], *depM[4]); (callback)) (val, arguments[0]->val, arguments[1]->val, arguments[2]->val, arguments[3]->val, arguments[4]->val);
break; break;
case 6 : case 6 :
((void (*)(fullMatrix<double> &, const fullMatrix<double>&, const fullMatrix<double>&, const fullMatrix<double>&, ((void (*)(fullMatrix<double> &, const fullMatrix<double>&, const fullMatrix<double>&, const fullMatrix<double>&,
const fullMatrix<double>&, const fullMatrix<double>&, const fullMatrix<double>&)) const fullMatrix<double>&, const fullMatrix<double>&, const fullMatrix<double>&))
(callback)) (val, *depM[0], *depM[1], *depM[2], *depM[3], *depM[4], *depM[5]); (callback)) (val, arguments[0]->val, arguments[1]->val, arguments[2]->val, arguments[3]->val, arguments[4]->val, arguments[5]->val);
break; break;
default : default :
Msg::Error("C callback not implemented for %i argurments", depM.size()); Msg::Error("C callback not implemented for %i argurments", arguments.size());
} }
} }
functionC (std::string file, std::string symbol, int nbCol, std::vector<const function *> dependencies): functionC (std::string file, std::string symbol, int nbCol, std::vector<const function *> dependencies):
......
...@@ -34,25 +34,29 @@ class dataCacheDouble; ...@@ -34,25 +34,29 @@ class dataCacheDouble;
// An abstract interface to functions // An abstract interface to functions
// more explanation at the head of this file // more explanation at the head of this file
class function { class function {
class argument {
//iMap is the id of the dataCacheMap, e.g. on interfaces
public:
int iMap;
const function *f;
fullMatrix<double> val;
argument(int iMap_, const function *f_) {
iMap = iMap_;
f = f_;
}
};
int _nbCol; int _nbCol;
bool _invalidatedOnElement; bool _invalidatedOnElement;
protected : protected :
virtual void call (dataCacheMap *m, fullMatrix<double> &res) {throw;}
virtual void call (dataCacheMap *m, const fullMatrix<double> &arg0, fullMatrix<double> &res) {throw;};
virtual void call (dataCacheMap *m, const fullMatrix<double> &arg0, const fullMatrix<double> &arg1, fullMatrix<double> &res) {throw;};
virtual void call (dataCacheMap *m, const fullMatrix<double> &arg0, const fullMatrix<double> &arg1, const fullMatrix<double> &arg2, fullMatrix<double> &res) {throw;};
virtual void call (dataCacheMap *m, const fullMatrix<double> &arg0, const fullMatrix<double> &arg1, const fullMatrix<double> &arg2, const fullMatrix<double> &arg3, fullMatrix<double> &res) {throw;};
virtual void call (dataCacheMap *m, const fullMatrix<double> &arg0, const fullMatrix<double> &arg1, const fullMatrix<double> &arg2, const fullMatrix<double> &arg3, const fullMatrix<double> &arg4, fullMatrix<double> &res) {throw;};
virtual void call (dataCacheMap *m, const fullMatrix<double> &arg0, const fullMatrix<double> &arg1, const fullMatrix<double> &arg2, const fullMatrix<double> &arg3, const fullMatrix<double> &arg4, const fullMatrix<double> &arg5, fullMatrix<double> &res) {throw;};
public : public :
std::vector<std::pair<int, const function*> > arguments; virtual void call (dataCacheMap *m, fullMatrix<double> &res)=0;
void addArgument(const function *f, int iMap = 0) { std::vector<argument*> arguments;
//iMap is the id of the dataCacheMap, e.g. on interfaces const fullMatrix<double> &addArgument(const function *f, int iMap = 0) {
arguments.push_back(std::pair<int, const function*>(iMap, f)); arguments.push_back(new argument(iMap, f));
return arguments.back()->val;
} }
virtual ~function(){}; virtual ~function();
static void registerBindings(binding *b); static void registerBindings(binding *b);
virtual void call (dataCacheMap *m, fullMatrix<double> &res, std::vector<const fullMatrix<double>*> &depM);
function(int nbCol, bool invalidatedOnElement = true); function(int nbCol, bool invalidatedOnElement = true);
inline int getNbCol()const {return _nbCol;} inline int getNbCol()const {return _nbCol;}
inline bool isInvalitedOnElement() { return _invalidatedOnElement;} inline bool isInvalitedOnElement() { return _invalidatedOnElement;}
...@@ -91,21 +95,15 @@ public : ...@@ -91,21 +95,15 @@ public :
return (_iDependOn.find(&other)!=_iDependOn.end()); return (_iDependOn.find(&other)!=_iDependOn.end());
} }
std::vector<dataCacheDouble*> _dependencies; std::vector<dataCacheDouble*> _dependencies;
std::vector<const fullMatrix<double>*> _depM;
int _nRowByPoint; int _nRowByPoint;
dataCacheMap &_cacheMap;
function *_function; function *_function;
protected: protected:
dataCacheMap &_cacheMap;
fullMatrix<double> _value; fullMatrix<double> _value;
// do the actual computation and put the result into _value // do the actual computation and put the result into _value
// still virtual because it is overrided by conservation law terms, as soon as conservation law terms will be regular functions, we will remove this // still virtual because it is overrided by conservation law terms, as soon as conservation law terms will be regular functions, we will remove this
virtual void _eval() virtual void _eval();
{
for(unsigned int i=0;i<_dependencies.size(); i++)
_depM[i] = &(*_dependencies[i])();
_function->call(&_cacheMap, _value, _depM);
}
public: public:
//set the value (without computing it by _eval) and invalidate the dependencies //set the value (without computing it by _eval) and invalidate the dependencies
// this function is needed to be able to pass the _value to functions like gemm or mult // this function is needed to be able to pass the _value to functions like gemm or mult
...@@ -143,11 +141,13 @@ public : ...@@ -143,11 +141,13 @@ public :
}; };
class dgDataCacheMap;
// more explanation at the head of this file // more explanation at the head of this file
class dataCacheMap { class dataCacheMap {
friend class dataCacheDouble; friend class dataCacheDouble;
dataCacheMap *_parent; dataCacheMap *_parent;
std::list<dataCacheMap*> _children; std::list<dataCacheMap*> _children;
std::vector<dataCacheMap*> _secondaryCaches;
int _nbEvaluationPoints; int _nbEvaluationPoints;
std::map<const function*, dataCacheDouble*> _cacheDoubleMap; std::map<const function*, dataCacheDouble*> _cacheDoubleMap;
std::set<dataCacheDouble*> _allDataCaches; std::set<dataCacheDouble*> _allDataCaches;
...@@ -162,9 +162,21 @@ class dataCacheMap { ...@@ -162,9 +162,21 @@ class dataCacheMap {
_toInvalidateOnElement.insert(data); _toInvalidateOnElement.insert(data);
} }
public: public:
virtual dgDataCacheMap *asDgDataCacheMap() {
Msg::Error("I'm not a dgDataCacheMap\n");
return NULL;
}
dataCacheMap *getSecondaryCache(int i) {
if (i==0)
return this;
return _secondaryCaches[i-1];
}
void addSecondaryCache(dataCacheMap *s) {
_secondaryCaches.push_back(s);
}
dataCacheDouble &get(const function *f, dataCacheDouble *caller=0); dataCacheDouble &get(const function *f, dataCacheDouble *caller=0);
dataCacheDouble &substitute(const function *f); dataCacheDouble &substitute(const function *f);
inline void setElement(MElement *element) { virtual void setElement(MElement *element) {
_element=element; _element=element;
for(std::set<dataCacheDouble*>::iterator it = _toInvalidateOnElement.begin(); it!= _toInvalidateOnElement.end(); it++) { for(std::set<dataCacheDouble*>::iterator it = _toInvalidateOnElement.begin(); it!= _toInvalidateOnElement.end(); it++) {
(*it)->_valid=false; (*it)->_valid=false;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment