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

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

Register Blogs Community New Posts Updated Threads Search

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
Old   June 24, 2006, 22: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, 10: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, 15: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, 16: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


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 Off
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
[Gmsh] Problem with Gmsh nishant_hull OpenFOAM Meshing & Mesh Conversion 23 August 5, 2015 03:09
[CAD formats] 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 16:57
which is a better way to loop in fortran? zonexo Main CFD Forum 7 June 23, 2006 05:47
Big loop problem of fortran Wen Long Main CFD Forum 9 April 1, 2004 13:38


All times are GMT -4. The time now is 09:51.