Learning Programming for Physics Research in Grad School

In summary, it is important to learn the basics of programming and how to solve problems, as this will help you in your research in physics involving computations and simulations. However, it is also important to take into account the area of physics you are interested in and the programming language that is most appropriate for that area.
  • #1
creepypasta13
375
0
I plan to start grad school in the fall and do not have a main interest for now (astrophysics, condensed matter, etc), but want to do research in something as long as it involves computations. I should probably get better at programming so that I could do research in physics involving computations and simulations a lot more efficiently than I did in my last research project where I wasted far too much time just learning basics of programming. I didn't take a computaitonal physics class an undergrad, so to make the best use of my time from now until the fall, should I just pick up a computational physics textbook and work through it, such as these?

https://www.amazon.com/dp/0139067442/?tag=pfamazon01-20
https://www.amazon.com/dp/0521532760/?tag=pfamazon01-20
https://www.amazon.com/dp/0471115908/?tag=pfamazon01-20

Or just work through my old 'Learn C++ in 30 days' textbook?
 
Last edited by a moderator:
Physics news on Phys.org
  • #2
I would say learn MATLAB. MATLAB is used heavily in Physics research at my university. In fact, our Computational Physics course is all MATLAB. That being said, C is equally important. Much of the MATLAB programming is done in MATLAB but in C the language due to the control the programmer has over how memory is utilized. Learn the software and C from whatever books you can find and you'll be well off I'd say. There are a lot of great resources online that are free, so don't feel the need to purchase a book online. Some of them are actually a joke.
 
  • #3
I should say that I already know the beginner basics of Matlab and C++. I just wanted to improve my skills in using the parts of those that are most relevant to doing research in Physics. For example, if object-oriented programming helps alot, then that's one thing I'd try to get better at. If some other concept in Matlab and C++ isn't, then I don't want to waste my time learning it
 
  • #4
One of the fascinating points about C++ is that it is very hard to use it efficiently unless you know general programming, algorithms and data structures, software design, CPUs, compiler and operating system environments, and finally C++ all to a sufficient degree. Even if details are hidden (you wouldn't code a sorting algorithm, for example) you still need to know how they work or you'll run into traps.

It takes several years to become a decent programmer (especially if going for C++), and if you are a beginner, every single programming experience does help, no matter which. Just be sure to always ask yourself how you could have improved the program you just wrote, and the process in which you wrote it. Do whatever works for you to keep on doing that.
 
  • #5
1) It vastly depends on the area of physics you want to get into. Astronomers and astrophysicists often use a mixture of C/C++ for hydrodynamic or many-body simulations and Python for other stuff. Other researchers might use Matlab, others use visual programming languages like LabView.

Simply: it depends on the department.2) Learn a programming language like C/C++ or Java. Stick with one of those two and you'll be fine. If you take the C route, then learn both C and C++, you'll probably end up mixing the two for your work anyway. Don't worry about picking the wrong language, as you'll see, it's not that important unless you have very specific aims in mind, like ultra-efficient code.

2) Also learn a scripting language like Python or Perl. Often it's a lot easier to kludge a quick script in Python than it is to bit-bang the solution in C.

3) Get to the point where you're learning how to solve problems and not remembering syntax. I think that's the key thing you should come away with - learning the language is secondary. Your main aim is to figure out how to apply your knowledge of conditional logic, loops, functions, etc to the task at hand - see 2). The syntax is merely an implementation issue.

Once you've got 3) down pat, it won't matter what language you use. Get used to writing pseudo-code and flowcharting and then apply it to the language that seems most appropriate.

4) Pick a project and make it happen. No amount of book work will make you a good programmer and frankly I could never learn from looking at 'hello world' examples. Get stuck in, screw up, read the error messages and understand what you're doing. Challenging yourself to code a program that you actually need or would find useful is a lot more satisfying and motivating than the banal crap in most textbooks. Trust me on this one, working through a textbook is one of the dullest things imaginable.

5) On the subject of textbooks, don't expect to learn by reading. I wouldn't waste your money on a Dummies book or a 30-day guide. Generally I buy a programming textbook as a reference and dip into it as necessary. Either that or I use the magic of Google.

The first book you linked to looked fine, but again, it'll only help you if you have a project or something to work on in mind. You're a student, so make use of your library. Don't pay for the introductory guides, borrow them and save your money for the encyclopaedic tomes that will come in useful later. Note that the classic Numerical Recipes in C is available online: http://www.nr.com/. I would recommend O'Reilly for your source of general interest textbooks, they're generally very well written.
 
  • #6
