11
$\begingroup$

I am a mechanical engineer, intermediated/advanced level in MATLAB and MATHEMATICA, and beginner in Python. I intend to get a PhD in aeroelasticity (FEM + CFD) and coding my own program.

I intend to do that because I really like to code and it is a skill that I would want to have. Due to large mesh, these computational method are huge time consuming, so I know that I will need a more efficient language to code.

I already code some basic programs for FEM and CFD, in both MATLAB and MATHEMATICA. A curious event it was when I was a undergraduate student. My advisor coded a program in Fortran. Due to my inexperience and short time, I translated the Fortran code to MATLAB. My advisor's code took ~5 s to run, while my MATLAB code took ~5 min. Probably my code could be more efficient, but I think that would be very difficult to get the same time editing my code.

Based on this, what and why languages do you recommend to me?

  1. C++
  2. Fortran
  3. Python

Thank you all in advance for all the advices.

$\endgroup$
  • $\begingroup$ In my own work, which is CFD, FEM, I use all of fortran/c/c++/python. I do most of the intensive computations in fortran/c/c++ and python for smaller work. I think it is good to know all of them. For FEM, I would recommend going with C++ and a library like deal.II $\endgroup$ – cfdlab May 1 at 3:27
  • $\begingroup$ How much do you expect to leverage the ecosystem? Some languages may be excellent in the abstract, but have a relatively scarce ecosystem for your specific topic. $\endgroup$ – Matthieu M. May 1 at 13:06
  • 1
    $\begingroup$ FRACTRAN. Can't be beat for conciseness. :-) $\endgroup$ – Carl Witthoft May 1 at 14:30
  • $\begingroup$ Fortran kicked your MATLAB's ass because Fortran gets compiled, and 50 years of compiler improvements have made it very good indeed. If you learned how to connect C-code to MATLAB (MEX files and all), you'd get speed improvements. But like the answers say, go with Julia, R, python . And all those can be tied to compiled C-code stuff as desired. $\endgroup$ – Carl Witthoft May 1 at 14:32
  • 1
    $\begingroup$ Why not keep using MATLAB as you know it already? This option isn't on your list. PhD isn't writing the fastest/best code, but doing research. Nobody cares how sexy is your code or what you used, only about results. That said, if advisor keeps using Fortran to develop new stuff, you have no choice but to pick that (assuming your code builds directly on his advancements - if you use his code as a library, you can obviously use anything). The last thing you want during PhD is to keep porting his code - it will waste a lot of time for no real gain. $\endgroup$ – Zizy Archer May 2 at 22:51
19
$\begingroup$

You should definitely check out Julia. Julia is a programming language which is similar to Python or MATLAB but utilizes a strong type-inference algorithm + JIT in order to optimize code. If types can be fully inferred in a function (which it usually can), then the code compiles in a fully static manner that matches C or Fortran performance. Translating mathematical MATLAB code to Julia usually isn't more than changing a few A(i) to A[i] (you may find the noteworthy differences page or the QuantEcon MATLAB-Python-Julia cheatsheet particularly helpful) and it's not uncommon to see speedups in the range of 1-2 orders of magnitude (if the code time wasn't fully determined by the speed of BLAS kernels of course).

It's a fully featured programming language, with packages for differential equations, machine learning, etc. all easily accessible. A nice site to search for companion tools is JuliaHub. Here's some benchmarks of differential equation solves across languages (Note: I am the author of these benchmarks). And here's a few examples for specialized CFD calculations:

YMMV of course, but it's worked out well for me.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ Hi, @Chris! Thank you for responding me. I'm sure that I'll look up these links. $\endgroup$ – Magela Apr 30 at 17:26
  • $\begingroup$ @FedericoPoloni sorry, the "YMMV of course, but it's worked out well for me." was there for that, but probably not explicit enough. I put a more direct note. Thanks for the comment. $\endgroup$ – Chris Rackauckas Apr 30 at 18:11
  • 4
    $\begingroup$ It is worthwhile to note that while Julia has good FEM packages and wrappers for some more familiar ones, CFD is really a beast in and of itself and I think you should take take into consideration what you would like to achieve. Julia is certainly a good lanuage for developing performant codes but if you really just need to use existing high-performance packages for specialized CFD problems, it may not be your best bet $\endgroup$ – whpowell96 Apr 30 at 23:07
  • $\begingroup$ @whpowell96 thanks for the comment. Indeed, I should've been more explicit about CFD. I added a few examples of high-performance specialized CFD packages written in pure Julia. $\endgroup$ – Chris Rackauckas May 1 at 2:26
  • $\begingroup$ I tried Julia in 2015, and I got an extremely poor impression of it. I immediately ran into a known bug that had gone unfixed for a long time after it was reported: github.com/JuliaLang/julia/issues/1334 . The startup time seemed unacceptable to me except for very specific applications where you don't care. It does have a very helpful user community and excellent documentation. Seems to me that the one really good reason to use julia is if you've already gotten used to matlab, and you want an open-source replacement. $\endgroup$ – Ben Crowell May 2 at 17:13
11
$\begingroup$

Start simple. Learn Python.

I have been paid to write programms for over forty years and I have used all the languages mentioned in other answers (except Julia - I had never heard of it before now). Each language has its strengths, and most have their weaknesses. Like human languages, code is a way of expressing and framing ideas and when you know how to program you can chose the language that helps you express ideas in the way that best suits the problems you are trying to solve. However, while you are still learning, the language you use will frame and constrain the thoughts you are able to express, which constrains the solutions you are able to consider.

Python, of all the languages I have used, has the advantage of always having many ways to achieve a given result and of always having one way that is just more elegant. (They use the term 'Pythonic' solution). It is simple enough and coherent enough to be an ideal language for teaching children to code while also being the language of choice for data analytics and AI and ML. It also has one other advantage I think worth mentioning; Python is a general purpose language. If you develop a solution in Python, it is usually easy enough to translate it into whatever language is required for a specific deployment.

| cite | improve this answer | |
$\endgroup$
  • 5
    $\begingroup$ Can't agree with this advice. Python is relatively easy and has some nice libraries (although I don't know about the questioner's application area), but is not in the same league as the other languages mentioned when it comes to raw performance on computationally intensive problems. $\endgroup$ – pjs Apr 30 at 23:14
  • 9
    $\begingroup$ @pjs a well written python program doing heavy work will spend very little time actually running python, instead calling out to BLAS/CUDA/MKL/etc. And it's a million times more comfortable (at least for me) to call those libraries from python than from fortran or C. $\endgroup$ – mbrig May 1 at 5:44
  • 2
    $\begingroup$ @pjs yes and no. Nothing will touch CUDA on a FGPU, but if you ahve ever tried to write CUDA utilities you will know that it is far, far nicer to use Python to call the appropriate libraries. Small change in your libraries and you can use your same solution to run a TenserFlow analysis on completly different hardware. $\endgroup$ – Paul Smith May 1 at 13:57
  • 1
    $\begingroup$ @pjs, the usual way to do this is with a library such as "numpy". You set the problem up in concise, easy-to-write Python, and let the library (probably written in a mix of C and assembly) do the heavy lifting of solving it. $\endgroup$ – Mark May 2 at 5:47
  • $\begingroup$ So you must very experienced in Python to achieve the performance you'd get having an intermediate level in Fortran or C - still never better and ofter much worse.. I wouldn't suggest Python for CFD unless dealing with simple 2-D problems (potential flow for example). $\endgroup$ – b-fg May 3 at 10:04
11
$\begingroup$

What is it you want to achieve?

If you want to develop methods/algorithms you might prefer a language that is flexible, and that you are familiar with. As you stated in your question, the Fortran code of your professor was hard to grasp for you, so you re-implemented it in MATLAB. This is very natural way of doing method development: first you implement your idea in a language you are familiar with; then in a second step, when all the teething problems have been ironed out, you re-implement your method in a language that is performant.

If you want to solve (computationally large) problems, then you are more or less restricted to the available frameworks. Chances are high that you will be given (a choice of) existing software, which to use/extend for your purposes. This takes the choice of programming language out of your hands.


I am working in the field of computational fluid dynamics (CFD) and discrete element method (DEM). Here, the computational framework determined the programming language, since developing a computational software from scratch and solve problems was out of the question.
Furthermore, solving a particular problem might be hard enough on its own, so why also deal with the trickeries of numerics, CFD and the like if other have done so before me?

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ Hello, @Dohn Joe. Thank you for answering me. If I understood what you mean, first I need to define exactly what I intend to do, and then think about language. So, I intend to study aeroelasticity in aircraft wings. Let's suppose I'll get the pressure field from OpenFOAM (C++), translated it in loads and then analyse the wing structural response from these loads. I'm more biased to structural side, so I intend to implement methods in FEM, the CFD would be to get the pressure field. So, suppossing I'll use code_aster (Fortran) for the FEM. Should I learn both C++ and Fortran? $\endgroup$ – Magela Apr 30 at 14:18
  • $\begingroup$ If you intend to use OpenFOAM for the CFD part, then having a good understanding of C++ is beneficial. I learned very much about OpenFOAM from its source code. $\endgroup$ – Dohn Joe Apr 30 at 15:52
  • 2
    $\begingroup$ @Diego are you sure you aren't putting the cart before the horse? The available software more or less determines which programming language to use. This decision might not be entirely yours: image you join a research group in which people mainly/entirely use the tool A. Would you then want to become the only person to choose tool B? In such a case, you prevent yourself from taking advantage of the accumulated experience of your colleagues. $\endgroup$ – Dohn Joe Apr 30 at 15:55
  • $\begingroup$ Yes, I'm being precipitated. But I'm thinking in a possible of doing PhD by my own. I mean, not having help from my advisor (in the code part, sure). Otherwise, by what I have seen, some PhD positions require knowledge in a specific language. As I don't have a university in mind by now, learn a new language could be a good skill to me. Do you agree? Again, thank you for responding me. $\endgroup$ – Magela Apr 30 at 17:25
  • $\begingroup$ @Magela It's pretty unusual (at least in the USA) for PhD students to strike off independently from their advisor. There's also a good chance that whatever program you get accepted to will have their own programming culture and expectations, with courses to match. $\endgroup$ – pjs Apr 30 at 23:20
7
$\begingroup$

I highly recommend to anyone, regardless of background, learning both

  • low-level, "fast" language (C, Rust, C++, Go)
  • high-level scripting language (Python, MATLAB, Mathematica, R, bash)

As a general statement, lower-level languages far more precisely control hardware and are very efficient, while higher-level languages are easier to write/describe logic in. Further, higher-level languages are great for controlling precise lower-level logic on.

Because of this relationship and the wide variety of solutions to troubles with building and verifying code, along with contributor backgrounds and preferences, projects of any size are unlikely to be purely written in a single language.


To your ends, modern high-level languages will have high-quality math and simulation libraries, which are written in a lower-level language (often C).

Understanding the way the lower-level library code works will help resolve problems experienced using the library in the higher-level language.

Examples from famous [Python] libraries

Scipy

Scipy on GitHub

openssl

openssl on GitHub

Pytorch

Pytorch on GitHub

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ While I like the approach of looking at what languages are commonly used, I'm not sure large frameworks are representative of the most common use case, and especially openssl seems like an odd data point for an aeroelasticist (it doesn't sound like they're intending to work with cryptography). $\endgroup$ – Luc May 2 at 14:55
  • $\begingroup$ MATLAB is quite awful as a scripting language, so I wouldn't include it on that list. (Actually, it's kind of awful in general.) Bash is also not very great for non-trivial non-shell related tasks. $\endgroup$ – Mateen Ulhaq May 3 at 14:50
  • $\begingroup$ Certainly they're both not great, but they are in extremely wide use, with many existing projects heavily relying upon them, meaning their usage must be understood often even to replace their logic. Further, they are often the only option in certain environments (proprietary control boards, government contracts, Jenkins Rube Goldberg machines) $\endgroup$ – ti7 May 4 at 16:50
