Silverfrost Forums

Welcome to our forums

A basic question

1 Dec 2012 2:31 #11224

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,102460 ! total number can be expressed as basic calculations
time = REAL(n)dt/(24.03600.0) ! exact time in days time2 = time2+dt/(24.03600.0) ! time calculated from time increments END DO WRITE(6,)'Time error is ', (time-time2)24.03600.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.03600.0 is just the number 114.862? In the finish of the DO, the error is (tempo-tempo2)24.03600.0 .. WHY?

Someone's patient can help me please?? :?:

3 Dec 2012 6:23 #11227

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.

5 Dec 2012 2:20 #11239

Dude.. thks a lot for read all my topic.

You solved my question n now I'm understanding better. 😃

Please login to reply.