Permalink
Browse files

use MooseX::Types::NumUnit more wholeheartedly

  • Loading branch information...
1 parent 2bcba40 commit 9690006aec1c25451df964b9611462145d2a557f @jberger committed Jun 18, 2012
View
@@ -12,63 +12,63 @@
use PDL::Graphics::Prima::Simple [700,500];
my $laser = Physics::UEMColumn::Laser->new(
- width => 1e-3,
- duration => 1e-12,
- energy => '4.75 eV',
+ width => '1 mm',
+ duration => '1 ps',
+ energy => '4.75 eV',
);
my $acc = Physics::UEMColumn::DCAccelerator->new(
- 'length' => 0.020,
- voltage => 20000,
+ length => '20 mm',
+ voltage => '20 kilovolts',
);
my $column = Physics::UEMColumn::Column->new(
- 'length' => 0.400,
- laser => $laser,
- accelerator => $acc,
+ length => '400 mm',
+ laser => $laser,
+ accelerator => $acc,
photocathode => Physics::UEMColumn::Photocathode->new(Ta),
);
my $solver_opts = {
- h_max => 5e-12,
+ h_max => 5e-12,
h_init => 1e-12 / 2,
};
my $sim = Physics::UEMColumn->new(
- column => $column,
- number => 1,
- debug => 1,
+ column => $column,
+ number => 1,
+ debug => 1,
solver_opts => $solver_opts,
);
-my $z_rf = 0.200;
-my $l_mag_lens = 25.4e-3;
-my $cooke_sep = 0.050;
-my $str_mag = 33e-13;
+my $z_rf = 20; #cm
+my $l_mag_lens = '1 in';
+my $cooke_sep = 5; #cm
+my $str_mag = 33e-13;
my $lens1 = Physics::UEMColumn::MagneticLens->new(
- location => $z_rf - $cooke_sep,
- 'length' => $l_mag_lens,
+ location => ($z_rf - $cooke_sep) . 'cm',
+ length => $l_mag_lens,
strength => $str_mag,
);
my $lens2 = Physics::UEMColumn::MagneticLens->new(
- location => $z_rf + $cooke_sep,
- 'length' => $l_mag_lens,
+ location => ($z_rf + $cooke_sep) . 'cm',
+ length => $l_mag_lens,
strength => $str_mag,
);
$sim->column->add_element($lens1);
$sim->column->add_element($lens2);
my $rf_cav = Physics::UEMColumn::RFCavity->new(
- location => $z_rf,
- 'length' => 0.02,
- strength => 2.3e5,
- frequency => 3e9,
+ location => $z_rf . 'cm',
+ length => '2 cm',
+ strength => '230 kilovolts / m',
+ frequency => '3 gigahertz',
);
$sim->column->add_element($rf_cav);
my $result = pdl( $sim->propagate );
-my $z = $result->slice('(1),');
+my $z = $result->slice('(1),');
my $st = $result->slice('(3),');
my $sz = $result->slice('(4),');
View
@@ -16,6 +16,9 @@ class Physics::UEMColumn {
use Physics::UEMColumn::Auxiliary ':all';
+ use MooseX::Types::NumUnit qw/num_of_unit/;
+ my $seconds = num_of_unit('s');
+
has 'debug' => ( isa => 'Num', is => 'ro', default => 0);
# possibly do transform here, in branches
@@ -35,10 +38,10 @@ class Physics::UEMColumn {
required => 1,
);
- has 'start_time' => ( isa => 'Num', is => 'rw', default => 0 );
- has 'end_time' => ( isa => 'Num', is => 'rw', lazy => 1, builder => '_est_init_end_time' );
+ has 'start_time' => ( isa => $seconds, is => 'rw', default => 0 );
+ has 'end_time' => ( isa => $seconds, is => 'rw', lazy => 1, builder => '_est_init_end_time' );
has 'steps' => (isa => 'Int', is => 'rw', default => 100); # this is not likely to be the number of output steps
- has 'step_width' => ( isa => 'Num', is => 'ro', lazy => 1, builder => '_set_step_width' );
+ has 'step_width' => ( isa => $seconds, is => 'ro', lazy => 1, builder => '_set_step_width' );
#when estimating end times what additional error should be given. Set to 1 for no extra time.
has 'time_error' => ( isa => 'Num', is => 'ro', default => 1.1 );
@@ -3,6 +3,8 @@ use Method::Signatures::Modifiers;
class Physics::UEMColumn::Column {
+ use MooseX::Types::NumUnit qw/num_of_unit/;
+
has laser => ( isa => 'Physics::UEMColumn::Laser', is => 'ro', required => 1);
has accelerator => ( isa => 'Physics::UEMColumn::Accelerator', is => 'ro', required => 1);
has photocathode => ( isa => 'Physics::UEMColumn::Photocathode', is => 'ro', required => 1);
@@ -17,7 +19,7 @@ class Physics::UEMColumn::Column {
default => sub{ [] },
);
- has 'length' => ( isa => 'Num', is => 'rw', required => 1 );
+ has 'length' => ( isa => num_of_unit('m'), is => 'rw', required => 1 );
method BUILD (Item $params) {
$self->add_element( $self->accelerator );
@@ -3,8 +3,11 @@ use Method::Signatures::Modifiers;
class Physics::UEMColumn::Element {
- has 'location' => ( isa => 'Num', is => 'ro', required => 1);
- has 'length' => ( isa => 'Num', is => 'ro', required => 1);
+ use MooseX::Types::NumUnit qw/num_of_unit/;
+ my $meters = num_of_unit('m');
+
+ has 'location' => ( isa => $meters, is => 'ro', required => 1);
+ has 'length' => ( isa => $meters, is => 'ro', required => 1);
has 'cutoff' => ( isa => 'Num', is => 'ro', default => 3); # relative distance to ignore effect
@@ -29,9 +32,10 @@ class Physics::UEMColumn::DCAccelerator
use Physics::UEMColumn::Auxiliary ':constants';
use Math::Trig qw/tanh sech/;
+ use MooseX::Types::NumUnit qw/num_of_unit/;
has '+location' => ( required => 0, default => 0 );
- has 'voltage' => ( isa => 'Num', is => 'ro', required => 1 );
+ has 'voltage' => ( isa => num_of_unit('V'), is => 'ro', required => 1 );
has 'sharpness' => ( isa => 'Num', is => 'ro', default => 10 );
override field () {
@@ -131,9 +135,10 @@ class Physics::UEMColumn::RFCavity
extends Physics::UEMColumn::Element {
use Physics::UEMColumn::Auxiliary ':constants';
+ use MooseX::Types::NumUnit qw/num_of_unit/;
- has 'strength' => (isa => 'Num', is => 'rw', required => 1);
- has 'frequency' => (isa => 'Num', is => 'ro', required => 1);
+ has 'strength' => (isa => num_of_unit('v/m'), is => 'rw', required => 1);
+ has 'frequency' => (isa => num_of_unit('Hz') , is => 'ro', required => 1);
#has 'radius' => (isa => 'Num', is => 'ro', required => 1);
has 'phase' => (isa => 'Num', is => 'ro', default => 0);
@@ -6,7 +6,7 @@ class Physics::UEMColumn::Laser {
use MooseX::Types::NumUnit qw/num_of_unit/;
has 'energy' => ( isa => num_of_unit('J'), is => 'ro', required => 1 );
- has 'width' => ( isa => 'Num', is => 'rw', required => 1 );
- has 'duration' => ( isa => 'Num', is => 'rw', required => 1 );
+ has 'width' => ( isa => num_of_unit('m'), is => 'rw', required => 1 );
+ has 'duration' => ( isa => num_of_unit('s'), is => 'rw', required => 1 );
}
@@ -11,7 +11,7 @@ class Physics::UEMColumn::Photocathode {
has 'energy_fermi' => ( isa => $type_energy, is => 'ro', required => 1 );
has 'work_function' => ( isa => $type_energy, is => 'ro', required => 1 );
- has 'location' => ( isa => 'Num', is => 'ro', default => 0 );
+ has 'location' => ( isa => num_of_unit('m'), is => 'ro', default => 0 );
has 'column' => ( isa => 'Physics::UEMColumn::Column', is => 'rw', predicate => 'has_column' );
method generate_pulse ( Num $num ) {
@@ -4,22 +4,23 @@ use Method::Signatures::Modifiers;
class Physics::UEMColumn::Pulse {
use Physics::UEMColumn::Auxiliary ':constants';
+ use MooseX::Types::NumUnit qw/num_of_unit/;
# user specified attributes
- has 'velocity' => ( isa => 'Num', is => 'rw', required => 1 );
-# has 'width' => ( isa => 'Num', is => 'rw', required => 1 );
-# has 'duration' => ( isa => 'Num', is => 'rw', required => 1 );
+ has 'velocity' => ( isa => num_of_unit('m/s'), is => 'rw', required => 1 );
- has 'location' => ( isa => 'Num', is => 'rw', default => 0 );
+ has 'location' => ( isa => num_of_unit('m'), is => 'rw', default => 0 );
has 'number' => ( isa => 'Num', is => 'rw', default => 1 );
# variance attributes
- has 'sigma_t' => ( isa => 'Num', is => 'rw', lazy => 1, builder => '_make_sigma_t' );
- has 'sigma_z' => ( isa => 'Num', is => 'rw', lazy => 1, builder => '_make_sigma_z' );
- has 'eta_t' => ( isa => 'Num', is => 'rw', lazy => 1, builder => '_make_eta_t' );
- has 'eta_z' => ( isa => 'Num', is => 'rw', lazy => 1, builder => '_make_eta_z' );
- has 'gamma_t' => ( isa => 'Num', is => 'rw', lazy => 1, builder => '_make_gamma_t' );
- has 'gamma_z' => ( isa => 'Num', is => 'rw', lazy => 1, builder => '_make_gamma_z' );
+ has 'sigma_t' => ( isa => num_of_unit('m^2'), is => 'rw', lazy => 1, builder => '_make_sigma_t' );
+ has 'sigma_z' => ( isa => num_of_unit('m^2'), is => 'rw', lazy => 1, builder => '_make_sigma_z' );
+ has 'eta_t' => ( isa => num_of_unit('(kg m / s)^2'), is => 'rw', lazy => 1, builder => '_make_eta_t' );
+ has 'eta_z' => ( isa => num_of_unit('(kg m / s)^2'), is => 'rw', lazy => 1, builder => '_make_eta_z' );
+ has 'gamma_t' => ( isa => num_of_unit('kg m^2 / s'), is => 'rw', lazy => 1, builder => '_make_gamma_t' );
+ has 'gamma_z' => ( isa => num_of_unit('kg m^2 / s'), is => 'rw', lazy => 1, builder => '_make_gamma_z' );
+
+ # derived quantities (units of gamma^2)
has 'liouville_gamma2_t' => ( isa => 'Num', is => 'rw', lazy => 1, builder => '_make_lg2_t' );
has 'liouville_gamma2_z' => ( isa => 'Num', is => 'rw', lazy => 1, builder => '_make_lg2_z' );

0 comments on commit 9690006

Please sign in to comment.