C++ Collision Program for Physics Project

In summary, the student is worried about programming the project and has minimal experience with C++. The student is willing to work hard to be ready for the programming, but is worried about the language and approach. There are many languages that could be used for this project, but the student is leaning towards using Python or Perl.
  • #1
DukeLuke
94
0
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?
 
Technology news on Phys.org
  • #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:
https://www.physicsforums.com/showthread.php?t=275996
 
  • #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.
 
  • #4
mathmate said:
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.

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.
mathmate said:
If he wants to make a program to illustrate his ideas, or for prototypes, VC++ is probably as good as any.

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.
mathmate said:
Other 'modern' languages are Java, Dev C++ (uses 3rd party graphics), VB.net.

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.
 
  • #5
shoehorn said:
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.

Thanks, I think this will be really helpful.

shoehorn said:
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.

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?
 
  • #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.

Properly written C++ can be as fast as, and in some cases faster, than something like FORTRAN for numerical calculations.

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.
 
  • #7
shoehorn said:
Eh, what? Properly written C++ can be as fast as, and in some cases faster, than something like FORTRAN for numerical calculations.
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.
 
  • #8
C++ is the language that is based on C. When Microsoft created their .NET platform, they created many languages that used the Common Langauge 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 Langauge 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:
  • #9
D H said:
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.

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.

DH said:
Optimization may help, but optimized code is a tad difficult to debug.

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.
 
  • #10

1. What is the purpose of a C++ Collision Program for a Physics Project?

The purpose of a C++ Collision Program for a Physics Project is to simulate and analyze collisions between objects in a virtual environment. This program can be used to study the laws of physics and their applications in real-world scenarios.

2. How does the C++ Collision Program handle collisions between objects?

The C++ Collision Program uses algorithms and mathematical equations to calculate the forces and velocities of objects involved in a collision. It takes into account factors such as mass, velocity, and angle of impact to determine the outcome of the collision.

3. Can the C++ Collision Program be used for both 2D and 3D collisions?

Yes, the C++ Collision Program can be used for both 2D and 3D collisions. It uses vector mathematics to calculate the movements of objects in three-dimensional space, making it suitable for a variety of physics projects.

4. Is the C++ Collision Program user-friendly for beginners?

The C++ Collision Program may be challenging for beginners as it requires a basic understanding of C++ programming and physics concepts. However, there are tutorials and resources available online to help beginners learn how to use the program.

5. Can the C++ Collision Program be customized for specific projects?

Yes, the C++ Collision Program can be customized for specific projects by adjusting the equations and parameters used for calculating collisions. This allows for a more accurate simulation of real-world scenarios and can be useful for advanced physics research projects.

Similar threads

  • Programming and Computer Science
Replies
16
Views
1K
  • Programming and Computer Science
Replies
1
Views
705
  • Programming and Computer Science
Replies
7
Views
332
  • Programming and Computer Science
Replies
8
Views
832
  • Programming and Computer Science
Replies
5
Views
954
  • Programming and Computer Science
Replies
3
Views
633
  • Programming and Computer Science
Replies
15
Views
1K
  • Programming and Computer Science
2
Replies
54
Views
3K
  • Programming and Computer Science
Replies
22
Views
9K
  • Programming and Computer Science
Replies
11
Views
2K
Back
Top