Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 5 of 5

Thread: for loop help

  1. #1
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts

    for loop help

    I was trying to create a simple 2D planetary simulator, and I was making it write the resultant data to a file. Well, the incrementing doesn't seem to be working, and I assume this may have to do with the problem. Only one line of data is written to the file.

    Code:
    #!usr/bin/perl
    
    #2D Planetary Gravity Simulator
    #version 1.0
    use Math::Trig;
    use Fcntl; 
    
    
    print "Input some parameters to create a simulation.\n";
    
    #parameters
    print "Star mass (M sol): ";
    my $starMass = <STDIN>;
    chomp $starMass;
    print "Planet mass (M earth): ";
    my $planetMass = <STDIN>;
    chomp $planetMass;
    print "Moon mass (M luna): ";
    my $moonMass = <STDIN>;
    chomp $moonMass;
    print "Planet location x (AU): ";
    my $planetLocX = <STDIN>;
    chomp $planetLocX;
    print "Planet location Y (AU): ";
    my $planetLocY = <STDIN>;
    chomp $planetLocY;
    print "Planet velocity (m/s): ";
    my $planetVel = <STDIN>;
    chomp $planetVel;
    print "Planet theta (deg): ";
    my $planetTheta = <STDIN>;
    chomp $planetTheta;
    print "Moon location x (Mm): ";
    my $moonLocX = <STDIN>;
    chomp $moonLocX;
    print "Moon location Y (Mm): ";
    my $moonLocY = <STDIN>;
    chomp $moonLocY;
    print "Moon velocity (m/s): ";
    my $moonVel = <STDIN>;
    chomp $moonVel;
    print "Moon theta (deg): ";
    my $moonTheta = <STDIN>;
    chomp $moonTheta;
    print "Timestep (h): ";
    my $timestep = <STDIN>;
    chomp $timestep;
    print "Iterations: ";
    my $iter = <STDIN>;
    chomp $iter;
    my $starLocX = 0;
    my $starLocY = 0;
    my $starTheta = 0.001;
    my $starVel = 0;
    my $count = $iter;
    
    #multiply distances
    my $planetLocX = $planetLocX*149598000;
    my $planetLocY = $planetLocY*149598000;
    my $moonLocX = $moonLocX*1000000;
    my $moonLocY = $moonLocY*1000000;
    
    #file output
    print "Type the filepath you want to save to: ";
    my $FILEPATH = <STDIN>;
    chomp $FILEPATH;
    sysopen(TEXT, $FILEPATH, O_CREAT|O_RDWR);
    printf TEXT "StarX StarY PlanetX PlanetY MoonX MoonY";
    close (TEXT);
    
    #calculate
    for($i = $iter; $i > 0; $i--)
    {
    #COUNTER
    my $counter = 0;
    $counter++;
    
    #PLANET-STAR CALCULATIONS
    #force magnitude
    my $g = 6.67*10**(1/11);
    my $kgMassStar = $starMass*(1.9889*10**30);
    my $kgMassPlan = $planetMass*(5.9742*10**24);
    my $theta = atan($planetLocY/$planetLocX);
    my $sine = sin($theta);
    my $distance = ($planetLocY/$sine);
    my $F = $g*(($kgMassStar*$kgMassPlan)/$distance);
    #acceleration magnitude
    my $planetAcceleration = ($F/$kgMassPlan);
    my $starAcceleration = ($F/$kgMassStar);
    my $starAccTheta = $theta; #result
    my $planetAccTheta = $theta+180; #result
    my $planetDeltaLoc = $planetAcceleration*($timestep*3600); #result
    my $starDeltaLoc = $starAcceleration*($timestep*3600); #result
    
    #PLANET-MOON CALCULATIONS
    #force magnitude
    my $kgMassMoon = $moonMass*(7.36*10**22);
    my $theta = atan($moonLocY-$planetLocY/$moonLocX-$planetLocX);
    my $sine = sin($theta);
    my $distance = ($moonLocY/$sine);
    my $F = $g*(($kgMassPlan*$kgMassMoon)/$distance);
    #acceleration magnitude
    $planetAcceleration = ($F/$kgMassPlan);
    $moonAcceleration = ($F/$kgMassMoon);
    $planetAccTheta2 = $theta; #result
    $moonAccTheta = $theta+180; #result
    $planetDeltaLoc2 = $planetAcceleration*($timestep*3600); #result
    $moonDeltaLoc = $moonAcceleration*($timestep*3600); #result
    
    #STAR-MOON CALCULATIONS
    #force
    my $theta = atan($moonLocY/$moonLocX);
    my $sine = sin($theta);
    my $distance = ($moonLocY/$sine);
    my $F = $g*(($kgMassStar*$kgMassPlan)/$distance);
    #acceleration magnitude
    $starAcceleration = ($F/$kgMassStar);
    $moonAcceleration = ($F/$kgMassMoon);
    $starAccTheta2 = $theta; #result
    $moonAccTheta2 = $theta+180; #result
    $moonDeltaLoc2 = $moonAcceleration*($timestep*3600); #result
    $starDeltaLoc2 = $starAcceleration*($timestep*3600); #result
    
    #VECTOR ADDITION
    #convert polar to cartesian
    	#planet star
    	my $sine = sin($planetAccTheta);
    	my $psVelY = ($planetDeltaLoc*$sine);
    	my $tan = tan($planetAccTheta);
    	my $psVelX = ($psVelY/$tan);
    	#planet moon
    	my $sine = sin($planetAccTheta2);
    	my $pmVelY = ($planetDeltaLoc2*$sine);
    	my $tan = tan($planetAccTheta2);
    	my $pmVelX = ($pmVelY/$tan);
    	#star planet
    	my $sine = sin($starAccTheta);
    	my $spVelY = ($starDeltaLoc*$sine);
    	my $tan = tan($starAccTheta);
    	my $spVelX = ($spVelY/$tan);
    	#star moon
    	my $sine = sin($starAccTheta2);
    	my $smVelY = ($starDeltaLoc2*$sine);
    	my $tan = tan($starAccTheta2);
    	my $smVelX = ($smVelY/$tan);
    	#moon planet
    	my $sine = sin($moonAccTheta);
    	my $mpVelY = ($moonDeltaLoc*$sine);
    	my $tan = tan($moonAccTheta);
    	my $mpVelX = ($mpVelY/$tan);
    	#moon star
    	my $sine = sin($moonAccTheta2);
    	my $msVelY = ($moonDeltaLoc2*$sine);
    	my $tan = tan($moonAccTheta2);
    	my $msVelX = ($msVelY/$tan);
    	#planet velocity
    	my $sine = sin($planetTheta);
    	my $pVelY = ($planetVel*$sine);
    	my $tan = tan($planetTheta);
    	my $pVelX = ($pVelY/$tan);
    	#moon velocity
    	my $sine = sin($moonTheta);
    	my $mVelY = ($planetVel*$sine);
    	my $tan = tan($moonTheta);
    	my $mVelX = ($mVelY/$tan);
    	#star velocity
    	my $sine = sin($starTheta);
    	my $sVelY = ($starVe*$sine);
    	my $tan = tan($starTheta);
    	my $sVelX = ($sVelY/$tan);
    #add cartesian vectors
    	#planet
    	my $pFinalX = ($psVelX+$pmVelX+$pVelX);
    	my $pFinalY = ($psVelY+$pmVelY+$pVelY);
    	#moon
    	my $mFinalX = ($msVelX+$mpVelX+$mVelX);
    	my $mFinalY = ($msVelY+$mpVelY+$mVelY);
    	#star
    	my $sFinalX = ($spVelX+$smVelX+$sVelX);
    	my $sFinalY = ($spVelY+$smVelY+$sVelY);
    #convert cartesian to polar
    	#planet
    	my $planetTheta = atan($pFinalY/$pFinalX);
    	my $planetVel = sqrt(($pFinalX)**2+($pFinalY)**2);
    	#moon
    	my $moonTheta = atan($mFinalY/$mFinalX);
    	my $moonVel = sqrt(($mFinalX)**2+($mFinalY)**2);
    	#star
    	my $starTheta = atan($sFinalY/$sFinalX);
    	my $starVel = sqrt(($sFinalX)**2+($sFinalY)**2);
    
    #ADD VELOCITIES TO CREATE NEW LOCATION
    #planet
    my $planetLocX = $planetLocX+$pFinalX;
    my $planetLocY = $planetLocY+$pFinalY;
    
    #moon
    my $moonLocX = $moonLocX+$mFinalX;
    my $moonLocY = $moonLocY+$mFinalY;
    #star
    my $starLocX = $starLocX+$sFinalX;
    my $starLocY = $starLocY+$sFinalY;
    
    #WRITE TO FILE
    sysopen(TEXT, $FILEPATH, O_RDWR);
    printf TEXT "$starLocX $starLocY $planetLocX $planetLocY $moonLocX $moonLocY";
    close (TEXT);
    #COUNTER PRINT
    print "Iteration $counter of $iter complete.\n";
    }
    print "\nYour simulation is done!";
    sleep 10;
    Last edited by atomic7732; 04-11-2011 at 04:15 AM. Reason: idk why i didn't put the code

  • #2
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Nevermind, I fixed it but I do have a problem with the variables. [s]Is the self referencing causing them to not change?[/s]

    I tried to make the Location Variables not self reference (starting near line 194), but that ended up not working. The output file prints the same values every single time.
    Last edited by atomic7732; 04-11-2011 at 04:27 AM. Reason: update

  • #3
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by atomic7732 View Post
    Nevermind, I fixed it but I do have a problem with the variables. [s]Is the self referencing causing them to not change?[/s]

    I tried to make the Location Variables not self reference (starting near line 194), but that ended up not working. The output file prints the same values every single time.
    the place of 'my $counter = 0;' isn't outside of the for loop?
    Code:
    #calculate
    for($i = $iter; $i > 0; $i--)
    {
    #COUNTER
    my $counter = 0;
    $counter++;
    best regards

  • #4
    New to the CF scene
    Join Date
    Apr 2011
    Posts
    3
    Thanks
    0
    Thanked 0 Times in 0 Posts
    That makes sense, but I don't think this means the output file will calculate all the other stuff... It kept printing the same location for the planet, star, and moon.

  • #5
    Master Coder
    Join Date
    Dec 2007
    Posts
    6,682
    Thanks
    436
    Thanked 890 Times in 879 Posts
    Quote Originally Posted by atomic7732 View Post
    That makes sense, but I don't think this means the output file will calculate all the other stuff... It kept printing the same location for the planet, star, and moon.
    there is no connection between $counter and the rest of the script, is just a counter, useless in my opinion, since will always be 1 if you live the declaration and initialization with 0 inside the loop. I don't understand some things for exmple what's wrong with open, why do you use sysopen, why do you sleep at the end of the script?
    is a good idea to insert use strict and warning pragmas at the begining of the script.
    Can you provide some input data for tests and output of what you expect?

    Edit: i get the script from your first post, correct the shbang, missing a '/' from '#!/usr/bin/perl', probably when you post. After i add both strict and warnings i check it using:
    Code:
    perl -c planet.pl
    and give me this:
    Code:
    "my" variable $planetLocX masks earlier declaration in same scope at planet.pl line 60.
    "my" variable $planetLocY masks earlier declaration in same scope at planet.pl line 61.
    "my" variable $moonLocX masks earlier declaration in same scope at planet.pl line 62.
    "my" variable $moonLocY masks earlier declaration in same scope at planet.pl line 63.
    "my" variable $theta masks earlier declaration in same scope at planet.pl line 100.
    "my" variable $sine masks earlier declaration in same scope at planet.pl line 101.
    "my" variable $distance masks earlier declaration in same scope at planet.pl line 102.
    "my" variable $F masks earlier declaration in same scope at planet.pl line 103.
    "my" variable $theta masks earlier declaration in same scope at planet.pl line 114.
    "my" variable $sine masks earlier declaration in same scope at planet.pl line 115.
    "my" variable $distance masks earlier declaration in same scope at planet.pl line 116.
    "my" variable $F masks earlier declaration in same scope at planet.pl line 117.
    "my" variable $sine masks earlier declaration in same scope at planet.pl line 129.
    "my" variable $sine masks earlier declaration in same scope at planet.pl line 134.
    "my" variable $tan masks earlier declaration in same scope at planet.pl line 136.
    "my" variable $sine masks earlier declaration in same scope at planet.pl line 139.
    "my" variable $tan masks earlier declaration in same scope at planet.pl line 141.
    "my" variable $sine masks earlier declaration in same scope at planet.pl line 144.
    "my" variable $tan masks earlier declaration in same scope at planet.pl line 146.
    "my" variable $sine masks earlier declaration in same scope at planet.pl line 149.
    "my" variable $tan masks earlier declaration in same scope at planet.pl line 151.
    "my" variable $sine masks earlier declaration in same scope at planet.pl line 154.
    "my" variable $tan masks earlier declaration in same scope at planet.pl line 156.
    "my" variable $sine masks earlier declaration in same scope at planet.pl line 159.
    "my" variable $tan masks earlier declaration in same scope at planet.pl line 161.
    "my" variable $sine masks earlier declaration in same scope at planet.pl line 164.
    "my" variable $tan masks earlier declaration in same scope at planet.pl line 166.
    "my" variable $sine masks earlier declaration in same scope at planet.pl line 169.
    "my" variable $tan masks earlier declaration in same scope at planet.pl line 171.
    Global symbol "$i" requires explicit package name at planet.pl line 74.
    Global symbol "$i" requires explicit package name at planet.pl line 74.
    Global symbol "$i" requires explicit package name at planet.pl line 74.
    Global symbol "$moonAcceleration" requires explicit package name at planet.pl line 106.
    Global symbol "$planetAccTheta2" requires explicit package name at planet.pl line 107.
    Global symbol "$moonAccTheta" requires explicit package name at planet.pl line 108.
    Global symbol "$planetDeltaLoc2" requires explicit package name at planet.pl line 109.
    Global symbol "$moonDeltaLoc" requires explicit package name at planet.pl line 110.
    Global symbol "$moonAcceleration" requires explicit package name at planet.pl line 110.
    Global symbol "$moonAcceleration" requires explicit package name at planet.pl line 120.
    Global symbol "$starAccTheta2" requires explicit package name at planet.pl line 121.
    Global symbol "$moonAccTheta2" requires explicit package name at planet.pl line 122.
    Global symbol "$moonDeltaLoc2" requires explicit package name at planet.pl line 123.
    Global symbol "$moonAcceleration" requires explicit package name at planet.pl line 123.
    Global symbol "$starDeltaLoc2" requires explicit package name at planet.pl line 124.
    Global symbol "$planetAccTheta2" requires explicit package name at planet.pl line 134.
    Global symbol "$planetDeltaLoc2" requires explicit package name at planet.pl line 135.
    Global symbol "$planetAccTheta2" requires explicit package name at planet.pl line 136.
    Global symbol "$starAccTheta2" requires explicit package name at planet.pl line 144.
    Global symbol "$starDeltaLoc2" requires explicit package name at planet.pl line 145.
    Global symbol "$starAccTheta2" requires explicit package name at planet.pl line 146.
    Global symbol "$moonAccTheta" requires explicit package name at planet.pl line 149.
    Global symbol "$moonDeltaLoc" requires explicit package name at planet.pl line 150.
    Global symbol "$moonAccTheta" requires explicit package name at planet.pl line 151.
    Global symbol "$moonAccTheta2" requires explicit package name at planet.pl line 154.
    Global symbol "$moonDeltaLoc2" requires explicit package name at planet.pl line 155.
    Global symbol "$moonAccTheta2" requires explicit package name at planet.pl line 156.
    Global symbol "$starVe" requires explicit package name at planet.pl line 170.
    planet.pl had compilation errors.


    best regards
    Last edited by oesxyl; 04-11-2011 at 05:06 PM.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •