XGCa
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
Classes | Functions
pseudo_inverse.cpp File Reference
#include "gptl.h"
#include <Kokkos_Core.hpp>
#include "timer_macro.hpp"
#include "space_settings.hpp"
#include "globals.hpp"
#include "sml.hpp"
#include "plasma.hpp"
#include "grid.hpp"
#include "domain_decomposition.hpp"
#include "distribution.hpp"
#include "velocity_grid.hpp"
#include "lagrange_weights.hpp"
#include <petscversion.h>
#include <petscsys.h>
#include <petscdmplex.h>
#include <petscds.h>
#include "petscdmswarm.h"
#include "petscksp.h"
#include "pseudo_inverse.hpp"
Include dependency graph for pseudo_inverse.cpp:

Classes

struct  MatShellCtx
 

Functions

int force_pseudo_inv ()
 
int pseudo_inv_particles_to_grid_petsc ()
 
int pseudo_inv_delta_grid ()
 
static PetscErrorCode crd_func (PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nf_dummy, PetscScalar *u, void *actx)
 
PetscErrorCode MatMultMtM_SeqAIJ (Mat MtM, Vec xx, Vec yy)
 
PetscErrorCode MatMultAddMtM_SeqAIJ (Mat MtM, Vec xx, Vec yy, Vec zz)
 
PetscInt pseudo_inverse_interpolate_particles_to_velocity_grid (const DM &dm, Kokkos::View< PetscReal *, Kokkos::LayoutRight, Kokkos::HostSpace > particle_coords, Kokkos::View< PetscReal *, Kokkos::LayoutRight, Kokkos::HostSpace > particle_weights, const PetscInt Nparticles, const PetscInt Nspecies, Kokkos::View< PetscReal *, Kokkos::LayoutRight, Kokkos::HostSpace > grid_coords, Kokkos::View< PetscReal *, Kokkos::LayoutRight, Kokkos::HostSpace > grid_weights)
 For description see source file. More...
 
PetscErrorCode pseudo_inv_create_Swarm (const DM dm, DM *sw)
 For description see source file. More...
 
PetscErrorCode pseudo_inv_create_projection (const DM dm, DM sw, const PetscInt thread_id, const PetscInt target_id, Kokkos::View< PetscReal *, Kokkos::LayoutRight, Kokkos::HostSpace > particle_coords, Kokkos::View< PetscReal *, Kokkos::LayoutRight, Kokkos::HostSpace > particle_weights, const PetscInt Nparticles, Vec rho, Mat *Mp_out)
 For description see source file. More...
 
PetscErrorCode pseudo_inv_set_rhs (const DM dm, Mat MM, Kokkos::View< PetscReal *, Kokkos::LayoutRight, Kokkos::HostSpace > grid_weights, const PetscInt Ngrid, Vec rho, Vec rhs)
 For description see source file. More...
 
PetscErrorCode pseudo_inv_grid_to_particles (const DM dm, DM sw, Vec rhs, Mat M_p, Mat PM_p, const PetscInt Ngrid, const PetscInt Nparticles, const PetscInt order, Kokkos::View< PetscReal *, Kokkos::LayoutRight, Kokkos::HostSpace > particle_weights, int &ksp_num_its, bool &ksp_converged)
 
void pseudo_inv_allocate (const Simulation< DeviceType > &sml, const Grid< DeviceType > &grid, const MagneticField< DeviceType > &magnetic_field, Plasma &plasma, const VelocityGrid &vgrid, const DomainDecomposition< DeviceType > &pol_decomp, Pseudo_inverse< DeviceType > &pseudo_inv)
 < For description see source file More...
 
void all_species_update_pseudo_inv_weights (const Simulation< DeviceType > &sml, const Grid< DeviceType > &grid, const MagneticField< DeviceType > &magnetic_field, Plasma &plasma, const VelocityGrid &vgrid, const DomainDecomposition< DeviceType > &pol_decomp, DMWrapper &pseudo_inv_dm, Pseudo_inverse< DeviceType > &pseudo_inv)
 For description see source file. More...
 
int max_n_ptl_on_node_and_vgrid_filled (const Simulation< DeviceType > &sml, const Grid< DeviceType > &grid, const MagneticField< DeviceType > &magnetic_field, Plasma &plasma, const VelocityGrid &vgrid, const DomainDecomposition< DeviceType > &pol_decomp, const std::string &msg, Kokkos::View< bool **, Kokkos::LayoutRight, DeviceType > vgrid_filled)
 

Detailed Description

1.0 A. Mollen, M. Adams, M. Knepley Last update Oct 2023

DESCRIPTION

C++ functions for performing the particles -> velocity grid and velocity grid -> particles interpolation using a pseudo-inverse. This functionality needs PETSc 3.14 or above.

Function Documentation

void all_species_update_pseudo_inv_weights ( const Simulation< DeviceType > &  sml,
const Grid< DeviceType > &  grid,
const MagneticField< DeviceType > &  magnetic_field,
Plasma plasma,
const VelocityGrid vgrid,
const DomainDecomposition< DeviceType > &  pol_decomp,
DMWrapper pseudo_inv_dm,
Pseudo_inverse< DeviceType > &  pseudo_inv 
)

