Modeling Physical Systems with Modern Object Oriented Perl

Download Modeling Physical Systems with Modern Object Oriented Perl

Post on 06-May-2015

609 views

Category:

Technology

4 download

Embed Size (px)

DESCRIPTION

Presented at YAPC::NA 2012. Conference site: http://act.yapcna.org/2012/talk/79 github repo: https://github.com/jberger/YAPCNA2012/tree/master/NumSim

TRANSCRIPT

<ul><li>1.Modeling Physical Systems with Modern Object Oriented PerlYAPC::NA 2012Joel BergerUniversity of Illinois at ChicagoJune 15, 2012Joel Berger (UIC)OO Physical Modeling June 15, 2012 1 / 15</li></ul><p>2. The ProblemsPhysical SimulationsDierential EquationsA set of rules that dene how variables change with some parameterdxx(t2 ) = x(t1 ) + dt dtExample: Exponential Growth dM = rM=M (t) = P exp(rt)dt Joel Berger (UIC)OO Physical Modeling June 15, 2012 2 / 15 3. The ProblemsPhysical SimulationsDierential EquationsA set of rules that dene how variables change with some parameterdxx(t2 ) = x(t1 ) + dt (x, f (t1, t2))dtExample: Exponential Growth dM = rM=M (t) = P exp(rt)dt Joel Berger (UIC)OO Physical Modeling June 15, 2012 2 / 15 4. The ProblemsPhysical SimulationsDierential EquationsA set of rules that dene how variables change with some parameterdxx(t2 ) = x(t1 ) + dt (x, f (t1, t2))dtExample: Exponential Growth dM = rM=M (t) = P exp(rt)dt Joel Berger (UIC)OO Physical Modeling June 15, 2012 2 / 15 5. The ProblemsMy Research: Ultrafast Electron MicroscopyJoel Berger (UIC) OO Physical Modeling June 15, 2012 3 / 15 6. The ProblemsMy Research: Ultrafast Electron MicroscopyPhotocathode Magnetic LensesDetectorJoel Berger (UIC) OO Physical Modeling June 15, 2012 3 / 15 7. The ProblemsMy Research: Ultrafast Electron MicroscopyLaserPhotocathode Magnetic LensesDetectorJoel Berger (UIC) OO Physical Modeling June 15, 2012 3 / 15 8. The ProblemsMy Research: Ultrafast Electron Microscopy Electron BeamLaserPhotocathode Magnetic LensesDetectorJoel Berger (UIC) OO Physical Modeling June 15, 2012 3 / 15 9. The ProblemsMy Research: Ultrafast Electron Microscopy RF Cavity Electron BeamLaserPhotocathode Magnetic LensesDetectorJoel Berger (UIC) OO Physical Modeling June 15, 2012 3 / 15 10. The ProblemsThe Challenge: A Flexible Interface to a Complex Model Magnetic LensMagnetic LensElectron Pulse v = vzw lRF Cavity z lensNeed: Compute dynamics of electron pulse (w(t), l(t))Note: Generation and optical elements add terms to DEWant: Representitive OO user-level interfaceJoel Berger (UIC)OO Physical Modeling June 15, 2012 4 / 15 11. The ProblemsThe Challenge: A Flexible Interface to a Complex Model Magnetic LensMagnetic LensElectron Pulse v = vzw lRF Cavity z lensNeed: Compute dynamics of electron pulse (w(t), l(t))Note: Generation and optical elements add terms to DEWant: Representitive OO user-level interfaceJoel Berger (UIC)OO Physical Modeling June 15, 2012 4 / 15 12. The ProblemsThe Challenge: A Flexible Interface to a Complex Model Magnetic LensMagnetic LensElectron Pulse v = vzw lRF Cavity z lensNeed: Compute dynamics of electron pulse (w(t), l(t))Note: Generation and optical elements add terms to DEWant: Representitive OO user-level interfaceJoel Berger (UIC)OO Physical Modeling June 15, 2012 4 / 15 13. The ProblemsThe Challenge: A Flexible Interface to a Complex Model Magnetic LensMagnetic LensElectron Pulse v = vzw lRF Cavity z lensNeed: Compute dynamics of electron pulse (w(t), l(t))Note: Generation and optical elements add terms to DEWant: Representitive OO user-level interfaceJoel Berger (UIC)OO Physical Modeling June 15, 2012 4 / 15 14. The ProblemsThe State of the ArtOld codes arelacking full 6D dynamicsoptimized for performance vs usabliltyhard to customizenear impossible to comprehendhttp://laacg.lanl.gov/laacg/services/download_sf.phtmlGetting Started with Poisson Supersh. . . We do not recommend trying to build an input le from scratch.Instead, nd an example le that is similar to the problem you are tryingto solve. Make a copy of the le and then make any necessarymodications to the geometry and options. Joel Berger (UIC)OO Physical Modeling June 15, 2012 5 / 15 15. The ProblemsThe State of the ArtOld codes arelacking full 6D dynamicsoptimized for performance vs usabliltyhard to customizenear impossible to comprehendhttp://laacg.lanl.gov/laacg/services/download_sf.phtmlGetting Started with Poisson Supersh. . . We do not recommend trying to build an input le from scratch.Instead, nd an example le that is similar to the problem you are tryingto solve. Make a copy of the le and then make any necessarymodications to the geometry and options. Joel Berger (UIC)OO Physical Modeling June 15, 2012 5 / 15 16. The ProblemsOther Attempts Modelica: Pros:Mathematica:Open-source, but behindPros:close-source variantsCan solve dynamicsUnique OO language forPretty-printing of math for physical simulationreadabilityClasses have DEs as propertiesCons: Cons:Closed-source and expensive!Lacks has-a relationshipNo OO and no key-valueComposing DEs not trivialdatatypesUser-facing object instantiationStill rather slow 2mins/simnot trivialSome numerical problems (?)Joel Berger (UIC) OO Physical ModelingJune 15, 2012 6 / 15 17. The ProblemsOther Attempts Modelica: Pros:Mathematica:Open-source, but behindPros:close-source variantsCan solve dynamicsUnique OO language forPretty-printing of math for physical simulationreadabilityClasses have DEs as propertiesCons: Cons:Closed-source and expensive!Lacks has-a relationshipNo OO and no key-valueComposing DEs not trivialdatatypesUser-facing object instantiationStill rather slow 2mins/simnot trivialSome numerical problems (?)Joel Berger (UIC) OO Physical ModelingJune 15, 2012 6 / 15 18. The ProblemsOther Attempts Modelica: Pros:Mathematica:Open-source, but behindPros:close-source variantsCan solve dynamicsUnique OO language forPretty-printing of math for physical simulationreadabilityClasses have DEs as propertiesCons: Cons:Closed-source and expensive!Lacks has-a relationshipNo OO and no key-valueComposing DEs not trivialdatatypesUser-facing object instantiationStill rather slow 2mins/simnot trivialSome numerical problems (?)Joel Berger (UIC) OO Physical ModelingJune 15, 2012 6 / 15 19. The ProblemsOther Attempts Modelica: Pros:Mathematica:Open-source, but behindPros:close-source variantsCan solve dynamicsUnique OO language forPretty-printing of math for physical simulationreadabilityClasses have DEs as propertiesCons: Cons:Closed-source and expensive!Lacks has-a relationshipNo OO and no key-valueComposing DEs not trivialdatatypesUser-facing object instantiationStill rather slow 2mins/simnot trivialSome numerical problems (?)Joel Berger (UIC) OO Physical ModelingJune 15, 2012 6 / 15 20. The ProblemsOther Attempts Modelica: Pros:Mathematica:Open-source, but behindPros:close-source variantsCan solve dynamicsUnique OO language forPretty-printing of math for physical simulationreadabilityClasses have DEs as propertiesCons: Cons:Closed-source and expensive!Lacks has-a relationshipNo OO and no key-valueComposing DEs not trivialdatatypesUser-facing object instantiationStill rather slow 2mins/simnot trivialSome numerical problems (?)Joel Berger (UIC) OO Physical ModelingJune 15, 2012 6 / 15 21. Perl Solution. . . But First, Some BookkeepingIf an object knows where it is, can it remember where it came from?use MooseX::Declare;use Method::Signatures::Modifiers;use MooseX::RememberHistory;class MyClass { use objects as data storagehas x =&gt; (traits =&gt; [RememberHistory], current data duringisa =&gt; Num, is =&gt; rw,simulationdefault =&gt; 0 all data afterwards);} works for xed width solversmy $obj = MyClass-&gt;new;adaptive solvers call functions$obj-&gt;x( 1 ); repeatedly (i.e. PerlGSL::DiffEq)$obj-&gt;x( 2 );print join , , @{ $obj-&gt;x_history };# 0, 1, 2Joel Berger (UIC) OO Physical ModelingJune 15, 2012 7 / 15 22. Perl Solution. . . But First, Some BookkeepingIf an object knows where it is, can it remember where it came from?use MooseX::Declare;use Method::Signatures::Modifiers;use MooseX::RememberHistory;class MyClass { use objects as data storagehas x =&gt; (traits =&gt; [RememberHistory], current data duringisa =&gt; Num, is =&gt; rw,simulationdefault =&gt; 0 all data afterwards);} works for xed width solversmy $obj = MyClass-&gt;new;adaptive solvers call functions$obj-&gt;x( 1 ); repeatedly (i.e. PerlGSL::DiffEq)$obj-&gt;x( 2 );print join , , @{ $obj-&gt;x_history };# 0, 1, 2Joel Berger (UIC) OO Physical ModelingJune 15, 2012 7 / 15 23. Perl Solution. . . But First, Some BookkeepingIf an object knows where it is, can it remember where it came from?use MooseX::Declare;use Method::Signatures::Modifiers;use MooseX::RememberHistory;class MyClass { use objects as data storagehas x =&gt; (traits =&gt; [RememberHistory], current data duringisa =&gt; Num, is =&gt; rw,simulationdefault =&gt; 0 all data afterwards);} works for xed width solversmy $obj = MyClass-&gt;new;adaptive solvers call functions$obj-&gt;x( 1 ); repeatedly (i.e. PerlGSL::DiffEq)$obj-&gt;x( 2 );print join , , @{ $obj-&gt;x_history };# 0, 1, 2Joel Berger (UIC) OO Physical ModelingJune 15, 2012 7 / 15 24. Perl Solution. . . But First, Some BookkeepingIf an object knows where it is, can it remember where it came from?use MooseX::Declare;use Method::Signatures::Modifiers;use MooseX::RememberHistory;class MyClass { use objects as data storagehas x =&gt; (traits =&gt; [RememberHistory], current data duringisa =&gt; Num, is =&gt; rw,simulationdefault =&gt; 0 all data afterwards);} works for xed width solversmy $obj = MyClass-&gt;new;adaptive solvers call functions$obj-&gt;x( 1 ); repeatedly (i.e. PerlGSL::DiffEq)$obj-&gt;x( 2 );print join , , @{ $obj-&gt;x_history };# 0, 1, 2Joel Berger (UIC) OO Physical ModelingJune 15, 2012 7 / 15 25. Perl Solution. . . But First, Some BookkeepingIf an object knows where it is, can it remember where it came from?use MooseX::Declare;use Method::Signatures::Modifiers;use MooseX::RememberHistory;class MyClass { use objects as data storagehas x =&gt; (traits =&gt; [RememberHistory], current data duringisa =&gt; Num, is =&gt; rw,simulationdefault =&gt; 0 all data afterwards);} works for xed width solversmy $obj = MyClass-&gt;new;adaptive solvers call functions$obj-&gt;x( 1 ); repeatedly (i.e. PerlGSL::DiffEq)$obj-&gt;x( 2 );print join , , @{ $obj-&gt;x_history };# 0, 1, 2Joel Berger (UIC) OO Physical ModelingJune 15, 2012 7 / 15 26. Perl Solution. . . But First, Some BookkeepingIf an object knows where it is, can it remember where it came from?use MooseX::Declare;use Method::Signatures::Modifiers;use MooseX::RememberHistory;class MyClass { use objects as data storagehas x =&gt; (traits =&gt; [RememberHistory], current data duringisa =&gt; Num, is =&gt; rw,simulationdefault =&gt; 0 all data afterwards);} works for xed width solversmy $obj = MyClass-&gt;new;adaptive solvers call functions$obj-&gt;x( 1 ); repeatedly (i.e. PerlGSL::DiffEq)$obj-&gt;x( 2 );print join , , @{ $obj-&gt;x_history };# 0, 1, 2Joel Berger (UIC) OO Physical ModelingJune 15, 2012 7 / 15 27. Perl Solution A Simple OO-DE ExamplePhysical Classesuse MooseX::RememberHistory;use MooseX::Declare;use Method::Signatures::Modifiers;class MyForce {has strength =&gt; ( isa =&gt; Num, is =&gt; rw, default =&gt; 0 );has affect =&gt; ( isa =&gt; CodeRef, is =&gt; ro, required =&gt; 1 );}class MyThing {has mass =&gt; ( isa =&gt; Num, is =&gt; ro, required =&gt; 1 );has x=&gt; ( traits =&gt; [ RememberHistory ], isa =&gt; Num,is =&gt; rw, default =&gt; 0 );has vx =&gt; ( traits =&gt; [ RememberHistory ], isa =&gt; Num,is =&gt; rw, default =&gt; 0 );} Joel Berger (UIC) OO Physical ModelingJune 15, 2012 8 / 15 28. Perl Solution A Simple OO-DE ExampleThe Solver: Attributesclass MySim {use List::Util sum; has start=&gt; ( isa =&gt; Num, is =&gt; ro, default =&gt; 0 ); has end=&gt; ( isa =&gt; Num, is =&gt; ro, default =&gt; 1 ); has steps=&gt; ( isa =&gt; Num, is =&gt; ro, default =&gt; 100 ); has step =&gt; ( isa =&gt; Num, is =&gt; ro, lazy =&gt; 1, builder =&gt; init_step); has time =&gt; ( traits =&gt; [ RememberHistory ], isa =&gt; Num, is =&gt; rw, lazy =&gt; 1, builder =&gt; init_time ); has things =&gt; ( isa =&gt; ArrayRef[MyThing], is =&gt; rw, default =&gt; sub{[]} ); has forces =&gt; ( isa =&gt; ArrayRef[MyForce], is =&gt; rw, default =&gt; sub{[]} ); method init_step () { my $step = ($self-&gt;end - $self-&gt;start) / $self-&gt;steps; return $step; } method init_time () { return $self-&gt;start } Joel Berger (UIC)OO Physical ModelingJune 15, 2012 9 / 15 29. Perl Solution A Simple OO-DE ExampleThe Solver: Methods method evolve ( MyThing $thing ) { my $dt = $self-&gt;step; my $vx = $thing-&gt;vx; my $force = sum map { $_-&gt;affect-&gt;($_, $thing) } @{ $self-&gt;forces }; my $acc = $force / ($thing-&gt;mass); $thing-&gt;vx( $vx + $acc * $dt ); $thing-&gt;x( $thing-&gt;x + $vx * $dt ); }method run () {while ($self-&gt;time &lt; $self-&gt;end) {$self-&gt;evolve( $_ ) for @{ $self-&gt;things };$self-&gt;time( $self-&gt;time + $self-&gt;step );}}} # end of class MySimJoel Berger (UIC) OO Physical ModelingJune 15, 2012 10 / 15 30. Perl Solution A Simple OO-DE ExampleThe Script#!/usr/bin/env perlmy $sim = MySim-&gt;new(use strict; use warnings;end =&gt; 5,use MySim; things =&gt; [ $thing ],use PDL; forces =&gt; [ $acc, $dec ],use PDL::Graphics::Prima::Simple;);my $thing = MyThing-&gt;new( mass =&gt; 2 ); $sim-&gt;run;my $acc = MyForce-&gt;new(my $time = pdl $sim-&gt;time_history;strength =&gt; 2, my $x= pdl $thing-&gt;x_history;affect =&gt; sub {shift-&gt;strength},); line_plot($time, $x);my $dec = MyForce-&gt;new(strength =&gt; -30,affect =&gt; sub {my ($self, $thing) = @_;return 0 if ($thing-&gt;x &lt; 0.1);return $self-&gt;strength;},);Joel Berger (UIC) OO Physical ModelingJune 15, 2012 11 / 15 31. Perl Solution A Simple OO-DE ExampleThe Script#!/usr/bin/env perlmy $sim = MySim-&gt;new(use strict; use warnings;end =&gt; 5,use MySim; things =&gt; [ $thing ],use PDL; forces =&gt; [ $acc, $dec ],use PDL::Graphics::Prima::Simple;);my $thing = MyThing-&gt;new( mass =&gt; 2 ); $sim-&gt;run;my $acc = MyForce-&gt;new(my $time = pdl $sim-&gt;time_history;strength =&gt; 2, my $x= pdl $thing-&gt;x_history;affect =&gt; sub {shift-&gt;strength},); line_plot($time, $x);my $dec = MyForce-&gt;new(strength =&gt; -30,affect =&gt; sub {my ($self, $thing) = @_;return 0 if ($thing-&gt;x &lt; 0.1);return $self-&gt;strength;},);Joel Berger (UIC) OO Physical ModelingJune 15, 2012 11 / 15 32. Units HandlingUnit Handling (The Implied Covenant)Mars Surveyor 98 OrbiterSoftware used force in NewtonsUsers entered force in Foot-PoundsThe Covenant:Between programmer and userUse the same units!Unexpected and possibly undocumented action at a distanceWith Perl and Moose we can do better . . .Joel Berger (UIC)OO Physical Modeling June 15, 2012 12 / 15 33. Units HandlingUnit Handling (The Implied Covenant)Mars Surveyor 98 Orbiter (Sorry Larry!)Software used force in NewtonsUsers entered force in Foot-PoundsThe Covenant:Between programmer and userUse the same units!Unexpected and possibly undocumented action at a distanceWith Perl and Moose we can do better . . . Joel Berger (UIC) OO Physical Modeling June 15, 2012 12 / 15 34. Units HandlingUnit Handling (The Implied Covenant)Mars Surveyor 98 Orbiter (Sorry Larry!)Software used force in NewtonsUsers entered force in Foot-PoundsThe Covenant:Between programmer and userUse the same units!Unexpected and possibly undocumented action at a distanceWith Perl and Moose we can do better . . . Joel Berger (UIC) OO Physical Modeling June 15, 2012 12 / 15 35. Units HandlingUnit Handling (The Implied Covenant)Mars Surveyor 98 Orbiter (Sorry Larry!)Software used force in NewtonsUsers entered force in Foot-PoundsThe Covenant:Between programmer and userUse the same units!Unexpected and possibly undocumented action at a distanceWith Perl and Moose we can do better ....</p>

Recommended

View more >