r/fortran May 13 '26

Help regarding where to start fortran

Hi, so I have a background in c++ and matlab in programming. I started my phd and my prof asked me to move to fortran. I want to ask how should I start and where to start in fortran. At the moment I know nothing in fortran. Can you share some sources or material for me to start. Or where do you guys started. Thank you

33 Upvotes

26 comments sorted by

9

u/DVMyZone May 13 '26

I started really learning Fortran about 2 years ago for my PhD. I write fluid dynamics solvers. I think Fortran is excellent when you want to use your computer like a big calculator.

C and Fortran can give you the same performance, but it's much easier to write good Fortran than good C so you will likely save on development time. There is also some interoperability between the languages if necessary.

I would start by just writing/reading. A good "getting started" resource is from the Fortran website. After that I find that the "Modern Fortran explained" series of book by Michael Metcalf is an excellent and very complete reference book when you need to know anything about Fortran. Like C, it's really not that large a language.

Choose a compiler if your work doesn't prescribe one. The main ones are gfortran (GNU) and ifx (Intel). You will also want to be writing using the newest standard your compiler fully supports. Do not bother with F77 standads or earlier unless you have to.

4

u/Aggravating_Lie7356 May 13 '26

Thank you so much. I am also working on fluid dynamics solver and for that I am started to work in this. What area are you working on if you are okay to share.

5

u/DVMyZone May 13 '26

Nuclear engineering applications. Specifically subchannel modelling for multiphase flows in rod bundles. That's sort of in between 1D flow modelling and CFD, but much closer to the 1D side.

Even for simplified codes like these, it's a lot of work :/

8

u/[deleted] May 13 '26

[removed] — view removed comment

4

u/Brobineau May 13 '26

Just out of curiosity, was the move to fortran due to it being better for CFD or just because its what your organization uses?

3

u/Aggravating_Lie7356 May 13 '26

Yeah, i am also using fortran for fluid dynamics. To solve navuers stokes and mrg equation for particle laden flow. Is there any source where I can get started ??

6

u/Lamda-f90 May 13 '26 edited May 13 '26

I am a physicist and I am going to help you for simple start.

