Working python-c interface. This is fun!

This commit is contained in:
Anne_ctwtm 2015-02-10 10:07:29 +01:00
parent c7e3f5fb1e
commit 2329bc8e8f
9 changed files with 249 additions and 34 deletions

View File

@ -51,7 +51,7 @@ add_library(sources ${src})
set_source_files_properties(timedomain.i PROPERTIES CPLUSPLUS ON) set_source_files_properties(timedomain.i PROPERTIES CPLUSPLUS ON)
set_source_files_properties(timedomain.i PROPERTIES SWIG_FLAGS "-py3") set_source_files_properties(timedomain.i PROPERTIES SWIG_FLAGS "-py3")
set_source_files_properties(timedomain.i PROPERTIES SWIG_FLAGS "-Wextra") set_source_files_properties(timedomain.i PROPERTIES SWIG_FLAGS "-Wextra")
set_source_files_properties(timedomain.i PROPERTIES SWIG_FLAGS "-includeall")
swig_add_module(timedomaineuler python timedomain.i) swig_add_module(timedomaineuler python timedomain.i)
swig_link_libraries(timedomaineuler sources nonlin armadillo ${PYTHON_LIBRARIES}) swig_link_libraries(timedomaineuler sources nonlin armadillo ${PYTHON_LIBRARIES})

View File

@ -5,7 +5,6 @@
TRACETHIS TRACETHIS
namespace td{ namespace td{
extern tasystem::Globalconf gc; extern tasystem::Globalconf gc;
SolutionAtGp::SolutionAtGp() { SolutionAtGp::SolutionAtGp() {

View File

@ -3,6 +3,8 @@
namespace td{ namespace td{
typedef double d; typedef double d;
typedef unsigned us;
class SolutionAtGp { class SolutionAtGp {
d rho_,m_=0,rhoE_=0; d rho_,m_=0,rhoE_=0;
public: public:

View File

@ -1,7 +1,4 @@
#include "solutioninstance.h" #include "solutioninstance.h"
#include "tube.h"
namespace td{ namespace td{
@ -14,7 +11,7 @@ namespace td{
TRACE(15,"SolutionInstance::setrho()"); TRACE(15,"SolutionInstance::setrho()");
for(auto gp=gps.begin();gp!=gps.end();gp++){ for(auto gp=gps.begin();gp!=gps.end();gp++){
gp->set(rho); gp->setData(rho);
} }
} // setrho() } // setrho()
int SolutionInstance::save(std::ofstream& str,data d){ int SolutionInstance::save(std::ofstream& str,data d){

View File

@ -1,6 +1,6 @@
#pragma once #pragma once
#include "solutionatgp.h" #include "solutionatgp.h"
#include "globalconf.h" #include "vtypes.h"
namespace td{ namespace td{
SPOILNAMESPACE SPOILNAMESPACE
@ -9,8 +9,6 @@ namespace td{
p,u p,u
}; };
extern tasystem::Globalconf gc;
class SolutionInstance{ class SolutionInstance{
vector<SolutionAtGp> gps; vector<SolutionAtGp> gps;
d time=0; d time=0;
@ -19,7 +17,7 @@ namespace td{
~SolutionInstance(){} ~SolutionInstance(){}
d getTime() const {return time;} d getTime() const {return time;}
void setTime(d t) {time=t;} void setTime(d t) {time=t;}
void set(us i,SolutionAtGp& sol){ gps.at(i)=sol;} void setData(us i,SolutionAtGp& sol){ gps.at(i)=sol;}
SolutionAtGp& get(us i){ return gps.at(i); } SolutionAtGp& get(us i){ return gps.at(i); }
void setrho(d rho); void setrho(d rho);

View File

@ -1,8 +1,6 @@
#include "tube.h" #include "tube.h"
namespace td{ namespace td{
// Definition of a Globalconf in this namespace. Is initialized in main()
tasystem::Globalconf gc; tasystem::Globalconf gc;
d Tube::pleft(d t){ d Tube::pleft(d t){
@ -12,15 +10,25 @@ namespace td{
return pleft; return pleft;
} }
Tube::Tube(d L,int gp): L(L),gp(gp),sol(SolutionInstance(gp)) { Tube::Tube(d L,int gp):
L(L),
gp(gp),
sol(SolutionInstance(gp)) {
TRACE(15,"Tube::Tube()"); TRACE(15,"Tube::Tube()");
dx=L/(gp-1); dx=L/(gp-1);
VARTRACE(15,gp); VARTRACE(15,gp);
sol.setrho(gc.rho0()); sol.setrho(gc.rho0());
} }
void Tube::DoIntegration(d dt){ void Tube::DoIntegration(d dt,int n){
TRACE(14,"Tube::DoIntegration()"); TRACE(14,"Tube::DoIntegration()");
int integrations=0;
while(integrations<n){
Integrate(dt);
}
}
void Tube::Integrate(d dt){
// Define new time // Define new time
d newt=t+dt; d newt=t+dt;
@ -41,8 +49,8 @@ namespace td{
d momfluxl=ip0.rho()*pow(ip0.u(),2)+pleft(t); d momfluxl=ip0.rho()*pow(ip0.u(),2)+pleft(t);
m=ip0.m()-la*(ip1.Mflux()-momfluxl); m=ip0.m()-la*(ip1.Mflux()-momfluxl);
rhoE=ip0.rhoE()-la*(ip1.Eflux()-ip0.Eflux()); rhoE=ip0.rhoE()-la*(ip1.Eflux()-ip0.Eflux());
newsolgp.set(rho,m,rhoE); newsolgp.setData(rho,m,rhoE);
newsol.set(i,newsolgp); newsol.setData(i,newsolgp);
} // Leftmost node } // Leftmost node
// TRACE(15,"leftmost done"); // TRACE(15,"leftmost done");
@ -55,8 +63,8 @@ namespace td{
m=0.5*(im1.m()+ip1.m())-lambda*(ip1.Mflux()-im1.Mflux()); m=0.5*(im1.m()+ip1.m())-lambda*(ip1.Mflux()-im1.Mflux());
rhoE=0.5*(im1.rhoE()+ip1.rhoE())-lambda*(ip1.Eflux()-im1.Eflux()); rhoE=0.5*(im1.rhoE()+ip1.rhoE())-lambda*(ip1.Eflux()-im1.Eflux());
newsolgp.set(rho,m,rhoE); newsolgp.setData(rho,m,rhoE);
newsol.set(i,newsolgp); newsol.setData(i,newsolgp);
} // for over all gridpoints in mid } // for over all gridpoints in mid
{ {
@ -69,8 +77,8 @@ namespace td{
m=0; m=0;
rhoE=ip0.rhoE()-la*(0-im1.Eflux()); rhoE=ip0.rhoE()-la*(0-im1.Eflux());
newsolgp.set(rho,m,rhoE); newsolgp.setData(rho,m,rhoE);
newsol.set(i,newsolgp); newsol.setData(i,newsolgp);
} }
// Finally, update time and solution // Finally, update time and solution

View File

@ -3,11 +3,10 @@
#define _TUBE_H_ #define _TUBE_H_
#include "solutioninstance.h" #include "solutioninstance.h"
#include "globalconf.h"
namespace td{ namespace td{
extern tasystem::Globalconf gc;
typedef double d; typedef double d;
class Tube{ class Tube{
@ -16,13 +15,13 @@ namespace td{
SolutionInstance sol; // Solutions at time instances SolutionInstance sol; // Solutions at time instances
d t=0; // Current time d t=0; // Current time
d pleft(d t); // Compute pressure bc d pleft(d t); // Compute pressure bc
void Integrate(d dt);
public: public:
Tube(double L,int gp); Tube(double L,int gp);
~Tube(){} ~Tube(){}
SolutionInstance& getSol() { return sol;} SolutionInstance& getSol() { return sol;}
void setSol(const SolutionInstance& sol) {this->sol=sol;} void setSol(const SolutionInstance& sol) {this->sol=sol;}
void DoIntegration(d dt); void DoIntegration(d dt,int n=1);
d getTime(){return t;} d getTime(){return t;}
}; };

View File

@ -1,21 +1,71 @@
%module timedomaineuler %module timedomaineuler
%{ %{
#include "solutionatgp.h" #include "vtypes.h"
#include "test.h"
#include "tube.h" #include "tube.h"
#include "globalconf.h"
SPOILNAMESPACE
namespace td{
extern tasystem::Globalconf gc;
}
%} %}
typedef double d; typedef double d;
typedef unsigned us;
namespace tasystem{
class Globalconf{
public:
d T0,p0; /* Reference temperature and pressure (used to initialize a lot of variables. */
// finite volume size, speed of sound,
// deltax of volume
d kappa; // Artificial viscosity tuning factor,
// typically between 0.25 and 0.75
gases::Gas gas;
Globalconf(us Nf=0,d freq=100,const string& gasstring="air",d T0=293.15,d p0=101325.0,d kappa=1.0,bool driven=true);
const us& Nf() const;
const us& Ns() const;
static Globalconf airSTP(us Nf,d freq,d kappa=1.0);
~Globalconf(){TRACE(-5,"~Globalconf()");}
bool isDriven() const;
void setDriven(bool d);
d getomg() const;
d getfreq() const;
d c0() const;
d rho0() const;
d deltanu0() const;
vd omgvec;
void setNf(us);
/* %rename set setNffreq */
/* void set(us Nf,d freq); // Set data for new frequency and */
// number of samples
void setomg(d omg);
void setfreq(d freq);
void setGas(const string& mat);
const string& getGas() const;
void setMass(d mass);
d getMass() const;
void show() const;
// void setgas(string g){ gas(g);}
}; /* Class Globalconf */
} /* namespace tasystem */
namespace td{ namespace td{
class SolutionAtGp { class SolutionAtGp {
d rho_,m_=0,rhoE_=0;
public: public:
// Dependent variables
const d& rho() const; const d& rho() const;
const d& m() const; const d& m() const;
const d& rhoE() const; const d& rhoE() const;
// Derived from dependent
d u() const; d u() const;
d ekin() const; d ekin() const;
d estat() const; d estat() const;
@ -34,19 +84,21 @@ namespace td{
~SolutionInstance(){} ~SolutionInstance(){}
d getTime() const; d getTime() const;
void setTime(d t); void setTime(d t);
void set(us i,SolutionAtGp& sol); void setData(us i,SolutionAtGp& sol);
SolutionAtGp& get(us i); SolutionAtGp& get(us i);
void setrho(d rho); void setrho(d rho);
}; };
tasystem::Globalconf gc;
class Tube{ class Tube{
public: public:
Tube(double L,int gp); Tube(double L,int gp);
~Tube(){} ~Tube(){}
SolutionInstance& getSol() { return sol;} SolutionInstance& getSol();
void setSol(const SolutionInstance& sol) {this->sol=sol;} void setSol(const SolutionInstance& sol);
void DoIntegration(d dt); void DoIntegration(d dt,int n=1);
d getTime(){return t;} d getTime();
}; };
} }

160
timedomain.ipynb Normal file
View File

@ -0,0 +1,160 @@
{
"metadata": {
"name": "",
"signature": "sha256:15ea21addc5fb3913b7c7be02aef3ba5cdc13caaf5cb403564b9e56bc36c4974"
},
"nbformat": 3,
"nbformat_minor": 0,
"worksheets": [
{
"cells": [
{
"cell_type": "code",
"collapsed": false,
"input": [
"from timedomaineuler import *\n",
"#import timedomaineuler"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 1
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"#Globalconf accessable with cvar.gc\n",
"f=85.785\n",
"T=1/f\n",
"loglevel=20\n",
"L=1.0\n",
"gp=300\n",
"gc=cvar.gc #Reference!\n",
"dx=L/(gp-1); # One left and right gp, so\n",
"CFL=0.95;\n",
"gc.setfreq(f)\n",
"tube=Tube(L,gp)\n",
"dt=min(CFL*dx/gc.c0(),T/50)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 2
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"gc.getfreq()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 3,
"text": [
"85.785"
]
}
],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"tube.DoIntegration()"
],
"language": "python",
"metadata": {},
"outputs": []
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"sol=tube.getSol()"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 3
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"gp0=sol.get(0)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 5
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"gp0.rho()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 8,
"text": [
"1.2043280930847857"
]
}
],
"prompt_number": 8
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"gp0.setData(1.222)"
],
"language": "python",
"metadata": {},
"outputs": [],
"prompt_number": 11
},
{
"cell_type": "code",
"collapsed": false,
"input": [
"gp0.rho()"
],
"language": "python",
"metadata": {},
"outputs": [
{
"metadata": {},
"output_type": "pyout",
"prompt_number": 12,
"text": [
"1.222"
]
}
],
"prompt_number": 12
},
{
"cell_type": "code",
"collapsed": false,
"input": [],
"language": "python",
"metadata": {},
"outputs": []
}
],
"metadata": {}
}
]
}