Need advice, doing a lot of calculations. Sockets? Multiprocessing?

I once made simulation that calculated planets temperature for every polygon depending on distance to sun, heat emission, heat absorption and other properties. But it is rather limited in size. So I’ve been thinking of ways how to improve calculation time. Splitting calculations into smaller chunks per tick, is a good choice but it would increase overall time.

Multiprocessing seems to be a good way to solve that. But BGE doesn’t work hand in hand with multiprocessing.

So I thought to make separate program that runs in background, receives needed data (lists of numbers, nothing blender specific) and does all calculations using multiprocessing. This program should communicate to BGE via sockets.

Would this be a good solution? Would it give improvement in time? Is sending data via sockets fast enough? Is there any better way?

I don’t know how to work with sockets, yet. So I’m wondering is it worth spending time to learn it.

what is your gpu?

Can you code openGL?

I think you may be able to use the color value+light value?

I am 100% uncertain however

My CPU is way better than GPU.

I can’t code in openGL, and I’m not sure if color values is enough, I’m not physicist, but if I manage I’ll make more related simulations :slight_smile:

If you can do the color calculation in a shader, that would be a simple way to parallelize it. Otherwise, what is your current performance like? Do you actually need to optimize yet?

Performance depend on polycount, so i can make need for optimization.

Is it possible via openGL to get amount of light is striking polygon event if that polygon is behind something? That would very useful.

How about my own solution about background program? Is it worth a try? I’ve done that simulation just for fun, so ugly solution like that would be ok.

A typical MVC can help you here.

M = Model
A system that calculates the simulated data (often called business logic). The best is you implement this independent from the BGE. This way you can use any implementation you want, even distributed processing or processing on several hardware entities (see cloud computing).

V = View
A system that presents the results to a viewer.
This can be anything. I guess you prefer to use the BGE.
Finally you can have different implementations. The view can but does not need to be part of the same process that processes the model. This parts need to interact with each other:
The view gets data from the model to be displayed

C = Controller
[not to be confused with the logic brick]
The controller interacts with the user and provides feedback to the view and model. It typically works in conjunction with the view. So I suggest you implement it into the same process as the view (bge).

The advantage:
You can have several different implementations of the View and Controller interacting with the same model.
You can have any number of Views and Controllers as you like.

Why do you need to do this real-time? Why do you need to do it for the invisible pixels/polygons?

So I could change any parameters at ay time.

So I could view planet at any angle and see how temperature values changed. And since energy accumulate over time, calculation for every polygon is necessary or I’m missing here something.

Any way that simulation is done just for fun, so there is no real need for anything, its just how i want it to be.

It would be useful to elaborate on “a lot of data”. Sockets are a useful means of transferring data, and on local computers don’t even go through the IP stack, so they’re quite fast. Because of this feature, they should not face the same throttling problems that can plague IP connections.

Still, you have many other choices - threads with queues, shaders etc. Best describe what the simulation requires.

Regards, Angus.

The way I currently have it I need two main things from BGE: distance from sun object to planet object and normal from every polygon, it is used to calculate at what angle polygon is facing toward sun object. All of that is used to do calculations for every polygon. There a bit more to that but it just numbers.

So after all calculation BGE should receive temperature values for each polygon for next calculation cycle, and to change polygon color accordingly.

The thing you are simulating can be simulated through a mathematical model rather than through experimental/numeric methods. I imagine there are plenty of studies done on the flow of heat through solid bodies, so you should be able to build an algorithm to calculate the heat based on the co-ordinates on the sphere rather than the distance and normal. This should make things run a lot faster.

If you still want to do it based on yoru collection method, I can see no way to optimize other than perhaps splitting the work over multiple frames (ie do some of the polygons this frame, some more the next frame, the rest the third frame, then back to the first lot)

If it’s ok to treat planets as perfect spheres, then yes, that would probably be the best approach.

I’d probably go with the mathematical spherical testing approach as well, as it seems like that would be the fastest method. It seems like it would be pretty accurate to real life (as for all intents and purposes, despite the varying shape of the planet, Earth’s still pretty much just a sphere). Alternatively,

  1. Test every nth polygon instead of each one, and interpolate the values between those tested polygons. It’d be harder to code and deal with, but it might be faster overall.

  2. Test only polygons that are visible or perhaps even just ones that are clicked on / interacted with in some stage.

If it’s a matter of you needing the data to be “dynamic” because the sun in the simulation moves, then update the polygon data every so often, like every hour of in-sim time.

I’ve noticed that typically trying to optimise by interpolating can lead to slower results dye to the time taken to handle the additional complexity. (very general statement here). Often optimisation Is not so clear cut.

Also, the earth is actually quite ‘bumpy’, funnily enough :slight_smile:

Thank you all for suggestions, I think I’m gonna try out splitting calculation into several logic ticks and going multiprocessing way with BGE, since that appears to be doable. Learning to code in openGL or studying mathematical model might be better solutions but that would take a lot of time to learn/figure out.