Design of an ODE Solver Environment. System of ODEs Consider the system of ODEs: Typically solved by forward Euler or 4th order Runge-Kutta.

Slides:



Advertisements
Similar presentations
NAG Workshop and Surgery
Advertisements

1 C++Tutorial Rob Jagnow This tutorial will be best for students who have at least had some exposure to Java or another comparable programming language.
Slide-1 University of Maryland Five Common Defect Types in Parallel Computing Prepared for Applied Parallel Computing Prof. Alan Edelman Taiga Nakamura.
Symbolic execution © Marcelo d’Amorim 2010.
Chapter 7: User-Defined Functions II
Expression Evaluation, Scope, Parameter Passing, Data Organization, Program Control, Binding By Ramon Quiusky CS 490 Fall 2003.
C Programming - Lecture 5
Kernighan/Ritchie: Kelley/Pohl:
Memory allocation CSE 2451 Matt Boggus. sizeof The sizeof unary operator will return the number of bytes reserved for a variable or data type. Determine:
1 CS 162 Introduction to Computer Science Chapter 8 Pointers Herbert G. Mayer, PSU Status 11/20/2014.
6/10/2015C++ for Java Programmers1 Pointers and References Timothy Budd.
COMP171 Data Structure & Algorithm Tutorial 1 TA: M.Y.Chan.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved Fundamentals of Strings and Characters Characters.
Rossella Lau Lecture 8, DCO10105, Semester B, DCO10105 Object-Oriented Programming and Design  Lecture 8: Polymorphism & C++ pointer  Inheritance.
Ordinary Differential Equations. Outline Announcements: –Homework II: Solutions on web –Homework III: due Wed. by 5, by Homework II Differential.
Adapted from Dr. Craig Chase, The University of Texas at Austin.
. Memory Management. Memory Organization u During run time, variables can be stored in one of three “pools”  Stack  Static heap  Dynamic heap.
C++ fundamentals.
Ordinary Differential Equations (ODEs) 1Daniel Baur / Numerical Methods for Chemical Engineers / Explicit ODE Solvers Daniel Baur ETH Zurich, Institut.
C++ Tutorial Rob Jagnow. Overview Pointers Arrays and strings Parameter passing Class basics Constructors & destructors Class Hierarchy Virtual Functions.
Numerical Solutions to ODEs Nancy Griffeth January 14, 2014 Funding for this workshop was provided by the program “Computational Modeling and Analysis.
Functional-Structural Plant Modelling with GroIMP and XL Tutorial and Workshop at Agrocampus Ouest, Angers, 5-7 May, 2015 Winfried Kurth University of.
GAlib A C++ Library of Genetic Algorithm Components Vanessa Herves Gómez Department of Computer Architecture and Technology,
C++ / G4MICE Course Session 3 Introduction to Classes Pointers and References Makefiles Standard Template Library.
1 COMP 2130 Introduction to Computer Systems Computing Science Thompson Rivers University.
Programming Languages and Paradigms Object-Oriented Programming.
Java and C++, The Difference An introduction Unit - 00.
CSE 332: C++ templates This Week C++ Templates –Another form of polymorphism (interface based) –Let you plug different types into reusable code Assigned.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 15: Linked data structures.
Working with Strings Lecture 2 Hartmut Kaiser
EE3561_Unit 8Al-Dhaifallah14351 EE 3561 : Computational Methods Unit 8 Solution of Ordinary Differential Equations Lesson 3: Midpoint and Heun’s Predictor.
OOD Case Study (For parallel treatment, see Chapter 2 of the text)
Algorithm Programming Bar-Ilan University תשס"ח by Moshe Fresko.
224 3/30/98 CSE 143 Recursion [Sections 6.1, ]
An Over View of Runge-Kutta Fehlberg and Dormand and Prince Methods. Numerical Methods To Solve Initial Value Problems William Mize.
RAS-Group, SoC, NUS (BAP: CS5234) Page 1 RAS Meeting (October-2002) J BAP Partitioning Challenge oPartitioning only – not packing J The BAP Software Overview.
Command Line Arguments plus Variable-Length Arrays Systems Programming.
Pointers OVERVIEW.
C Programming in Linux Jacob Chan. C/C++ and Java  Portable  Code written in one system and works in another  But in C, there are some libraries that.
COP4020 Programming Languages Names, Scopes, and Bindings Prof. Xin Yuan.
Advanced Computer Science Lesson 4: Reviewing Loops and Arrays Reading User Input.
CS 376b Introduction to Computer Vision 01 / 23 / 2008 Instructor: Michael Eckmann.
C Functions Three major differences between C and Java functions: –Functions are stand-alone entities, not part of objects they can be defined in a file.
C By Example 1 The assumption is that you know Java and need to extend that knowledge so you can program in C. 1. Hello world 2. declarations 3. pass by.
Topic 3: C Basics CSE 30: Computer Organization and Systems Programming Winter 2011 Prof. Ryan Kastner Dept. of Computer Science and Engineering University.
Solution to Midterm Exam
CSE 232: C++ memory management Overview of Arrays Arrays are the simplest kind of data structure –One item right after another in memory (“contiguous range”
Functions & Pointers in C Jordan Erenrich
Recap Cubic Spline Interpolation Multidimensional Interpolation Curve Fitting Linear Regression Polynomial Regression The Polyval Function The Interactive.
Intro to Simulink April 15, Copyright , Andy Packard. This work is licensed under the Creative Commons.
More About Data Types & Functions. General Program Structure #include statements for I/O, etc. #include's for class headers – function prototype statements.
Linked lists. Data structures to store a collection of items Data structures to store a collection of items are commonly used Typical operations on such.
STL CSSE 250 Susan Reeder. What is the STL? Standard Template Library Standard C++ Library is an extensible framework which contains components for Language.
CSCC69: Operating Systems Tutorial 10. Hints on testing Assignment 3 How to test tlb replacement algorithms? – Write a program that creates an array larger.
Functions Functions, locals, parameters, and separate compilation.
11 Introduction to Object Oriented Programming (Continued) Cats.
Object Oriented Programming. OOP  The fundamental idea behind object-oriented programming is:  The real world consists of objects. Computer programs.
CSE 332: C++ pointers, arrays, and references Overview of Pointers and References Often need to refer to another object –Without making a copy of the object.
Object Oriented Programming in C++ Chapter 7 Dynamic Binding.
Intro to Simulink Modified by Gary Balas 20 Feb 2011 Copyright , Andy Packard. This work is licensed under.
Stacks This presentation shows – how to implement the stack – how it can be used in real applications.
Dynamic Memory Management & Static Class Members Lecture No 7 Object Oriented Programming COMSATS Institute of Information Technology.
C++ Functions A bit of review (things we’ve covered so far)
CSC 143 P 1 CSC 143 Recursion [Chapter 5]. CSC 143 P 2 Recursion  A recursive definition is one which is defined in terms of itself  Example:  Compound.
Object-Oriented Programming Review 1. Object-Oriented Programming Object-Oriented Programming languages vary but generally all support the following features:
Command-Line Arguments
typedef typedef int Index; typedef char Letter; Index i; i = 17;
Built-In (a.k.a. Native) Types in C++
NAG Workshop and Surgery
C Programming - Lecture 5
Presentation transcript:

Design of an ODE Solver Environment

System of ODEs Consider the system of ODEs: Typically solved by forward Euler or 4th order Runge-Kutta

Traditional Approach Traditionally solved by library routines: SUBROUTINE RK4 (Y,T,F,WORK1,N,TSTEP,TOL1,TOL2,…) Y: current value of y T: current value for time t F: external function defining f WORK1: work array N: length of array TSTEP:time step value TOL1,TOL2:algorithmic parameters

Model Problem Define and you get:

Traditional Interface That is, the function F has the following interface: SUBROUTINE F (YDOT,Y,T,C1,C2,C3,C4,OMEGA) NB! Problem-dependent interface!! SUBROUTINE F (YDOT,Y,T) (Bad) Solution: COMMON block (global data) Solvers like RK4 wants:

The Object-Oriented Way User program can access base class (solver unknown) Class hierarchy of solvers Embedding of problem-specific parameters Uniform interface for function f

Suggested Design... RungeKutta4A RungeKutta4 RungeKutta2 ForwardEulerODEProblemODESolverOscillator...

Class ODESolver class ODESolver { protected: // members only visible in subclasses ODEProblem* eqdef; // definition of the ODE in user's class public: // members visible also outside the class ODESolver (ODEProblem* eqdef_) { eqdef = eqdef_; } virtual ~ODESolver () {} // always needed, does nothing here... virtual void init(); // initialize solver data structures virtual void advance (Vec(real)& y, real& t, real& dt); };

Class ODEProblem class ODEProblem { protected: ODESolver* solver; // some ODE solver Vec(real) y, y0; // solution (y) and initial condition (y0) real t, dt, T; // time loop parameters public: ODEProblem () {} virtual ~ODEProblem (); virtual void timeLoop (); virtual void equation (Vec(real)& f, const Vec(real)& y, real t); virtual int size (); // no of equations in the ODE system virtual void scan (); virtual void print (Os os); };

The Time Loop void ODEProblem:: timeLoop () { Os outfile (aform("%s.y",casename.c_str()), NEWFILE); t = 0; y = y0; outfile << t << " "; y.print(outfile); outfile << '\n'; while (t <= T) { solver->advance (y, t, dt); // update y, t, and dt outfile << t << " "; y.print(outfile); outfile << '\n'; }

Class Oscillator class Oscillator : public ODEProblem { protected: real c1,c2,c3,c4,omega; // problem dependent paramters public: Oscillator () {} virtual void equation (Vec(real)& f, const Vec(real)& y, real t); virtual int size () { return 2; } // 2x2 system of ODEs virtual void scan (); virtual void print (Os os); };

Defining the ODE void Oscillator::equation (Vec(real)& f, const Vec(real)& y_, real t_) { f(1) = y_(2); f(2) = -c1*(y_(2)+c2*y_(2)*abs(y_(2))) - c3*(y_(1)+c4*pow3(y_(1))) + sin(omega*t_); }

Class ForwardEuler class ForwardEuler : public ODESolver { Vec(real) scratch1; // needed in the algorithm public: ForwardEuler (ODEProblem* eqdef_); virtual void init (); // for allocating scratch1 virtual void advance (Vec(real)& y, real& t, real& dt); };

Stepping the Solver void ForwardEuler:: advance (Vec(real)& y, real& t, real& dt) { eqdef->equation (scratch1, y, t); // evaluate scratch1 (as f) const int n = y.size(); for (int i = 1; i <= n; i++) y(i) += dt * scratch1(i); t += dt; }

Parameter Classes class ODESolver_prm { public: String method; // name of subclass in ODESolver hierarchy ODEProblem* problem; // pointer to user's problem class ODESolver* create (); // create correct subclass of ODESolver }; ODESolver* ODESolver_prm:: create () { ODESolver* ptr = NULL; if (method == "ForwardEuler") ptr = new ForwardEuler (problem); else if (method == "RungeKutta4") ptr = new RungeKutta4 (problem); else errorFP("ODESolver_prm::create", "Method \"%s\" is not available",method.c_str()); return ptr; }

Problem Input void ODEProblem:: scan () { const int n = size(); // call size in actual subclass y.redim(n); y0.redim(n); s_o << "Give " << n << " initial conditions: "; y0.scan(s_i); s_o > dt; s_o > T; ODESolver_prm solver_prm; s_o << "Give name of ODE solver: "; s_i >> solver_prm.method; solver_prm.problem = this; solver = solver_prm.create(); solver->init(); // more reading in user's subclass }

Problem Input void Oscillator:: scan () { // first we need to do everything that ODEProblem::scan does: ODEProblem::scan(); // additional reading here: s_o << "Give c1, c2, c3, c4, and omega: "; s_i >> c1 >> c2 >> c3 >> c4 >> omega; print(s_o); // convenient check for the user }

The Main Program int main (int argc, const char* argv[]) { initDiffpack (argc, argv); Oscillator problem; problem.scan(); // read input data and initialize problem.timeLoop(); // solve problem return 0; }

What Has Been Gained? Some “disturbance” of basic numerics Flexible library Faster to define problem (f) than hardcoding solver Simple example, ideas transfer to very complicated scenarios

Exercise: Extending the ODE Environment Use the ODE environment to solve the problem u’(t) = -u(t) u(0) = 1 The analytical solution to this problem is u(t)=e -t.

Exercise: Extending the ODE Environment II Add the adaptive solver Runge-Kutta-Fehlberg to the class hierarchy of solvers. Solve the test problem given in the description of the RKF algorithm. Introduce run-time choices of problems, similar to the choice of solvers. See Exercises 1.12/1.13 in [HPL] for advice. Details of the RKF method and the test problem can be found at