For description see source file.

Update pseudo-inverse full weights (not alpha weights) of all non-adiabatic species.

Parameters
[in]smlcontains simulation control parameters
[in]gridspatial grid object
[in]magnetic_fieldmagnetic field object
[in]plasmaplasma object
[in]vgridcontains the velocity grid dimensions
[in]pol_decompcontains poloidal decomposition info
[in]pseudo_inv_dmpseudo-inverse mesh object
[in,out]pseudo_invpseudo-inverse object (contains pseudo-inverse arrays)
Returns
void

Here is the call graph for this function:

Here is the caller graph for this function:

static PetscErrorCode crd_func ( PetscInt  dim,
PetscReal  time,
const PetscReal  x[],
PetscInt  Nf_dummy,
PetscScalar *  u,
void *  actx 
)
static

Helper function for coordinates in PETSc.

Parameters
[in]dimDimension.
[in]timeTime.
[in]xInput vector.
[in]Nf_dummyDummy.
[out]uOutput vector.
[out]actx
Returns
PetscErrorCode

Here is the caller graph for this function:

int force_pseudo_inv ( )

Here is the caller graph for this function:

PetscErrorCode MatMultAddMtM_SeqAIJ ( Mat  MtM,
Vec  xx,
Vec  yy,
Vec  zz 
)

Helper function for pseudo_inv_grid_to_particles.

Parameters
[in]MtMPETSc shell matrix.
[in]xxPETSc vector.
[in]yyPETSc vector.
[out]zzPETSc vector.
Returns
PetscErrorCode

Here is the caller graph for this function:

PetscErrorCode MatMultMtM_SeqAIJ ( Mat  MtM,
Vec  xx,
Vec  yy 
)

Helper function for pseudo_inv_grid_to_particles.

Parameters
[in]MtMPETSc shell matrix.
[in]xxPETSc vector.
[out]yyPETSc vector.
Returns
PetscErrorCode

Here is the caller graph for this function:

int max_n_ptl_on_node_and_vgrid_filled ( const Simulation< DeviceType > &  sml,
const Grid< DeviceType > &  grid,
const MagneticField< DeviceType > &  magnetic_field,
Plasma plasma,
const VelocityGrid vgrid,
const DomainDecomposition< DeviceType > &  pol_decomp,
const std::string &  msg,
Kokkos::View< bool **, Kokkos::LayoutRight, DeviceType vgrid_filled 
)

Obtain maximum number of particles (for any non-adiabatic species) on any 3D real-space mesh node in each plane, and which of the nodes are filled in velocity space (i.e. all velocity cells have enough particles) for all non-adiabatic species (used for the pseudo-inverse allocation and calculation). A node is filled for element order Q (= 0 for nearest neighbor, = 1 for linear, = 2 for quadratic, = 3 for cubic, ...) if all QxQ velocity cells have at least Q^2 particles and the corner cells at least Q*(Q + 1) particles.

Parameters
[in]smlcontains simulation control parameters
[in]gridspatial grid object
[in]magnetic_fieldmagnetic field object
[in]plasmaplasma object
[in]vgridcontains the velocity grid dimensions
[in]pol_decompcontains poloidal decomposition info
[in]msgmessage printed to the log in combination with statistics
[in,out]vgrid_filledempty velocity cells array (non-adiabatic species, node)
Returns
maximum number of particles on any mesh node for any non-adiabatic species

Here is the call graph for this function:

Here is the caller graph for this function:

void pseudo_inv_allocate ( const Simulation< DeviceType > &  sml,
const Grid< DeviceType > &  grid,
const MagneticField< DeviceType > &  magnetic_field,
Plasma plasma,
const VelocityGrid vgrid,
const DomainDecomposition< DeviceType > &  pol_decomp,
Pseudo_inverse< DeviceType > &  pseudo_inv 
)

< For description see source file

Set up and allocate pseudo-inverse object.

Parameters
[in]smlcontains simulation control parameters
[in]gridspatial grid object
[in]magnetic_fieldmagnetic field object
[in]plasmaplasma object
[in]vgridcontains the velocity grid dimensions
[in]pol_decompcontains poloidal decomposition info
[in,out]pseudo_invpseudo-inverse object (contains pseudo-inverse arrays)
Returns
void

Here is the call graph for this function:

Here is the caller graph for this function:

PetscErrorCode pseudo_inv_create_projection ( const DM  dm,
DM  sw,
const PetscInt  thread_id,
const PetscInt  target_id,
Kokkos::View< PetscReal *, Kokkos::LayoutRight, Kokkos::HostSpace >  particle_coords,
Kokkos::View< PetscReal *, Kokkos::LayoutRight, Kokkos::HostSpace >  particle_weights,
const PetscInt  Nparticles,
Vec  rho,
Mat *  Mp_out 
)

For description see source file.

Creates a PETSc projection matrix used for the pseudo-inverse velocity interpolation from particle coordinates and weights, based on the PETSc DM velocity grid object containing the velocity grid coordinates and the element (interpolation) order.

Parameters
[in]dmPETSc DM velocity grid object.
[in]swPETSc DM Swarm object.
[in]thread_idID of thread if using OpenMP.
[in]target_idID of thread for visualization if using OpenMP (normally not used, needs PETSc input flags).
[in]particle_coords2*Nparticles-sized array containing the particle coordinates.
[in]particle_weightsNparticles-sized array containing the particle weights.
[in]NparticlesNumber of particles being interpolated.
[out]rhoPETSc helper vector.
[out]Mp_outPETSc projection matrix created.
Returns
PetscErrorCode

Here is the caller graph for this function:

PetscErrorCode pseudo_inv_create_Swarm ( const DM  dm,
DM *  sw 
)

For description see source file.

Creates a PETSc DM Swarm object used for the pseudo-inverse velocity interpolation, based on the PETSc DM velocity grid object containing the velocity grid coordinates and the element (interpolation) order.

Parameters
[in]dmPETSc DM velocity grid object.
[out]swPETSc DM Swarm object created.
Returns
PetscErrorCode

Here is the caller graph for this function:

int pseudo_inv_delta_grid ( )

Here is the caller graph for this function:

PetscErrorCode pseudo_inv_grid_to_particles ( const DM  dm,
DM  sw,
Vec  rhs,
Mat  M_p,
Mat  PM_p,
const PetscInt  Ngrid,
const PetscInt  Nparticles,
const PetscInt  order,
Kokkos::View< PetscReal *, Kokkos::LayoutRight, Kokkos::HostSpace >  particle_weights,
int &  ksp_num_its,
bool &  ksp_converged 
)

Performs the velocity grid -> particles interpolation by solving the linear system containing the pseudo-inverse and gives the updated particle weights, based on the PETSc DM velocity grid object containing the velocity grid coordinates and the element (interpolation) order.

Parameters
[in]dmPETSc DM velocity grid object.
[in]swPETSc DM Swarm object.
[in]rhsPETSc right-hand side vector.
[in]M_pPETSc projection matrix.
[in]PM_pPETSc preconditioner matrix.
[in]NgridNumber of velocity grid points.
[in]NparticlesNumber of particles being interpolated.
[in]orderThe interpolation order (>= 0). (dm already contains the order but used in check of nonsingular matrix.)
[out]particle_weightsNparticles-sized array containing the particle weights.
[out]ksp_num_itsNumber of iterations for linear solver in PETSc.
[out]ksp_convergedtrue if the pseudo-inverse calculation converged, otherwise false.
Returns
PetscErrorCode

Here is the call graph for this function:

Here is the caller graph for this function:

int pseudo_inv_particles_to_grid_petsc ( )

Here is the caller graph for this function:

PetscErrorCode pseudo_inv_set_rhs ( const DM  dm,
Mat  MM,
Kokkos::View< PetscReal *, Kokkos::LayoutRight, Kokkos::HostSpace >  grid_weights,
const PetscInt  Ngrid,
Vec  rho,
Vec  rhs 
)

For description see source file.

Sets the right-hand side used for the pseudo-inverse velocity interpolation from new grid weights, based on the PETSc DM velocity grid object containing the velocity grid coordinates and the element (interpolation) order.

Parameters
[in]dmPETSc DM velocity grid object.
[in]MMPETSc mass matrix velocity grid object.
[in]grid_weightsNgrid-sized array containing the new velocity grid weights.
[in]NgridNumber of velocity grid points.
[out]rhoPETSc helper vector.
[out]rhsPETSc right-hand side vector.
Returns
PetscErrorCode

Here is the caller graph for this function:

PetscInt pseudo_inverse_interpolate_particles_to_velocity_grid ( const DM &  dm,
Kokkos::View< PetscReal *, Kokkos::LayoutRight, Kokkos::HostSpace >  particle_coords,
Kokkos::View< PetscReal *, Kokkos::LayoutRight, Kokkos::HostSpace >  particle_weights,
const PetscInt  Nparticles,
const PetscInt  Nspecies,
Kokkos::View< PetscReal *, Kokkos::LayoutRight, Kokkos::HostSpace >  grid_coords,
Kokkos::View< PetscReal *, Kokkos::LayoutRight, Kokkos::HostSpace >  grid_weights 
)

For description see source file.

Performs the particles -> velocity grid interpolation of marker particle weights to 2D velocity grid weights.

Parameters
[in]dmis the PETSc DM object containing the velocity grid coordinates and the element (interpolation) order (created by initialize_pseudo_inverse_interpolation).
[in]particle_coordsis a 2*Nparticles-sized array containing the particle coordinates.
[in]particle_weightsis a Nparticles-sized array containing the particle weights.
[in]Nparticlesis the number of particles being interpolated.
[in]Nspeciesis the number of species being interpolated, typically 1.
[in,out]grid_coordsis a 2*NGridPoints-sized array containing the velocity grid coordinates.
[in,out]grid_weightsis a NGridPoints-sized array containing the velocity grid weights.
Returns
the number of grid points.

Here is the call graph for this function:

Here is the caller graph for this function: