Best programming language for physics

  • Thread starter Crake
  • Start date
  • #1
64
1
Hello,

I'm not sure if this is the right place for this thread, but I think it is (This forum is for computational physics as well)

So, my question is quite "soft". What is (in your opinion, obviously) the best programming language for physics? Also, what programming language is most used in physics?

Nowadays, if you had to learn a new language, as your first programming language, what would you pick?
 

Answers and Replies

  • #2
phinds
Science Advisor
Insights Author
Gold Member
2021 Award
17,374
8,905
This question is asked about once a week. I suggest a forum search.
 
  • #3
TumblingDice
Gold Member
472
47
BASIC is a friendly, easy to learn language with simple variable definitions and statements.

If this is for intensive calculations like modeling/simulation, "C" would be more appropriate to the task.

Perhaps I'm showing my age when I say, if there's a lot of math (not meaning reiterative), I'd consider FORTRAN. :)
 
  • #5
Astronuc
Staff Emeritus
Science Advisor
20,166
3,743
The two principal languages would be Fortran (F90 or later) and C++. These days, object-oriented programming is widely used. There is a huge volume of legacy codes written in earlier versions of Fortran, particularly Fortran 77, and more recently C++.

http://www.dmoz.org/Computers/Programming/Languages/Fortran/Source_Code/Physics/

Most physics problems involve solving systems of differential equations, both ordinary and partial, and in many cases, non-linear.

Python is often used to support programs written in Fortran or C++.

Then there are packages like Matlab, Mathematica, Maple, . . . .

Linking to other threads is useful.
 
  • #6
13,316
7,230
There is also Java with the Open Source Physics toolkit (www.compadre.org/osp). It comes with many examples of how to do a variety of different simulations and its open source. Programs are developed using Eclipse or Netbeans.
 
  • #7
992
127
Python is probably the most user friendly and intuitive language for beginners IMO.

It's relatively weak when doing numerical calculations, which is why people use it to implement numerical algorithms in Fortran or C++ (both strong in that area).
 
  • #8
263
4
I read from one book that Python also comes with several different implementations, one is PyPy and NumPy. The latter is said to be quite good in numerical computation while the former is faster than the standard Python.
 
  • #9
Student100
Education Advisor
Gold Member
1,649
416
The first language I'd recommend you learn is C. That makes learning C++ rather simple, and you can focus more on OOP than syntax. I'm not sure how I feel about python, it just doesn't perform all that well unless you invest a lot of time in it, but it is open. I'm not a fan of Java at all.
 
  • #10
13,316
7,230
In answer to the last question of the OP, I would choose Scala. It seems general purpose languages are moving toward functional programmings in conjunction with OO.

When you look at legacy systems, the oldest apps are written in FORTRAN, more recent ones are in C++ and the latest ones are in Java. I see this trend moving towards Scala in the coming years. What makes Scala so appealing is its ability to work well with Java libraries while at the same time bringing a new way of thinking about software design...
 
  • #11
455
11
Start learning programming with python then move on to another lower level language if you need it. Python will allow you to do the most without needing to write the same code again and again. You can easily use modules to replace other programs like matlab etc.

The older guys use fortran and c++. The younger people use python,java,and c++. You can do GPGPU in python and java if you need it or move down to lower level c++. There is no point at getting down lower than C++ because compilers are optimized pretty well to the point that your java code wont lose a significant amount of performance relative to a lower language. There is a point of diminishing returns and if there wasnt we would all be writing in Assembly which physicists usually dont.
 
  • #12
13,316
7,230
And the professionals don't bother to argue about stuff like this, they choose the best language to get the job done and then go home to do other things...
 
  • #13
D H
Staff Emeritus
Science Advisor
Insights Author
15,415
687
And the professionals don't bother to argue about stuff like this, they choose the best language to get the job done and then go home to do other things...
Adding my two bits to what you wrote: There is no single language that stands out as the "best programming language for physics". A good programmer knows several languages. The old adage "learn a new language / tool every year" is more applicable now than ever.

We get these kinds of threads on a very regular basis, and they invariably decay into a religious war over computer languages. From what I've seen, the detractors of language XYZ (pick your language) invariably do not know what they're writing about.
 
  • #14
455
11
And the professionals don't bother to argue about stuff like this, they choose the best language to get the job done and then go home to do other things...

thats not actionable advice.

From what I've seen, the detractors of language XYZ (pick your language) invariably do not know what they're writing about.
Different languages have their strengths and weakness which means some languages have features which make detractions reasonable. Assembly has trade offs when compared to Python is a simple example of such tradeoffs.

The simplest answer is to learn the programming language that the people around you know so you could have someone to look for answer to questions.
 
  • #15
1,083
25
I don't know if this is helpful but in my experience the best coding language is often going to be dictated by the group you are in and what language that group writes their codes in. My group wrote theirs in C++ so I needed to learn C/C++. Also in my brief experience it seems it is important to be able to competently code with one language of ones choice (at minimum) but be able to read code in other languages.
 
  • #16
1,948
201
The languages most often used are still FORTRAN and C/C++ so, as a general answer I would tell a physicist wannabe to learn those. If you have a specific group you know you will be working with them ask them what language they use and follow their lead.
 
  • #17
153
1
Are people really serious when they advocate FORTRAN of all things as anyones first or primary language? Like really?
 
  • #18
