Physics-Particles
view release on metacpan or search on metacpan
lib/Physics/Particles.pm view on Meta::CPAN
# See the POD documentation at the end of this
# document for detailed copyright information.
# (c) 2002-2003 Steffen Mueller, all rights reserved.
package Physics::Particles;
use 5.006;
use strict;
use warnings;
use constant C_VACUUM => 299792458;
use constant C_VACUUM_SQUARED => 299792458 * 299792458;
use Carp;
use Data::Dumper;
use vars qw/$VERSION/;
$VERSION = '1.02';
# constructor new
#
# Does not require any arguments. All arguments
# directly modify the object as key/value pairs.
# returns freshly created simulator object.
sub new {
my $proto = shift;
my $class = ref $proto || $proto;
# Make a new object with default values.
my $self = {
forces => [], # All forces (callbacks) will be stored here.
p => [], # All particles (hashrefs) will be stored here.
p_attr => {
x => 0,
y => 0,
z => 0,
vx => 0,
vy => 0,
vz => 0,
m => 1,
n => '',
},
@_
};
bless $self => $class;
}
# method set_particle_default
#
# Takes a hash reference whichis then used as is for
# particle default attributes such as position, velocity, mass,
# unique ID, or whatever properties you fancy.
sub set_particle_default {
my $self = shift;
my $hashref = shift;
ref $hashref eq 'HASH'
or croak "Must pass hash reference to set_particle_default().";
$self->{p_attr} = $hashref;
return 1;
}
# method add_particle
#
# Takes key/value pairs of particle attributes.
# Attributes default to whatever has been set using
# set_particle_default.
# A new particle represented by the attributes is then
# created in the simulation and its particle number is
# returned. Attributes starting with an underscore are
# reserved to internal attributes (so don't use any of them).
sub add_particle {
my $self = shift;
my $particle = {
%{$self->{p_attr}},
@_,
_fx => 0,
_fy => 0,
_fz => 0,
};
my $particle_no = $self->_make_particle();
$self->{p}[$particle_no] = $particle;
return $particle_no;
}
# private method _make_particle
#
# Returns a currently unused particle number or
# appends an empty particle to the particle list.
sub _make_particle {
my $self = shift;
my $count = 0;
foreach (@{$self->{p}}) {
return $count unless ref $_;
$count++;
( run in 1.587 second using v1.01-cache-2.11-cpan-0bb4e1dffa6 )