Faloren said:
1) It vastly depends on the area of physics you want to get into. Astronomers and astrophysicists often use a mixture of C/C++ for hydrodynamic or many-body simulations and Python for other stuff. Other researchers might use Matlab, others use visual programming languages like LabView.

Simply: it depends on the department.

I will likely go do research in condensed matter, plasma, or astrophysics

4) Pick a project and make it happen. No amount of book work will make you a good programmer and frankly I could never learn from looking at 'hello world' examples. Get stuck in, screw up, read the error messages and understand what you're doing. Challenging yourself to code a program that you actually need or would find useful is a lot more satisfying and motivating than the banal crap in most textbooks. Trust me on this one, working through a textbook is one of the dullest things imaginable.
..
The first book you linked to looked fine, but again, it'll only help you if you have a project or something to work on in mind. ..Note that the classic Numerical Recipes in C is available online: http://www.nr.com/. I would recommend O'Reilly for your source of general interest textbooks, they're generally very well written.

I see. I know the basics of C++ and Matlab but am still rather a beginner at using them for physics problems. Today, I was able to write a program to numerically solve the Heat equation, just so you know what level I'm at, but I had to spend a lot of time learning new techniques like the disp, input, and mesh functions on Matlab. It would be nice if I could find some online resources that had practice problems like that instead of having to buy a textbook, but I haven't found any yet. No, I'm not currently in school though I wish I were
 
Last edited:
  • #7
creepypasta13 said:
I will likely go do research in condensed matter, plasma, or astrophysics



I see. I know the basics of C++ and Matlab but am still rather a beginner at using them for physics problems. Today, I was able to write a program to numerically solve the Heat equation, just so you know what level I'm at, but I had to spend a lot of time learning new techniques like the disp, input, and mesh functions on Matlab. It would be nice if I could find some online resources that had practice problems like that instead of having to buy a textbook, but I haven't found any yet. No, I'm not currently in school though I wish I were

If you ever want to (or have to) learn something like C++, my advice to you is to get large repositories (or code bases) that have some kind of large integrated system that's relevant to your domain.

An example might be say a simulation library/package or perhaps a library created specifically for computational physics.

Then basically look at the library and analyze it, look at what changes have been made over the course of the project, and get some experience writing some custom code ranging from using the existing code-base to calculating something (maybe a few dozen lines) to adding a new feature to maybe modifying the structure or design.

Get exposed to other projects and understand the direction that the designers have taken.

I guarantee if you head along this path, you will learn all the things that you are taught in a traditional comp-sci degree and if you become really engrossed, you will probably learn a lot more.

Get your hands dirty, slog it out, and you will become proficient.
 
  • #8
I would suggest working with Python first. It is gaining wide acceptance in scientific computation. Add a package called SciPy, which includes a large body of numerical algorithms and a MATLAB-like interface (pylab) with fantastic plotting abilities (matplotlib). Python is a simple, elegant, and very versatile general-purpose programming language. You will be able to immediately begin implementing and trying out different algorithms in your books, while the language will get out of your way. Your programs may not be the fastest ever written, but I believe that is a secondary concern to learning the algorithms and gaining a broad understanding of the field.

creepypasta13 said:
in my last research project where I wasted far too much time just learning basics of programming

This doubly motivates me to recommend Python. :tongue:

There are some good books available:

https://www.amazon.com/dp/3540435085/?tag=pfamazon01-20

This is not to say that you should not learn C++. You should (and probably will have to) learn C++ at some point, but it need not be now. Once you understand some algorithms in a high-level language, you will be much more effective at implementing them later in C++.

- Warren
 
Last edited by a moderator:
  • #9
I definitely second learning Python :smile: It's the best programming language as far as keeping annoying implementation details from getting in your way, thereby letting you write the most productive code quickly. The sense I've gotten is that the top 3 languages to focus on for physical computing are Python, C++, and Fortran.

As an added bonus, Python enforces a sensible use of whitespace. This is especially important in physics because one of the common problems with code written by scientists is that it looks horrible and is pretty incomprehensible if you're trying to learn how it works. Whatever language you work in, PLEASE do the scientific community a favor and use a sensible coding style - consistent indentation, descriptive variable names, and documentation/comments. If you can learn to use a version control system, better yet.
 
  • #10
