<

Perl 6 and the Real World

Physical Modelling with Perl 6

Moritz Lenz <moritz.lenz@gmail.com>

Max Planck Institute for the Science of Light

Perl 6 and the Real World - Structure

What is a Model?

Example Model

Example Model

Model takes into account

Model neglects

Is it a good model?

Another model: free falling

Free falling: Solved in Perl 6

use Math::Model;

my $m = Math::Model.new(
    derivatives => {
        velocity      => 'height',
        acceleration  => 'velocity',
    },
    variables   => {
        acceleration  => { $:gravity },   # m / s**2
        gravity       => { -9.81 },       # m / s**2
    },
    # ...

Free falling: Solved in Perl 6

    # ...
    initials    => {
        height        => 50,              # m
        velocity      => 0,               # m/s
    },
    captures    => ('height', 'velocity'),
);

$m.integrate(:from(0), :to(4.2), :min-resolution(0.2));
$m.render-svg('free-fall.svg', :title('Free falling'));

Model result

The model in detail

use Math::Model;

my $m = Math::Model.new(
    derivatives => {
        velocity      => 'height',
        acceleration  => 'velocity',
    },

Derivative: slope of another quantity

Common derivatives in Mechanics

Derivative          Of

velocity            position
angular velocity    angle
acceleration        velocity
power               energy
force               momentum
                    (= mass * velocity)

Common derivatives

current             charge

birth rate          population
    - mortality rate

profit              funds

Using derivatives

Rest of the model

    variables   => {
        acceleration  => { $:gravity },   # m / s**2
        gravity       => { -9.81 },       # m / s**2
    },
    initials    => {
        height        => 50,              # m
        velocity      => 0,               # m/s
    },
    captures    => ('height', 'velocity'),
);

$m.integrate(:from(0), :to(4.2), :min-resolution(0.2));
$m.render-svg('free-fall.svg', :title('Free falling'));

Perl 6 stuff

Extending the model - Spring, damping

Spring, gravity, damping: source code

# ...
variables   => {
    acceleration  => { $:gravity + $:spring + $:damping },
    gravity       => { -9.81 },
    spring        => { - 2 * $:height },
    damping       => { - 0.2 * $:velocity },
},
# ...

Spring, gravity, damping: results

Further extensions

External driving force: Code

sub MAIN($freq) {
  my $m = Math::Model.new(
    # ...
    variables   => {
      acceleration  => { $:gravity + $:spring
                          + $:damping + $:ext_force },
      gravity       => { -9.81 },
      spring        => { - 2 * $:height },
      damping       => { - 0.2 * $:velocity },
      ext_force     => { sin(2 * pi * $:time * $freq) },
    },
    # ...
  );

  my %h = $m.integrate(:from(0), :to(70), :min-resolution(5));
  $m.render-svg("spring-freq-$freq.svg", 
     :title("Spring with damping, external force at $freq"));

Driving force: low frequency

Driving force: higher frequency

Driving force: higher frequency

Driving force: higher frequency

Driving force: higher frequency

Driving force: higher frequency

External driving force: Observations

Amplitude vs. Frequency

Resonance controls ...

String theories

Limits of Math::Model

Summary