C/++/# Open Source C# Physics Library

214
21
Summary
I've started work on a physics library in C#. I work professionally as a programmer and have a physics degree, however the idea of turning physics into code never really occurred to me. So far it's been fun. Project is open source. Join!
TL:DR;

I've started an open source C# physics library: University Physics

The Long Version

So it turns out there's relatively little in the way of full physics libraries written in C# (the best I can find is bepuphysics which seems to be focused only on classical mechanics). With this discovery as motivation, I've decided to start work on a C# library for performing as much general physics calculation as possible, in all sub-topics of physics.

So that's mechanics, electromagnetism, thermodynamics, quantum mechanics, fluid dynamics... anything involving turning the real world into equations and numbers. The library will be useful in basic games, simulations and as a basic learning tool for people interested in physics and programming via C#.

The aim is to simplify the work that is required to turn a set of physical values or items into a useful result. A personal example is in a physics simulator I created several years ago, in which a load of particles floated around the screen and repelled each other on close proximity with other particles.

For that simulator, I needed to write the code for electrostatic force myself from scratch. Via this new physics library, it would be as simple as:

C#:
ParticleSet particles = new ParticleSet
{
    Particles = new List<Particle>()
    {
        new Particle(mass: 1, position: new Vector(0,1,0), charge: 1),
        new Particle(mass: 1, position: new Vector(0,2,0), charge: 1),
        new Particle(mass: 1, position: new Vector(3,2,0), charge: 4),
        new Particle(mass: 1, position: new Vector(1,0,0), charge: 1),
    }
}

particleSet.Move(timeDelta: t);
So basically the user just creates what they want, sets it up and presses 'Go'.

At first I'm going to go through the classic University Physics textbook (Young & Freedman), turning as much of the content as possible into usable objects and classes. After that I'll move onto more advanced subjects, or just whatever takes my fancy as I go.

The primary aim at the moment is to just create a library of 'physics stuff'. Performance and optimisation will come later.

I'm setting up this thread for two purposes:

  1. To provide updates on progress
  2. To inform others about the project so they can join in if they wish

I've never worked on an open source project before, so this aspect is being entered with a mixture of interest and mild trepidation.

If you're interested in contributing to the project, reply to this thread or follow/fork the project on GitHub.

Project Repository is here: University Physics

Thanks :oldbiggrin:
 
10,937
4,448
Theres a couple of other libraries for Java:

- open source physics aka OSP at www.compadre.org
- processing IDE has several third party physical sim and particle systems libraries that are quite awesome

And theres the Gould Christian Tobochnik book with how to use OSP in a variety of situations.

You might get some inspiration and code ideas from them.

Have fun coding.
 
10,937
4,448
Also you should check out the Openstax University Physics book. Its free and is comparable to University Physics that you mentioned.

Openstax.org
 
214
21
Also you should check out the Openstax University Physics book. Its free and is comparable to University Physics that you mentioned.

Openstax.org
Thanks, looks like a good resource.

I already saw OSP before I created this post. Looks like it could prove useful for some stuff, though I must admit I'm more keen to do my work 'from scratch', for the sake of my own learning. OSP is certainly good to have available anyway. I'll see how things go :smile:
 
10,937
4,448
There some things in sims that you will trip over when trying to over optimize your code and so it’s always good to see how others approached it.
 
48
9
Check out Computational Physics by Jos Thijssen, it's the best book I could find on 'generic' computational physics. Others are either too simple or more complex but specialized on narrow subjects.

A lot of my projects on GitHub are started from ideas from that book: https://github.com/aromanro?tab=repositories They are described on my blog, here: https://compphys.go.ro/ The code is C++, though.
 

phinds

Science Advisor
Insights Author
Gold Member
15,388
5,032
So it turns out there's relatively little in the way of full physics libraries written in C#
Hardly surprising since it's a proprietary language and hardly used compared to the more popular languages.

from C#

C# ("C sharp") is Microsoft's attempt to create a C language that is purely object-oriented like Java. C# is proprietary to Microsoft and thus is not used on other platforms (Apple, Unix, etc). I don't know if it is still true, but an early comment about C# was "C# is sort of Java with reliability, productivity and security deleted".
 
214
21
Check out Computational Physics by Jos Thijssen, it's the best book I could find on 'generic' computational physics. Others are either too simple or more complex but specialized on narrow subjects.

A lot of my projects on GitHub are started from ideas from that book: https://github.com/aromanro?tab=repositories They are described on my blog, here: https://compphys.go.ro/ The code is C++, though.
Nice, you've got some pretty cool stuff there. I intend make a load of graphical sims after I've got the library done.
 
