ASCOT5
Loading...
Searching...
No Matches
Functions
particle.c File Reference

Marker structs and conversions between them. More...

#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include "ascot5.h"
#include "error.h"
#include "consts.h"
#include "physlib.h"
#include "gctransform.h"
#include "particle.h"
#include "B_field.h"
#include "E_field.h"

Go to the source code of this file.

Functions

void particle_allocate_fo (particle_simd_fo *p_fo, int nmrk)
 Allocates struct representing particle markers.
 
void particle_to_fo_dummy (particle_simd_fo *p_fo, int j)
 Makes a dummy FO simulation marker.
 
void particle_to_gc_dummy (particle_simd_gc *p_gc, int j)
 Makes a dummy GC simulation marker.
 
void particle_to_ml_dummy (particle_simd_ml *p_ml, int j)
 Makes a dummy ML simulation marker.
 
int particle_cycle_fo (particle_queue *q, particle_simd_fo *p, B_field_data *Bdata, int *cycle)
 Replace finished FO markers with new ones or dummies.
 
int particle_cycle_gc (particle_queue *q, particle_simd_gc *p, B_field_data *Bdata, int *cycle)
 Replace finished GC markers with new ones or dummies.
 
int particle_cycle_ml (particle_queue *q, particle_simd_ml *p, B_field_data *Bdata, int *cycle)
 Replace finished ML markers with new ones or dummies.
 
void particle_input_to_state (input_particle *p, particle_state *ps, B_field_data *Bdata)
 Converts input marker to a marker state.
 
a5err particle_state_to_fo (particle_state *p, int i, particle_simd_fo *p_fo, int j, B_field_data *Bdata)
 Convert state into a FO SIMD marker.
 
void particle_fo_to_state (particle_simd_fo *p_fo, int j, particle_state *p, B_field_data *Bdata)
 Convert FO to state.
 
a5err particle_state_to_gc (particle_state *p, int i, particle_simd_gc *p_gc, int j, B_field_data *Bdata)
 Convert state into a GC SIMD marker.
 
void particle_gc_to_state (particle_simd_gc *p_gc, int j, particle_state *p, B_field_data *Bdata)
 Convert GC to state.
 
a5err particle_state_to_ml (particle_state *p, int i, particle_simd_ml *p_ml, int j, B_field_data *Bdata)
 Convert state to a ML SIMD marker.
 
void particle_ml_to_state (particle_simd_ml *p_ml, int j, particle_state *p, B_field_data *Bdata)
 Convert ML to state.
 
int particle_fo_to_gc (particle_simd_fo *p_fo, int j, particle_simd_gc *p_gc, B_field_data *Bdata)
 Convert FO struct into a GC struct.
 
void particle_copy_fo (particle_simd_fo *p1, int i, particle_simd_fo *p2, int j)
 Copy FO struct.
 
void particle_copy_gc (particle_simd_gc *p1, int i, particle_simd_gc *p2, int j)
 Copy GC struct.
 
void particle_copy_ml (particle_simd_ml *p1, int i, particle_simd_ml *p2, int j)
 Copy ML struct.
 
a5err particle_input_p_to_state (particle *p, particle_state *ps, B_field_data *Bdata)
 Convert an input particle marker to particle state.
 
a5err particle_input_gc_to_state (particle_gc *p, particle_state *ps, B_field_data *Bdata)
 Convert an input guiding center marker to particle state.
 
a5err particle_input_ml_to_state (particle_ml *p, particle_state *ps, B_field_data *Bdata)
 Convert an input field line marker to particle state.
 
void particle_offload_fo (particle_simd_fo *p)
 Offload particle struct to GPU.
 

Detailed Description

Marker structs and conversions between them.

ASCOT5 is a code that simulates markers.

This file contains functions to generate dummy markers, fetching markers from the marker queue, and handles conversions between various marker structs.

The relationship between the seven different marker structs is:

particle >–+ +--> particle_simd_fo | | particle_gc >–particle_state--> particle_simd_gc | | particle_ml >–+ +--> particle_simd_ml

The structs on the left are input structs which are constructed when marker data is read. These structs must be independend of any other input data. A consistent representation of markers is provided by particle_state which can be constructed from any type of input marker.

Particle state can be used to obtain any information from marker physical quantities such as guiding center energy, which requires one knows both guiding center magnetic moment and local magnetic field value. All markers are represented by particle state when simulation begins and again when simulation ends.

During simulation, markers are represented by a SIMD compatible structure (all fields in these structs are NSIMD long arrays representing NSIMD markers), and the type of SIMD structure depends on the simulation type: whether simulation models markers as particles, guiding centers or field lines. Each simulation struct can be constructed from particle state.

