Formulæ in with Specification of Formulæ in with University of Göttingen 28 February 2012
Formulæ in with Outline 1 2 3 4 5
Formulæ in with ultimately we want to specify reactions like 2H 2 + O 2 k f kb 2H 2 O use operator overloading to enable this in automatically derive differential equations from this and solve these numerically possible for elementary reactions via law of mass action
Formulæ in with Functions like in C++ define function with special name name is operator<symbol>, where <symbol> is one of +, -, *, /, %,... unary vs. binary operators count parameters of function plus one extra for non-static functions (left operand is this)
Formulæ in with Functions Example class Complex { double real, imag ; public Complex ( double real, double imag ) { this. r e a l = r e a l ; this. imag = imag ; public s t a t i c Complex operator+ ( Complex a, Complex b ) { return new Complex ( a. r e a l + b. real, a. imag + b. imag ) ;...
Formulæ in with Functions Conclusion allows: but not yet: Complex a = new Complex ( 1, 2 ) ; Complex b = new Complex ( 3, 4 ) ; Complex c = a + b ; Complex d = 1 + a ; Complex e = a + 1; could be resolved by providing additional operator overloads better solutions are implicit conversions
Formulæ in with Implicit Conversion like in C++ extends autoboxing from primitive to reference types can be defined in any of the following ways: class C { C(S source ) ; s t a t i c C valueof (S source ) ; T tot ( ) ; t tvalue ( ) ; follows existing patterns like tostring(), intvalue(), etc.
Formulæ in with Implicit Conversion Example class Complex {... @de. grogra. x l. lang. ConversionConstructor public C( double d ) { r e a l = d ; imag = 0;
Formulæ in with Introduction reaction equation: reaction rate: r = 1 ν 1 d[a] dt ν 1 A + ν 2 B +... ν 1A + ν 2B +... = 1 ν 2 d[b] dt = 1 ν 1 d[a ] dt = 1 ν 2 d[b ] dt =... rate law: r = k[a] ν 1 [B] ν 2...
Formulæ in with Parse Tree Generation Reaction left right Expression Expression Term Term Term 2H 2 O 2 2H 2 O in : 2*H2 + O2 <=> 2*H2O;
Formulæ in with Molecule / / each e n t i t y i n the r e a c t i o n i s a molecule class Molecule { / / predefine some instances of Molecule so t h a t they / / can be s t a t i c a l l y imported i n t o c u r r e n t scope public s t a t i c f i n a l Molecule H2 = new Molecule ( "H2" ) ; public s t a t i c f i n a l Molecule H2O = new Molecule ( "H2O" ) ; public s t a t i c f i n a l Molecule O2 = new Molecule ( "O2" ) ; S t r i n g name ; public Molecule ( S t r i n g name) { this. name = name ; public S t r i n g t o S t r i n g ( ) { return name ;
Formulæ in with Reaction Arrow / / stores the whole r e a c t i o n class Reaction { Expression l e f t, r i g h t ; double kf, kb ; / / i n f e r r e a c t i o n from l e f t and r i g h t side public s t a t i c Reaction operator <=> ( Expression lhs, Expression rhs ) { Reaction r e s u l t = new Reaction ( ) ; r e s u l t. l e f t = l h s ; r e s u l t. r i g h t = rhs ; return r e s u l t ;
Formulæ in with Expressions / / an expression i s a l i s t of terms class Expression { f i n a l ArrayList <Term> terms = new ArrayList <Term > ( ) ; public void add ( Term term ) { terms. add ( term ) ; / / terms are combined by + operator public s t a t i c Expression operator+ ( Expression expr, Term term ) { expr. add ( term ) ; return expr ;
Formulæ in with Term / / f a c t o r i s s t o i c h i o m e t r i c c o e f f i c i e n t f o r molecule class Term { double f a c t o r ; Molecule m; public Term ( Molecule m) { this ( 1, m) ; public Term ( double factor, Molecule m) { this. f a c t o r = f a c t o r ; this.m = m; public s t a t i c Term operator ( double factor, Term term ) { term. f a c t o r = f a c t o r ; return term ;
Formulæ in with Implicit Conversions Some implicit conversions are needed to cover remaining cases: 1) Molecule Term 2) Molecule Expression 3) Term Expression
Formulæ in with Application put all reactions into a Model derive ODEs from reaction equation for numerical integration use law of mass action need to assign indices to each Molecule map Molecule int with java.util.hashmap can then use any standard method for integration
Formulæ in with Example 1 Reaction A k 1 B k 2 C const Molecule A = new Molecule ( "A" ) ; const Molecule B = new Molecule ( "B" ) ; const Molecule C = new Molecule ( "C" ) ; public void run ( ) { Reaction r1 = A <=> B ; / / f i r s t reaction : A > B r1. setforwardrateconstant ( 2 ) ; f i n a l Model model = new Model ( ) ; model. addslope ( r1 ) ; model. add (B <=> C, 1 ) ; / / second r e a c t i o n : B > C...
Formulæ in with Example 1 Reaction A k 1 B k 2 C 10 8 concentration 6 4 A B C 2 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time
Formulæ in with Example 2 Michaelis-Menten reaction: in : E + S k f kr ES k cat E + P f i n a l Model model = new Model ( ) ; model. add (E + S <=> ES, 3, 0. 1 ) ; model. add (ES <=> E + P, 2 ) ;
Formulæ in with Example 2 Michaelis-Menten 10 8 concentration 6 4 E S ES P 2 0 0 0.5 1 1.5 2 2.5 3 3.5 4 4.5 5 time
Formulæ in with operator overloading very versatile makes chemical reactions part of the language syntax checking included also other applications right-hand sides of rules iostreams-like input/output retrofit existing classes like BigInteger and BigDecimal with overloaded operators
Formulæ in with Thank you for your attention.
Formulæ in with
Formulæ in with Backup Slides
Formulæ in with Example 1 Reaction A k 1 B k 2 C... / / assign i n d i c e s f i n a l HashMap m = new HashMap ( ) ; i n t count = model. assignindices ( 0, m) ; / / a l l o c a t e memory double [ ] y0 = new double [ count ] ; double [ ] y = new double [ count ] ; / / set i n i t i a l c o n d i t i o n s setvalue (m, y0, A, 10);...
Formulæ in with Example 1 Reaction A k 1 B k 2 C... / / prepare d i f f e r e n t i a l equations / / getrate ( ) w i l l be c a l l e d by the i n t e g r a t o r ODE ode = new ODE( ) { public void getrate ( double [ ] out, double t, double [ ] y ) { Arrays. f i l l ( out, 0 ) ; model. eval ( out, t, y ) ; ;...
Formulæ in with Example 1 Reaction A k 1 B k 2 C... / / create numerical s o l v e r Solver s o l v e r = new F i r s t O r d e r I n t e g r a t o r A d a p t e r ( new C l a s s i c a l R u n g e K u t t a I n t e g r a t o r ( 0. 0 0 1 ) ) ;...
Formulæ in with Example 1 Reaction A k 1 B k 2 C... / / setup monitor f u n c t i o n to p l o t s t a t e over time solver. setmonitor ( 1, new Monitor ( ) { public void g ( double [ ] out, double t, double [ ] y ) { / / t r i g g e r at 20Hz out [ 0 ] = s i n ( PI t 2 0 ) ; public boolean handleevent ( i n t i, double t, double [ ] y ) { / / p l o t data or whatever return false ; ) ;...
Formulæ in with Example 1 Reaction A k 1 B k 2 C... / / perform i n t e g r a t i o n s o l v e r. i n t e g r a t e ( ode, 0, y0, 5, y ) ;