6
$\begingroup$

You have some great answers already. I think there is no single answer to your question. What language(s) you choose to learn depends on what you intend to do.

When I was a graduate student I too learned some Matlab, Mathematica, Maple, SAS, Stata, because my university had licenses and because in my area of research that's what others used, so I could easily take existing programs and modify them to my needs. That was a while back. I have since dabbled in Python, Julia, and some statistics software like R and Stata. So what would I recommend?

It's a multidimensional decision to make. Here are some criteria based on my limited knowledge (I haven't tried Ruby, another popular choice):

  • Open source: Forget Matlab and Mathematica. Go for R, Python, Julia, C++ and all that. I avoid proprietary software whenever possible.
  • User base: This depends on what you do. Go for R, Python, Stata, C++, C#, Java, Mathematica. You can search stackoverflow for questions you'd need to answer and see which software gets the most support.
  • Ease of learning: R, Julia, Maple are intuitive and easy to learn. Python isn't all that hard, but less easy. With C++ I didn't get very far because of the learning curve. I prefer R over Python for its IDE and because it serves my needs immediately. In Python, there are so many libraries and so many ways to do the same thing (like computing a quantile or producing a simple plot) that it quickly gets confusing.
  • Ease of use: R and Stata have great IDEs. Python people will try to sell you their IDE, but in my experience none are as good as, say, RStudio. RStudio is the main reason I use R every day.