The good thing about Python as well is that a lot of hte scientific community in certain areas use IDL, which is sort of like a commercialized version of Python. So you can kind of say that you have some indirect experience with IDL as well (with the open source version, let's say).

However, there is something to say about learning FORTRAN or C/C++. This would probably apply more for C/C++ since there's memory management you have ot worry about, but someone else in the thread said it; you have to learn a lot about how computers actually work on the software (and even a bit on the hardware parts) to use it effectively. However, this can be a good thing sometimes. Recently I've been screwing with my friend's Nvidia GFX card that allows you to write programs and run them on its GPU, which means you can write highly parallelized code and run it on like a sort of simulated supercluster (much slower, obviously). Other conventional methods of high performance computing with CPUs are somewhat analogous, but of course the GPU programming has been very simplified with the CUDA framework (basically C with some extra libraries and headers to access hardware stuff without having to slog around with the super technical parts of it). High performance computing is pretty big in science, so it might be in your interest to at least be familiar with it.

Anyway, the reason I mention this can be seen through a sort of analogy (though I don't know how much this might help, but we'll give it a shot). It's like when the senior programming gurus tell you to never rely on the compiler optimizations when you're writing your code. Sure, the optimization is there, and the compiler will probably get it, but it's better to know how to optimize your code, why it works the way it does, etc. because these things will help you when 1) the compiler misses what it's supposed to do (it's not omnipotent or perfect, they're just programs written by lowly humans after all) and 2) when you're in a situation where there is no compiler to do these things for you. So knowing how to take advantage of a computer is always better than relying on the generalizations of computer scientists or computational physicists that are (supposedly) much more competent in areas of operating systems, algorithms, and other things like that, but because they're not the ones writing code specifically for your purposes, there's always a chance that something will be missed, and it's pretty likely that they'll miss something that is important to your purposes.

So, learn as much as you can. There may be more important things to learn now, and you don't have to dedicate your life all of sudden to become a computer scientist for a year or two, but you will always be more valuable when you know how to take advantage of a computer by knowing the fundamentals of computing and you'll easily have a safety net as far as employment goes (people are always in need of geeks who know how to work with computers). Plus it'll help your simulations go faster, with better accuracy, less resources, etc. Plus it's just really cool. :)
 
  • #11
chroot said:
There are some good books available:

https://www.amazon.com/dp/3540435085/?tag=pfamazon01-20

- Warren

Hey chroot, is that book for beginners? I read the reviews and most of them were intermediate to advanced programmers. I have dabbled with Python and know only the bare basics. Although, I'd probably like that book because it has a particular goal in mind with science/math.
 
Last edited by a moderator:
  • #12
DrummingAtom said:
Hey chroot, is that book for beginners? I read the reviews and most of them were intermediate to advanced programmers. I have dabbled with Python and know only the bare basics. Although, I'd probably like that book because it has a particular goal in mind with science/math.

I just got lucky on the Amazon "Look Inside" feature and got the part of the preface that explains:
Required Background. This book is aimed at readers with programming experience. Many of the comments throughout the text address Fortran and C programmers and try to show how much faster and more convenient Python code development turns out to be.

Hope that helps.

As to the OP: take chroot's advice. I wish I had a much deeper knowledge of algorithms, it would help me immensely in my research.
 
  • #13
btw,the whole 'python' (python+iPython+numpy+scipy+pylab+WhatEverApplePy) is available as a single package/installation for Windows:

http://www.pythonxy.com
 
  • #14
chiro said:
An example might be say a simulation library/package or perhaps a library created specifically for computational physics.

Then basically look at the library and analyze it, look at what changes have been made over the course of the project, and get some experience writing some custom code ranging from using the existing code-base to calculating something (maybe a few dozen lines) to adding a new feature to maybe modifying the structure or design.

Get exposed to other projects and understand the direction that the designers have taken.

I guarantee if you head along this path, you will learn all the things that you are taught in a traditional comp-sci degree and if you become really engrossed, you will probably learn a lot more.

Get your hands dirty, slog it out, and you will become proficient.

I've been trying to find some, but the only ones I've seen looked way too complex for me using all sorts of commands I've never seen before on C++. I'll try to just look at one of them and work through it

chroot said:
I would suggest working with Python first. It is gaining wide acceptance in scientific computation. Add a package called SciPy, which includes a large body of numerical algorithms and a MATLAB-like interface (pylab) with fantastic plotting abilities (matplotlib). Python is a simple, elegant, and very versatile general-purpose programming language. You will be able to immediately begin implementing and trying out different algorithms in your books, while the language will get out of your way. Your programs may not be the fastest ever written, but I believe that is a secondary concern to learning the algorithms and gaining a broad understanding of the field.

There are some good books available:

https://www.amazon.com/dp/3540435085/?tag=pfamazon01-20

I see. I'll try to find some online guides that are free before purchasing that book.

This is not to say that you should not learn C++. You should (and probably will have to) learn C++ at some point, but it need not be now. Once you understand some algorithms in a high-level language, you will be much more effective at implementing them later in C++.

- Warren

Just to clarify in case you didn't understand. I already know the basics of C++. I completed my last research project in it. But I'm by no means an expert in it

hadsed said:
The good thing about Python as well is that a lot of hte scientific community in certain areas use IDL, which is sort of like a commercialized version of Python. So you can kind of say that you have some indirect experience with IDL as well (with the open source version, let's say).

However, there is something to say about learning FORTRAN or C/C++. This would probably apply more for C/C++ since there's memory management you have ot worry about, but someone else in the thread said it; you have to learn a lot about how computers actually work on the software (and even a bit on the hardware parts) to use it effectively. However, this can be a good thing sometimes. Recently I've been screwing with my friend's Nvidia GFX card that allows you to write programs and run them on its GPU, which means you can write highly parallelized code and run it on like a sort of simulated supercluster (much slower, obviously). Other conventional methods of high performance computing with CPUs are somewhat analogous, but of course the GPU programming has been very simplified with the CUDA framework (basically C with some extra libraries and headers to access hardware stuff without having to slog around with the super technical parts of it). High performance computing is pretty big in science, so it might be in your interest to at least be familiar with it.

Anyway, the reason I mention this can be seen through a sort of analogy (though I don't know how much this might help, but we'll give it a shot). It's like when the senior programming gurus tell you to never rely on the compiler optimizations when you're writing your code. Sure, the optimization is there, and the compiler will probably get it, but it's better to know how to optimize your code, why it works the way it does, etc. because these things will help you when 1) the compiler misses what it's supposed to do (it's not omnipotent or perfect, they're just programs written by lowly humans after all) and 2) when you're in a situation where there is no compiler to do these things for you. So knowing how to take advantage of a computer is always better than relying on the generalizations of computer scientists or computational physicists that are (supposedly) much more competent in areas of operating systems, algorithms, and other things like that, but because they're not the ones writing code specifically for your purposes, there's always a chance that something will be missed, and it's pretty likely that they'll miss something that is important to your purposes.

So, learn as much as you can. There may be more important things to learn now, and you don't have to dedicate your life all of sudden to become a computer scientist for a year or two, but you will always be more valuable when you know how to take advantage of a computer by knowing the fundamentals of computing and you'll easily have a safety net as far as employment goes (people are always in need of geeks who know how to work with computers). Plus it'll help your simulations go faster, with better accuracy, less resources, etc. Plus it's just really cool. :)

Thats a good point. I wanted to do physics using high performance computing, but I really am not interested in the way computers work in the software. But if it really helps with the simulations to understand that, then I'll certainly spend time learning about it
 
Last edited by a moderator:
  • #15
physiker_192 said:
btw,the whole 'python' (python+iPython+numpy+scipy+pylab+WhatEverApplePy) is available as a single package/installation for Windows:

http://www.pythonxy.com


Thanks
 
  • #16
creepypasta13 said:
Thanks

It all depends how down the rabbit hole you want to go. "Computational" means different things. I personally have never used a suite or a library, I always code everything myself. For me I focus on the computational aspect just as much as the physics. If you want to develop the sort of skill set that I'm developing then the choice of language is extremely important (in reality you might want multiple but just pick one to start). Something like python, java, and ESPECIALLY MATLAB is useless. You need something low-level, like C/C++ or FORTRAN 70, I personally like C++ because I can toss the occasional object-oriented bit in and I find it WAY more readable. The best thing you want to do is a basic book/course on computer architecture. Basically, understand how a computer takes those things you write in a notepad document and turns it into machine code. That's incredibly important if you want to specialize in COMPUTATIONAL physics.

For example, if you just declare an array in your code it makes a world of difference between what language you're using (and even what compiler). For example, in C++ one might want to declare an N x M array and a beginners book will tell you to just declare an array of N pointers then do a for loop and allocate an array of length M to each element. Problem is that's a bad idea for C++ (or any language really) because there's no guarantee that a computer will put each of those arrays anywhere nearby in memory, the better move is to start out by allocating a whole contiguous block of N*M elements and then using pointer arithmetic to divy it up into your N x M array. The point is that stuff like this makes a difference between you code taking a week to run or a 100 years if you want to go deep into computational. Of course you come across people who discretize some PDE (backward, forward euler, verlet, whatever) in MATLAB and call it computational. Personally, I don't think you're really working in computational if you don't have even a passing undergrad knowledge of ASSEMBLY. But like I said, it's all about how far down the rabbit hole you want to go.
 
  • #17
Wow, it makes me sad that I would be the only here whose first thought was FORTRAN!

In general, just pick a language and work through it. While all languages have their differences, once you learn how to program in one language you can pick up any other language pretty quickly.

So just pick whatever you feel more comfortable with.
 
  • #18
Birkeland said:
Wow, it makes me sad that I would be the only here whose first thought was FORTRAN!

In general, just pick a language and work through it. While all languages have their differences, once you learn how to program in one language you can pick up any other language pretty quickly.

So just pick whatever you feel more comfortable with.

Well you're lucky with FORTRAN. The problem with things like C#, JAVA, Python, etc. is that they're so call "higher-level" programming languages and all that object-oriented, polymorphism, ignore implementation abstractness of it comes with a big cost in terms of how a computer actually computes it. "Robustness" is actually deadly when you get to the nitty gritty. And something like MATLAB? Forget about it, a scripting SOFTWARE? Try doing a Monte-Carlo average over 1000 realization in MATLAB, it'll take like 6 minutes. The same will take under 6 SECONDS in a lower-level language like C++
 
  • #19
maverick_starstrider said:
The problem with things like C#, JAVA, Python, etc. is that they're so call "higher-level" programming languages and all that object-oriented, polymorphism, ignore implementation abstractness of it comes with a big cost in terms of how a computer actually computes it.

Which is why C++ is cool. You can get object-oriented polymorphism without losing any performance. With template metaprogramming you can have all of the overhead of polymorphism stripped away at compile time, and so you are running "bare metal" vectorized instructions.

You can program C++ as if it were fortran, then you can do what boost::math which is a work of art.

Forget about it, a scripting SOFTWARE? Try doing a Monte-Carlo average over 1000 realization in MATLAB, it'll take like 6 minutes. The same will take under 6 SECONDS in a lower-level language like C++

Or what you can do is to encaspulate everything in well designed building blocks. The reason that scipy is cool is that you get all of the good things of a scripting language, but if you do anything compute intensive, the scripting language passes everything down to C (or in some cases hand tuned machine language if you are using MKL), and let the magic take place there.
 
  • #20
cgk said:
One of the fascinating points about C++ is that it is very hard to use it efficiently unless you know general programming, algorithms and data structures, software design, CPUs, compiler and operating system environments, and finally C++ all to a sufficient degree.

Maybe...

The thing about C++ that makes it easy and hard to learn is that it is a multi-paradigm language. You can program C++ as if you were programming Fortran, or C or Lisp or Java. If all you know is FORTRAN, and you want to program C++ as if it was Fortran, you can.

However, if you need to mixed Fortran like programming with Java like programming, then you are pretty much dead with anything other than C++.

It takes several years to become a decent programmer (especially if going for C++), and if you are a beginner, every single programming experience does help, no matter which. Just be sure to always ask yourself how you could have improved the program you just wrote, and the process in which you wrote it. Do whatever works for you to keep on doing that.

Also don't just *write* code. Learn to *read* code. Something that works for me is that I coded enough to be able to react emotionally to good and bad code.
 
  • #21
creepypasta13 said:
I see. I know the basics of C++ and Matlab but am still rather a beginner at using them for physics problems. Today, I was able to write a program to numerically solve the Heat equation, just so you know what level I'm at, but I had to spend a lot of time learning new techniques like the disp, input, and mesh functions on Matlab.

Download programs that other people have written and then add something to it. It's really important if you want to programing to be able to read other people's code. You might download something, and then find it *PAINFUL* to read and impossible to change, at which point you know what bad code looks like.
 
  • #22
twofish-quant said:
Maybe...

The thing about C++ that makes it easy and hard to learn is that it is a multi-paradigm language. You can program C++ as if you were programming Fortran, or C or Lisp or Java. If all you know is FORTRAN, and you want to program C++ as if it was Fortran, you can.

However, if you need to mixed Fortran like programming with Java like programming, then you are pretty much dead with anything other than C++.
Absolutely. It is kind of sad, but C++ is still the best language for many kinds of tasks. Especially for building very large, robust applications in which performance is important. There is a reason why it is used as main programming language for most large commercial Windows applications.

On the other hand, C++ is also by far the most complicated programming language in practical use, and maybe the only one where even after 15 years of intensive use one can still come across syntactic constructs one has never seen before, or across a four line piece of code for which it takes 20minutes to check whether it is correct or not. The main problem in practice, however, is that its flexibility makes it really simple to create absolutely horrible code. And interestingly, the worst code is not even from absolute beginners, but rather from intermediate coders who take abstraction and generality way over the top and create a an unmaintainable 1000 line class-hierarchy-metaprogramming-exception-mess where 50 lines of C-style code would have been sufficient. Writing decent C++ code takes lots of experience, and there is no way of learning that other than doing it on concrete projects.
 
  • #23
Hm. There's a newer 'version' of C++... in case anyone is interested. Google had something to say about this (in a good, supportive way).

http://en.wikipedia.org/wiki/D_(programming_language)

But I don't think this will ever get to academia in our lifetimes (they're still using FORTRAN 77... it's kind of ridiculous).
 
  • #24
hadsed said:
Hm. There's a newer 'version' of C++... in case anyone is interested. Google had something to say about this (in a good, supportive way).

http://en.wikipedia.org/wiki/D_(programming_language)

That's not C++, but simply another programming language with the same spirit. While the D language may be better in various respects, one of the strongest points about C++ is its wide portability and wide support with libraries, APIs, programming environments[1] etc and of course the gigantic code bases already present.

As long as neither MS, the GCC or LLVM team or Apple adopt and push this language it won't be going anywhere on the long run. Of course it's a nice thing for writing your personal programs in, but starting any large projects in it at this point in time would be very dangerous, because there is no guarantee that any (maintained) compiler will still be around in five years. Also, C++ may be a mess, but due to the long experience it is known how to deal with it in almost all scenarios. That is not the case for D.


[1] Ever used Visual C++? That program and the SDKs are what is keeping Microsoft in business, and will continue to do so for an indefinite amount of time. Simply because it is much more *pleasant* to program on/for Windows than for any other platform due to the great tool support and documentation.

But I don't think this will ever get to academia in our lifetimes (they're still using FORTRAN 77... it's kind of ridiculous).
While I personally despise Fortran, one must admit that it is a semi-decent language for certain jobs. And it's not like you could simply re-write your 2 million line of Fortran code software thing on a whim. While the language itself is a joke, the algorithms and know how in the existing software may still be high-tech. And as long as the LAPACK is around, Fortran will continue to be used.
 
  • #25
maverick_starstrider said:
The problem with things like C#, JAVA, Python, etc. is that they're so call "higher-level" programming languages and all that object-oriented, polymorphism, ignore implementation abstractness of it comes with a big cost in terms of how a computer actually computes it.

Actually, well-written Python spends almost all of its CPU time in highly-optimized library code, written in C++ and tuned for performance by people who are more capable than either you or I. This is one of the many reasons it is being adopted so widely for scientific computing.

The same argument applies to FORTRAN, which you apparently do not despise...

- Warren
 
  • #26
cgk said:
a four line piece of code for which it takes 20minutes to check whether it is correct or not

There's an old saying, "You can write bad Fortran code in any language." :wink:
 
  • #27
cgk said:
That's not C++, but simply another programming language with the same spirit. While the D language may be better in various respects, one of the strongest points about C++ is its wide portability and wide support with libraries, APIs, programming environments[1] etc and of course the gigantic code bases already present.

As long as neither MS, the GCC or LLVM team or Apple adopt and push this language it won't be going anywhere on the long run. Of course it's a nice thing for writing your personal programs in, but starting any large projects in it at this point in time would be very dangerous, because there is no guarantee that any (maintained) compiler will still be around in five years. Also, C++ may be a mess, but due to the long experience it is known how to deal with it in almost all scenarios. That is not the case for D.

[1] Ever used Visual C++? That program and the SDKs are what is keeping Microsoft in business, and will continue to do so for an indefinite amount of time. Simply because it is much more *pleasant* to program on/for Windows than for any other platform due to the great tool support and documentation.
It's not inconceivable that Microsoft could pick up D and make something of it. They've come up with a lot of crap that isn't widely used (think J++), and eventually C++ will need to be replaced. No one can say how long that will take, since if you look at FORTRAN...well, it's still around. Also, I don't know anyone that would think of writing code for a serious project in a brand new language, just because...

I'd like to see D get somewhere, it seems a lot more streamlined and organized. Sure it's cool to be that guy who's a total hacker physicist that's better in IT than most IT people (not saying a lot though I guess), but in the end you're wasting a lot of time screwing with memory management and all this other stuff when you could be doing physics. I think that's one of the reasons FORTRAN is still around. Half of our time is spent fixing the damned computers when we could be running simulations or actually be doing physics and not have to be certified sysadmins. Eventually, physicists (even computational ones) ought to be doing physics, and sure the way it's structured right now provides a good safety net for a lot of physicists/IT admins/software engineers, and that's good for them, but I don't think it's good for physics. It's kind of the reason why civilization came up with specialization in the first place, so things get done more efficiently.

While I personally despise Fortran, one must admit that it is a semi-decent language for certain jobs. And it's not like you could simply re-write your 2 million line of Fortran code software thing on a whim. While the language itself is a joke, the algorithms and know how in the existing software may still be high-tech. And as long as the LAPACK is around, Fortran will continue to be used.
Yup.
 
  • #28
hadsed said:
Hm. There's a newer 'version' of C++... in case anyone is interested. Google had something to say about this (in a good, supportive way).

There are a ton of "newer" versions of C++. However, one thing about C++ is that it's evolving so there are some interesting things that are getting pulled into C++0x.

One thing that makes C++ useful is that no one company "owns" C++. If you have a company own a language then your priorities are less important that that company's, and you are in deep trouble if that company decides that that language is no longer important (Oracle and Java).

But I don't think this will ever get to academia in our lifetimes (they're still using FORTRAN 77... it's kind of ridiculous).

It's not.

There are *very* good reasons why Fortran 77 is an excellent language for scientific programming, and one thing that you need to do to be a good C++ programmer is to appreciate Fortran 77 and know when to write C++ that looks like Fortran 77.
 
  • #29
chroot said:
Actually, well-written Python spends almost all of its CPU time in highly-optimized library code, written in C++ and tuned for performance by people who are more capable than either you or I. This is one of the many reasons it is being adopted so widely for scientific computing.

You tune it for performance by building it on top of Fortran and C++ libraries that are highly tuned. The hard part in library design is not so much performance since in the end you will be running LAPACK. The hard part is to design the bricks so that you can both get the high performance and also have the flexibility to put the bricks together.

Also using a well designed library protects you if the hardware changes. For example, with the current CPU's, the name of the game is to keep your data on L1 cache and minimize access to main memory. However, with GPU's and grid computing, the game is different.

If you build things with building blocks then to some degree you can replace ATLAS with MAGMA, and get speedups with GPU.
 
  • #30
hadsed said:
It's not inconceivable that Microsoft could pick up D and make something of it.

I think it is inconceivable. Microsoft wants to rule the world. Building on a language that Google uses means that it has to share world domination.

Also language wars are extremely political because of money and power issues. If you have a Java-shop then people that know Java get the money and power. The politics turn out to be more important than technical issues, because if you have money and power behind a language, the technical issues will get fixed. NEWLANG may be better now, but if you put tens of billions of dollars behind OLDLANG, that can change.

They've come up with a lot of crap that isn't widely used (think J++)

J++ makes perfect sense if you think politically. J++ was intended to try to kill Java. If Microsoft didn't put out anything, then you would have developers create an alternative infrastructure for Java which Microsoft doesn't control (bad). If they put out something that was totally compatible with Sun's impelementation, then they have to share world domination.

So the solution is to put out a crappy implementation of Java, and then get people to use C#.

eventually C++ will need to be replaced. No one can say how long that will take, since if you look at FORTRAN...well, it's still around.

Great languages evolve. There are some really interesting things coming out of C++0x and boost. If you want to get your mind blown, take a look at boost::math. WOW!

Also, I don't know anyone that would think of writing code for a serious project in a brand new language, just because...

it will cost a ton of time and money.

Also no one writes any serious project in one language.

Sure it's cool to be that guy who's a total hacker physicist that's better in IT than most IT people (not saying a lot though I guess), but in the end you're wasting a lot of time screwing with memory management and all this other stuff when you could be doing physics.

Except that sometimes the core of what you are trying to do involves pushing the hardware at the limits. In the world of high-perfomance supercomputing, you *have* to care about memory management and cache access, because that makes the difference between code that takes three weeks to run, and something that can't get run at all.

I think that's one of the reasons FORTRAN is still around. Half of our time is spent fixing the damned computers when we could be running simulations or actually be doing physics and not have to be certified sysadmins.

It depends on what you mean by "doing physics." Pushing a supercomputer to its limits is "doing physics". If you are doing grid computing, then you have to learn a ton about the details of how networks work, which is pretty useful once you get your Ph.D.

Eventually, physicists (even computational ones) ought to be doing physics, and sure the way it's structured right now provides a good safety net for a lot of physicists/IT admins/software engineers, and that's good for them, but I don't think it's good for physics.

I think it is. If you are an expert at neutrino processes and an expert at C++, you can come up with cool and creative ideas that someone that knows *only* neutrino processes and C++ can't do.

It's kind of the reason why civilization came up with specialization in the first place, so things get done more efficiently.

But it doesn't work. One big problem with academia is that I think it's *too* specialized, which is one reason I got out of it. One problem with specialization is that it naturally leads to some pretty unhealthy social structures, in which the people that specialize in "getting money and power" end up with all of the money and power.

