CFD Online Logo CFD Online URL
www.cfd-online.com
[Sponsors]
Home > Forums > Main CFD Forum

Re: which is a better way to loop in fortran?

Register Blogs Members List Search Today's Posts Mark Forums Read

Reply
 
LinkBack Thread Tools Display Modes
Old   June 24, 2006, 21:56
Default Re: which is a better way to loop in fortran?
  #1
zonexo
Guest
 
Posts: n/a
Tks for your suggestion Tom, but what if i need to multiply by 2 for some values of i, but 10 for some other values. it's something like this:

integer multi(10) for i=1,2,7,9, multi(i)=1

do j=1,10 do i=1,10 if (multi(i)=1) then

a(i,j)=2*i*j else

a(i,j)=10*i*j end do end do

of cos, values of i/j are very small. but in reality, i,j can be 100 or 1000. in this case, will changing the innner loop to a(i,j)=(1-multi(i))*10*i*j + multi(i)*2*i*j be better?

i can't think of a better way.

for others a(i,j)=10*i*j. Normally, i would use a logical variable multi(10), where multi(1)=.true. for the 1st case, .false. for 2nd case.

Here's just an example, but in
  Reply With Quote

Old   June 25, 2006, 09:40
Default Re: which is a better way to loop in fortran?
  #2
HelpfulSoul
Guest
 
Posts: n/a
I would have thought that holding the coefficients 2 and 10 in the multi() array and simply using

DO i=1,10 DO j=1,10

a(i,j)=multi(i)*i*j END DO END DO

would be better as it avoids the branch statements of your first bit if code and avoids the extra arithmetic of your second bit of code.
  Reply With Quote

Old   June 25, 2006, 14:12
Default Re: which is a better way to loop in fortran?
  #3
Adrin Gharakhani
Guest
 
Posts: n/a
Now that you've gone this far in simplifying you can go one step further and actually assign multi(i) = c*i, where c is your conditional constant (2, 10, etc.). This is an O(N) process. Now you can write the code as

Do j=1,10; Do i=1,10; a(i,j)=multi(i)*j; enddo; enddo

Adrin Gharakhani
  Reply With Quote

Old   June 25, 2006, 15:19
Default Re: which is a better way to loop in fortran?
  #4
HelpfulSoul
Guest
 
Posts: n/a
and the multiplication can be eliminate completely with

a(:,1)=multi(

DO j=2,10 a(:,j)=a(:,j-1)+multi( END DO
  Reply With Quote

Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Problem with Gmsh nishant_hull Open Source Meshers: Gmsh, Netgen, CGNS, ... 18 April 22, 2015 08:43
my stl surface is seen as just a line rcastilla OpenFOAM Meshing & Mesh Conversion 2 January 6, 2010 02:30
NACA0012 geometry/design software needed Franny Main CFD Forum 13 July 7, 2007 15:57
which is a better way to loop in fortran? zonexo Main CFD Forum 7 June 23, 2006 04:47
Big loop problem of fortran Wen Long Main CFD Forum 9 April 1, 2004 12:38


All times are GMT -4. The time now is 11:59.