From 2329bc8e8f934548db36c09d28f0e5d4ed21666f Mon Sep 17 00:00:00 2001 From: Anne_ctwtm Date: Tue, 10 Feb 2015 10:07:29 +0100 Subject: [PATCH] Working python-c interface. This is fun! --- CMakeLists.txt | 2 +- src/solutionatgp.cpp | 1 - src/solutionatgp.h | 2 + src/solutioninstance.cpp | 5 +- src/solutioninstance.h | 6 +- src/tube.cpp | 28 ++++--- src/tube.h | 7 +- timedomain.i | 72 +++++++++++++++--- timedomain.ipynb | 160 +++++++++++++++++++++++++++++++++++++++ 9 files changed, 249 insertions(+), 34 deletions(-) create mode 100644 timedomain.ipynb diff --git a/CMakeLists.txt b/CMakeLists.txt index ad8436e..3b2ee98 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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}) diff --git a/src/solutionatgp.cpp b/src/solutionatgp.cpp index ced3dce..f2d7f6d 100644 --- a/src/solutionatgp.cpp +++ b/src/solutionatgp.cpp @@ -5,7 +5,6 @@ TRACETHIS namespace td{ - extern tasystem::Globalconf gc; SolutionAtGp::SolutionAtGp() { diff --git a/src/solutionatgp.h b/src/solutionatgp.h index 2bb5e37..f62a11f 100644 --- a/src/solutionatgp.h +++ b/src/solutionatgp.h @@ -3,6 +3,8 @@ namespace td{ typedef double d; + typedef unsigned us; + class SolutionAtGp { d rho_,m_=0,rhoE_=0; public: diff --git a/src/solutioninstance.cpp b/src/solutioninstance.cpp index 0213e8d..da2e80f 100644 --- a/src/solutioninstance.cpp +++ b/src/solutioninstance.cpp @@ -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){ diff --git a/src/solutioninstance.h b/src/solutioninstance.h index d05ba51..40c540d 100644 --- a/src/solutioninstance.h +++ b/src/solutioninstance.h @@ -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 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); diff --git a/src/tube.cpp b/src/tube.cpp index 3ddaac5..bbb6688 100644 --- a/src/tube.cpp +++ b/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(integrationssol=sol;} - void DoIntegration(d dt); + void DoIntegration(d dt,int n=1); d getTime(){return t;} }; diff --git a/timedomain.i b/timedomain.i index 6383816..201a143 100644 --- a/timedomain.i +++ b/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(); }; } diff --git a/timedomain.ipynb b/timedomain.ipynb new file mode 100644 index 0000000..0d476f9 --- /dev/null +++ b/timedomain.ipynb @@ -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": {} + } + ] +} \ No newline at end of file