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

Collision Program

  1. Dec 10, 2008 #1
    During my school's one month January term I will be programming full time with a professor in the physics department at my school for credit. He is interested in creating a C++ program that deals with particle collisions in physics. I have talked with him about what he expects, and he explained that he wanted to start very simply so I can get the hang of it (Newtonian collisions). Once this is finished the goal is begin adding relativity and quantum mechanics to it. This is a project that has potential to continue after the one-month term. I believe I have a good understanding of the physics to start the project, but I'm worried about the programming. The professor has plenty of experience with Fortran, but not with C++, and I have only had one semester of C++ programming classes. I think I have a good grasp of the basics to create the program (classes, functions, arrays, etc.), but not how to correctly structure a large program. I have done all my programming with Microsoft visual studio creating win console applications. I have about three weeks until we start, and I willing to work to be ready for the programming. I realize this is a long post, and I'm grateful for any help you can give me. My questions are,

    Is a win console app the best approach for this project? (Should I learn something else?)

    Are there any websites/sources that show examples of source code for larger programs similar to this or the proper way to write a program like this?
  2. jcsd
  3. Dec 10, 2008 #2
    It depends on what is eventually required of the program.

    VC++ is OK for most experimental work, for prototypes, and even for graphics. It will lack speed if and when you need it. If you mentor will eventually want to do complex calculations that requires days of computation, VC++ may not be a good choice. If he wants to make a program to illustrate his ideas, or for prototypes, VC++ is probably as good as any. The last time I looked at VS graphics, it wasn't very fast.

    Other 'modern' languages are Java, Dev C++ (uses 3rd party graphics), VB.net.
    If graphics is not important, and quick and easy programming is expected, you can think of Perl or Python.

    In fact, there is a recent post of someone with a similar problem as yours and he used vPython as his language. You can find the post at:
  4. Dec 10, 2008 #3
    There's acres of code in Box2d which you can cannibalize for collisions involving Newtonian physics. CERN have similar stuff available for HEP physics, although it'll be fundamentally different to the Newtonian case.

    By the way, my estimation is that this isn't really a project that could be done in a single semester, particularly by someone with so little C++ experience. Simulating relativistic particle collisions with any accuracy is a seriously complex undertaking.
  5. Dec 10, 2008 #4
    Eh, what? Presumably you're using "VC++" to refer to Visual C++, which is simply a C++ IDE for Windows. Properly written C++ can be as fast as, and in some cases faster, than something like FORTRAN for numerical calculations.

    Again, what? C++ is the absolute last thing I'd use for quick prototyping of ideas. That's what Python + NumPy/SciPy, Matlab, or Mathematica are for.

    DevC++ is an IDE. It is not a language.

    Apologies if I've gotten the wrong end of the stick here but the suggestions you made were pretty bizarre.
  6. Dec 10, 2008 #5
    Thanks, I think this will be really helpful.

    I should have been clearer, from the way I understood it I'm just starting a project that will be ongoing. I think the reason that we are starting simply is so that the professor I will be working with has a chance to get used to C++ before more difficult things (that I don't know how to program) like relativity and Monte Carlo simulations are added. My goal is to finish the programming I know as quickly as possible so I have a chance to learn the more difficult areas.

    One more question. I have read that Fortran can used within C++ if the right header file is included, and this is done to speed up calculations. To me this sounds like it might be a good idea, especially because that is what my professor is used to. What are the drawbacks/advantages to this approach?
  7. Dec 10, 2008 #6
    I apologize if I have not made a difference between IDE and the language. To me the brand is very important since Microsoft introduced C++ in the .net suite. As the .net family of languages uses CLI (common language interface) I have written it off for efficiency.

    It is very true depending on how one defines properly written. The .net people will say that using pointers is not properly written, because it will bypass the CLI, but that's one of the few ways you get any speed out of Visual C++.

    I am not sure even properly written graphics applications on .net will run with much efficiency.

    I appreciate your clarifications and acknowledge your corrections.
  8. Dec 10, 2008 #7

    D H

    User Avatar
    Staff Emeritus
    Science Advisor

    Eh, what? The primary reason FORTRAN is still around is because of its speed. Properly written C++, where "properly written" means "in the spirit of object oriented programming" can be horrendously slow compared to C, which in turn is typically slower than FORTRAN.

    Seemingly simple calculations in C++ such as (integer expression)*(double expression) and (expression using member data) are painstakingly slow in unoptimized C++. Forget all about performance if you use STL objects in a nested loop. Optimization may help, but optimized code is a tad difficult to debug.
  9. Dec 10, 2008 #8
    C++ is the language that is based on C. When Microsoft created their .NET platform, they created many languages that used the Common Language Interface and were based on existing languages such as Java (J#), Visual Basic (VB.NET), and Visual C++ (they also created C#, a combination of Java and C++ combining the best of both). It is commonly mistaken that VC++ is just an IDE, in reality it is a language itself that runs on the Common Language Runtime.

    As far as speed goes, Visual C++ is based on the .NET platform, therefore it uses Just In Time compiling. The first time the code runs, it runs considerably slower then if you run it a second time, the reason being is that the first time you run it the CLR compiles it to native machine executable code from Microsoft Intermediate Language.

    All code ultimately has to be converted to machine executable code in order to be executed by the processor. In my opinion the real magic comes in during compile time optimizations. Well written assembly code will always run faster then C code. But very few assembly programmers can beat the highly complex optimization algorithms used in compilers today.

    If anyone wants to take a look at Visual C++: http://www.functionx.com/vcnet/

    So the question is, are you using Visual C++ or just using the Microsoft's Visual C++ compiler along with 'regular' C++? If former is the case, then I suggest that instead of using Visual C++ you use something like C#, as it's very much integrated within the runtime. Of course, this isn't the best choice for writing an application like yours because of as others have pointed out, speed.

    If latter is the case, then I think your primary issue will be handling the graphics. If you are planning on writing a fully fledged physics simulator with which the user could interact in a game like manner, you will need to learn one of the many graphics API. I suggest you start out with OpenGL, as it's very easy to learn and there are quite a few resources online. You will also need to learn the Win32 API, so that you can create Graphical User Interface applications. Console applications will only work if you want your simulator to do something like output a stream of data (telemetry).
    Last edited: Dec 10, 2008
  10. Dec 11, 2008 #9
    Two things:

    • No, in this context "properly written" does not mean "in the spirit of OOP". It means "adapted to the task at hand" where the task at hand is assumed to belong to some sort of scientific/technical domain.
    • As an example, I've dealt in the past with lattice QCD simulations for which unoptimized C++ code runs 10-15% faster than unoptimized F90. This is not as uncommon as to be remarkable.

    Indeed, so why bother? I generally trust my compilers and have never had a need to debug optimized code. The /O* flags are generally the last stage of any build process.
  11. Dec 17, 2008 #10


    User Avatar

Share this great discussion with others via Reddit, Google+, Twitter, or Facebook