App-Physics-ParticleMotion

 view release on metacpan or  search on metacpan

lib/App/Physics/ParticleMotion.pm  view on Meta::CPAN

package App::Physics::ParticleMotion;

use 5.006001;
use strict;
use warnings;

our $VERSION = '1.01';

use Carp qw/croak carp/;
use Math::Symbolic qw/:all/;
use Math::Symbolic::Compiler;
use Math::RungeKutta qw//;
use Math::Project3D;
use Tk qw/DoOneEvent DONT_WAIT MainLoop/;
use Tk::Cloth;
use Time::HiRes qw/time sleep/;
use Config::Tiny;

# a short little helper
sub _def_or ($$) { defined( $_[0] ) ? $_[0] : $_[1] }

sub new {
	my $proto = shift;
	my $class = ref($proto)||$proto;

	my $self = {
		run => 0,
		config => Config::Tiny->new(),
	};
	bless $self => $class;

	return $self;
}


sub config {
	my $self = shift;
	my $name = shift;

	return $self->{config} if not defined $name;
	
	if (ref($name) eq 'Config::Tiny') {
		$self->{config} = $name;
		return $name;
	}

	eval {$self->{config} = Config::Tiny->read($name)};
	croak "Could not read file '$name': $@." if ($@);

	return $self->{config};
}

sub run {
	my $app = shift;
	die "Cannot run app more than once." if $app->{run};
	$app->{run} = 1;
	my $config = $app->config();
	
	# Coordinate and velocity variable names
	my @coords = qw( x  y  z  );
	my @speeds = qw( vx vy vz );
	
	# Extract particle options from configuration file
	my @particles = map { $config->{$_} }
	  sort { substr( $a, 1 ) <=> substr( $b, 1 ) }
	  grep { /^p\d+$/ }
	  keys %$config;
	
	# Default colors for particles
	$_->{color} = defined( $_->{color} ) ? $_->{color} : '#FFFFFF'
	  foreach @particles;
	$_->{colort} = defined( $_->{colort} ) ? $_->{colort} : '#000000'
	  foreach @particles;
	
	# General configuration options
	my $gen_conf     = $config->{_};
	my $dimensions   = $gen_conf->{dimensions} || 3;
	$app->{dimensions} = $dimensions;

	my $particles    = scalar(@particles);
	$app->{particles} = \@particles;
	my $free         = $dimensions * $particles;
	$app->{free} = $free;
	my $axiscolor    = $gen_conf->{axiscolor} || '#000000';
	$app->{axiscolor} = $axiscolor;
	my $displayscale = $gen_conf->{zoom} || 20;
	$app->{displayscale} = $displayscale;
	my $time_warp = $gen_conf->{timewarp} || 1;
	$app->{time_warp} = $time_warp;
	my $epsilon   = $gen_conf->{epsilon}  || 0.00000000001;

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.965 second using v1.00-cache-1.14-grep-28634ff-cpan-4d46879620f )