Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

New to FORTRAN, needs help!

Status
Not open for further replies.

Stratos13

Programmer
Oct 2, 2012
9
CA
Hello everybody,

So for our universities 4th year capstone project I was that unlucky guy to be given the task of creating design software for a compressor.. I have experience with C, visual basic, Java, and MATLAB.. however my prof wants me to code it in FORTRAN -.-

I am looking for some guidance as to what version of FORTRAN I should work with (while maintaining these requirements)

- It must be FORTRAN 90 or newer because I will have to use NIST REFPROP (only compatible with FORTRAN 90 or newer)
- I am running windows 7 (will consider changing if it is strongly advised)
- Easy to read and use environment with a debugger is needed

Also keep in mind I am a mechanical engineer, so I don't know a whole lot about programming, just the basics. So if anyone could explain how FORTRAN can be used with visual studio and what the disadvantages of doing this are that would be great.. or even if you don't have the time to explain just post useful references (books, websites, etc) and i'll get reading.

Any help is appreciated,
Thanks
 
Forgot to mention I am running a 64-bit operating system
 
Unlucky? You are luckier than you think...you have been asked to program in Fortran, which is easier than all those other languages that you already know! Well, maybe not easier than matlab (don't remember matlab anymore), but certainly easier and safer than C and Java...and it handles arrays a-la-matlab.

I personally do not use IDEs or debuggers; I simply use g95 from the command line and type my programs in, say, Notepad++.

I think it is possible to integrate some fortran into visual studio, but I don't even use VS.

There are other IDEs like CodeBlocks or something like that.

There is also Simply Fortran which a learned about not long ago; they simply provide some open source stuff, nicely packages, ready for installation and added their own IDE...for a reasonable $25 bucks.

Anyway, there are a few options.

 
Having a degree in mechanical engineering I support Salgerman's view that you are the lucky guy to be allowed to program in Fortran. This much more suits the way of thinking of a mechanical angineer, than any other of the fancy languages of today.

I would give the Salford compiler a try which has an IDE, that works fine - but is not visual studio. But they offer a free of charge personal edition for students and non profit applications. The only drawback I see is, that doing a Windows application by the quickwin package is somewhat tricky. Try this website:
My favourite book on Fortran is 'Fortran 90/95 for Scientists and Engineers' by Stephen J. Chapman. There are editions for newer versions of Fortran, check on amazon. This book starts and explains Forttran quite nicely starting from scratch and slowly building up. Most of the examples are taken from physics or engineering. This might be just what you need.

Norbert


The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Hey, thanks for the help guys. It's going good so far, and FORTRAN is actually a pretty neat language to know.

One question I have is: What is the difference between INTENT(IN) vs. PARAMETER. They seem to do the exact same thing to me..
 
That are differen things
PARAMETER statement/attribute is used for declaration of an constant, for example:
Code:
integer, parameter :: N = 100 ! array dimension
real, parameter :: pi = 3.14159
INTENT statement/attribute have to do with declaration of function/subroutine arguments.
By default all arguments are passed by reference, which can cause side effects. So the default method is INTENT(IN OUT). To avoid unwanted side effects INTENT(IN) is used for input data and INTENT(OUT) for output data. It's something like call by value and call by reference.
Example:
Code:
...
write(*,*) 'Input = ', my_in
call my_sub(my_in, my_out)
write(*,*) 'Result = ', my_out
...
...
subroutine my_sub(my_in, my_out)
  real, intent(in) :: my_in
  real, intent(out) :: my_out
  ...
  my_out = ... ! do something
end subroutine my_sub
 
Since you know C, I would like to offer another explanation/point-of-view to PARAMETER in addition to mikron's.

I have not done C in years, but if I remember correctly, C has these "#define" statements that one can use to define constants or even short macros; from what I remember, these #s are pre-process before compilation and what the preprocessor does is to do a literal substitution, for example:

#define MY_PI 3.1415926

Now, wherever "MY_PI" appears in the code, it will be replaced, literally, with "3.1415926".

That's how you could think of about Fortran PARAMETERS.

So, a PARAMETER is a constant throughout the entire program; an INTENT(IN) variable is a constant only temporarily inside the corresponding procedure.

 
Sorry to be a little bit contradictive:

PARAMETER should be used as an attribute, the statement is kept only for backward compatibility. Use is just like mikrom said:

Integer, parameter :: Pi = 3.14159

This is defined in the programming unit only that includes the statement. Ususally you would put all your paranmeters in one module and use this in any subroutine / function that uses any of the parameters. Basic thing: Parameters are a fixed value that cannot be modified by the program. Any violation will cause an error.

intent(in) or intent(out)
is not really needed - most of the time. Your compiler will give you an errormessage, if a variable with an intent(in) attribute appears to the left of an equal sign, and if intent(out) is specified if it is not.

For a single programming unit it is in fact the same if you specify intent(in) or parameter. But you use them differently in somewhat advanced programming techniques:

You use parameter as attribute on values that you never want to change within your program. Like Pi, e, other values. You specify their value at programming time once and for all never to be modified again. Most of the time I use them for flagging certain information. For instance:

Code:
module general
    integer, parameter :: iEnglish = 30040
    integer, parameter :: iGerman = 30041
    integer, parameter :: iFrench = 30042
    integer, parameter :: iSpanish = 30043
end module

Somewhere in my code the user selects his language. When further on I have to check on the language it is much easier to memorize and debug if I have a statement

if (iLang .eq. iEnglish) then .....

instead of

if (iLang .eq. 30040) then .....

Both statements are absolutely identical in effect for the compiler substitutes iEnglish by 30040 at compilation time, but legibility of the first one is far better.


The use of intent (in) or intent (out) is to make your coding more safe. Besides you need it for some explicit interfacing e.g. for keyword arguments, but this is nothing for the beginner. Imagine you call a routine within a do-loop. You would want to make sure that the subroutine does not modify your loop counter, but still may need it as an argument. If you specify it intent(in) in your subroutine then you can be sure that it will not be modified within this sub (and cause very hard to find bugs in the calling routine). You may achieve the same by careful programming, but just in case....

Hope that clarifies it a bit.
Norbert





The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Thanks, Norbert...and I did not see much contradiction; except that you caught me...I was indeed thinking of the global statement PARAMETER and not the attribute style of defining a parameter, possibly, inside a procedure. (still a bit stuck on f77)

You seem to confirm that a parameter is replaced with its value at compilation time, like I said it happens in C. Does that mean that if you look at the object file, you wouldn't find the parameter label at all? I was not sure of this in Fortran; I know in C is truly a literal substitution to the point that you can do most anything, not just a single number, but parenthesis, short macros, etc.

Cheers

Germán
 
Germán,

not so sure about that one. I remember reading - without being able to locate my source right now - that when the compiler encounters the named parameter then the value is included. The Compacq docs have it that 'For compilation purposes, writing the name is the same as writing the value'. If the constant's name is replaced in the objectfile allready or if there is any other option, I have no idea.

As far as f90/95 is concerned - I do not know about newer versions - the parameter attribute can only be applied to a single number or character string, maybe an array, but not to a macro. The docs do not prohibit arrays but I never tried it. For me it would be contradictory to use arrays, for I introduce them for better mnemonics. So iLang(1) = 30040
iLang(2) = 30041
Is not too big an advantage to using the numbers themselves.

Norbert




The optimist believes we live in the best of all possible worlds - the pessimist fears this might be true.
 
Awesome, it makes alot more sense now. Thanks everyone! Time to start coding :p
 
Awesome, it makes alot more sense now. Thanks everyone! Time to start coding :p
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top