Finite Volume Code

14
FINITE VOLUME CODE Tim Handy

description

Finite Volume Code. Tim Handy. Goals. Modular Code – Didn’t want to have to rework all of my code to add a new feature Dynamic Variables/Functions – Minimal assumptions about code requirements apriori No anticipation of special variables required for a particular method - PowerPoint PPT Presentation

Transcript of Finite Volume Code

FINITE VOLUME CODETim Handy

Goals

1. Modular Code – Didn’t want to have to rework all of my code to add a new feature

2. Dynamic Variables/Functions – Minimal assumptions about code requirements apriori

1. No anticipation of special variables required for a particular method

2. Ability to use scriptable C (Ch) for non-constant, analytic input conditions

3. Try some stuff I don’t normally do

High Level

Simulation

Mesh Initial ConditionsBoundary Conditions

Interpolation Advection Output

Cell

1-Dimensional Level

Simulation1D

Mesh1D- Uniform1D

Initial Conditions(In Progress)

Boundary Conditions

- Periodic1D

Interpolation- Interpolation1D

Advection- Advection1D

Output

Cell1D

Simulation1Dclass Simulation1D : public Simulation{ protected: Mesh1D* mesh; BoundaryCondition1D* bc; InitialCondition* ic; Advection1D* advect; Interpolation1D* interp; Output* output; std::string resultsfile; std::map<std::string, std::string> solvers; std::multimap<int,outputinfo*> write_info;

.

.

.}; Primary constructor takes an input file as an argument and sets up other classes Abusing polymorphism as much as possible Simvars is a C++ Map of string->double

Allows for dynamic addition/removal of variables, clear accessing (no need to remember array indices) Stores generic information other classes may require or are pertinent at the simulation level

Solvers maps a string->string “interpolation”->”PPM”, “advection”->”Euler”

Allows easy input file parsing (no need for 40 cases for different variables)

Mesh1D

class Mesh1D : public Mesh{ public:

Cell1D** cells;int num_ghost;int index_lb, index_ub;int Ntotal;...

};

Holds array (probably change to vector ASAP) of Cell1Ds (Total size = Ncells + 2*num_ghost)

Makes no assumptions about distribution of cells, just here to hold generic information and generic function calls/virtual functions

In 1D case, holds information on

Cell1D

class Cell1D : public Cell{

public:

double xl, xr, xc, dx;double A;

void populate_vars(){ vars["density"] = 0.0; vars["density_w"] = 0.0; vars["density_e"] = 0.0; vars["xl"] = this->xl; vars["xr"] = this->xr; vars["xc"] = this->xc; vars["dx"] = this->dx; vars["A"] = this->A; vars["vx"] = 0.0; vars["vx_e"] = 0.0; vars["vx_w"] = 0.0;} void add_var(std::string var, double val); void remove_var(std::string var);

class Cell{ protected: int index; std::map<char,Cell*> neighbors; public: std::map<std::string,double> vars;

…};

Interpolation1Dclass Interpolation1D{ private: typedef void(*ptr2Interp)(Mesh1D*,double,std::string); public: std::map<std::string, void (*)(Mesh1D*,double,std::string)> func_ptrs; Interpolation1D() { // probably need to be a bit safer here func_ptrs["FOU"] = FOupwind1D; func_ptrs["SOU"] = linear_upwind1D; func_ptrs["PPM"] = PPM; func_ptrs["PPMnonmono"] = nonmonoPPM; }

…};

Interpolation 1D contains the First Order, Second Order, and PPM schemes that operate on a given mesh for a given variable

Contains a map of strings->function pointers (Member functions are made static to make this easy) Essentially just a wrapper for a set of functions, but allows easy modularity

Input File

N 64CFL 0.25tmin 0tmax 6.283185307180V0 -1xmin 0xmax 6.283185307180maxiter 3000solver interpolation PPMnonmonowrite density.dat density 1write velocity.dat vx 4write xc.dat xc 1

How to Run a SimulationSimulation1D* sim = new Simulation1D(in); sim->simulate();… void simulate() { this->t = this->simvars["tmin"]; double Tmax = this->simvars["tmax"]; int iteration = 0;

do { iteration++; double dt = this->stable_timestep(); this->set_timestep(dt); this->t += dt; if(this->t > Tmax) { dt -= this->t-Tmax; this->t = Tmax; } std::string interp_method = this->solvers["interpolation"]; interp->run(interp_method, this->mesh, dt, "density"); std::string advect_method = this->solvers["advection"]; advect->run(advect_method, this->mesh, dt, "density"); this->write_output(iteration); }while(t<Tmax && iteration < this->simvars["maxiter"]); }

Temporal Rate of Convergence

Spatial Rate of Convergence

Limited Visible Change

Current/Future Work

Rework InitialCondition/BoundaryCondition + Source terms to utilize scriptable C functions using Ch for MMS & incorporate Automatic Differentiation library

Minor code reorganization and front/back-end options (snapshots, forced timestep)

Incorporate new mesh types/BC