I'm a naturally curious person, and I get really annoyed when someone tells me what I should be curious about. Being curious about lots of different things turns out to be useful. I'm curious about neutrino processes. I'm curious about sociology. I'm curious about computer languages. It turns out that to do my job, I have to be proficient at all of this.
 
  • #31
Your choice of programming language doesn't matter since you are still relatively new to it. Pick any language and stick with it to the end. The reason I say this is because once you master a language then it is really easy to pick up other languages because it would just be a matter of learning new syntax and unique features. Don't worry about the speed of your first language either, it should not be a critical feature at this point. I would recommend python as it is very easy to use, with vast efficient API's written in C. If your code is slow in that language, then you can use a technique called profiling to find out which parts are slow and you can optimize these parts in C or fortran and interface to other languages with shared libraries (those .dll files on windows and .so files on linux). As long as your language gets the job done use it.

Edit: I noticed the original post asked nothing about which programming language to use, but somehow the discussion digressed that way. I agree with what one poster said about any programming experience is good experience. I would like to add that if you get frustrated with learning the language then by all means take a break. Often the solution comes up when you are more relaxed and if you don't then you have the opportunity to revisit problems with a fresh perspective.
 
Last edited:
  • #32
Birkeland said:
In general, just pick a language and work through it. While all languages have their differences, once you learn how to program in one language you can pick up any other language pretty quickly.

So just pick whatever you feel more comfortable with.

Fizex said:
Your choice of programming language doesn't matter since you are still relatively new to it. Pick any language and stick with it to the end. The reason I say this is because once you master a language then it is really easy to pick up other languages because it would just be a matter of learning new syntax and unique features. Don't worry about the speed of your first language either, it should not be a critical feature at this point. I would recommend python as it is very easy to use, with vast efficient API's written in C.

I would say I am most familiar and comfortable with MATLAB at the moment as there's still lots about C++ and Python that I don't know about. I would say I'm already familiar with most of its basic functions so when I've been doing some practice problems these past few days, the thing I'm learning the most about are the syntax to create plots.

twofish-quant said:
Download programs that other people have written and then add something to it. It's really important if you want to programing to be able to read other people's code. You might download something, and then find it *PAINFUL* to read and impossible to change, at which point you know what bad code looks like.

Most of the programs I've found online are either really complex to me (ie involve hundreds or thousands of lines of C++ code) or too simple. But I found this site to be the closest to my level
http://www.algarcia.org/nummeth/Programs2E.html
 
  • #33
Looking at others people's codes and trying to analyze and understand how it works is a waste of time in my opinion.

What you can do which should benefit you much more is to pick some field and write a code/program in connection.
For example, if you like semiconductor physics then you can write a code for bandstructure calculation and visualization/plotting.

Edit: if I'm not mistaken, computational physics books basically consist of tackling multiple mini projects/codes.
 
  • #34
If it matters for the discussion, every undegraduate physics major in my university must take a numerical analysis course in which they learn the basics of fortran 90 so as to write simple programs.
 
  • #35
I never really quite understood the point of FORTRAN 90. Why not just use C, or in the case of compatibility issues, just make calls to the old F77 code?
@twofish: I checked out boost::math. This seems like a much better library than what I've been trying to use (GNU Scientific Library). I will have to download it and play around with it.

I still don't know. I've always been told that C++ was rather thrown together and not really well managed as far as features go. I'm still something of a noob with it, but sometimes I catch glimpses of this, so I always imagined that something like D (or another more elegant/organized/easier language) would come along and make the world happy again.
physiker_192 said:
Looking at others people's codes and trying to analyze and understand how it works is a waste of time in my opinion.

What you can do which should benefit you much more is to pick some field and write a code/program in connection.
For example, if you like semiconductor physics then you can write a code for bandstructure calculation and visualization/plotting.

Edit: if I'm not mistaken, computational physics books basically consist of tackling multiple mini projects/codes.

Completely disagree. This is exactly how I learned how to program 8 years ago, and how I learned each programming language I know. Maybe I'm different, but I've never actually read a programming book and only half paid attention in the two classes I took for Java and MATLAB.

I don't know how useful it is to tell people how they should learn because programming can be rather difficult and abstract the first time you encounter it. What works for me or you might not work so well for someone else. I think it's more helpful to tell how you learn instead of telling people how they should learn.
 
Last edited:

Similar threads

Replies
2
Views
648
  • STEM Academic Advising
Replies
7
Views
920
  • STEM Academic Advising
Replies
3
Views
938
  • STEM Academic Advising
Replies
2
Views
632
Replies
11
Views
3K
  • STEM Academic Advising
Replies
3
Views
1K
Replies
9
Views
1K
Replies
16
Views
1K
Replies
7
Views
818
Replies
1
Views
872
Back
Top