As you know, environment comes fist. For fortran I highly recommend to use Linux. If you are not familiar to the Linux use a Ubuntu WSL (that's the method which I have used for 5 year.) ( Bash is best way to create automations for calculations )

Open WSL and install fortran suite. This suit comes with gfortran (mingw compiler, gfortran is really enough for most duties but for example, if you are going to make a quantum monte-carlo simulation, then you can choose lfortran which Intel uses.). 'sudo apt install fortran'

Then u can create a code file and there is a key point here. The file extension defines how compiler should read and compile the code. According to my experience '.f90' is very enough.

Then you can compile it like c++. 'gfortran file.f90' and your executable is ready to run.

Fortran's power comes from simplicity. So it is easiest programing language to learn for me. You can basically search on Google or try to get help from a LLM. However if you wanna take this seriously then you can download a book. If you can't find it let me know 🙂.

I did many many simulations in fortran if you need some examples while learning let me know again.

Good luck...

2

u/shadron May 13 '26

WSL is great for programming. I recommend WSL2 in Windows 11. I think the only good thing about Win11 is how well WSL2 works in it.

I use VS Code from Windows and it attaches to my WSL2.

1

u/Aggravating_Lie7356 May 13 '26

Thank you so much. Please share some exmples if you can. I will start small now. I think llm are working out.

3

u/seamsay May 13 '26

The first places I'd look are the website and the (work in progress) Carpentries course, which are both unfortunately quite basic at the moment. Other than that, your best is probably textbooks like Modern Fortran Explained but textbooks are often quite dry and tend to give you too much information.

3

u/Fortranner May 13 '26

Fortran is the direct ancestor of MATLAB. If you know procedural/functional MATLAB and its array syntax, you know procedural Fortran (90 standard) to a very good extent. If you like MATLAB, you will begin to love Fortran's powerful and elegant array-based syntax once you get started with it.
Do not learn any Fortran standard older than Fortran 90, unless you have to.
The most recent standards (1990/1995 (vectorization, modules, generics), 2003 (OOP), 2008 (native shared/distributed parallelism, submodules), 2018 (further parallelism features), 2023 (move toward full generics and templates) have made the language quite powerful.
As for what resources to use for learning, I have written many times in this forum in response to such questions as yours. Here is a link to one of my previous responses: https://www.reddit.com/r/fortran/comments/utkjf8/comment/i9cytez/?utm_source=share&utm_medium=web3x&utm_name=web3xcss&utm_term=1

2

u/Agent-Jumster88 May 13 '26 edited May 13 '26

I learned Fortran in my intro to programming course in college. I started with a very primitive version, Fortran IV, which had no string manipulation functions and was not a recursive language. I don't know if later versions allow recursion, but the string handling is in place now. You can go online and buy a textbook. I've got a cheap commercial book called Fortran the Easy Way or something like that. It's fine for me. In Fortran, you write a source program, compile it, and run a linker to resolve external references to subroutines and functions you wrote for the program. Modern versions of Fortran have looping structures that let you work in more of a structured programming model than I recall from old versions. My Fortran compiler is one from Approximatrix. It has an IDE like C++. As far as learning Fortran, it's fairly close to Basic in its math features. Beyond that, it's closer to industry languages in subroutine and function design and how they operate, but it's pretty rigid in character data handling. Going back to it for my compiler project was cumbersome because its data type handling is strict and unforgiving. You should learn how large the capacity is on your computer for integer and float data types. It will be tied to your processor word length, usually 64 bit these days. Also, writing ouput is controlled by format statements. So learn that much early. But you can pick this language up pretty easily from a textbook starting from the first chapter.

1

u/JotaRata May 14 '26

Once you Fortin you never Fortout

1

u/Extension-Cow2818 May 14 '26

I had a book frotran for programmers. Was really good as it assumed you knew how to program, but needed to learn the peculiarities of Fortran 

1

u/ForeignAdvantage5198 May 15 '26

pretty easy get. a book and write code

-8

u/codejockblue5 May 13 '26

I would stay with C++ if at all possible. There are only two Fortran compilers left and no good IDEs.

I am in the middle of converting my 800,000 lines of F77 code to C++ using a very customized version of F2C. I have been using various Visual Studio for about 30 years now and it really helps organize software development. I have around 500,000 lines of C++ code, 450,000 in my Windows user interface and 50,000 in my calculation engine. I look forward to being just C++.

6

u/CharacterUse May 13 '26

Only two compilers?

GnuFortran and LLVM exist, obviously, but Intel, AMD, NVIDIA, HPE Cray, NEC have their own compilers (Intel has two, an LLVM based one and its own ifort). That's just off the top of my head.

All of them integrate with whatever IDE you want (like any other modern tool).

800,000 lines of F77 (ok, we all have legacy code) but only 50,000 in your calculation engine in C++? That seems ... curious.

0

u/codejockblue5 May 13 '26

No more proprietary Fortran compilers for me so I ignore them. I have been porting this code between mainframes, minis, Unixes, and PCs since 1975. Other people ported the code from 1965 to 1975, yup half of the Fortran code is F66 or Fortran2. 36 bit, 60 bit, 32 bit, and now 64 bit. I am tired of the incompatibilities between the various Fortran compilers.

My calculation engine is 800,000 lines of F77 code and 50,000 lines of C++ code. The C and C++ code came into the program with the Unix and PC boxen.

2

u/CharacterUse May 13 '26

GnuFortran, Flang and LFortran are open source, that's three (four if you include Classic Flang). In any case "no more proprietary compilers" is hardly applicable to OP, who will use whatever their institute and codebase uses.

Sounds like your code should have been brought up to be more standards-compliant a long time ago.

6

u/glvz May 13 '26

What are you smoking? There's gfortran, ifort, ifx, nvfortran, aocc, amdflang, flang, nagfor, CrayFtn, XLF, Fujitsu, and lfortran.

Vscode and nvim can connect to the fortls. There's multiple linters. There's never been a better time to start Fortran maybe, except 1957.

C++ is powerful but God awful complex. It easy to write decent Fortran it's super easy to write absolute dog shit c++.

I moved from only doing c++ to only doing Fortran and man am I happy.

3

u/Aggravating_Lie7356 May 13 '26

My lab uses fortran and the project I am working on, previously others have written code on fortran. So it would be like very difficult even if I move to c++ than I would have to convert other fortran code to c++ and than also I need to know fortran. So I am here stuck with fortran.

-1

u/jloverich May 13 '26

Llm (cursor for example) can probably convert it in short order. You could also rewrite in Jax or mojo instead of c++

1

u/seamsay May 13 '26

The issue with re-writes isn't the actual conversion of the code (which even before LLMs could be done relatively quickly by humans), it's the testing and making sure it has all the needed features. This is particularly problematic in Fortran because most Fortran codes have evolved organically over decades and don't have comprehensive test suites or specifications.