So in principle, one can have any type of marker input and still use any kind of simulation method, e.g. input can be guiding centers but these can be modelled as particles, or to be precise, the particles are modelled whose guiding centers are given as inputs.

Exception to this rule: field line input can only be used to simulate field lines as field lines have no mass.

If one is to implement a new simulation mode, e.g. particle simulation in Boozer coordinates, one is advised to implement a new marker struct for it.

Definition in file particle.c.

Function Documentation

◆ particle_allocate_fo()

void particle_allocate_fo ( particle_simd_fo * p_fo,
int nmrk )

Allocates struct representing particle markers.

Size used for memory allocation is NSIMD for CPU run and the total number of particles for GPU.

Parameters
p_fomarker struct to allocate
nmrkthe number of markers that the struct represents

Definition at line 69 of file particle.c.

◆ particle_to_fo_dummy()

void particle_to_fo_dummy ( particle_simd_fo * p_fo,
int j )

Makes a dummy FO simulation marker.

A dummy marker is a marker whose fields have possible but unrealistic values. It is intended to act as a filler for SIMD arrays that would otherwise be left uninitialized.

A dummy marker has negative one (-1) ID and negative one (-1) queue index, and it is not running (0). Dummy marker should be ignored when encountered during the simulation.

Parameters
p_fopointer to particle_simd_fo array where dummy is placed
jindex of the new dummy in the SIMD arrays

Definition at line 132 of file particle.c.

◆ particle_to_gc_dummy()

void particle_to_gc_dummy ( particle_simd_gc * p_gc,
int j )

Makes a dummy GC simulation marker.

A dummy marker is a marker whose fields have possible but unrealistic values. It is intended to act as a filler for SIMD arrays that would otherwise be left uninitialized.

A dummy marker has negative one (-1) ID and negative one (-1) queue index, and it is not running (0). Dummy marker should be ignored when encountered during the simulation.

Parameters
p_gcpointer to particle_simd_gc array where dummy is placed
jindex of the new dummy in the SIMD arrays

Definition at line 172 of file particle.c.

◆ particle_to_ml_dummy()

void particle_to_ml_dummy ( particle_simd_ml * p_ml,
int j )

Makes a dummy ML simulation marker.

A dummy marker is a marker whose fields have possible but unrealistic values. It is intended to act as a filler for SIMD arrays that would otherwise be left uninitialized.

A dummy marker has negative one (-1) ID and negative one (-1) queue index, and it is not running (0). Dummy marker should be ignored when encountered during the simulation.

Parameters
p_mlpointer to particle_simd_ml array where dummy is placed
jindex of the new dummy in the SIMD arrays

Definition at line 218 of file particle.c.

◆ particle_cycle_fo()

int particle_cycle_fo ( particle_queue * q,
particle_simd_fo * p,
B_field_data * Bdata,
int * cycle )

Replace finished FO markers with new ones or dummies.

A marker has finished simulation when marker.running = 0. If queue has unsimulated markers, one is picked to replace the finished marker. If not, a dummy marker is used as a replacement instead. Finished marker is converted to marker state and stored in the queue.

This function updates queue.next and queue.finished fields when a marker has finished simulation. This is done thread-safe.

This function returns values indicating what was done for each marker in a SIMD array: 0 : Nothing -1 : Finished marker replaced with a dummy (queue is empty) 1 : Finished marker replaced with a fresh one

Parameters
qpointer to marker queue
ppointer to SIMD structure of markers
Bdatapointer to magnetic field data
cyclepointer to integer array where what was done for each marker is stored
Returns
Number of markers within the SIMD structure that are still running

Definition at line 268 of file particle.c.

◆ particle_cycle_gc()

int particle_cycle_gc ( particle_queue * q,
particle_simd_gc * p,
B_field_data * Bdata,
int * cycle )

Replace finished GC markers with new ones or dummies.

A marker has finished simulation when marker.running = 0. If queue has unsimulated markers, one is picked to replace the finished marker. If not, a dummy marker is used as a replacement instead. Finished marker is converted to marker state and stored in the queue.

This function updates queue.next and queue.finished fields when a marker has finished simulation. This is done thread-safe.

This function returns values indicating what was done for each marker in a SIMD array: 0 : Nothing -1 : Finished marker replaced with a dummy (queue is empty) 1 : Finished marker replaced with a fresh one

Parameters
qpointer to marker queue
ppointer to SIMD structure of markers
Bdatapointer to magnetic field data
cyclepointer to integer array where what was done for each marker is stored
Returns
Number of markers within the SIMD structure that are still running

Definition at line 367 of file particle.c.

◆ particle_cycle_ml()

int particle_cycle_ml ( particle_queue * q,
particle_simd_ml * p,
B_field_data * Bdata,
int * cycle )

Replace finished ML markers with new ones or dummies.

A marker has finished simulation when marker.running = 0. If queue has unsimulated markers, one is picked to replace the finished marker. If not, a dummy marker is used as a replacement instead. Finished marker is converted to marker state and stored in the queue.

This function updates queue.next and queue.finished fields when a marker has finished simulation. This is done thread-safe.

This function returns values indicating what was done for each marker in a SIMD array: 0 : Nothing -1 : Finished marker replaced with a dummy (queue is empty) 1 : Finished marker replaced with a fresh one

Parameters
qpointer to particle queue
ppointer to SIMD structure of markers
Bdatapointer to magnetic field data
cyclepointer to integer array where what was done for each marker is stored
Returns
Number of markers within the SIMD structure that are still running

Definition at line 461 of file particle.c.

◆ particle_input_to_state()

void particle_input_to_state ( input_particle * p,
particle_state * ps,
B_field_data * Bdata )

Converts input marker to a marker state.

When marker input is read, the data is stored in one of the input marker structs (particle, particle_gc, particle_ml). This function converts these structs into a marker state, which then can be used to create simulation structs.

If an error is encountered while converting, the error message is stored to the state.

Todo

Parameter p can be a particle_state itself but this option is not dealt with

This sets p->type = input_particle_type_s which is not in accordance with the documentation.

Parameters
ppointer to marker input
pspointer to state where converted marker will be stored
Bdatapointer to magnetic field data

Definition at line 550 of file particle.c.

◆ particle_state_to_fo()

a5err particle_state_to_fo ( particle_state * p,
int i,
particle_simd_fo * p_fo,
int j,
B_field_data * Bdata )

Convert state into a FO SIMD marker.

This function assumes markers are drawn from a marker queue where they will be returned at the same position once simulation for this marker has ended,

State is converted into a FO marker by copying all the fields, except for magnetic field (and rho) which is evaluated here at FO position and then stored.

The simulation marker is set as running unless there is an error or the state already have active end condition.

If state marker already has an error flag, this error flag is returned and nothing is done. If an error occurs within this function, the conversion is terminated and the error is returned. It is the responsibility of the caller to store the error in correct struct.

This is a SIMD function.

Parameters
ppointer to a state being converted
iindex of this state in the marker queue
p_fopointer to SIMD structure where marker is being stored
jindex where in the SIMD structure marker is stored
Bdatapointer to magnetic field data

Definition at line 659 of file particle.c.

◆ particle_fo_to_state()

void particle_fo_to_state ( particle_simd_fo * p_fo,
int j,
particle_state * p,
B_field_data * Bdata )

Convert FO to state.

This function converts FO simulation marker to a marker state. No new state structure is initialized, but an existing one is filled with parameters corresponding to the converted marker.

This is a SIMD function.

Parameters
p_fopointer to SIMD structure
jSIMD index of the marker to be converted
ppointer to state whose fields are used to store the converted marker
Bdatapointer to magnetic field data

Definition at line 744 of file particle.c.

◆ particle_state_to_gc()

a5err particle_state_to_gc ( particle_state * p,
int i,
particle_simd_gc * p_gc,
int j,
B_field_data * Bdata )

Convert state into a GC SIMD marker.

This function assumes markers are drawn from a marker queue where they will be returned at the same position once simulation for this marker has ended,

State is converted into a GC marker by simply copying all the necessary fields.

The simulation marker is set as running unless there is an error or the state already have active end condition.

If state marker already has an error flag, this error flag is returned and nothing is done. If an error occurs within this function, the conversion is terminated and the error is returned. It is the responsibility of the caller to store the error in correct struct.

This is a SIMD function.

Parameters
ppointer to a state being converted
iindex of this state in the marker queue
p_gcpointer to SIMD structure where marker is being stored
jindex where in the SIMD structure marker is stored
Bdatapointer to magnetic field data

Definition at line 871 of file particle.c.

◆ particle_gc_to_state()

void particle_gc_to_state ( particle_simd_gc * p_gc,
int j,
particle_state * p,
B_field_data * Bdata )

Convert GC to state.

This function converts GC simulation marker to a marker state. No new state structure is initialized, but an existing one is filled with parameters corresponding to the converted marker.

This is a SIMD function.

Parameters
p_gcpointer to SIMD structure
jSIMD index of the marker to be converted
ppointer to state whose fields are used to store the converted marker
Bdatapointer to magnetic field data

Definition at line 936 of file particle.c.

◆ particle_state_to_ml()

a5err particle_state_to_ml ( particle_state * p,
int i,
particle_simd_ml * p_ml,
int j,
B_field_data * Bdata )

Convert state to a ML SIMD marker.

This function assumes markers are drawn from a marker queue where they will be returned at the same position once simulation for this marker has ended,

State is converted into a ML marker by using particle position as initial position. Other fields are simply copied exceot pitch, which is calculated from vpar as pitch = 2*(vpar >= 0) - 1, i.e., sign of vpar determines sign of pitch.

The simulation marker is set as running unless there is an error or the state already have active end condition.

If state marker already has an error flag, this error flag is returned and nothing is done. If an error occurs within this function, the conversion is terminated and the error is returned. It is the responsibility of the caller to store the error in correct struct.

This is a SIMD function.

Todo
A minor thing but it would be better if guiding center position were used instead. This would require evaluation of the magnetic field.
Parameters
ppointer to a state being converted
iindex of this state in the marker queue
p_mlpointer to SIMD structure where marker is being stored
jindex where in the SIMD structure marker is stored
Bdatapointer to magnetic field data

Definition at line 1052 of file particle.c.

◆ particle_ml_to_state()

void particle_ml_to_state ( particle_simd_ml * p_ml,
int j,
particle_state * p,
B_field_data * Bdata )

Convert ML to state.

This function converts ML simulation marker to a marker state. No new state structure is initialized, but an existing one is filled with parameters corresponding to the converted marker.

Field line position is stored in both particle and guiding center position fields. Direction is stored in vpar field. rdot, phidot, zdot, mu, zeta, mass, and charge are left empty.

This is a SIMD function.

Parameters
p_mlpointer to SIMD structure
jSIMD index of the marker to be converted
ppointer to state whose fields are used to store the converted marker
Bdatapointer to magnetic field data

Definition at line 1117 of file particle.c.

◆ particle_fo_to_gc()

int particle_fo_to_gc ( particle_simd_fo * p_fo,
int j,
particle_simd_gc * p_gc,
B_field_data * Bdata )

Convert FO struct into a GC struct.

Deprecated
Do we need this as same can be accomplished by turning FO to state and state to GC?
Parameters
p_fofo SIMD structure being transformed
jindex where in the SIMD structure marker is stored
p_gcgc SIMD structure where marker is transformed
Bdatapointer to magnetic field data

Definition at line 1185 of file particle.c.

◆ particle_copy_fo()

void particle_copy_fo ( particle_simd_fo * p1,
int i,
particle_simd_fo * p2,
int j )

Copy FO struct.

Parameters
p1SIMD structure for input
iindex for the copied input
p2SIMD structure for output
jindex for the output slot

Definition at line 1302 of file particle.c.

◆ particle_copy_gc()

void particle_copy_gc ( particle_simd_gc * p1,
int i,
particle_simd_gc * p2,
int j )

Copy GC struct.

Parameters
p1SIMD structure for input
iindex for the copied input
p2SIMD structure for output
jindex for the output slot

Definition at line 1355 of file particle.c.

◆ particle_copy_ml()

void particle_copy_ml ( particle_simd_ml * p1,
int i,
particle_simd_ml * p2,
int j )

Copy ML struct.

Parameters
p1SIMD structure for input
iindex for the copied input
p2SIMD structure for output
jindex for the output slot

Definition at line 1404 of file particle.c.

◆ particle_input_p_to_state()

a5err particle_input_p_to_state ( particle * p,
particle_state * ps,
B_field_data * Bdata )

Convert an input particle marker to particle state.

Parameters
ppointer to particle marker input
pspointer to state where converted marker will be stored
Bdatapointer to magnetic field data
Returns
zero if there was no error

Definition at line 1449 of file particle.c.

◆ particle_input_gc_to_state()

a5err particle_input_gc_to_state ( particle_gc * p,
particle_state * ps,
B_field_data * Bdata )

Convert an input guiding center marker to particle state.

Parameters
ppointer to guiding center marker input
pspointer to state where converted marker will be stored
Bdatapointer to magnetic field data
Returns
zero if there was no error

Definition at line 1540 of file particle.c.

◆ particle_input_ml_to_state()

a5err particle_input_ml_to_state ( particle_ml * p,
particle_state * ps,
B_field_data * Bdata )

Convert an input field line marker to particle state.

Parameters
ppointer to field line marker input
pspointer to state where converted marker will be stored
Bdatapointer to magnetic field data
Returns
zero if there was no error

Definition at line 1656 of file particle.c.

◆ particle_offload_fo()

void particle_offload_fo ( particle_simd_fo * p)

Offload particle struct to GPU.

Parameters
ppointer to the particle struct to be offloaded.

Definition at line 1726 of file particle.c.