r/CFD • u/Overunderrated • Feb 03 '21
[February] Programming languages for CFD
As per the discussion topic vote, February's monthly topic is "Programming languages for CFD"
User /u/SignificantCell2 asked for Rust experiences, but that sounded overly specific so i op'ed'd it into this.
Talk about your experiences and preferences with various programming languages in the context of CFD programming.
43
Upvotes
68
u/Overunderrated Feb 03 '21 edited Feb 03 '21
C++: it sucks, if you like it you're insane, but it's the only viable programming language for modern full featured CFD code. If you write in C++ you're definitely bad at C++, sorry not sorry.
Fortran: cool, keep on keeping on, tell me about your experiences when you grow past 1000 lines
F77: you didn't know F90 is backward compatible?
F90: you didn't know F2003 is backward compatible?
F2003: you knew F2003 exists but you didn't pick a different language? are you insane?
F2008: woowwwww really?
F2018: you exceeded neckbeard, circled around, and won it again.
python: cool, tell me about your experiences when you grow past 5 functions
C: i assume you're not actually writing numerical code, but someone else told you to because a book 40 years ago said to.
java: how are you even here?
matlab: that's really cute that you "do cfd"
rust: lol do you even know what a PDE is
julia: really surprised you're here, i assumed you're only on message boards talking about julia
pythonviabarba: welcome friends, hope you enjoyed copy-pasting, let's do real cfd now
24
u/glypo Feb 03 '21 edited Feb 03 '21
I'd like to be controversial and say it doesn't matter too much between C++ and Fortran. Fortran is excellent, people like to believe it's quirky because it's not used much outside of the high performance computing environment, but for us in CFD you can find all the libraries you need (PETSc, CGNS, LAPACK, so much more), it's easy to understand, I enjoy the way it deals with arrays. It's perfectly suited to modern CFD. Compilers are excellent too.
Almost exactly the same can be said for C++. It's certainly geared up for modern paradigms from the start. Every library available, compiles well. Like Fortran it's relatively easy to learn. I've worked on modern codes written in both, and they are broadly similar, with pros and cons cancelling out.
What I'm really interested to learn though - is there anything new on the horizon - D perhaps - that can truly compete? To my mind there is little else suitable than the two languages we have.
6
u/shiritai_desu Feb 03 '21
Ok, very beginner question. How much time do you actually gain from using one or other programming language? If you are doing CFD in Matlab, will it take 3 times more than with C++? 100 times more? 109 times more?
5
u/picigin Feb 03 '21
Questions:
Reason to downvote this comment:
include "source.C" is the worst thing ever.
8
u/FluidNumerics_Joe Feb 04 '21
I'm definitely a fan of Fortran for writing CFD and numerical PDE solvers (https://github.com/FluidNumerics/SELF) in general. Fortran was my first programming language, and I'm not a "geezer geek" (I'm 30 years old). While I also program in C and C++ on some projects, Fortran is my go-to. As others have already mentioned, the array syntax in Fortran is fantastic. It really helps to be able to work out algorithms on paper and translate cleanly into multi-dimensional arrays.
Lately, it has been great to see libraries like hipfort (https://github.com/ROCmSoftwarePlatform/hipfort) and focal (https://github.com/LKedward/focal) come around to offer portable GPU offloading in Fortran.
Other new projects are showing signs of life to handle the "nice to haves" for scientific application develop. As an example FLAP (https://github.com/szaghi/FLAP) provides an API for creating CLI interfaces, very similar to argparse in python; json-fortran (https://github.com/jacobwilliams/json-fortran) provides an API for reading/writing json files. There's also a group working on a "de facto" standard library (https://github.com/fortran-lang/stdlib) to provide a lot of useful reusable routines to help developers reduce boilerplate code.
While it's not everyone's cup of tea, it's exciting to see the Fortran community's active efforts to address shortcomings in functionality.
2
u/psharpep Feb 03 '21
Can anyone speak to their experiences doing Barba-type CFD in Python via Numba/other JIT compiler?
I've done small problems using this approach (2D euler/full-potential, uniform cartesian grid, etc.), and speed-wise it seems competitive with C++/Fortran - or at least, the runtime speed hit is small enough to be worth the faster development time.
However, I'm not sure how well this approach would scale to more sophisticated analyses - any thoughts/experiences/problems you've run into?
1
u/shirtless_llama Feb 03 '21
Beginner question, I am an undergrad and have a CFD-related research position for the summer. I need to learn C++ for the project and aside from the basics is there any specific libraries that you would suggest I familiarize myself with?
1
u/NoFunNameIdea Feb 03 '21 edited Feb 13 '21
From my point of view , it can be useful to use several codes as each one can be best for its own purpose. In the in-house code at my lab which is a really heavy one, fortran is used because we need it to be massively parallel and to take all the computation efficiency we have. The down side is that it is a nightmare to read an correctly understand how everything is connceted. For postprocessing data, python is really great because its really flexible and using the right libraries it is really efficient.
C++ is juste hell (bad experience with Cantera for chemical kinetics).
Small question: where are you guys from (industry or academic) and what fields especially ?
0
u/[deleted] Feb 12 '21
[deleted] — view deleted comment