ascot4fusion / ascot5

ASCOT5 is a high-performance orbit-following code for fusion plasma physics and engineering
https://ascot4fusion.github.io/ascot5/
GNU Lesser General Public License v3.0
21 stars 6 forks source link

rho5d distribution for bbnbi5 runs only populates the first theta bin #86

Closed Otsooni closed 4 months ago

Otsooni commented 4 months ago

rho5d distribution from bbnbi5 only populates the first theta bin, even though the bbnbi resulting markers would suggest other theta bins should also be populated.

Does the current bbnbi not support rho5d? If so, it would be nice to be implemented.

miekkasarki commented 4 months ago

In principle it should support all distributions. Maybe a good idea to check bbnbi.c is there a bug somewhere.

Otsooni commented 4 months ago

I confirmed that (obviously) the direct marker data from the bbnbi has theta variation, by creating a rho5d distribution on the python side from the markers. These are the integrated plots as rho- theta images: rho5d rho5d_python

Curiously, the total particle has is clearly not preserved. I wonder if that means the distribution is losing markers somewhere.

I did not find any obvious faults on the c code from either bbnbi.c or diag_rho5D, but I might just use the rest of the afternoon searching for them.

miekkasarki commented 4 months ago

My guess is that there is a conversion (between radians and degrees) error somewhere. You could try to increase the poloidal grid bin number to 120. If all the markers are still at the first bin, then we can rule this out.

Are the units same in both plots? The top plot shows "markers in bin / bin volume" and the bin volume in this case is 0.1 rho x 10 deg.

miekkasarki commented 4 months ago

Put a print in diag/dist_rho5d.c in the correct update function and check if theta=0 for all markers.

Otsooni commented 4 months ago

I increased the grid size to 180, and the problem persisted. Moreover, I assigned PHIMAX as 7, so we can safely rule it out.

Yes the units are same for both plots. Both should be particle per bin (and the bins are identical for both)

Otsooni commented 4 months ago

Put a print in diag/dist_rho5d.c in the correct update function and check if theta=0 for all markers.

Correct! Theta and consequently i_theta are zero.

miekkasarki commented 4 months ago

Exactly zero or close to zero?

Here theta is missing: https://github.com/ascot4fusion/ascot5/blob/a7bff59eacc587d617e9314ab4c492ee106e5eca/src/bbnbi5.c#L278 See here what fields should be initialized: https://github.com/ascot4fusion/ascot5/blob/a7bff59eacc587d617e9314ab4c492ee106e5eca/src/particle.h#L40

miekkasarki commented 4 months ago

You can check from particle.c how theta is computed.

Otsooni commented 4 months ago

Well, I only printed the first decimal, so everything said 0.0.

Otsooni commented 4 months ago

Exactly zero or close to zero?

Here theta is missing:

https://github.com/ascot4fusion/ascot5/blob/a7bff59eacc587d617e9314ab4c492ee106e5eca/src/bbnbi5.c#L278

See here what fields should be initialized: https://github.com/ascot4fusion/ascot5/blob/a7bff59eacc587d617e9314ab4c492ee106e5eca/src/particle.h#L40

Neither is rho initialized there. I'll put the theta calculation on the same place as rho

Otsooni commented 4 months ago

Never mind that, rho is needed during the simulation of course but not theta, I'll initialize it for collecting the distribution.

miekkasarki commented 4 months ago

One mystery still remains. That fixes the initial theta, but when the markers are pushed the integrator calculates Delta theta. Why that is not included? I mean if it were, then the printed theta would be small but not constantly zero.

miekkasarki commented 4 months ago

Could you put a print here as well https://github.com/ascot4fusion/ascot5/blob/a7bff59eacc587d617e9314ab4c492ee106e5eca/src/simulate/step/step_fo_vpa.c#L185

This is where theta is incremented when the marker is neutral.

Is RHS zero or does the code even reach this block?

Otsooni commented 4 months ago

Could you put a print here as well

https://github.com/ascot4fusion/ascot5/blob/a7bff59eacc587d617e9314ab4c492ee106e5eca/src/simulate/step/step_fo_vpa.c#L185

This is where theta is incremented when the marker is neutral.

Is RHS zero or does the code even reach this block?

I don't see that function even getting called directly in bbnbi

miekkasarki commented 4 months ago

True, I was wrong. It doesn't use that. This is where theta incrementation should be https://github.com/ascot4fusion/ascot5/blob/a7bff59eacc587d617e9314ab4c492ee106e5eca/src/bbnbi5.c#L387

Otsooni commented 4 months ago

I got it to work. I didn't feel the need to update the theta for every simulation step (as it's not used for anything, at least currently), so I just put it at the end. I'll create a branch or something and push it through.

miekkasarki commented 4 months ago

If it's not too much trouble, I'd suggest updating theta at every step. Right now it doesn't matter but it becomes relevant when someone wants to have the neutral density from beams.

Otsooni commented 4 months ago

Well, I decided to be nice and to implement it as you suggested. I just didn't initially like it due to lowering the performance. Preferably the theta would only be updated when it would be used for each time step.