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

luna template binding

parent abf2c6cf
No related branches found
No related tags found
No related merge requests found
Showing
with 837 additions and 592 deletions
#ifndef _LUNA_SIGNATURE_H_
#define _LUNA_SIGNATURE_H_
extern "C" {
#include "lua.h"
#include "lauxlib.h"
}
#include <vector>
class binding {
};
class methodBinding {
public:
std::string _luaname;
virtual int call (lua_State *L)=0;
methodBinding(const std::string luaname){
_luaname=luaname;
}
};
class constructorBinding {
public:
virtual int call (lua_State *L)=0;
};
template <typename T> class classBinding {
typedef struct { T *pT; bool owned;} userdataType;
public:
static void push(lua_State *L, T* obj, bool owned) {
userdataType *ud = static_cast<userdataType*>(lua_newuserdata(L, sizeof(userdataType)));
ud->pT=obj;
ud->owned=owned;
luaL_getmetatable(L, T::className); // lookup metatable in Lua registry
lua_setmetatable(L, -2);
}
static void Register(lua_State *L) {
lua_newtable(L);
int methods = lua_gettop(L); //-- create methods , i.e. the class itself
luaL_newmetatable(L, T::className);
int metatable = lua_gettop(L); // -- create metatable of methods
// store method table in globals so that
// scripts can add functions written in Lua.
lua_pushstring(L, T::className);
lua_pushvalue(L, methods);
lua_settable(L, LUA_GLOBALSINDEX); // -- global[T::className] = methods
// lua_pushliteral(L, "__metatable");
// lua_pushvalue(L, methods);
// lua_settable(L, metatable); // hide metatable from Lua getmetatable()
lua_pushliteral(L, "__index");
lua_pushvalue(L, methods);
lua_settable(L, metatable); // metatable.__index=methods
lua_pushliteral(L, "__tostring");
lua_pushcfunction(L, tostring_T);
lua_settable(L, metatable);
lua_pushliteral(L, "__gc");
lua_pushcfunction(L, gc_T);
lua_settable(L, metatable);
lua_newtable(L); // mt for method table
int mt = lua_gettop(L);
lua_pushliteral(L, "__call");
lua_pushlightuserdata(L,(void*)T::constructorMethod);
lua_pushcclosure(L, callConstructor, 1);
lua_pushliteral(L, "new");
lua_pushvalue(L, -2); // dup new_T function
lua_settable(L, methods); // add new_T to method table
lua_settable(L, mt); // mt.__call = new_T
if(T::parentClassName!=std::string("")){
lua_pushliteral(L,"__index");
lua_pushstring(L,T::parentClassName);
lua_gettable(L,LUA_GLOBALSINDEX);
lua_settable(L,mt); // mt.__index = global[T::parentClassName] // this is the inheritance bit
}
lua_setmetatable(L, methods); // setmetatable(methods, mt)
// fill method table with methods from class T
for (methodBinding **l = T::methods; *l; l++) {
lua_pushstring(L, (*l)->_luaname.c_str());
lua_pushlightuserdata(L, (void*)*l);
lua_pushcclosure(L, callMethod, 1);
lua_settable(L, methods); //methods.(l->name) = callMethod(l)
}
lua_pop(L, 2); // drop metatable and method table
}
// get userdata from Lua stack and return pointer to T object
static T *check(lua_State *L, int narg) {
userdataType *ud = static_cast<userdataType*>(lua_touserdata(L, narg));
if(!ud) luaL_typerror(L, narg, T::className);
return ud->pT; // pointer to T object*/
}
private:
classBinding(); // hide default constructor
// member function dispatcher
static int callMethod(lua_State *L) {
methodBinding *l = static_cast<methodBinding*>(lua_touserdata(L, lua_upvalueindex(1)));
return l->call(L); // call member function
}
static int callConstructor(lua_State *L) {
constructorBinding *l = static_cast<constructorBinding*>(lua_touserdata(L, lua_upvalueindex(1)));
if(!l){
printf("this class does not have constructor\n");
//throw();
}
return l->call(L); // call member function
}
// garbage collection metamethod
static int gc_T(lua_State *L) {
userdataType *ud = static_cast<userdataType*>(lua_touserdata(L, 1));
/*if(ud->owned) //disable gc
delete ud->pT; // call destructor for T objects*/
return 0;
}
static int tostring_T (lua_State *L) {
char buff[32];
userdataType *ud = static_cast<userdataType*>(lua_touserdata(L, 1));
T *t = static_cast<T*>(lua_touserdata(L, 1));
sprintf(buff, "%p", ud->pT);
lua_pushfstring(L, "%s (%s)", T::className, buff);
return 1;
}
};
template<class type>
class lua_template {
public:
static type get(lua_State *L, int ia){
printf("error cannot get generic class in lua, only pointers are implemented\n");
}
static void push(lua_State *L, type obj){
printf("error cannot push generic class in lua, only pointers are implemented\n");
}
};
template <class type>
class lua_template<type*>{
public:
static type* get(lua_State *L, int ia){
type *a=classBinding<type>::check(L,ia);
return a;
}
static void push(lua_State *L, type *obj){
classBinding<type>::push(L,obj,false);
}
};
template<>
class lua_template<lua_State *>{
public:
static lua_State *get(lua_State *L, int ia){
return L;
}
static void push(lua_State *L, int i){
printf("error cannot push a lua_State in lua\n");
}
};
template<>
class lua_template<int>{
public:
static int get(lua_State *L, int ia){
int a= luaL_checkint(L,ia);
return a;
}
static void push(lua_State *L, int i){
lua_pushinteger(L,i);
}
};
template<class type>
class lua_template<std::vector<type > >{
public:
static std::vector<type> get(lua_State *L, int ia){
std::vector<type> v;
size_t size=lua_objlen(L,ia);
v.resize(size);
for(size_t i=0;i<size;i++){
lua_pushinteger(L,i+1);
lua_gettable(L,ia);
v[i]=lua_template<type>::get(L,-1);
lua_pop(L,1);
}
return v;
}
static void push(lua_State *L, std::vector<type> a){
}
};
template<>
class lua_template<double>{
public:
static double get(lua_State *L, int ia){
return luaL_checknumber(L,ia);
}
static void push(lua_State *L, double v){
lua_pushnumber(L,v);
}
};
template<>
class lua_template<std::string>{
public:
static std::string get(lua_State *L, int ia){
return luaL_checkstring(L,ia);
}
static void push(lua_State *L, std::string s){
lua_pushstring(L,s.c_str());
}
};
//full : 4 args with return
template <class objectType, class returnType=void, class arg0Type=void, class arg1Type=void, class arg2Type=void, class arg3Type=void>
class methodBindingTemplate:public methodBinding {
typedef returnType (objectType::*callback)(arg0Type,arg1Type,arg2Type,arg3Type);
callback _f;
public:
methodBindingTemplate(const std::string luaname,callback f):methodBinding(luaname){
_f=f;
}
int call (lua_State *L) {
objectType *obj = classBinding<objectType>::check(L,1);
arg0Type a0 = lua_template<arg0Type>::get(L,2);
arg1Type a1 = lua_template<arg1Type>::get(L,3);
arg2Type a2 = lua_template<arg2Type>::get(L,4);
arg3Type a3 = lua_template<arg3Type>::get(L,5);
returnType r=(obj->*(_f))(a0,a1,a2,a3);
lua_template<returnType>::push(L,r);
return 1;
}
};
//3 args with return
template <class objectType, class returnType, class arg0Type, class arg1Type, class arg2Type>
class methodBindingTemplate<objectType,returnType,arg0Type,arg1Type,arg2Type,void>:public methodBinding {
typedef returnType (objectType::*callback)(arg0Type,arg1Type,arg2Type);
callback _f;
public:
methodBindingTemplate(const std::string luaname,callback f):methodBinding(luaname){
_f=f;
}
int call (lua_State *L) {
objectType *obj = classBinding<objectType>::check(L,1);
arg0Type a0 = lua_template<arg0Type>::get(L,2);
arg1Type a1 = lua_template<arg1Type>::get(L,3);
arg2Type a2 = lua_template<arg2Type>::get(L,4);
returnType r=(obj->*(_f))(a0,a1,a2);
lua_template<returnType>::push(L,r);
return 1;
}
};
//2 args with return
template <class objectType, class returnType, class arg0Type, class arg1Type>
class methodBindingTemplate<objectType,returnType,arg0Type,arg1Type,void,void>:public methodBinding {
typedef returnType (objectType::*callback)(arg0Type,arg1Type);
callback _f;
public:
methodBindingTemplate(const std::string luaname,callback f):methodBinding(luaname){
_f=f;
}
int call (lua_State *L) {
objectType *obj = classBinding<objectType>::check(L,1);
arg0Type a0 = lua_template<arg0Type>::get(L,2);
arg1Type a1 = lua_template<arg1Type>::get(L,3);
returnType r=(obj->*(_f))(a0,a1);
lua_template<returnType>::push(L,r);
return 1;
}
};
//1 arg with return
template <class objectType, class returnType, class arg0Type>
class methodBindingTemplate<objectType,returnType,arg0Type,void,void,void>:public methodBinding {
typedef returnType (objectType::*callback)(arg0Type);
callback _f;
public:
methodBindingTemplate(const std::string luaname,callback f):methodBinding(luaname){
_f=f;
}
int call (lua_State *L) {
objectType *obj = classBinding<objectType>::check(L,1);
arg0Type a0 = lua_template<arg0Type>::get(L,2);
returnType r=(obj->*(_f))(a0);
lua_template<returnType>::push(L,r);
return 1;
}
};
//0 arg with return
template <class objectType, class returnType>
class methodBindingTemplate<objectType,returnType,void,void,void,void>:public methodBinding {
typedef returnType (objectType::*callback)();
callback _f;
public:
methodBindingTemplate(const std::string luaname,callback f):methodBinding(luaname){
_f=f;
}
int call (lua_State *L) {
objectType *obj = classBinding<objectType>::check(L,1);
returnType r=(obj->*(_f))();
lua_template<returnType>::push(L,r);
return 1;
}
};
//4 args without return
template <class objectType, class arg0Type, class arg1Type, class arg2Type, class arg3Type>
class methodBindingTemplate<objectType,void,arg0Type,arg1Type,arg2Type,arg3Type>:public methodBinding {
typedef void (objectType::*callback)(arg0Type,arg1Type,arg2Type,arg3Type);
callback _f;
public:
methodBindingTemplate(const std::string luaname,callback f):methodBinding(luaname){
_f=f;
}
int call (lua_State *L) {
objectType *obj = classBinding<objectType>::check(L,1);
arg0Type a0 = lua_template<arg0Type>::get(L,2);
arg1Type a1 = lua_template<arg1Type>::get(L,3);
arg2Type a2 = lua_template<arg2Type>::get(L,4);
arg3Type a3 = lua_template<arg3Type>::get(L,5);
(obj->*(_f))(a0,a1,a2,a3);
return 0;
}
};
//3 args without return
template <class objectType, class arg0Type, class arg1Type, class arg2Type>
class methodBindingTemplate<objectType,void,arg0Type,arg1Type,arg2Type,void>:public methodBinding {
typedef void (objectType::*callback)(arg0Type,arg1Type,arg2Type);
callback _f;
public:
methodBindingTemplate(const std::string luaname,callback f):methodBinding(luaname){
_f=f;
}
int call (lua_State *L) {
objectType *obj = classBinding<objectType>::check(L,1);
arg0Type a0 = lua_template<arg0Type>::get(L,2);
arg1Type a1 = lua_template<arg1Type>::get(L,3);
arg2Type a2 = lua_template<arg2Type>::get(L,4);
(obj->*(_f))(a0,a1,a2);
return 0;
}
};
//2 args without return
template <class objectType, class arg0Type, class arg1Type>
class methodBindingTemplate<objectType,void,arg0Type,arg1Type,void,void>:public methodBinding {
typedef void (objectType::*callback)(arg0Type,arg1Type);
callback _f;
public:
methodBindingTemplate(const std::string luaname,callback f):methodBinding(luaname){
_f=f;
}
int call (lua_State *L) {
objectType *obj = classBinding<objectType>::check(L,1);
arg0Type a0 = lua_template<arg0Type>::get(L,2);
arg1Type a1 = lua_template<arg1Type>::get(L,3);
(obj->*(_f))(a0,a1);
return 0;
}
};
//1 arg without return
template <class objectType, class arg0Type>
class methodBindingTemplate<objectType,void,arg0Type,void,void,void>:public methodBinding {
typedef void (objectType::*callback)(arg0Type);
callback _f;
public:
methodBindingTemplate(const std::string luaname,callback f):methodBinding(luaname){
_f=f;
}
int call (lua_State *L) {
objectType *obj = classBinding<objectType>::check(L,1);
arg0Type a0 = lua_template<arg0Type>::get(L,2);
(obj->*(_f))(a0);
return 0;
}
};
//0 arg without return
template <class objectType>
class methodBindingTemplate<objectType,void,void,void,void,void>:public methodBinding {
typedef void (objectType::*callback)();
callback _f;
public:
methodBindingTemplate(const std::string luaname,callback f):methodBinding(luaname){
_f=f;
}
int call (lua_State *L) {
objectType *obj = classBinding<objectType>::check(L,1);
(obj->*(_f))();
return 0;
}
};
//constructor 4 args
template<class objectType, class arg0Type=void, class arg1Type=void, class arg2Type=void, class arg3Type=void>
class constructorBindingTemplate:public constructorBinding {
int call (lua_State *L){
lua_remove(L,1);
arg0Type a0 = lua_template<arg0Type>::get(L,1);
arg1Type a1 = lua_template<arg1Type>::get(L,2);
arg2Type a2 = lua_template<arg2Type>::get(L,3);
arg3Type a3 = lua_template<arg3Type>::get(L,4);
classBinding<objectType>::push(L, new objectType(a0,a1,a2,a3),true);
return 1;
}
};
//constructor 3 args
template<class objectType, class arg0Type, class arg1Type, class arg2Type>
class constructorBindingTemplate<objectType,arg0Type,arg1Type,arg2Type,void>:public constructorBinding {
int call (lua_State *L){
lua_remove(L,1);
arg0Type a0 = lua_template<arg0Type>::get(L,1);
arg1Type a1 = lua_template<arg1Type>::get(L,2);
arg2Type a2 = lua_template<arg2Type>::get(L,3);
classBinding<objectType>::push(L, new objectType(a0,a1,a2),true);
return 1;
}
};
//constructor 2 args
template<class objectType, class arg0Type, class arg1Type>
class constructorBindingTemplate<objectType,arg0Type,arg1Type,void,void>:public constructorBinding {
int call (lua_State *L){
lua_remove(L,1);
arg0Type a0 = lua_template<arg0Type>::get(L,1);
arg1Type a1 = lua_template<arg1Type>::get(L,2);
classBinding<objectType>::push(L,new objectType(a0,a1),true);
return 1;
}
};
//constructor 1 args
template<class objectType, class arg0Type>
class constructorBindingTemplate<objectType,arg0Type,void,void,void>:public constructorBinding {
int call (lua_State *L){
lua_remove(L,1);
arg0Type a0 = lua_template<arg0Type>::get(L,1);
classBinding<objectType>::push(L, new objectType(a0),true);
return 1;
}
};
//constructor 0 args
template<class objectType>
class constructorBindingTemplate<objectType,void,void,void,void>:public constructorBinding {
int call (lua_State *L){
lua_remove(L,1);
classBinding<objectType>::push(L, new objectType(),true);
return 1;
}
};
#endif
/*
Copyright (c) 2005 Leonardo Palozzi
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
/*
* Lenny Palozzi - lenny.palozzi@home.com
*/
#ifndef _LUNA_H_
#define _LUNA_H_
extern "C" {
#include <lua.h>
}
extern "C" {
#include "lua.h"
#include "lauxlib.h"
}
template <typename T> class Luna {
typedef struct { T *pT; } userdataType;
public:
typedef int (T::*mfp)(lua_State *L);
typedef struct { const char *name; mfp mfunc; } RegType;
static void Register(lua_State *L) {
lua_newtable(L);
int methods = lua_gettop(L);
luaL_newmetatable(L, T::className);
int metatable = lua_gettop(L);
// store method table in globals so that
// scripts can add functions written in Lua.
lua_pushstring(L, T::className);
lua_pushvalue(L, methods);
lua_settable(L, LUA_GLOBALSINDEX);
lua_pushliteral(L, "__metatable");
lua_pushvalue(L, methods);
lua_settable(L, metatable); // hide metatable from Lua getmetatable()
lua_pushliteral(L, "__index");
lua_pushvalue(L, methods);
lua_settable(L, metatable);
lua_pushliteral(L, "__tostring");
lua_pushcfunction(L, tostring_T);
lua_settable(L, metatable);
lua_pushliteral(L, "__gc");
lua_pushcfunction(L, gc_T);
lua_settable(L, metatable);
lua_newtable(L); // mt for method table
int mt = lua_gettop(L);
lua_pushliteral(L, "__call");
lua_pushcfunction(L, new_T);
lua_pushliteral(L, "new");
lua_pushvalue(L, -2); // dup new_T function
lua_settable(L, methods); // add new_T to method table
lua_settable(L, mt); // mt.__call = new_T
lua_setmetatable(L, methods);
// fill method table with methods from class T
for (RegType *l = T::methods; l->name; l++) {
/* edited by Snaily: shouldn't it be const RegType *l ... ? */
lua_pushstring(L, l->name);
lua_pushlightuserdata(L, (void*)l);
lua_pushcclosure(L, thunk, 1);
lua_settable(L, methods);
}
lua_pop(L, 2); // drop metatable and method table
}
// get userdata from Lua stack and return pointer to T object
static T *check(lua_State *L, int narg) {
userdataType *ud =
static_cast<userdataType*>(luaL_checkudata(L, narg, T::className));
if(!ud) luaL_typerror(L, narg, T::className);
return ud->pT; // pointer to T object
}
private:
Luna(); // hide default constructor
// member function dispatcher
static int thunk(lua_State *L) {
// stack has userdata, followed by method args
T *obj = check(L, 1); // get 'self', or if you prefer, 'this'
lua_remove(L, 1); // remove self so member function args start at index 1
// get member function from upvalue
RegType *l = static_cast<RegType*>(lua_touserdata(L, lua_upvalueindex(1)));
return (obj->*(l->mfunc))(L); // call member function
}
// create a new T object and
// push onto the Lua stack a userdata containing a pointer to T object
static int new_T(lua_State *L) {
lua_remove(L, 1); // use classname:new(), instead of classname.new()
T *obj = new T(L); // call constructor for T objects
userdataType *ud =
static_cast<userdataType*>(lua_newuserdata(L, sizeof(userdataType)));
ud->pT = obj; // store pointer to object in userdata
luaL_getmetatable(L, T::className); // lookup metatable in Lua registry
lua_setmetatable(L, -2);
return 1; // userdata containing pointer to T object
}
// garbage collection metamethod
static int gc_T(lua_State *L) {
userdataType *ud = static_cast<userdataType*>(lua_touserdata(L, 1));
T *obj = ud->pT;
delete obj; // call destructor for T objects
return 0;
}
static int tostring_T (lua_State *L) {
char buff[32];
userdataType *ud = static_cast<userdataType*>(lua_touserdata(L, 1));
T *obj = ud->pT;
sprintf(buff, "%p", obj);
lua_pushfstring(L, "%s (%s)", T::className, buff);
return 1;
}
};
#endif
...@@ -25,7 +25,6 @@ set(SRC ...@@ -25,7 +25,6 @@ set(SRC
findLinks.cpp findLinks.cpp
SOrientedBoundingBox.cpp SOrientedBoundingBox.cpp
GeomMeshMatcher.cpp GeomMeshMatcher.cpp
LuaBindings_Geo.cpp
MVertex.cpp MVertex.cpp
MFace.cpp MFace.cpp
MElement.cpp MElement.cpp
......
...@@ -27,6 +27,9 @@ ...@@ -27,6 +27,9 @@
#include "Context.h" #include "Context.h"
#include "OS.h" #include "OS.h"
#include "OpenFile.h"
#include "CreateFile.h"
#if defined(HAVE_MESH) #if defined(HAVE_MESH)
#include "Field.h" #include "Field.h"
#include "Generator.h" #include "Generator.h"
...@@ -1360,3 +1363,30 @@ GModel *GModel::buildCutGModel(gLevelset *ls) ...@@ -1360,3 +1363,30 @@ GModel *GModel::buildCutGModel(gLevelset *ls)
return cutGM; return cutGM;
} }
void GModel::load(std::string fileName){
GModel *temp = GModel::current();
GModel::setCurrent(this);
MergeFile(fileName.c_str());
GModel::setCurrent(temp);
}
void GModel::save(std::string fileName){
GModel *temp = GModel::current();
GModel::setCurrent(this);
int guess = GuessFileFormatFromFileName(fileName);
CreateOutputFile (fileName, guess);
GModel::setCurrent(temp);
}
#ifdef HAVE_LUA
#include "Bindings.h"
const char GModel::className[]="GModel";
const char GModel::parentClassName[]="";
methodBinding *GModel::methods[]={
new methodBindingTemplate<GModel,int,int>("mesh",&GModel::mesh),
new methodBindingTemplate<GModel,void,std::string>("load",&GModel::load),
new methodBindingTemplate<GModel,void,std::string>("save",&GModel::save),
0
};
constructorBinding *GModel::constructorMethod=new constructorBindingTemplate<GModel>();
#endif
...@@ -19,6 +19,11 @@ ...@@ -19,6 +19,11 @@
#include "SBoundingBox3d.h" #include "SBoundingBox3d.h"
#include "discreteFace.h" #include "discreteFace.h"
#ifdef HAVE_LUA
class methodBinding;
class constructorBinding;
#endif
class Octree; class Octree;
class FM_Internals; class FM_Internals;
class GEO_Internals; class GEO_Internals;
...@@ -433,6 +438,17 @@ class GModel ...@@ -433,6 +438,17 @@ class GModel
int readDIFF(const std::string &name); int readDIFF(const std::string &name);
int writeDIFF(const std::string &name, bool binary=false, int writeDIFF(const std::string &name, bool binary=false,
bool saveAll=false, double scalingFactor=1.0); bool saveAll=false, double scalingFactor=1.0);
void save(std::string fileName);
void load(std::string fileName);
#ifdef HAVE_LUA
static const char className[];
static const char parentClassName[];
static methodBinding *methods[];
static constructorBinding *constructorMethod;
#endif
}; };
#endif #endif
#include "LuaBindings_Geo.h"
#include "OpenFile.h"
#include "CreateFile.h"
#if defined(HAVE_LUA)
// define the name of the object that will be used
// in Lua
const char LuaGModel::className[] = "GModel";
// Define the methods we will expose to Lua
#define _method(class, name) {#name, &class::name}
Luna<LuaGModel>::RegType LuaGModel::methods[] = {
_method(LuaGModel, mesh),
_method(LuaGModel, load),
_method(LuaGModel, save),
{0,0}
};
LuaGModel::LuaGModel(lua_State *L){
_gm = new GModel;
}
LuaGModel::~LuaGModel(){
delete _gm;
}
void LuaGModel::Register (lua_State *L){
Luna<LuaGModel>::Register(L);
}
int LuaGModel::load(lua_State *L){
GModel *temp = GModel::current();
GModel::setCurrent(_gm);
MergeFile(luaL_checkstring(L, 1));
GModel::setCurrent(temp);
return 1;
}
int LuaGModel::mesh(lua_State *L){
_gm -> mesh((int)luaL_checknumber(L, 1));
return 1;
}
int LuaGModel::save(lua_State *L){
GModel *temp = GModel::current();
GModel::setCurrent(_gm);
int guess = GuessFileFormatFromFileName(std::string(lua_tostring(L, 1)));
CreateOutputFile (std::string(lua_tostring(L, 1)), guess);
GModel::setCurrent(temp);
return 1;
}
#endif
#ifndef _LUA_BINDINGS_GEO_
#define _LUA_BINDINGS_GEO_
#include "GmshConfig.h"
#if defined(HAVE_LUA)
// include lua stuff
extern "C" {
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
// Use luna for bindings
#include "luna.h"
// The header file for the GModel
#include "GModel.h"
// we do not use the GModel::current()
// this should not be a class anyway ...
// this should be removed !!!!!! and be put
// inside GModel itself
class LuaGModel{
GModel *_gm;
public:
static const char className[];
static Luna<LuaGModel>::RegType methods[];
static void Register(lua_State *L);
LuaGModel(lua_State *L);
~LuaGModel();
// Methods we will bind
int load (lua_State *L); // load a file
int mesh (lua_State *L); // make the mesh
int save (lua_State *L); // save a file
GModel * getGModel () const {return _gm;}
private:
};
#endif // HAVE LUA
#endif // _LUA_BINDINGS_GEO_
...@@ -290,56 +290,26 @@ bool fullMatrix<double>::svd(fullMatrix<double> &V, fullVector<double> &S) ...@@ -290,56 +290,26 @@ bool fullMatrix<double>::svd(fullMatrix<double> &V, fullVector<double> &S)
} }
#if defined(HAVE_LUA) #if defined(HAVE_LUA)
template<> #include "Bindings.h"
int fullMatrix<double>::gemm(lua_State *L)
{
#if defined(HAVE_BLAS)
int n = lua_gettop(L);
if (n < 2)throw;
fullMatrix<double> *a = Luna<fullMatrix<double> >::check(L,1);
fullMatrix<double> *b = Luna<fullMatrix<double> >::check(L,2);
if(!a || !b)throw;
// printf("%d %d\n",a->size1(),b->size1());
double alpha=1, beta=1;
if (n > 2) alpha = luaL_checknumber(L, 3);
if (n > 3) beta = luaL_checknumber(L, 4);
int M = size1(), N = size2(), K = a->size2();
int LDA = a->size1(), LDB = b->size1(), LDC = size1();
F77NAME(dgemm)("N", "N", &M, &N, &K, &alpha, a->_data, &LDA, b->_data, &LDB,
&beta, _data, &LDC);
return 0;
#else
throw;
return 0;
#endif
}
// define the name of the object that will be used in Lua
template<> template<>
const char fullMatrix<double>::className[]="fullMatrix"; const char fullMatrix<double>::className[]="fullMatrix";
template<> template<>
const char fullMatrix<float>::className[]="fullMatrixFloat"; const char fullMatrix<float>::className[]="fullMatrixFloat";
// Define the methods we will expose to Lua
#define _method(class, name) {#name, &class::name}
template<> template<>
Luna<fullMatrix<double> >::RegType fullMatrix<double>::methods[] = { const char fullMatrix<double>::parentClassName[]="";
_method(fullMatrix<double> , size1), template<>
_method(fullMatrix<double> , size2), const char fullMatrix<float>::parentClassName[]="";
_method(fullMatrix<double> , get), template<>
_method(fullMatrix<double> , set), methodBinding *fullMatrix<double>::methods[]={
_method(fullMatrix<double> , gemm), new methodBindingTemplate<const fullMatrix<double>,int>("size1",&fullMatrix<double>::size1),
{0,0} new methodBindingTemplate<const fullMatrix<double>,int>("size2",&fullMatrix<double>::size2),
new methodBindingTemplate<const fullMatrix<double>,double,int,int>("get",&fullMatrix<double>::get),
new methodBindingTemplate<fullMatrix<double>,void,int,int,double>("set",&fullMatrix<double>::set),
new methodBindingTemplate<fullMatrix<double>,void,const fullMatrix<double>*,const fullMatrix<double> *>("gemm",&fullMatrix<double>::gemm),
0
}; };
// this function has to be called in the main in order to register
// the names defined above
template<> template<>
void fullMatrix<double>::Register (lua_State *L){ constructorBinding *fullMatrix<double>::constructorMethod = new constructorBindingTemplate<fullMatrix<double>,int,int>();
Luna<fullMatrix<double> >::Register(L);
}
#endif #endif
......
...@@ -11,17 +11,6 @@ ...@@ -11,17 +11,6 @@
#include "GmshConfig.h" #include "GmshConfig.h"
#include "GmshMessage.h" #include "GmshMessage.h"
#if defined(HAVE_LUA)
// include lua stuff
extern "C" {
#include "lua.h"
#include "lauxlib.h"
#include "lualib.h"
}
// Use luna for c++ class member functions bindings
#include "luna.h"
#endif // HAVE LUA
template <class scalar> class fullMatrix; template <class scalar> class fullMatrix;
template <class scalar> template <class scalar>
...@@ -104,6 +93,10 @@ class fullVector ...@@ -104,6 +93,10 @@ class fullVector
printf("\n"); printf("\n");
} }
}; };
#if defined(HAVE_LUA)
class methodBinding;
class constructorBinding;
#endif
template <class scalar> template <class scalar>
class fullMatrix class fullMatrix
...@@ -113,51 +106,17 @@ class fullMatrix ...@@ -113,51 +106,17 @@ class fullMatrix
int _r, _c; int _r, _c;
scalar *_data; scalar *_data;
public: public:
#if defined(HAVE_LUA) inline scalar get (int r, int c)const {
static const char className[]; return (*this)(r,c);
static Luna<fullMatrix<double> >::RegType methods[];
static void Register(lua_State *L);
fullMatrix(lua_State *L){
int n = lua_gettop(L);
if (n == 1){
fullMatrix<scalar> * _ud = (fullMatrix<scalar> *) lua_touserdata(L, 1);
_c = _ud->_c;
_r = _ud->_r;
_own_data = false;
_data = _ud->_data;
return;
} }
if (n != 2)throw; inline void set(int r, int c, scalar v){
_r = luaL_checkint(L, 1); (*this)(r,c)=v;
_c = luaL_checkint(L, 2);
_data = new scalar[_r * _c];
_own_data = true;
scale(0.);
}
int size1(lua_State *L){
lua_pushinteger(L, _r);
return 1;
}
int size2(lua_State *L){
lua_pushinteger(L, _c);
return 1;
}
int get(lua_State *L){
int r = luaL_checkint(L, 1);
int c = luaL_checkint(L, 2);
lua_pushnumber(L, (*this)(r,c));
return 1;
} }
#if defined(HAVE_LUA)
int set(lua_State *L){ static const char className[];
int r = luaL_checkint(L, 1); static const char parentClassName[];
int c = luaL_checkint(L, 2); static methodBinding *methods[];
scalar val = luaL_checknumber(L, 3); static constructorBinding *constructorMethod;
(*this)(r,c) = val;
return 0;
}
int gemm(lua_State *L);
#endif // HAVE LUA #endif // HAVE LUA
fullMatrix(scalar *original, int r, int c){ fullMatrix(scalar *original, int r, int c){
_r = r; _r = r;
...@@ -260,6 +219,9 @@ class fullMatrix ...@@ -260,6 +219,9 @@ class fullMatrix
} }
#endif #endif
; ;
inline void gemm (const fullMatrix<scalar> *a, const fullMatrix<scalar> *b){
gemm(*a,*b);
}
void gemm(const fullMatrix<scalar> &a, const fullMatrix<scalar> &b, void gemm(const fullMatrix<scalar> &a, const fullMatrix<scalar> &b,
scalar alpha=1., scalar beta=1.) scalar alpha=1., scalar beta=1.)
#if !defined(HAVE_BLAS) #if !defined(HAVE_BLAS)
......
...@@ -2,7 +2,7 @@ model = GModel () ...@@ -2,7 +2,7 @@ model = GModel ()
model:load ('edge.geo') model:load ('edge.geo')
--model:mesh (1) --model:mesh (1)
--model:save ('edge_new.msh') --model:save ('edge_new.msh')
model:load ('edge_new.msh') model:load ('edge.msh')
dg = dgSystemOfEquations (model) dg = dgSystemOfEquations (model)
dg:setOrder(1) dg:setOrder(1)
...@@ -17,20 +17,21 @@ v:set(2,0,0) ...@@ -17,20 +17,21 @@ v:set(2,0,0)
nu=fullMatrix(1,1); nu=fullMatrix(1,1);
nu:set(0,0,0) nu:set(0,0,0)
dg:setConservationLaw('AdvectionDiffusion',createFunction.constant(v),createFunction.constant(nu)) law = ConservationLawAdvection(FunctionConstant(v):getName(),FunctionConstant(nu):getName())
dg:setConservationLaw(law)
-- boundary condition -- boundary condition
outside=fullMatrix(1,1) outside=fullMatrix(1,1)
outside:set(0,0,0.) outside:set(0,0,0.)
dg:addBoundaryCondition('Left','OutsideValues',createFunction.constant(outside)) bndcondition=law:newOutsideValueBoundary(FunctionConstant(outside):getName())
dg:addBoundaryCondition('Right','OutsideValues',createFunction.constant(outside)) law:addBoundaryCondition('Left',bndcondition)
law:addBoundaryCondition('Right',bndcondition)
dg:setup() dg:setup()
-- initial condition -- initial condition
function initial_condition( _x , _f ) function initial_condition( xyz , f )
xyz = fullMatrix(_x)
f = fullMatrix(_f)
for i=0,xyz:size1()-1 do for i=0,xyz:size1()-1 do
x = xyz:get(i,0) x = xyz:get(i,0)
y = xyz:get(i,1) y = xyz:get(i,1)
...@@ -38,14 +39,14 @@ function initial_condition( _x , _f ) ...@@ -38,14 +39,14 @@ function initial_condition( _x , _f )
f:set (i, 0, math.exp(-100*((x+0.8)^2))) f:set (i, 0, math.exp(-100*((x+0.8)^2)))
end end
end end
dg:L2Projection(createFunction.lua(1,'initial_condition','XYZ')) dg:L2Projection(FunctionLua(1,'initial_condition',{'XYZ'}):getName())
dg:exportSolution('output/Adv1D_00000') dg:exportSolution('output/Adv1D_00000')
-- main loop -- main loop
n = 5 n = 5
for i=1,50*n do for i=1,100*n do
norm = dg:RK44(0.0325) norm = dg:RK44(0.03)
if (i % n == 0) then if (i % n == 0) then
print('iter',i,norm) print('iter',i,norm)
dg:exportSolution(string.format("output/Adv1D-%05d", i)) dg:exportSolution(string.format("output/Adv1D-%05d", i))
......
model = GModel () model = GModel ()
model:load ('square.geo') model:load ('square.geo')
model:load ('square.msh') model:load ('square.msh')
model2 = GModel()
model2:load('')
vmodel = {GModel(),GModel()}
vmodel[1]:load('')
vmodel[2]:load('')
dg = dgSystemOfEquations (model) dg = dgSystemOfEquations (model)
dg:setOrder(5) dg:setOrder(5)
-- conservation law -- conservation law
-- advection speed -- advection speed
v=fullMatrix(3,1); v=fullMatrix(3,1);
v:set(0,0,0.15) v:set(0,0,0.15)
v:set(1,0,0.05) v:set(1,0,0.05)
v:set(2,0,0) v:set(2,0,0)
-- diffusivity -- diffusivity
nu=fullMatrix(1,1); nu=fullMatrix(1,1);
nu:set(0,0,0.001) nu:set(0,0,0.001)
dg:setConservationLaw('AdvectionDiffusion',createFunction.constant(v),createFunction.constant(nu)) law = ConservationLawAdvection(FunctionConstant(v):getName(),FunctionConstant(nu):getName())
dg:setConservationLaw(law)
-- boundary condition -- boundary condition
outside=fullMatrix(1,1) outside=fullMatrix(1,1)
outside:set(0,0,0.) outside:set(0,0,0.)
dg:addBoundaryCondition('Border','OutsideValues',createFunction.constant(outside)) law:addBoundaryCondition('Border',law:newOutsideValueBoundary(FunctionConstant(outside):getName()))
dg:setup() dg:setup()
-- initial condition -- initial condition
function initial_condition( _x , _f ) function initial_condition( xyz , f )
xyz = fullMatrix(_x)
f = fullMatrix(_f)
for i=0,xyz:size1()-1 do for i=0,xyz:size1()-1 do
x = xyz:get(i,0) x = xyz:get(i,0)
y = xyz:get(i,1) y = xyz:get(i,1)
...@@ -35,7 +43,7 @@ function initial_condition( _x , _f ) ...@@ -35,7 +43,7 @@ function initial_condition( _x , _f )
f:set (i, 0, math.exp(-100*((x-0.2)^2 +(y-0.3)^2))) f:set (i, 0, math.exp(-100*((x-0.2)^2 +(y-0.3)^2)))
end end
end end
dg:L2Projection(createFunction.lua(1,'initial_condition','XYZ')) dg:L2Projection(FunctionLua(1,'initial_condition',{'XYZ'}):getName())
dg:exportSolution('output/Advection_00000') dg:exportSolution('output/Advection_00000')
......
...@@ -7,9 +7,7 @@ SOUND = V/MACH ...@@ -7,9 +7,7 @@ SOUND = V/MACH
--[[ --[[
Function for initial conditions Function for initial conditions
--]] --]]
function free_stream( x, f ) function free_stream( XYZ, FCT )
FCT = fullMatrix(f)
XYZ = fullMatrix(x)
for i=0,XYZ:size1()-1 do for i=0,XYZ:size1()-1 do
FCT:set(i,0,RHO) FCT:set(i,0,RHO)
FCT:set(i,1,RHO*V) FCT:set(i,1,RHO*V)
...@@ -30,12 +28,13 @@ myModel:load ('step.msh') ...@@ -30,12 +28,13 @@ myModel:load ('step.msh')
print'*** Create a dg solver ***' print'*** Create a dg solver ***'
DG = dgSystemOfEquations (myModel) DG = dgSystemOfEquations (myModel)
DG:setOrder(order) DG:setOrder(order)
DG:setConservationLaw('PerfectGas2d') law=ConservationLawPerfectGas2d()
DG:addBoundaryCondition('Walls','Wall') DG:setConservationLaw(law)
law:addBoundaryCondition('Walls',law:newWallBoundary())
FS = createFunction.lua(4, 'free_stream', 'XYZ') FS = FunctionLua(4, 'free_stream', {'XYZ'}):getName()
DG:addBoundaryCondition('LeftRight','FreeStream',FS) law:addBoundaryCondition('LeftRight',law:newOutsideValueBoundary(FS))
DG:setup() DG:setup()
...@@ -56,7 +55,7 @@ print('DT=',dt) ...@@ -56,7 +55,7 @@ print('DT=',dt)
for i=1,1000 do for i=1,1000 do
norm = DG:RK44(dt) norm = DG:RK44(dt)
print('*** ITER ***',i,norm) print('*** ITER ***',i,norm)
if (i % 20 == 0) then if (i % 10 == 0) then
DG:exportSolution(string.format("solution-%03d", i)) DG:exportSolution(string.format("solution-%03d", i))
end end
end end
......
...@@ -9,19 +9,18 @@ dg:setOrder(5) ...@@ -9,19 +9,18 @@ dg:setOrder(5)
-- advection speed -- advection speed
nu=fullMatrix(1,1); nu=fullMatrix(1,1);
nu:set(0,0,0.01) nu:set(0,0,0.01)
dg:setConservationLaw('AdvectionDiffusion','',createFunction.constant(nu)) law = ConservationLawAdvection('',FunctionConstant(nu):getName())
dg:setConservationLaw(law)
-- boundary condition -- boundary condition
outside=fullMatrix(1,1) outside=fullMatrix(1,1)
outside:set(0,0,0.) outside:set(0,0,0.)
dg:addBoundaryCondition('Border','0Flux') law:addBoundaryCondition('Border',law:new0FluxBoundary())
dg:setup() dg:setup()
-- initial condition -- initial condition
function initial_condition( _x , _f ) function initial_condition( xyz , f )
xyz = fullMatrix(_x)
f = fullMatrix(_f)
for i=0,xyz:size1()-1 do for i=0,xyz:size1()-1 do
x = xyz:get(i,0) x = xyz:get(i,0)
y = xyz:get(i,1) y = xyz:get(i,1)
...@@ -29,7 +28,7 @@ function initial_condition( _x , _f ) ...@@ -29,7 +28,7 @@ function initial_condition( _x , _f )
f:set (i, 0, math.exp(-100*((x-0.2)^2 +(y-0.3)^2))) f:set (i, 0, math.exp(-100*((x-0.2)^2 +(y-0.3)^2)))
end end
end end
dg:L2Projection(createFunction.lua(1,'initial_condition','XYZ')) dg:L2Projection(FunctionLua(1,'initial_condition',{'XYZ'}):getName())
dg:exportSolution('output/Diffusion_00000') dg:exportSolution('output/Diffusion_00000')
......
model = GModel() model = GModel()
model:load ('stommel_square.msh') model:load ('stommel_square.msh')
print('load..')
order = 3
dg = dgSystemOfEquations (model) dg = dgSystemOfEquations (model)
order=1
dg:setOrder (order) dg:setOrder (order)
claw = ShallowWater2d()
claw:addBoundaryCondition('Wall',claw:newWallBoundary())
dg:setConservationLaw('ShallowWater2d') dg:setConservationLaw(claw)
dg:addBoundaryCondition('Wall','Wall')
dg:setup() dg:setup()
dg:exportSolution('output/init')
dg:exportSolution('output/solution_00000') for i=1,10000 do
for i=1,1000 do
norm = dg:RK44(80*(3/(2.*order+1))) norm = dg:RK44(80*(3/(2.*order+1)))
if ( i%10 ==0 ) then if ( i%100 ==0 ) then
print ('iter ', i, norm) print ('iter ', i, norm)
end end
if ( i%100 ==0 ) then if ( i%100 ==0 ) then
......
--[[ --[[
Function for initial conditions Function for initial conditions
--]] --]]
function initial_condition( _x , _f ) function initial_condition( xyz , f )
xyz = fullMatrix(_x)
f = fullMatrix(_f)
for i=0,xyz:size1()-1 do for i=0,xyz:size1()-1 do
X = xyz:get(i,0) - .5 X = xyz:get(i,0) - .5
Y = xyz:get(i,1) - .5 Y = xyz:get(i,1) - .5
...@@ -15,24 +13,22 @@ function initial_condition( _x , _f ) ...@@ -15,24 +13,22 @@ function initial_condition( _x , _f )
end end
end end
--[[
Example of a lua program driving the DG code
--]]
print'*** Loading the mesh and the model ***' print'*** Loading the mesh and the model ***'
myModel = GModel () myModel = GModel ()
myModel:load('box.geo') --myModel:load('box.geo')
myModel:load('box.msh') --myModel:load('box.msh')
myModel:load('square.msh')
print'*** Create a dg solver ***' print'*** Create a dg solver ***'
DG = dgSystemOfEquations (myModel) DG = dgSystemOfEquations (myModel)
DG:setOrder(1) DG:setOrder(1)
DG:setConservationLaw('WaveEquation') law=ConservationLawWaveEquation(2)
DG:setConservationLaw(law)
DG:addBoundaryCondition('Border','Wall') law:addBoundaryCondition('Border',law:newWallBoundary())
DG:setup() DG:setup()
initialCondition = createFunction.lua(3,'initial_condition','XYZ') initialCondition = FunctionLua(3,'initial_condition',{'XYZ'}):getName()
print'*** setting the initial solution ***' print'*** setting the initial solution ***'
...@@ -45,12 +41,12 @@ DG:exportSolution('output/solution_000') ...@@ -45,12 +41,12 @@ DG:exportSolution('output/solution_000')
print'*** solve ***' print'*** solve ***'
dt = 0.00125; dt = 0.00125;
N = 1; N = 1000;
for i=1,N do for i=1,N do
norm = DG:RK44(dt) norm = DG:RK44(dt)
print('*** ITER ***',i,norm) print('*** ITER ***',i,norm)
if (i % 10 == 0) then if (i % 10 == 0) then
DG:exportSolution(string.format("solution-%03d", i)) DG:exportSolution(string.format("output/solution-%03d", i))
end end
end end
......
...@@ -26,7 +26,7 @@ class dgBoundaryConditionOutsideValue : public dgBoundaryCondition { ...@@ -26,7 +26,7 @@ class dgBoundaryConditionOutsideValue : public dgBoundaryCondition {
if(riemannSolver){ if(riemannSolver){
for(int i=0;i<_value.size1(); i++) for(int i=0;i<_value.size1(); i++)
for(int j=0;j<_value.size2(); j++) for(int j=0;j<_value.size2(); j++)
_value(i,j) = (*riemannSolver)(i,j*2); _value(i,j) = (*riemannSolver)(i,j);
} }
} }
}; };
...@@ -55,10 +55,23 @@ class dgBoundaryCondition0Flux : public dgBoundaryCondition { ...@@ -55,10 +55,23 @@ class dgBoundaryCondition0Flux : public dgBoundaryCondition {
} }
}; };
dgBoundaryCondition *dgBoundaryCondition::newOutsideValueCondition(dgConservationLaw &claw,const std::string outsideValueFunctionName) { dgBoundaryCondition *dgConservationLaw::newOutsideValueBoundary(const std::string outsideValueFunctionName) {
return new dgBoundaryConditionOutsideValue(claw,outsideValueFunctionName); return new dgBoundaryConditionOutsideValue(*this,outsideValueFunctionName);
} }
dgBoundaryCondition *dgBoundaryCondition::new0FluxCondition(dgConservationLaw &claw) { dgBoundaryCondition *dgConservationLaw::new0FluxBoundary() {
return new dgBoundaryCondition0Flux(claw); return new dgBoundaryCondition0Flux(*this);
} }
#include "Bindings.h"
const char dgConservationLaw::className[]="ConservationLaw";
const char dgConservationLaw::parentClassName[]="";
methodBinding * dgConservationLaw::methods[]={
new methodBindingTemplate<dgConservationLaw,void,std::string,dgBoundaryCondition*>("addBoundaryCondition",&dgConservationLaw::addBoundaryCondition),
new methodBindingTemplate<dgConservationLaw,dgBoundaryCondition*>("new0FluxBoundary",&dgConservationLaw::new0FluxBoundary),
new methodBindingTemplate<dgConservationLaw,dgBoundaryCondition*,std::string>("newOutsideValueBoundary",&dgConservationLaw::newOutsideValueBoundary),
0};
constructorBinding * dgConservationLaw::constructorMethod=NULL;
const char dgBoundaryCondition::className[]="BoundaryCondition";
const char dgBoundaryCondition::parentClassName[]="";
methodBinding * dgBoundaryCondition::methods[]={0};
constructorBinding * dgBoundaryCondition::constructorMethod=NULL;
...@@ -9,6 +9,10 @@ ...@@ -9,6 +9,10 @@
#include "fullMatrix.h" #include "fullMatrix.h"
class dataCacheDouble; class dataCacheDouble;
class dataCacheMap; class dataCacheMap;
#ifdef HAVE_LUA
class constructorBinding;
class methodBinding;
#endif
class dgConservationLaw; class dgConservationLaw;
...@@ -16,9 +20,12 @@ class dgBoundaryCondition { ...@@ -16,9 +20,12 @@ class dgBoundaryCondition {
public: public:
virtual ~dgBoundaryCondition () {} virtual ~dgBoundaryCondition () {}
virtual dataCacheDouble *newBoundaryTerm(dataCacheMap &cacheMapLeft) const = 0; virtual dataCacheDouble *newBoundaryTerm(dataCacheMap &cacheMapLeft) const = 0;
//a generic boundary condition using the Riemann solver of the conservation Law #if defined(HAVE_LUA)
static dgBoundaryCondition *newOutsideValueCondition(dgConservationLaw &claw,const std::string outsideValueFunctionName); static const char className[];
static dgBoundaryCondition *new0FluxCondition(dgConservationLaw &claw); static const char parentClassName[];
static methodBinding *methods[];
static constructorBinding *constructorMethod;
#endif
}; };
class dgConservationLaw { class dgConservationLaw {
...@@ -45,21 +52,26 @@ class dgConservationLaw { ...@@ -45,21 +52,26 @@ class dgConservationLaw {
return it->second; return it->second;
} }
inline void addBoundaryCondition(const std::string tag, dgBoundaryCondition * condition) { void addBoundaryCondition(std::string tag, dgBoundaryCondition * condition) {
if(_boundaryConditions.find(tag)!=_boundaryConditions.end()) if(_boundaryConditions.find(tag)!=_boundaryConditions.end())
throw; throw;
_boundaryConditions[tag]=condition; _boundaryConditions[tag]=condition;
} }
//a generic boundary condition using the Riemann solver of the conservation Law
dgBoundaryCondition *newOutsideValueBoundary(std::string outsideValueFunctionName);
dgBoundaryCondition *new0FluxBoundary();
#ifdef HAVE_LUA
static const char className[];
static const char parentClassName[];
static methodBinding *methods[];
static constructorBinding *constructorMethod;
#endif
}; };
dgConservationLaw *dgNewConservationLawAdvection(const std::string vname,const std::string nuname);
dgConservationLaw *dgNewConservationLawShallowWater2d();
dgConservationLaw *dgNewConservationLawWaveEquation(int);
dgConservationLaw *dgNewPerfectGasLaw2d(); dgConservationLaw *dgNewPerfectGasLaw2d();
dgBoundaryCondition *dgNewBoundaryConditionWaveEquationWall(int);
dgBoundaryCondition *dgNewBoundaryConditionShallowWater2dWall();
dgBoundaryCondition *dgNewBoundaryConditionPerfectGasLaw2dWall(); dgBoundaryCondition *dgNewBoundaryConditionPerfectGasLaw2dWall();
dgBoundaryCondition *dgNewBoundaryConditionPerfectGasLaw2dFreeStream(std::string&); dgBoundaryCondition *dgNewBoundaryConditionPerfectGasLaw2dFreeStream(std::string&);
......
...@@ -4,10 +4,9 @@ ...@@ -4,10 +4,9 @@
#include "SPoint3.h" #include "SPoint3.h"
#include "MElement.h" #include "MElement.h"
#include "function.h" #include "function.h"
#include "dgConservationLawAdvection.h"
class dgConservationLawAdvection : public dgConservationLaw { class dgConservationLawAdvection::advection : public dataCacheDouble {
std::string _vFunctionName,_nuFunctionName;
class advection : public dataCacheDouble {
dataCacheDouble &sol, &v; dataCacheDouble &sol, &v;
public: public:
advection(std::string vFunctionName, dataCacheMap &cacheMap): advection(std::string vFunctionName, dataCacheMap &cacheMap):
...@@ -23,7 +22,7 @@ class dgConservationLawAdvection : public dgConservationLaw { ...@@ -23,7 +22,7 @@ class dgConservationLawAdvection : public dgConservationLaw {
} }
} }
}; };
class riemann : public dataCacheDouble { class dgConservationLawAdvection::riemann : public dataCacheDouble {
dataCacheDouble &normals, &solLeft, &solRight,&v; dataCacheDouble &normals, &solLeft, &solRight,&v;
public: public:
riemann(std::string vFunctionName, dataCacheMap &cacheMapLeft, dataCacheMap &cacheMapRight): riemann(std::string vFunctionName, dataCacheMap &cacheMapLeft, dataCacheMap &cacheMapRight):
...@@ -46,7 +45,7 @@ class dgConservationLawAdvection : public dgConservationLaw { ...@@ -46,7 +45,7 @@ class dgConservationLawAdvection : public dgConservationLaw {
} }
} }
}; };
class diffusion : public dataCacheDouble { class dgConservationLawAdvection::diffusion : public dataCacheDouble {
dataCacheDouble &solgrad, &nu; dataCacheDouble &solgrad, &nu;
public: public:
diffusion(std::string nuFunctionName, dataCacheMap &cacheMap): diffusion(std::string nuFunctionName, dataCacheMap &cacheMap):
...@@ -59,43 +58,44 @@ class dgConservationLawAdvection : public dgConservationLaw { ...@@ -59,43 +58,44 @@ class dgConservationLawAdvection : public dgConservationLaw {
for(int i=0; i< solgrad().size1()/3; i++) { for(int i=0; i< solgrad().size1()/3; i++) {
_value(i,0) = -solgrad(i*3,0)*nu(i,0); _value(i,0) = -solgrad(i*3,0)*nu(i,0);
_value(i,1) = -solgrad(i*3+1,0)*nu(i,0); _value(i,1) = -solgrad(i*3+1,0)*nu(i,0);
_value(i,2) = -solgrad(i*3+1,0)*nu(i,0); _value(i,2) = -solgrad(i*3+2,0)*nu(i,0);
} }
} }
}; };
public: dataCacheDouble *dgConservationLawAdvection::newConvectiveFlux( dataCacheMap &cacheMap) const {
dataCacheDouble *newConvectiveFlux( dataCacheMap &cacheMap) const {
if( !_vFunctionName.empty()) if( !_vFunctionName.empty())
return new advection(_vFunctionName,cacheMap); return new advection(_vFunctionName,cacheMap);
else else
return NULL; return NULL;
} }
dataCacheDouble *newMaximumDiffusivity( dataCacheMap &cacheMap) const { dataCacheDouble *dgConservationLawAdvection::newMaximumDiffusivity( dataCacheMap &cacheMap) const {
if( !_nuFunctionName.empty()) if( !_nuFunctionName.empty())
return &cacheMap.get(_nuFunctionName); return &cacheMap.get(_nuFunctionName);
else else
return NULL; return NULL;
} }
dataCacheDouble *newRiemannSolver( dataCacheMap &cacheMapLeft, dataCacheMap &cacheMapRight) const { dataCacheDouble *dgConservationLawAdvection::newRiemannSolver( dataCacheMap &cacheMapLeft, dataCacheMap &cacheMapRight) const {
if( !_vFunctionName.empty()) if( !_vFunctionName.empty())
return new riemann(_vFunctionName,cacheMapLeft, cacheMapRight); return new riemann(_vFunctionName,cacheMapLeft, cacheMapRight);
else else
return NULL; return NULL;
} }
dataCacheDouble *newDiffusiveFlux( dataCacheMap &cacheMap) const { dataCacheDouble *dgConservationLawAdvection::newDiffusiveFlux( dataCacheMap &cacheMap) const {
if( !_nuFunctionName.empty()) if( !_nuFunctionName.empty())
return new diffusion(_nuFunctionName,cacheMap); return new diffusion(_nuFunctionName,cacheMap);
else else
return NULL; return NULL;
} }
dgConservationLawAdvection(std::string vFunctionName, std::string nuFunctionName) dgConservationLawAdvection::dgConservationLawAdvection(std::string vFunctionName, std::string nuFunctionName)
{ {
_vFunctionName = vFunctionName; _vFunctionName = vFunctionName;
_nuFunctionName = nuFunctionName; _nuFunctionName = nuFunctionName;
_nbf = 1; _nbf = 1;
} }
};
dgConservationLaw *dgNewConservationLawAdvection(std::string vFunctionName, std::string nuFunctionName) { #include "Bindings.h"
return new dgConservationLawAdvection(vFunctionName,nuFunctionName); const char * dgConservationLawAdvection::className = "ConservationLawAdvection";
} const char * dgConservationLawAdvection::parentClassName = "ConservationLaw";
constructorBinding *dgConservationLawAdvection::constructorMethod = new constructorBindingTemplate<dgConservationLawAdvection,std::string,std::string>();
methodBinding *dgConservationLawAdvection::methods []={0};
#ifndef _DG_CONSERVATION_LAW_ADVECTION_H
#define _DG_CONSERVATION_LAW_ADVECTION_H
#include "dgConservationLaw.h"
class constructorBinding;
class methodBinding;
class dgConservationLawAdvection : public dgConservationLaw {
std::string _vFunctionName,_nuFunctionName;
class advection;
class riemann;
class diffusion;
public:
dataCacheDouble *newConvectiveFlux( dataCacheMap &cacheMap) const;
dataCacheDouble *newMaximumDiffusivity( dataCacheMap &cacheMap) const;
dataCacheDouble *newRiemannSolver( dataCacheMap &cacheMapLeft, dataCacheMap &cacheMapRight) const;
dataCacheDouble *newDiffusiveFlux( dataCacheMap &cacheMap) const;
dgConservationLawAdvection(std::string vFunctionName, std::string nuFunctionName);
static const char * className;
static const char * parentClassName;
static methodBinding *methods[];
static constructorBinding *constructorMethod;
};
#endif
#include "dgConservationLaw.h" #include "dgConservationLawPerfectGas.h"
#include "function.h" #include "function.h"
const double GAMMA = 1.4; const double GAMMA = 1.4;
...@@ -127,12 +127,9 @@ static inline void _ROE2D (const double &_GAMMA, ...@@ -127,12 +127,9 @@ static inline void _ROE2D (const double &_GAMMA,
FLUX[k] = -lflux; FLUX[k] = -lflux;
} }
} }
class dgPerfectGasLaw2d : public dgConservationLaw {
// perfect gas law, GAMMA is the only parameter // perfect gas law, GAMMA is the only parameter
class advection : public dataCacheDouble { class dgPerfectGasLaw2d::advection : public dataCacheDouble {
dataCacheDouble &sol; dataCacheDouble &sol;
public: public:
advection(dataCacheMap &cacheMap): advection(dataCacheMap &cacheMap):
...@@ -172,7 +169,7 @@ class dgPerfectGasLaw2d : public dgConservationLaw { ...@@ -172,7 +169,7 @@ class dgPerfectGasLaw2d : public dgConservationLaw {
} }
}; };
class riemann : public dataCacheDouble { class dgPerfectGasLaw2d::riemann : public dataCacheDouble {
dataCacheDouble &normals, &solL, &solR; dataCacheDouble &normals, &solL, &solR;
public: public:
riemann(dataCacheMap &cacheMapLeft, dataCacheMap &cacheMapRight): riemann(dataCacheMap &cacheMapLeft, dataCacheMap &cacheMapRight):
...@@ -193,12 +190,6 @@ class dgPerfectGasLaw2d : public dgConservationLaw { ...@@ -193,12 +190,6 @@ class dgPerfectGasLaw2d : public dgConservationLaw {
const double ny = normals(1,i); const double ny = normals(1,i);
_ROE2D (GAMMA,nx,ny,solLeft,solRight,FLUX); _ROE2D (GAMMA,nx,ny,solLeft,solRight,FLUX);
/*
printf("RSOLL %g %g %g %g\n",solLeft[0],solLeft[1],solLeft[2], solLeft[3]);
printf("RSOLR %g %g %g %g\n",solRight[0],solRight[1],solRight[2], solRight[3]);
printf("RN %g %g\n",nx,ny);
printf("RFLUX %g %g %g %g\n",FLUX[0],FLUX[1],FLUX[2],FLUX[3]);
*/
_value(i,0) = FLUX[0]; _value(i,0) = FLUX[0];
_value(i,1) = FLUX[1]; _value(i,1) = FLUX[1];
_value(i,2) = FLUX[2]; _value(i,2) = FLUX[2];
...@@ -210,24 +201,23 @@ class dgPerfectGasLaw2d : public dgConservationLaw { ...@@ -210,24 +201,23 @@ class dgPerfectGasLaw2d : public dgConservationLaw {
} }
} }
}; };
public: dataCacheDouble *dgPerfectGasLaw2d::newConvectiveFlux( dataCacheMap &cacheMap) const {
dataCacheDouble *newConvectiveFlux( dataCacheMap &cacheMap) const {
return new advection(cacheMap); return new advection(cacheMap);
} }
dataCacheDouble *newRiemannSolver( dataCacheMap &cacheMapLeft, dataCacheMap &cacheMapRight) const { dataCacheDouble *dgPerfectGasLaw2d::newRiemannSolver( dataCacheMap &cacheMapLeft, dataCacheMap &cacheMapRight) const {
return new riemann(cacheMapLeft, cacheMapRight); return new riemann(cacheMapLeft, cacheMapRight);
} }
dataCacheDouble *newDiffusiveFlux( dataCacheMap &cacheMap) const { dataCacheDouble *dgPerfectGasLaw2d::newDiffusiveFlux( dataCacheMap &cacheMap) const {
return 0; return 0;
} }
dataCacheDouble *newSourceTerm (dataCacheMap &cacheMap) const { dataCacheDouble *dgPerfectGasLaw2d::newSourceTerm (dataCacheMap &cacheMap) const {
return 0; return 0;
} }
dgPerfectGasLaw2d() dgPerfectGasLaw2d::dgPerfectGasLaw2d()
{ {
_nbf = 4; // \rho \rho u \rho v \rho e _nbf = 4; // \rho \rho u \rho v \rho e
} }
};
class dgBoundaryConditionPerfectGasLaw2dWall : public dgBoundaryCondition { class dgBoundaryConditionPerfectGasLaw2dWall : public dgBoundaryCondition {
class term : public dataCacheDouble { class term : public dataCacheDouble {
...@@ -268,7 +258,12 @@ class dgBoundaryConditionPerfectGasLaw2dWall : public dgBoundaryCondition { ...@@ -268,7 +258,12 @@ class dgBoundaryConditionPerfectGasLaw2dWall : public dgBoundaryCondition {
return new term(cacheMapLeft); return new term(cacheMapLeft);
} }
}; };
dgBoundaryCondition *dgPerfectGasLaw2d::newWallBoundary() const {
return new dgBoundaryConditionPerfectGasLaw2dWall();
}
#if 0 // JF : I commented this out as I think this equivalent to the generic OutsideValue condition
can you confirm and remove it ?
class dgBoundaryConditionPerfectGasLaw2dFreeStream : public dgBoundaryCondition { class dgBoundaryConditionPerfectGasLaw2dFreeStream : public dgBoundaryCondition {
class term : public dataCacheDouble { class term : public dataCacheDouble {
dataCacheDouble &sol,&normals,&freeStream; dataCacheDouble &sol,&normals,&freeStream;
...@@ -313,16 +308,13 @@ class dgBoundaryConditionPerfectGasLaw2dFreeStream : public dgBoundaryCondition ...@@ -313,16 +308,13 @@ class dgBoundaryConditionPerfectGasLaw2dFreeStream : public dgBoundaryCondition
return new term(cacheMapLeft,_freeStreamName); return new term(cacheMapLeft,_freeStreamName);
} }
}; };
#endif
#include "Bindings.h"
const char *dgPerfectGasLaw2d::className = "ConservationLawPerfectGas2d";
const char *dgPerfectGasLaw2d::parentClassName = "ConservationLaw";
methodBinding *dgPerfectGasLaw2d::methods[] ={
new methodBindingTemplate<const dgPerfectGasLaw2d,dgBoundaryCondition*>("newWallBoundary",&dgPerfectGasLaw2d::newWallBoundary),
0};
constructorBinding *dgPerfectGasLaw2d::constructorMethod=new constructorBindingTemplate<dgPerfectGasLaw2d>();
dgBoundaryCondition *dgNewBoundaryConditionPerfectGasLaw2dWall() {
return new dgBoundaryConditionPerfectGasLaw2dWall();
}
dgBoundaryCondition *dgNewBoundaryConditionPerfectGasLaw2dFreeStream(std::string &freeStreamName) {
return new dgBoundaryConditionPerfectGasLaw2dFreeStream(freeStreamName);
}
dgConservationLaw *dgNewPerfectGasLaw2d() {
return new dgPerfectGasLaw2d();
}
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment