1
$\begingroup$

OK, I know a bit of C++ (very basic syntax), and I want to do physics simulation in C++, like stuff like (also the things mentioned here):

  • Ripples and waves over a 2-d surface
  • Vibrating string/membrane
  • Various electric/magnetic fields (as per my wish) in 2d/3d
  • Gas molecules simulation
  • Newtonian mechanics stuff (rotating objects, Newtonian gravity etc)

Can anybody suggest some sources/libraries (or what that's called, something like vpython for python) so where I can learn so that I can simulate such stuff on my PC?

Note that I'm NOT asking for books for doing it (though recommendations are welcome)

$\endgroup$
  • $\begingroup$ (BTW, I asked in in Physics SE and there people suggested me to post it here too) $\endgroup$ – Chebotarev Density Mar 22 '18 at 6:42
  • $\begingroup$ I would suggest deal.II . It is very well documented (also has tutorials videos) and has most of the examples that you want. It has a steep learning curve (C++ wise) but opens you up to possibilities of extending the physics beyond your basic toy problems. $\endgroup$ – Mathnoob Mar 22 '18 at 10:07
  • 1
    $\begingroup$ I would say deal.II is a bad idea given that questioner has only very limited knowledge of C++. (Though, deal.II is a good library.) This library makes extensive use of advanced C++ features. I am not entirely sure what the question is all about. The library vpython is a visualization library. Are you looking for a visualization library in C++? Are you looking for numerical libraries for the simulation of physical systems? Otherwise, I would suggest that you get yourself a good C++ book and a book about the numerical simulations of ODEs and PDEs. $\endgroup$ – H. Rittich Mar 22 '18 at 12:40
  • $\begingroup$ @Mathnoob Thanks for the recommendation, but unfortunately its way too difficult for me. Do you know something more basic but for the same purpose ? $\endgroup$ – Chebotarev Density Mar 22 '18 at 15:52
  • $\begingroup$ @H.Rittich Sorry if it was not clear, I don't have any idea how to begin with simulating stuff. For a very concrete example: what are steps in order the things I should do to be able to simulate this $\endgroup$ – Chebotarev Density Mar 22 '18 at 15:54
5
$\begingroup$

I think you are missing a very important and crucial step that lies exactly between the physics and simulation: the mathematical model.

In order to model any physics, one has to formulate the mathematical description of the physical phenomenon. Depending on the goals of the simulation, different approximations and assumptions can be made resulting in various complexity of the systems and requiring more or less complicated numerical techniques to solve them.

You mentioned several very different areas of physics that you are interesting simulating, so, I guess, your interest lies more in the numerical methods/visualization rather than in any particular field. I will take electromagnetics (EM) as an example:

  • the full glory of EM is covered by Maxwell's Equations
  • various forms of which (and approximations) can be solved in 1D, 2D, and 3D.
  • sometimes it is appropriate to use surface discretization (PEC - perfect electric conductor), sometimes you have to use volume discretization
  • different properties of materials (nonlinearity, anisotropy, etc) might need to be taken into account.

What I am trying to say, is that to simulate any physics you first have to understand (to some extent) the mathematical model behind it.

Next, usually, you need to solve this model using some numerical technique. Depending on the model, different techniques can be applicable and preferable. Again, I will use the ones that are commonly used for EM, but other fields will be similar:

  • finite-difference (finite-difference time-domain)
  • finite-element method (FEM)
  • integral-equation methods (boundary element)
  • physical/geometrical optics

...etc. Different libraries (including C++ ones) can be used to solve those problems: deal.ii, FEniCS...

However, my advice would be to start with something simpler and use a more appropriate language/platform to begin with numerical simulation of the physical phenomenon. Say, starting solving wave-equation in 1-D using Matlab (Octave) and visualizing simple propagation. Also using numerous already available toolboxes for PDE, ODE, and particular areas of physics should be easier there.

I would also suggest a book/course that has nothing to do with C++, but was my "bible" in computational science:

$\endgroup$
  • $\begingroup$ Thanks for your answer ! I'm a high school senior, so I have moderately good proficiency in high school math and physics. So for a concrete example, to be able to simulate this, what steps should I follow ? Also, thanks for the books, they definitely look very interesting. $\endgroup$ – Chebotarev Density Mar 22 '18 at 15:57
  • $\begingroup$ Tough question. The simulations you are referring to are in 2D and already require some background. I would still start from 1-D wave equation. Maybe, trying to understand how finite-differences work in a very basic scenario. I also found this not too bad. I bet, other implementation also exists. Again, if I would not dive into something looking too fancy and complicated, but start slowly. $\endgroup$ – Anton Menshov Mar 23 '18 at 4:13
  • $\begingroup$ OK, so for something basic and non-interactive like 1d wave going on a string (but on C++), can you suggest something similar ? (Yesterday I was on mobile, and today I checked the two books - but they're graduate texts and I don't even know basic undergrad first year physics - and they're mentioning "Bose-Einstien" and "Laplacian transformation" which idk :| Could you suggest some more basics books ?) $\endgroup$ – Chebotarev Density Mar 23 '18 at 8:01
  • 1
    $\begingroup$ @AlexKChen, I would suggest that you start with a simpler model, e.g., the vibration of a mass attached to a string. I strongly suggest this article: wired.com/2013/12/numerical-calculations-can-seem-like-magic $\endgroup$ – nicoguaro Mar 24 '18 at 2:51
  • $\begingroup$ @nicoguaro, that's actually very good material in my opinion! $\endgroup$ – Anton Menshov Mar 24 '18 at 2:55
3
$\begingroup$

Fenics may be one of the simplest free software to use to simulate wave equations and other differential equations using finite element solutions. It runs with a python interface with C++ underneath for performance, which makes it a bit user-friendlier than a pure C++ solution.

Gas molecule simulations are commonly studied using molecular dynamics (crudely, integrating Newton's equations of motion). Depending on how advanced of realistic of particle interactions there are quite a few choices of simulators to choose from, such as HOOMD, LAAMPS, GROMACS, CHARMM, AMBER, NAMD... Each project probably comes with a recommended visualization. I recall at least LAAMPS is pretty friendly with VMD (GUI), and HOOMD and LAAMPS I believe both work with Ovito. Many of these also are python script interfaces over a C++ library of some kind. The technical barrier to entry here may also be lower than for the finite-element simulations as well.

$\endgroup$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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