Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Optimize C++ to use for computational physics?

  1. Feb 10, 2017 #1
    I'm studying 2nd year for a bachelor's degree in physics and I'm interested in programming (Beginner C++ programmer). I have already done simulations like harmonic motions, Ising model etc but I know this can go further and I need help with that. I use Ubuntu for programming with c++. (GCC compiler)

    1- So far I just have used GNUPLOT for drawing plots and exporting as .xpm pictures and joining them to make .gif files to demonstrate models like Ising. I searched for a great amount of time and I found out there are libraries like Qt, Octave, SDL,... but honestly I couldn't use any of them. Seems like they are suited for game programming and I need something more simple.

    2- What libraries can I use for more advanced mathematical functions like determinants, inverse matrices, integral calculations, solving ODEs etc so I don't have to write my own functions each time and I can simply use something like float a = det(B).

    3- What else should I do to optimize my programming for computational physics? I'm taking courses in computational physics soon and already started to read books.

  2. jcsd
  3. Feb 10, 2017 #2


    User Avatar

    Staff: Mentor

    Have a look at GSL https://www.gnu.org/software/gsl/

    What do you mean by optimizing? Making your program run faster?
  4. Feb 10, 2017 #3
    Thanks! Looks like just what I want.

    Things like not having to code everything, faster calculation, easier plotting, higher precision calculation. Whatever pro computational physicists do for a better result.
    In other words I don't want to start from inventing wheel and fire. :grin:
  5. Feb 10, 2017 #4


    User Avatar

    Staff: Mentor

    Then there is no simple answer that can be given in a post. You'll have to pick up a good book on computation physics or numerical methods.
  6. Feb 10, 2017 #5


    User Avatar
    Gold Member

    This goes into the programming realm, so you have to improve your programming skills (in general and in C++ that you use particularly), in order to write your programs in a more elegant and concise way. A good textbook on C++ and a lot of patience - as C++ is not a toy but has extreme capabilities, should do fine. Along the way, you will find good libraries that you can incorporate into your code, in the more efficient way. Although it's ultimately a hard thing to achieve, it can be done.

    Alternatively, if it's not mandatory for you to work your way through C++, you can use some mathematical package like Mathematica or Matlab. There are very decent counterparts in the open source world - like Octave, too.
  7. Feb 10, 2017 #6
    That'll come with experience. You just have to know what tools you need and that's not taught in school. Not only are there usually good tools for what you need, but there are often many of them. You'll end up with a list of tools that you can use for any given task and be able to identify the best one, or (sometimes) you'll have to write your own.
  8. Feb 11, 2017 #7
    For advanced visualization you have visit ( https://wci.llnl.gov/simulation/computer-codes/visit ), paraview ( http://www.paraview.org ), vtk ( http://www.vtk.org ). For simple plotting you can just use python matplotlib ( http://matplotlib.org ), matlab, mathematica, idl, octave, R, etc.

    for high performance you might want to use GPU acceleration through something like Cuda or OpenCL, or just a library which exploits it for you. You basically have many options https://en.wikipedia.org/wiki/Basic_Linear_Algebra_Subprograms#Implementations

    You also have GNU or Boost Multi-precision libraries for the case where you need more precision than double or long double. Although I understand that methodology is even more important than datatype (you'll need a lot of knowledge about numerical methods.

    Since C++ is tedious to program, you will want to use a scripting language sometimes when you need compute some quick result.
    Last edited: Feb 11, 2017
  9. Feb 12, 2017 #8
    You'll also probably learn that long calculations you will tend to do in two parts: baking and rendering.

    Baking does all of the calculations and stores the results. You usually try not to render this at the same time since rendering itself is slow. You want to make sure that the end result is correct before committing all of that horsepower drawing.
  10. Feb 19, 2017 #9


    User Avatar
    Science Advisor
    Gold Member
    2017 Award

    I completely agree. One process does the physics calculations and dumps accurate, detailed data into files (or puts the data on a network if the process is "real-time"). Then you can have a multitude of possibilities for post-processing, analyzing, and displaying.
  11. Feb 22, 2017 #10
    Since you are a beginner, the best you can do is to rely on libraries (made by experts) and slowly improve your coding skills / algorithms.
    But you are actually doing that, so it's already OK. Just don't be impatient. At this point any 'advanced' method usually related with 'performance optimization' would be most likely just waste of time.
  12. Mar 2, 2017 #11
    Using a linear algebra library like armadillo could be a good start (uses lapack). Then you also have the nice feature of various prebuilt matrix structures and classes. Although it is fun to play with the nitty-gritty low level stuff yourself as well.
    One handy tips is to use optimization levels during compilation (O3 option for example). Another tips is to try and implement different solutions and time them, what you think is intuitively fastest isn't always.
  13. Mar 4, 2017 #12
    You can optimize certain common functions to provide faster estimates.

    For example, I often create an array of 360 floats and precalculate sin and cos functions. A lookup in ram is much faster than actually doing a sinf() and for my purposes, being off by a half a degree doesn't matter.

    There is also tricks like this well known example
    Code (Text):
    float Q_rsqrt( float number )
       long i;
       float x2, y;
       const float threehalfs = 1.5F;

       x2 = number * 0.5F;
       y  = number;
       i  = * ( long * ) &y;                       // evil floating point bit level hacking
       i  = 0x5f3759df - ( i >> 1 );               // what the ****?
       y  = * ( float * ) &i;
       y  = y * ( threehalfs - ( x2 * y * y ) );   // 1st iteration
    //    y  = y * ( threehalfs - ( x2 * y * y ) );   // 2nd iteration, this can be removed

       return y;
    Which approximates an inverse square root. (Everyone I know leaves Carmack's original comments in there too.

    This is code that gets used by me a LOT. Any time I need to determine distance.
    Code (Text):
    static float fastnorm(float diffx, float diffy){
            //need absolute values for distance measurement
            diffx = fabs(diffx); diffy = fabs(diffy);
            //There are two simple approximations to sqrt(x^2 + y^2):
            //  max(x, y) -> good for x >> y or x << y          (square-shaped unit circle)
            //  1/sqrt(2) * (x + y) -> good for x = y (approx.) (diamond-shaped unit circle)
            //The worse approximation is always bigger. By taking the maximum of both,
            //we get an octagon-shaped upper approximation of the unit circle. The
            //approximation can be refined by a prefactor (called a) below.
            static const float a = (1 + sqrt(4 - 2 * sqrt(2))) / 2;
            static const float b = sqrt(0.5);
            const float metricOne = diffx > diffy ? diffx : diffy;
            const float metricTwo = b * (diffx + diffy);
            const float bigger = metricOne > metricTwo ? metricOne : metricTwo;
            return a * bigger;

        inline static float regnorm(float diffx, float diffy){
            return sqrt(diffx * diffx + diffy * diffy);
    Last edited: Mar 4, 2017
  14. Mar 4, 2017 #13
    Use libraries that are developed by people dedicated to producing the best code for some goal they/you are interested in.
    Much of that is open source, and one thing that C++ is really good at is incorporating external functions into C++ objects.
    Inventing a better wheel is pointless, when you already have very good wheels.
  15. Mar 11, 2017 #14


    User Avatar
    Gold Member

    I don't know, I think sometimes it's good to write your own functions because it allows you to get a feeling about how the algorithm works [and so the limitations of the algorithm]. If you did computational physics you might have seen such examples in the integration parts...
    Of course, writing a code more than once is retarded (not optimal), and for that reason you can always write the classes that contain those methods. That way you will also be able to determine how optimal your code is, in terms of time performance; by e.g. comparing to an existing code that does the same thing [or even sit over your code and think how you can make it run faster]... So for example the "divide and conquer" algorithmic thinking comes after you realize that just looping over all your input is causing extra, unnecessary calculations... also is the speed the only thing you are looking for? or precision as well? those two are the two opposite sides most of the time [at least for my experience]... all these are helpful for you in the beginning to build up your algorithmic thinking [afterall as a physicist you'll end up spending more time writing a code than expecting the results]...
    Also C++ is more like a DIY project... It's like you need a chair, and C++ comes and gives you the raw material and tools you need for it, but you should make it yourself... in the end you'll end up with a chair that is optimal for you, since you know exactly how you wanted it to be... other languages provide better solutions [eg giving you a bunch of chairs they have siting around]. It's a point where python owns C++ [although I'm subjective and think it owns it in all points].
  16. Mar 12, 2017 #15
    Learning how to properly use tools like gprof and valgrind are probably the most important when it comes to optimizing your code. They show you exactly where the slow parts are.
  17. Mar 13, 2017 #16
    At beginner phase the gain from the low level performance optimization is usually lower than from the algorithm level.
    Save these for the time when the coding skills are already established.
    No amount of gprof can save a weak algorithm.
  18. Mar 21, 2017 #17

    Please listen to me. I work for years in computer optimisation. Use Eigen3 for matrix computing, it is a ultra optimised library but with an high level of programming due to it's meta-template building. Everyone in intensive computing use MKL, direct Blas on Eigen. Eigen is best for simplicity and performance.

    For C++ Optimisation you need to check the flag (-O3) of G++, and if you want you can use intel compiler to save 10% more and profile you executable.
  19. Dec 4, 2017 #18
    I know this reply comes quite late, but maybe it's still useful. I have a computational physics blog, here: https://compphys.go.ro
    I'm using among other libraries, Eigen (which was mentioned above) for matrix-related things, VTK for visualization (but also some code written by me), FFTW for Fourier Transforms, to name a few.

    Most of the posts come with a C++ project attached (the repositories are here: https://github.com/aromanro?tab=repositories ) but there is also some JavaScript code in there for the easier ones. For some of the projects I write prototypes with some scripting language (like Octave or python) but I do not put that code online.
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook

Have something to add?
Draft saved Draft deleted

Similar Discussions: Optimize C++ to use for computational physics?
  1. C++ Optimize a function (Replies: 11)