1

I'm an applied physics student and currently working on a simulation of the magnetic interactions of multiple protons within a protein.

Me having only little experience with programming and almost none with OOP, there is a lot of information for me to process. Especially in terms on how to design my model in a way that is flexible and can be expanded easily later on.

So far, I've been working on my class diagram. My approach was to create a physObject class that implements different behaviour-interfaces. So in my case, all Protons will implement the MagneticBaheviour interface.

I've tried keeping my design as loosely coupled as possible for it to reusable. So later on someone might add i.e. a GravitationalBehaviour to simulate the gravitational behaviour of physical objects.

Model class diagram

So this is my current sketch of my class diagram. However I'm unsatisfied with some connections, and I'm not sure how to visualize my complex system in an uml diagramm. I.e. for the calculation of the ActingBField I'll need information on the distances to other physObjects. Also each time i.e. the location of a physObject changes, the ActingBField has to be calculated on every existing physObject again.

In addition, having an acting magnetic field on a physObject that has MagneticBehaviour would result in the call of BlochEquations, therefore resulting in a change of Magnetization, resulting in a new calculation of ActingBField. You get the idea.

So my system includes a lot of recursive patterns and the objects need to be able to pass needed information automatically when the value of an specific object changes.

My first approach was to use an Observer pattern, making my physObject an Observable and each Behaviour-interface being an Observer. However I also read posts to event-/ case-/ state-driven programming. This also sounds like it would fit my problem, but I dont know which one to apply for a simulation. I guess I could somehow define a call condition to some methods, I have no idea how to even visualize that design though.

Last but not least, all I've been doing so far is focus on the physical model of the simulation. However for this simulation to be usable, I figured I'll need to find a way to connect this model to some kind of user, that i.e. sets the time period of the simulation or selects which data to look at.

How would I visualize that using a uml diagram? What concepts do I need to consider or completely forgot about?

Im happy for any help in the right direction and I'm grateful for anyone that made it so far. Thanks for reading ^^

3
  • 1
    The most important concept you have forgotten about is YAGNI - "So later on someone might add i.e. a GravitationalBehaviour to simulate the gravitational behaviour of physical objects." Commented Nov 30, 2022 at 9:11
  • Scientific computing? Don't overcomplicate it. You probably aren't going to maintain this simulation for a long time adding zillions of new features, right? Commented Nov 30, 2022 at 18:43
  • btw you have to imagine (roughly) the program in your head and then draw the diagram based on your imagination of the program - not just draw a diagram that looks good, and then move onto a program. If you can't visualize the program then putting it on a diagram won't necessarily help Commented Nov 30, 2022 at 18:44

2 Answers 2

3

I would consider first principles before going down the rabbit-hole of OO modelling.

Best approach

It seems to me you have a room, a list of items in the room, and some values associated with each item (including a 3D position, so three numbers, and some sort of magnetisation value). You probably don't need to represent the room - just start with the list of items.

Now, for a small number of items, you can draw that easily enough on a blackboard. What are you trying to simulate? Is it the movement of each item in space under mutual magnetic forces? If we assume stepping forward in time by say 1 second, how would you calculate on paper the changes in positions due to magnetic forces? Write that algorithm down.

The computer can now be programmed to do these calculations for a larger number of items, and a larger number of steps of time, than you would wish to do by hand.

Other points on OOP

The primary industrial application of object-oriented programming, which is actually found effective, is to allow teams consisting of far more than one person (often thousands of people in different companies) to write interacting software components.

Twenty or thirty years ago, there was a lot of broader hype about how OOP was supposed to be about modelling the "real world". In reality, this approach most often just leads to simpleton models of the real world which aren't real at all, but also (paradoxically) extremely complicated and poorly integrated programs.

What you are trying to do with the computer is not to simulate the physical world, it is to automate a process of calculation, to produce a set of numbers that represent the state of the physical world at different moments in time.

Once you have the numbers calculated, they can be graphed and animated by the computer, in the same way that producing a graph on paper starts with calculating the numbers, and how a flip-book of graphs starts with drawing each individual graph.

4
  • What I'm trying to simulate a how the magnetization (vector) of a Proton changes when there are other Protons nearby. Each Proton having a magnetization and thus having an influence on the ActingBField (vector) of a selected Proton. If an physObject has a magnetization and an ActingBField the BlochEquations need to be called which calculates the new magnetizationof the Proton I've already written the algorithms to calculate this behaviour and I could just put thos in a for loop that goes through every timestep.
    – Meck3l
    Commented Nov 30, 2022 at 16:27
  • What I want to do though is to create a design that automates these calls and I'm not sure on how to do that or even visualize that in a class diagram. Would i implement this by connecting the objects by reference and checking wether the value changed or how would this work?
    – Meck3l
    Commented Nov 30, 2022 at 16:28
  • 1
    @Meck3l, my recommendation would be to just go with the for-loop. I don't fully understand the physics to which you refer, but the computer implementation shouldn't be radically different to what you do to calculate on paper. It's probable that you don't even need classes for this task - just arrays and structs. What you presumably want to end up with, is numeric data which describes the positions of each particle for each step in time, beginning with the initial conditions, and proceeding for so many time-steps as you wish the simulation to run. The results can then be graphed or animated.
    – Steve
    Commented Nov 30, 2022 at 17:06
  • "What you are trying to do with the computer is not to simulate the physical world, it is to automate a process of calculation, to produce a set of numbers that represent the state of the physical world at different moments in time." — this paragraph should be boldface in size 72pt font. Most physics equations I see don't model a real world object. Given some input, the equations produce a prediction of some change in state. That is the perspective you need when designing a system like this Commented Dec 2, 2022 at 3:36
0

So it has been a while since my question and since then I have adapted my design. So here is what I came up with:

class diagram

The particle properties are now clearer and all the calculations will happen in my PhysicsSystems. The PhysicsWorld will keep track of all the PhysicsSystems and run the update()-method once every timestep of the simulation.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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