D H
Staff Emeritus
Science Advisor
Insights Author
15,415
687
Are people really serious when they advocate FORTRAN of all things as anyones first or primary language? Like really?
Really. The question wasn't the best language(s) to learn in general. The question at hand is what are the best language(s) for physics, and Fortran (note the spelling) most certainly is in that category.

Note well: I am not speaking as a Fortran aficionado. I personally haven't used it for 20 years. There are still times when I regret the switch to C/C++. Fortran is quite widely used for physics and related applications where sheer number crunching power is paramount.
 
  • #19
cgk
Science Advisor
524
42
D H,
even in Fortran[1] is still widely used, most of the applications occur either in legacy codes or in codes directly depending on other Fortran codes. And it is one of the few languages which one can truly classify as "bad" without further qualifying this statement. It is not even only the language, the language implementations are also sub-par: Take five different compilers, and you will find five different subset of the Fortran 95(!!!) standard which work as standardized.

While being able to read and write Fortran can still be important, I cannot see how it could possibly be a good idea to use this language for starting programming. Many *very* important idoms in programming, including very basic ones (like, compound data structures) simply cannot be expressed in any meaningful way in this language. Starting programming with this is bound to create many problems when later moving to more powerful languages. However, the other way around (say, starting with Python and C++ or Java and then using Fortran if you really have to) does not have this problem at all.

([1] and it used to be spelled in upper case)
 
  • #20
455
11
Fortran is mostly around as legacy code or code written by some theorist in high energy and it is losing market share among physicists. Programs are being rewritten to other languages like C++, an example of which is Pythia which used to be in FORTRAN in early versions.

ROOT is in C++. CMSSW is C++ based.

GPGPU is done using CUDA and OpenCL which the base libraries are done in C++.

Matplotlib is used by physicists in many fields to generate graphs and can be used to do GPGPU using pyopencl.
 
  • #21
63
0
There are numerous programming languages useful for Physics. More importantly, it depends on the level of physics you are doing and how much previous programming experience you have. Fortran is a very old yet popular language among physicists. One language that I particularly like is Mathematica. It is used by numerous people for advanced mathematics, physics etc. Other options include Python for basic projects, C++ or Java for more large-scale endeavors.
 
  • #22
13,316
7,230
It's true that fortran is around mostly in legacy code but some that code will still be around when you plan to retire in twenty years or more. Many physical models are written in fortran and thoroughly tested and entities that depend on these models would rather port them than rewrite them in a more modern language.

When I worked at GE some years ago they were still using this ancient machine that ran one application at a time rather than rewrite the software because it ran some sort of stock maintenance program. We thought it was so silly but nevertheless the management was adamant that it be used. It was so important that they were afraid that they would lose their reputation if they screwed up a run by rewriting it. It had a lot of complex business logic in it and its results were highly visible.
 
  • #23
455
11
It's true that fortran is around mostly in legacy code but some that code will still be around when you plan to retire in twenty years or more. Many physical models are written in fortran and thoroughly tested and entities that depend on these models would rather port them than rewrite them in a more modern language.

When I worked at GE some years ago they were still using this ancient machine that ran one application at a time rather than rewrite the software because it ran some sort of stock maintenance program. We thought it was so silly but nevertheless the management was adamant that it be used. It was so important that they were afraid that they would lose their reputation if they screwed up a run by rewriting it. It had a lot of complex business logic in it and its results were highly visible.
In a business environment like GE you will have different incentives at play. COBOL is still used in some business settings and will be still used in some business settings in 20 years from now but it will be less common than now.

In physics there isnt the same incentives to stay at the status quo especially in a research environment hence why rewrites like the one for PYTHIA to go from FORTRAN to C++ were able to be done.
 
Last edited:
  • #24
13,316
7,230
I hate to disagree but there are still some research environments where they use very old Fortran models and they won't change languages any time soon due to acceptance and stability of the models. Its been proposed and rejected many times.
 
  • #25
UltrafastPED
Science Advisor
Gold Member
1,912
216
While doing my PhD in physics I used MatLab, LabView, and Java ... Java for all of the complex analysis and image processing.

In my previous professional programming/engineering life I used - from beginning to end:
Assember, Fortran II, Fortran IV, Cobol, APL, Snobol, lots more Cobol, RPG I & II, lots of PL/I, dBase II, IBM PC assembler, C, Visual Basic, C++, then the stuff listed above.

Along the way I also wrote Fortran compilers, and taught computer programming (in three different languages) for a few years.

As others have said: use the tool that works for the job at hand. If you are modifying old code, then use that.
Each environment and application requires different tools. Most people only see a fraction of what is possible because they have only worked in limited environments.

So I don't disagree with any of the previous answers - but there are many correct answers; the best one for your situation is something that you will determine in the future. For example, the comments about Fortran by D H are certainly correct - especially if you are doing computational physics. But when I took a course in soft materials simulations the course was taught in Java. And in the engineering courses almost everything was MatLab.

One addition here: if you are an engineer you should probably start with MatLab.
 

Related Threads on Best programming language for physics

Replies
8
Views
2K
  • Last Post
Replies
22
Views
29K
  • Last Post
Replies
12
Views
4K
  • Last Post
Replies
9
Views
2K
Replies
22
Views
19K
Replies
1
Views
2K
Replies
17
Views
13K
  • Last Post
Replies
20
Views
13K
Replies
4
Views
1K
Replies
7
Views
5K
Top