Your SlideShare is downloading. ×
Modeling Physical Systems with Modern Object Oriented Perl
Upcoming SlideShare
Loading in...5
×

Thanks for flagging this SlideShare!

Oops! An error has occurred.

×
Saving this for later? Get the SlideShare app to save on your phone or tablet. Read anywhere, anytime – even offline.
Text the download link to your phone
Standard text messaging rates apply

Modeling Physical Systems with Modern Object Oriented Perl

601
views

Published on

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

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

Published in: Technology, Business

0 Comments
0 Likes
Statistics
Notes
  • Be the first to comment

  • Be the first to like this

No Downloads
Views
Total Views
601
On Slideshare
0
From Embeds
0
Number of Embeds
0
Actions
Shares
0
Downloads
5
Comments
0
Likes
0
Embeds 0
No embeds

Report content
Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
No notes for slide

Transcript

  • 1. Modeling Physical Systems with Modern Object Oriented Perl YAPC::NA 2012 Joel Berger University of Illinois at Chicago June 15, 2012Joel Berger (UIC) OO Physical Modeling June 15, 2012 1 / 15
  • 2. The ProblemsPhysical SimulationsDifferential EquationsA set of rules that define how variables change with some parameter dx x(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 SimulationsDifferential EquationsA set of rules that define how variables change with some parameter dx x(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 SimulationsDifferential EquationsA set of rules that define how variables change with some parameter dx x(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 Microscopy Joel Berger (UIC) OO Physical Modeling June 15, 2012 3 / 15
  • 6. The ProblemsMy Research: Ultrafast Electron Microscopy Photocathode Magnetic Lenses Detector Joel Berger (UIC) OO Physical Modeling June 15, 2012 3 / 15
  • 7. The ProblemsMy Research: Ultrafast Electron Microscopy Laser Photocathode Magnetic Lenses Detector Joel Berger (UIC) OO Physical Modeling June 15, 2012 3 / 15
  • 8. The ProblemsMy Research: Ultrafast Electron Microscopy Electron Beam Laser Photocathode Magnetic Lenses Detector Joel Berger (UIC) OO Physical Modeling June 15, 2012 3 / 15
  • 9. The ProblemsMy Research: Ultrafast Electron Microscopy RF Cavity Electron Beam Laser Photocathode Magnetic Lenses Detector Joel Berger (UIC) OO Physical Modeling June 15, 2012 3 / 15
  • 10. The ProblemsThe Challenge: A Flexible Interface to a Complex Model Magnetic Lens Magnetic LensElectron Pulse v = vˆ z w l RF Cavity “z lens” Need: Compute dynamics of electron pulse (w(t), l(t)) Note: Generation and optical elements add terms to DE Want: Representitive OO user-level interface Joel Berger (UIC) OO Physical Modeling June 15, 2012 4 / 15
  • 11. The ProblemsThe Challenge: A Flexible Interface to a Complex Model Magnetic Lens Magnetic LensElectron Pulse v = vˆ z w l RF Cavity “z lens” Need: Compute dynamics of electron pulse (w(t), l(t)) Note: Generation and optical elements add terms to DE Want: Representitive OO user-level interface Joel Berger (UIC) OO Physical Modeling June 15, 2012 4 / 15
  • 12. The ProblemsThe Challenge: A Flexible Interface to a Complex Model Magnetic Lens Magnetic LensElectron Pulse v = vˆ z w l RF Cavity “z lens” Need: Compute dynamics of electron pulse (w(t), l(t)) Note: Generation and optical elements add terms to DE Want: Representitive OO user-level interface Joel Berger (UIC) OO Physical Modeling June 15, 2012 4 / 15
  • 13. The ProblemsThe Challenge: A Flexible Interface to a Complex Model Magnetic Lens Magnetic LensElectron Pulse v = vˆ z w l RF Cavity “z lens” Need: Compute dynamics of electron pulse (w(t), l(t)) Note: Generation and optical elements add terms to DE Want: Representitive OO user-level interface Joel Berger (UIC) OO Physical Modeling June 15, 2012 4 / 15
  • 14. The ProblemsThe “State of the Art”Old codes are lacking full 6D dynamics optimized for performance vs usablilty hard to customize near impossible to comprehendhttp://laacg.lanl.gov/laacg/services/download_sf.phtmlGetting Started with Poisson Superfish. . . We do not recommend trying to build an input file “from scratch.”Instead, find an example file that is similar to the problem you are tryingto solve. Make a copy of the file and then make any necessarymodifications to the geometry and options. Joel Berger (UIC) OO Physical Modeling June 15, 2012 5 / 15
  • 15. The ProblemsThe “State of the Art”Old codes are lacking full 6D dynamics optimized for performance vs usablilty hard to customize near impossible to comprehendhttp://laacg.lanl.gov/laacg/services/download_sf.phtmlGetting Started with Poisson Superfish. . . We do not recommend trying to build an input file “from scratch.”Instead, find an example file that is similar to the problem you are tryingto solve. Make a copy of the file and then make any necessarymodifications 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 variants Can solve dynamics Unique OO language for Pretty-printing of math for physical simulation readability Classes have DEs as propertiesCons: Cons: Closed-source and expensive! Lacks “has-a” relationship No OO and no key-value Composing DEs not trivial datatypes User-facing object instantiation Still rather slow ∼2mins/sim not trivial Some numerical problems (?) Joel Berger (UIC) OO Physical Modeling June 15, 2012 6 / 15
  • 17. The ProblemsOther Attempts Modelica: Pros:Mathematica: Open-source, but behindPros: close-source variants Can solve dynamics Unique OO language for Pretty-printing of math for physical simulation readability Classes have DEs as propertiesCons: Cons: Closed-source and expensive! Lacks “has-a” relationship No OO and no key-value Composing DEs not trivial datatypes User-facing object instantiation Still rather slow ∼2mins/sim not trivial Some numerical problems (?) Joel Berger (UIC) OO Physical Modeling June 15, 2012 6 / 15
  • 18. The ProblemsOther Attempts Modelica: Pros:Mathematica: Open-source, but behindPros: close-source variants Can solve dynamics Unique OO language for Pretty-printing of math for physical simulation readability Classes have DEs as propertiesCons: Cons: Closed-source and expensive! Lacks “has-a” relationship No OO and no key-value Composing DEs not trivial datatypes User-facing object instantiation Still rather slow ∼2mins/sim not trivial Some numerical problems (?) Joel Berger (UIC) OO Physical Modeling June 15, 2012 6 / 15
  • 19. The ProblemsOther Attempts Modelica: Pros:Mathematica: Open-source, but behindPros: close-source variants Can solve dynamics Unique OO language for Pretty-printing of math for physical simulation readability Classes have DEs as propertiesCons: Cons: Closed-source and expensive! Lacks “has-a” relationship No OO and no key-value Composing DEs not trivial datatypes User-facing object instantiation Still rather slow ∼2mins/sim not trivial Some numerical problems (?) Joel Berger (UIC) OO Physical Modeling June 15, 2012 6 / 15
  • 20. The ProblemsOther Attempts Modelica: Pros:Mathematica: Open-source, but behindPros: close-source variants Can solve dynamics Unique OO language for Pretty-printing of math for physical simulation readability Classes have DEs as propertiesCons: Cons: Closed-source and expensive! Lacks “has-a” relationship No OO and no key-value Composing DEs not trivial datatypes User-facing object instantiation Still rather slow ∼2mins/sim not trivial Some numerical problems (?) Joel Berger (UIC) OO Physical Modeling June 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 storage has ’x’ => ( traits => [’RememberHistory’], current data during isa => ’Num’, is => ’rw’, simulation default => 0 all data afterwards );} works for fixed width solversmy $obj = MyClass->new; adaptive solvers call functions$obj->x( 1 ); repeatedly (i.e. PerlGSL::DiffEq)$obj->x( 2 );print join ’, ’, @{ $obj->x_history };# 0, 1, 2 Joel Berger (UIC) OO Physical Modeling June 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 storage has ’x’ => ( traits => [’RememberHistory’], current data during isa => ’Num’, is => ’rw’, simulation default => 0 all data afterwards );} works for fixed width solversmy $obj = MyClass->new; adaptive solvers call functions$obj->x( 1 ); repeatedly (i.e. PerlGSL::DiffEq)$obj->x( 2 );print join ’, ’, @{ $obj->x_history };# 0, 1, 2 Joel Berger (UIC) OO Physical Modeling June 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 storage has ’x’ => ( traits => [’RememberHistory’], current data during isa => ’Num’, is => ’rw’, simulation default => 0 all data afterwards );} works for fixed width solversmy $obj = MyClass->new; adaptive solvers call functions$obj->x( 1 ); repeatedly (i.e. PerlGSL::DiffEq)$obj->x( 2 );print join ’, ’, @{ $obj->x_history };# 0, 1, 2 Joel Berger (UIC) OO Physical Modeling June 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 storage has ’x’ => ( traits => [’RememberHistory’], current data during isa => ’Num’, is => ’rw’, simulation default => 0 all data afterwards );} works for fixed width solversmy $obj = MyClass->new; adaptive solvers call functions$obj->x( 1 ); repeatedly (i.e. PerlGSL::DiffEq)$obj->x( 2 );print join ’, ’, @{ $obj->x_history };# 0, 1, 2 Joel Berger (UIC) OO Physical Modeling June 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 storage has ’x’ => ( traits => [’RememberHistory’], current data during isa => ’Num’, is => ’rw’, simulation default => 0 all data afterwards );} works for fixed width solversmy $obj = MyClass->new; adaptive solvers call functions$obj->x( 1 ); repeatedly (i.e. PerlGSL::DiffEq)$obj->x( 2 );print join ’, ’, @{ $obj->x_history };# 0, 1, 2 Joel Berger (UIC) OO Physical Modeling June 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 storage has ’x’ => ( traits => [’RememberHistory’], current data during isa => ’Num’, is => ’rw’, simulation default => 0 all data afterwards );} works for fixed width solversmy $obj = MyClass->new; adaptive solvers call functions$obj->x( 1 ); repeatedly (i.e. PerlGSL::DiffEq)$obj->x( 2 );print join ’, ’, @{ $obj->x_history };# 0, 1, 2 Joel Berger (UIC) OO Physical Modeling June 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’ => ( isa => ’Num’, is => ’rw’, default => 0 ); has ’affect’ => ( isa => ’CodeRef’, is => ’ro’, required => 1 );}class MyThing { has ’mass’ => ( isa => ’Num’, is => ’ro’, required => 1 ); has ’x’ => ( traits => [ ’RememberHistory’ ], isa => ’Num’, is => ’rw’, default => 0 ); has ’vx’ => ( traits => [ ’RememberHistory’ ], isa => ’Num’, is => ’rw’, default => 0 );} Joel Berger (UIC) OO Physical Modeling June 15, 2012 8 / 15
  • 28. Perl Solution A Simple OO-DE ExampleThe Solver: Attributesclass MySim { use List::Util ’sum’; has ’start’ => ( isa => ’Num’, is => ’ro’, default => 0 ); has ’end’ => ( isa => ’Num’, is => ’ro’, default => 1 ); has ’steps’ => ( isa => ’Num’, is => ’ro’, default => 100 ); has ’step’ => ( isa => ’Num’, is => ’ro’, lazy => 1, builder => ’init_step’); has ’time’ => ( traits => [ ’RememberHistory’ ], isa => ’Num’, is => ’rw’, lazy => 1, builder => ’init_time’ ); has ’things’ => ( isa => ’ArrayRef[MyThing]’, is => ’rw’, default => sub{[]} ); has ’forces’ => ( isa => ’ArrayRef[MyForce]’, is => ’rw’, default => sub{[]} ); method init_step () { my $step = ($self->end - $self->start) / $self->steps; return $step; } method init_time () { return $self->start } Joel Berger (UIC) OO Physical Modeling June 15, 2012 9 / 15
  • 29. Perl Solution A Simple OO-DE ExampleThe Solver: Methods method evolve ( MyThing $thing ) { my $dt = $self->step; my $vx = $thing->vx; my $force = sum map { $_->affect->($_, $thing) } @{ $self->forces }; my $acc = $force / ($thing->mass); $thing->vx( $vx + $acc * $dt ); $thing->x( $thing->x + $vx * $dt ); } method run () { while ($self->time < $self->end) { $self->evolve( $_ ) for @{ $self->things }; $self->time( $self->time + $self->step ); } }} # end of class MySim Joel Berger (UIC) OO Physical Modeling June 15, 2012 10 / 15
  • 30. Perl Solution A Simple OO-DE ExampleThe Script#!/usr/bin/env perl my $sim = MySim->new(use strict; use warnings; end => 5,use MySim; things => [ $thing ],use PDL; forces => [ $acc, $dec ],use PDL::Graphics::Prima::Simple; );my $thing = MyThing->new( mass => 2 ); $sim->run;my $acc = MyForce->new( my $time = pdl $sim->time_history; strength => 2, my $x = pdl $thing->x_history; affect => sub {shift->strength},); line_plot($time, $x);my $dec = MyForce->new( strength => -30, affect => sub { my ($self, $thing) = @_; return 0 if ($thing->x < 0.1); return $self->strength; },); Joel Berger (UIC) OO Physical Modeling June 15, 2012 11 / 15
  • 31. Perl Solution A Simple OO-DE ExampleThe Script#!/usr/bin/env perl my $sim = MySim->new(use strict; use warnings; end => 5,use MySim; things => [ $thing ],use PDL; forces => [ $acc, $dec ],use PDL::Graphics::Prima::Simple; );my $thing = MyThing->new( mass => 2 ); $sim->run;my $acc = MyForce->new( my $time = pdl $sim->time_history; strength => 2, my $x = pdl $thing->x_history; affect => sub {shift->strength},); line_plot($time, $x);my $dec = MyForce->new( strength => -30, affect => sub { my ($self, $thing) = @_; return 0 if ($thing->x < 0.1); return $self->strength; },); Joel Berger (UIC) OO Physical Modeling June 15, 2012 11 / 15
  • 32. Units HandlingUnit Handling (The Implied Covenant)Mars Surveyor ’98 Orbiter Software used force in Newtons Users entered force in Foot-PoundsThe Covenant: Between programmer and user “Use the same units!” Unexpected and possibly undocumented action at a distance With 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 Newtons Users entered force in Foot-PoundsThe Covenant: Between programmer and user “Use the same units!” Unexpected and possibly undocumented action at a distance With 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 Newtons Users entered force in Foot-PoundsThe Covenant: Between programmer and user “Use the same units!” Unexpected and possibly undocumented action at a distance With 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 Newtons Users entered force in Foot-PoundsThe Covenant: Between programmer and user “Use the same units!” Unexpected and possibly undocumented action at a distance With Perl and Moose we can do better . . . Joel Berger (UIC) OO Physical Modeling June 15, 2012 12 / 15
  • 36. Units HandlingUnit Handling (The Implied Covenant)Mars Surveyor ’98 Orbiter (Sorry Larry!) Software used force in Newtons Users entered force in Foot-PoundsThe Covenant: Between programmer and user “Use the same units!” Unexpected and possibly undocumented action at a distance With Perl and Moose we can do better . . . Joel Berger (UIC) OO Physical Modeling June 15, 2012 12 / 15
  • 37. Units HandlingUnit Handling (The Implied Covenant)Mars Surveyor ’98 Orbiter (Sorry Larry!) Software used force in Newtons Users entered force in Foot-PoundsThe Covenant: Between programmer and user “Use the same units!” Unexpected and possibly undocumented action at a distance With Perl and Moose we can do better . . . Joel Berger (UIC) OO Physical Modeling June 15, 2012 12 / 15
  • 38. Units HandlingMooseX::Types::NumUnit Str to Num coercions convert unit if neededuse MooseX::Declare;use Method::Signatures::Modifiers;class SphericalCow { use MooseX::Types::NumUnit qw/num_of_unit/; has ’radius’ => ( isa => num_of_unit(’m’), is => ’rw’, default => 1 ); has ’velocity’ => ( isa => num_of_unit(’m/s’), is => ’rw’, default => 0 );}use strict; use warnings;my $cow = SphericalCow->new( radius => ’1 ft’,);print $cow->radius . "n"; # 0.3048 Joel Berger (UIC) OO Physical Modeling June 15, 2012 13 / 15
  • 39. Real World ExampleExample of Physics::UEMColumnBack to Electron Column Modeling Magnetic Lens Magnetic LensElectron Pulse v = vˆ z w l RF Cavity “z lens” As yet unreleased Physics::UEMColumn https://github.com/jberger/Physics-UEMColumn Uses: PerlGSL::DiffEq on CPAN C-level solver of Perl-level DE closures Joel Berger (UIC) OO Physical Modeling June 15, 2012 14 / 15
  • 40. FinallyAcknowledgements Graduate College Dean’s Fellowship (Major Student Funding) LAS Ph.D. Travel Award (Conference Funding) Department of Energy #DE-FG52-09NA29451 (UEM Research Grant)https://github.com/jberger/YAPCNA2012 Joel Berger (UIC) OO Physical Modeling June 15, 2012 15 / 15

×