 |
forums.silverfrost.com Welcome to the Silverfrost forums
|
View previous topic :: View next topic |
Author |
Message |
iury
Joined: 30 Nov 2012 Posts: 5 Location: Brazil
|
Posted: Sat Dec 01, 2012 3:31 pm Post subject: A basic question |
|
|
Ok, I'm new on programming world. Be patient please.
I'm reading introduction to FORTRAN95 by this website: http://www.oceanmodelling.com/Tutorials/FORTRAN_1_3.html
So, on the DO LOOPS examples they shown this:
PROGRAM Do_Loop_Examples
IMPLICIT NONE
! Declaration section
INTEGER, PARAMETER :: ntot = 100 ! total number of counts
INTEGER, PARAMETER :: nx = 100 ! channel dimension
REAL :: h(nx) ! declaration of a channel with 100 elements
REAL :: dt ! time step in seconds
REAL :: time, time2 ! time counters
INTEGER :: n, k ! integer counters
! Assignment of parameter values
dt = 60.0 ! time step is set to 60 seconds
!=========
! Example 1: A simple time counter
WRITE(6,*) "EXAMPLE 1: A SIMPLE TIME COUNTER"
PAUSE
DO n = 1, ntot ! change n from 1 to ntot at steps of 1
! DO n = ntot, 1, -1 ! this would count backwards from ntot to 1 at steps of -1
! DO n = 1, ntot, 2 ! this would count at steps of 2 => n = 1,3,5,6,etc
time = REAL(n)*dt ! time in seconds
WRITE(6,*) time," secs; ", time/60.0," min; ", time/3600.0," hrs"
! PAUSE ! enable this if you want to see each step
END DO ! this is the end reference of the DO loop
!=========
! Example 2: Illustration of round-off errors
WRITE(6,*) "EXAMPLE 2: ILLUSTRATION OF ROUND_OFF ERRORS"
PAUSE
time = 0.0
time2 = 0.0
DO n = 1,10*24*60 ! total number can be expressed as basic calculations
time = REAL(n)*dt/(24.0*3600.0) ! exact time in days
time2 = time2+dt/(24.0*3600.0) ! time calculated from time increments
END DO
WRITE(6,*)"Time error is ", (time-time2)*24.0*3600.0," seconds"
!=========
! Example 3: Configuration of a channel with depths decreasing from 200 metres to
! 100 metres over the channel length
WRITE(6,*) "EXAMPLE 3: ASSIGMENT OF CHANNEL DEPTHS"
PAUSE
DO k = 1, nx
h(k) = 200.0-REAL(k-1)/REAL(nx-1)*100.0
END DO
WRITE(6,*) (h(k),k=1,nx,10) ! simplified form of DO loop for WRITE statements
END PROGRAM Do_Loop_Examples
I have some questions:
In the EXAMPLE 2:
Why he define time and time 2 equal to 0.0?
I dont understood the time2 on the "DO". time2 = time2+dt/(24.0*3600.0) ! time calculated from time increments
What increments? "time" is a matrix calculated by the loop in "n", but time2 have no loop, why (tempo-tempo2)*24.0*3600.0 is just the number 114.862? In the finish of the DO, the error is (tempo-tempo2)*24.0*3600.0 .. WHY?
Someone's patient can help me please??  |
|
Back to top |
|
 |
DanRRight
Joined: 10 Mar 2008 Posts: 2937 Location: South Pole, Antarctica
|
Posted: Mon Dec 03, 2012 7:23 am Post subject: |
|
|
I know that the first step is the most difficult, then all is simple.
Your question probably comes from the one of confusions of Fortran which contradict the common sense of learning it novices but after some time you feel that it's ... OK and then you will even like it. This specific confusion comes both from the ancient history and from the necessity of Fortran to be the absolute king in calculation speed. When we write
time2 = time2 + dt (which looks like a nonsense)
we mean
new_time2 = old_time2 + dt
old_time2 = new_time2
That saves you CPU time and space: no new variable is needed and we save on one assignment. Today Fortran compilers probably will optimize that by themselves. Now you understand why dt is an increment
Now, suppose you are calculating time from some initial time0. You can do that by adding whole hours as increments or adding with more calculation work second by second every step introducing rounding errors so two results will be different. That was general idea, try to adapt it to your case. |
|
Back to top |
|
 |
iury
Joined: 30 Nov 2012 Posts: 5 Location: Brazil
|
Posted: Wed Dec 05, 2012 3:20 am Post subject: |
|
|
Dude.. thks a lot for read all my topic.
You solved my question n now I'm understanding better.  |
|
Back to top |
|
 |
|
|
You cannot post new topics in this forum You cannot reply to topics in this forum You cannot edit your posts in this forum You cannot delete your posts in this forum You cannot vote in polls in this forum
|
Powered by phpBB © 2001, 2005 phpBB Group
|