Working python-c interface. This is fun!
This commit is contained in:
parent
c7e3f5fb1e
commit
2329bc8e8f
@ -51,7 +51,7 @@ add_library(sources ${src})
|
||||
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 "-Wextra")
|
||||
|
||||
set_source_files_properties(timedomain.i PROPERTIES SWIG_FLAGS "-includeall")
|
||||
swig_add_module(timedomaineuler python timedomain.i)
|
||||
swig_link_libraries(timedomaineuler sources nonlin armadillo ${PYTHON_LIBRARIES})
|
||||
|
||||
|
@ -5,7 +5,6 @@
|
||||
TRACETHIS
|
||||
|
||||
namespace td{
|
||||
|
||||
extern tasystem::Globalconf gc;
|
||||
|
||||
SolutionAtGp::SolutionAtGp() {
|
||||
|
@ -3,6 +3,8 @@
|
||||
namespace td{
|
||||
|
||||
typedef double d;
|
||||
typedef unsigned us;
|
||||
|
||||
class SolutionAtGp {
|
||||
d rho_,m_=0,rhoE_=0;
|
||||
public:
|
||||
|
@ -1,7 +1,4 @@
|
||||
#include "solutioninstance.h"
|
||||
#include "tube.h"
|
||||
|
||||
|
||||
|
||||
namespace td{
|
||||
|
||||
@ -14,7 +11,7 @@ namespace td{
|
||||
TRACE(15,"SolutionInstance::setrho()");
|
||||
|
||||
for(auto gp=gps.begin();gp!=gps.end();gp++){
|
||||
gp->set(rho);
|
||||
gp->setData(rho);
|
||||
}
|
||||
} // setrho()
|
||||
int SolutionInstance::save(std::ofstream& str,data d){
|
||||
|
@ -1,6 +1,6 @@
|
||||
#pragma once
|
||||
#include "solutionatgp.h"
|
||||
#include "globalconf.h"
|
||||
#include "vtypes.h"
|
||||
|
||||
namespace td{
|
||||
SPOILNAMESPACE
|
||||
@ -9,8 +9,6 @@ namespace td{
|
||||
p,u
|
||||
};
|
||||
|
||||
extern tasystem::Globalconf gc;
|
||||
|
||||
class SolutionInstance{
|
||||
vector<SolutionAtGp> gps;
|
||||
d time=0;
|
||||
@ -19,7 +17,7 @@ namespace td{
|
||||
~SolutionInstance(){}
|
||||
d getTime() const {return time;}
|
||||
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); }
|
||||
|
||||
void setrho(d rho);
|
||||
|
28
src/tube.cpp
28
src/tube.cpp
@ -1,8 +1,6 @@
|
||||
#include "tube.h"
|
||||
|
||||
namespace td{
|
||||
|
||||
// Definition of a Globalconf in this namespace. Is initialized in main()
|
||||
tasystem::Globalconf gc;
|
||||
|
||||
d Tube::pleft(d t){
|
||||
@ -12,15 +10,25 @@ namespace td{
|
||||
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()");
|
||||
dx=L/(gp-1);
|
||||
VARTRACE(15,gp);
|
||||
sol.setrho(gc.rho0());
|
||||
}
|
||||
|
||||
void Tube::DoIntegration(d dt){
|
||||
void Tube::DoIntegration(d dt,int n){
|
||||
TRACE(14,"Tube::DoIntegration()");
|
||||
int integrations=0;
|
||||
while(integrations<n){
|
||||
Integrate(dt);
|
||||
}
|
||||
}
|
||||
void Tube::Integrate(d dt){
|
||||
|
||||
// Define new time
|
||||
d newt=t+dt;
|
||||
@ -41,8 +49,8 @@ namespace td{
|
||||
d momfluxl=ip0.rho()*pow(ip0.u(),2)+pleft(t);
|
||||
m=ip0.m()-la*(ip1.Mflux()-momfluxl);
|
||||
rhoE=ip0.rhoE()-la*(ip1.Eflux()-ip0.Eflux());
|
||||
newsolgp.set(rho,m,rhoE);
|
||||
newsol.set(i,newsolgp);
|
||||
newsolgp.setData(rho,m,rhoE);
|
||||
newsol.setData(i,newsolgp);
|
||||
|
||||
} // Leftmost node
|
||||
// TRACE(15,"leftmost done");
|
||||
@ -55,8 +63,8 @@ namespace td{
|
||||
m=0.5*(im1.m()+ip1.m())-lambda*(ip1.Mflux()-im1.Mflux());
|
||||
rhoE=0.5*(im1.rhoE()+ip1.rhoE())-lambda*(ip1.Eflux()-im1.Eflux());
|
||||
|
||||
newsolgp.set(rho,m,rhoE);
|
||||
newsol.set(i,newsolgp);
|
||||
newsolgp.setData(rho,m,rhoE);
|
||||
newsol.setData(i,newsolgp);
|
||||
|
||||
} // for over all gridpoints in mid
|
||||
{
|
||||
@ -69,8 +77,8 @@ namespace td{
|
||||
m=0;
|
||||
rhoE=ip0.rhoE()-la*(0-im1.Eflux());
|
||||
|
||||
newsolgp.set(rho,m,rhoE);
|
||||
newsol.set(i,newsolgp);
|
||||
newsolgp.setData(rho,m,rhoE);
|
||||
newsol.setData(i,newsolgp);
|
||||
}
|
||||
|
||||
// Finally, update time and solution
|
||||
|
@ -3,11 +3,10 @@
|
||||
#define _TUBE_H_
|
||||
|
||||
#include "solutioninstance.h"
|
||||
#include "globalconf.h"
|
||||
|
||||
namespace td{
|
||||
|
||||
extern tasystem::Globalconf gc;
|
||||
|
||||
typedef double d;
|
||||
|
||||
class Tube{
|
||||
@ -16,13 +15,13 @@ namespace td{
|
||||
SolutionInstance sol; // Solutions at time instances
|
||||
d t=0; // Current time
|
||||
d pleft(d t); // Compute pressure bc
|
||||
|
||||
void Integrate(d dt);
|
||||
public:
|
||||
Tube(double L,int gp);
|
||||
~Tube(){}
|
||||
SolutionInstance& getSol() { return sol;}
|
||||
void setSol(const SolutionInstance& sol) {this->sol=sol;}
|
||||
void DoIntegration(d dt);
|
||||
void DoIntegration(d dt,int n=1);
|
||||
d getTime(){return t;}
|
||||
};
|
||||
|
||||
|
72
timedomain.i
72
timedomain.i
@ -1,21 +1,71 @@
|
||||
%module timedomaineuler
|
||||
%{
|
||||
#include "solutionatgp.h"
|
||||
#include "test.h"
|
||||
#include "vtypes.h"
|
||||
#include "tube.h"
|
||||
#include "globalconf.h"
|
||||
SPOILNAMESPACE
|
||||
namespace td{
|
||||
extern tasystem::Globalconf gc;
|
||||
}
|
||||
%}
|
||||
|
||||
|
||||
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{
|
||||
|
||||
class SolutionAtGp {
|
||||
d rho_,m_=0,rhoE_=0;
|
||||
public:
|
||||
// Dependent variables
|
||||
const d& rho() const;
|
||||
const d& m() const;
|
||||
const d& rhoE() const;
|
||||
// Derived from dependent
|
||||
d u() const;
|
||||
d ekin() const;
|
||||
d estat() const;
|
||||
@ -34,19 +84,21 @@ namespace td{
|
||||
~SolutionInstance(){}
|
||||
d getTime() const;
|
||||
void setTime(d t);
|
||||
void set(us i,SolutionAtGp& sol);
|
||||
void setData(us i,SolutionAtGp& sol);
|
||||
SolutionAtGp& get(us i);
|
||||
void setrho(d rho);
|
||||
};
|
||||
|
||||
tasystem::Globalconf gc;
|
||||
|
||||
class Tube{
|
||||
public:
|
||||
Tube(double L,int gp);
|
||||
~Tube(){}
|
||||
SolutionInstance& getSol() { return sol;}
|
||||
void setSol(const SolutionInstance& sol) {this->sol=sol;}
|
||||
void DoIntegration(d dt);
|
||||
d getTime(){return t;}
|
||||
SolutionInstance& getSol();
|
||||
void setSol(const SolutionInstance& sol);
|
||||
void DoIntegration(d dt,int n=1);
|
||||
d getTime();
|
||||
};
|
||||
}
|
||||
|
||||
|
160
timedomain.ipynb
Normal file
160
timedomain.ipynb
Normal 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": {}
|
||||
}
|
||||
]
|
||||
}
|
Loading…
Reference in New Issue
Block a user