When I teach statistics, my students prefer R, the admin people ask for SPSS, the old economists ask for Matlab and Stata, and the forward-looking younger guys go for Python. I learned Python in the early 2000s and I wasn't too impressed, the switch from 2 to 3 caused the 'great python stagnation' (don't know if that's a phrase), but Python 3 is now thriving. I use it more and more. Julia has an awesome community and that's the language I wish I was using more.

| cite | improve this answer | |
$\endgroup$
  • 1
    $\begingroup$ R as a programming language is much harder to learn that Python (e.g. R boasts a number of different object systems you would need to understand and has all sorts of weird semantics such as nonstandard evaluation), though I agree that RStudio is in a league of its own as far as user-friendly IDEs go. $\endgroup$ – John Coleman May 1 at 11:01
  • $\begingroup$ @JohnColeman, I think it depends on what your purpose is and, possibly also, on which one you start with. For statistics, web scraping, plotting (with the ggplot library) I have found R pretty easy to learn. When in doubt, try median(x). With Python, you need to choose your module, write things like thislibrary.median(x) or x.thatlibrary.thismodule.median() or define shorthands like tl.median(x)... The typical undergraduate student in first or second year has a hard time with that. It's not "programming" per se, but it's the sort of hurdle I had in mind when I wrote that R was easier. $\endgroup$ – PatrickT May 1 at 11:13
  • $\begingroup$ With R I've found that some things are very easy and some things are fairly hard, with little middle ground. With Python, most things seem to have roughly the same level of difficulty (I am exaggerating of course). For computation which is easily vectorized, R is much easier than python (e.g. x <- seq(-pi,pi,0.001) ... plot(x,sin(x))) but if you start getting to tasks that involve actual programming, then Python becomes easier (especially if string manipulation is required). But, R is definitely a serious candidate for what OP wants, so I'm glad you put in a good word for it. $\endgroup$ – John Coleman May 1 at 11:47
  • $\begingroup$ @JohnColeman, You're probably right, for serious programming I struggle with whatever software I use. :-) And that's where the importance of the "community" comes in. At this time I find just as much help on stackoverflow for R and Python (though too often the search hits take you to Python 2 when you're looking for Python 3). And as Python tends to attract more programmers, there is a larger community to help with difficult questions. On this, the Julia community is also outstanding. $\endgroup$ – PatrickT May 1 at 12:24
  • $\begingroup$ I'm a big R fan, but I agree that starting to learn Python is way easier than starting to learn R. It's true that statistics and linear algebra are easier with R, but R was invented to do statistics. When it comes to programming - even with small programs - guessing the kind of object a given operation will yield is hard for newbies. That was a problem in my beginnings with R but it hadn't been a problem in my previous beginnings with Python. $\endgroup$ – Pere May 2 at 7:34
5
$\begingroup$

Given what you've said, I would learn C++. For one, it allows you to use MPI and lots of libraries for FEM, such as Deal.ii (which all members of this forum are obligated to mention as per our contract with Prof. Wolfgang /s). Also, if you're using C++ I imagine it would be easier to pair with openfoam (I don't know for sure as I work within my own Fortran code or the C/C++ developed NASA codes). The other thing about C++ is while most groups won't translate legacy fortran into C++, a lot of the new development is in C++ and this would make it easier to get jobs down the road. Also, in my experience I found using PETSc and trilinos was easier with c than fortran, but ymmv.

| cite | improve this answer | |
$\endgroup$
  • $\begingroup$ Hi, @EMP. Thank you, I really appreciate your answer. Not doubt, get a job is always in my mind. I'm almost sure that I'll invest my time in C++. $\endgroup$ – Magela Apr 30 at 17:28
  • $\begingroup$ I'd avoid C++. It's rather error prone. You want to avoid C at all costs. Since C++ is based on C, I'd avoid C++. Since you made it to grad school without digging your teeth into programing, you best go with modern and an easy to program language. $\endgroup$ – historystamp May 2 at 6:37
  • $\begingroup$ If he's using openfoam, presumably he'll have to use C++ anyway, why not get actually good at it and experienced? Also, if you want to do performative CSE work, you need to work in either Fortran, C, or C++ at some point. Even the "python" codes have the computational kernels in those three languages. Julia is nowhere near the level of saturation/maturity of those languages, so it doesn't seem like the right suggestion (even though I'd like to learn it at some point). $\endgroup$ – EMP May 2 at 14:51
4
$\begingroup$

Learn 2. Likely Python and (C++ or Fortran).

Learn how to integrate them, for example writing orchestration/loading/analysis in Python and compute kernels that matter for performance in the other language.

There is one-time overhead vs a single language but you will have much more flexibility both for your research and for any later jobs.

| cite | improve this answer | |
$\endgroup$
2
$\begingroup$

I'm a software developer myself, and, all other things being equal, I'd suggest C#. It has a very good, free IDE (Visual Studio Community edition), it is highly performant and has tons of support available on the internet - lots of tutorials, samples, libraries, etc.

That said Dohn Joe does raise a valid point - if what you need to do is so specific and complicated that you need a special library for that, and it only exists in one specific language - then your hands are tied. But for a general purpose programming language - C# is up there with the best.

| cite | improve this answer | |
$\endgroup$
  • 3
    $\begingroup$ C# is a great language, I would argue the obvious choice if you're making Desktop applications, but for the purposes on algorithmic number crunching applications it isn't really the ideal choice. $\endgroup$ – Turksarama May 1 at 5:34
  • $\begingroup$ @Turksarama - Webpages are well made in it too. But what's the problem with number crunching? $\endgroup$ – Vilx- May 1 at 10:51
  • $\begingroup$ To start learning, I think C# is a good place. It's fairly easy to get into c++ from there, and you can do number crunching in it, but that's not something I would have a beginner do. There are ways to write code to make it run more efficient, and newbies tend not to know that. $\endgroup$ – Ben May 1 at 14:05
  • $\begingroup$ @Vilx- to be fair, it will be just fine at it, but it isn't ideal in the sense that it isn't designed specifically for the application. The top answer at the moment mentions Julia, which is a good choice because it has proper built in functions for linear algebra, and being a dynamic language allows for much cleaner function signatures if you want to, for example, use a single function to work on matrices with different numbers of dimensions. It gives you the ease of use of Python combined with the speed of C, it's a perfect language for its domain. $\endgroup$ – Turksarama May 2 at 0:07
  • $\begingroup$ You want a portable language which C# isn't. $\endgroup$ – historystamp May 2 at 6:36

Not the answer you're looking for? Browse other questions tagged or ask your own question.