214
21
Hardly surprising since it's a proprietary language and hardly used compared to the more popular languages.

from C#
A very out of date opinion.

C# is number 5 in the TIOBE Index (it used to be 3rd!).

I follow programming trends and chatter much more closely than I follow physics. Among professional programmers, C# is generally considered to be "not quite as widely used but generally better than Java". It's cross-platform and works on Linux too.

I know it was hated and seen as a corporate invasion by Microsoft when it was released. That was a long time ago.
 
10,937
4,448
However, kotlin is considered a better than Java replacement that is rising in the ranks. Google chose it as the language of choice for Android. It’s less verbose than java, interoperates with Java and runs on the same JVM.


C#’s inability to run outside the windows ecosystem limits it for many cross platform applications though.

 
10,937
4,448
Okay lets get back to the thread topic.

Programmers choose languages based on the application development environment and for purposes of this thread the OP is filling a perceived need in the C# .Net world.

Hence C# is the language of choice.

Does anyone have some useful suggestions for important routines for this new library?

I know a convolution routine would be good and perhaps some interpolation routines for 1D and 2D arrays.
 
214
21
214
21
Project Update

Started work on some Thermodynamics classes.

It's all very simple stuff for now.

An energy converter:

C#:
EnergyConverter e = new EnergyConverter(500, EnergyMeasure.Joules);

// e.Joules = 500;
// e.ElectronVolts = 3.1208E21;
// e.MegaElectronVolts  = 3.1208E15
// e.KilowattHours  = 0.00013889;

A Temperature class:

C#:
Temperature temp = new Temperature(100, TemperatureType.Kelvin);

// temp.Kelvin  = 100;
// temp.Celsius = -173.15;
// temp.Fahrenheit = -279.67;

//// Can be instantiated with implicit conversion from double value (in Kelvin)

Temperature temp2 = 100;

// temp2.Kelvin  = 100;
// temp2.Celsius = -173.15;
// temp2.Fahrenheit = -279.67;

And a simple class of methods for performing basic substance calculations

C#:
// BasicThermodynamics.SubstanceEquations

//Based on pV = nRT

double volume = 100; // m^3
double n_moles = 12; // moles
Temperature temp = 350; // Kelvin

double pressure = BasicThermodynamics.SubstanceEquations.Pressure(volume, n_moles, temp);

// pressure = 349.208

// Also contains method for:
//
// Volume()
// n_Moles()
// Temperature()
// MolarMass()
// TotalTranslationalEnergy ()
// EnergyPerMolecule()
// RMS_Speed()
// MeanFreePath()

I was never too keen on thermodynamics so I probably won't do much more advanced than this for now.
 
214
21
Project Update

Added VectorField class, especially useful for simulating motion in things like fluid dynamics.

Simply create a system of particles (or any class derived from PhysicsObjectBase)

C#:
List<Particle> myParticles = new List<Particle();

Random rand = new Random();

VelocityField vField = FluidDynamics.CommonVelocityFields.RankineVortex;

for(int i = 0; i< 500; i++){
    
    int xPos = r.Next(0, 500);   
    int yPos = r.Next(0, 500);

    Vector pos = new Vector(xPos, yPos);

    Particle p = new Particle(mass: 5, position: pos);
    
    p.ApplyVelocityField(vField);
    
    myParticles.Add(p);
}
Then, for example, the system can be set to evolve with time.

C#:
while(simulationRunning){
    
    foreach(Particle p in myParticles){
        
        p.Move(timeDelta: t);
        //timeDelta would come from a timer/clock of some variety.
    }
    
    // Particles will move as dictated by their given velocity field.
}
I'm working on a visual representation of this now, to show how it can be used in graphical simulation.
 

Want to reply to this thread?

"Open Source C# Physics Library" You must log in or register to reply here.

Related Threads for: Open Source C# Physics Library

Replies
1
Views
2K
  • Posted
Replies
2
Views
1K
Replies
3
Views
6K
  • Posted
Replies
1
Views
2K
  • Posted
Replies
2
Views
2K
Replies
5
Views
2K
Replies
10
Views
2K
Replies
11
Views
728

Physics Forums Values

We Value Quality
• Topics based on mainstream science
• Proper English grammar and spelling
We Value Civility
• Positive and compassionate attitudes
• Patience while debating
We Value Productivity
• Disciplined to remain on-topic
• Recognition of own weaknesses
• Solo and co-op problem solving
Top