Skip to content

Commit

Permalink
add support for calling Particles3D::initialize without gravity
Browse files Browse the repository at this point in the history
  • Loading branch information
mabruzzo committed Jul 26, 2024
1 parent f6e4913 commit 4e40aef
Show file tree
Hide file tree
Showing 7 changed files with 153 additions and 44 deletions.
35 changes: 17 additions & 18 deletions src/gravity/grav3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,39 +16,38 @@

Grav3D::Grav3D(void) {}

void Grav3D::Initialize(Real x_min, Real y_min, Real z_min, Real x_max, Real y_max, Real z_max, Real Lx, Real Ly,
Real Lz, int nx, int ny, int nz, int nx_real, int ny_real, int nz_real, Real dx_real,
Real dy_real, Real dz_real, int n_ghost_pot_offset, struct Parameters *P)
void Grav3D::Initialize(const SpatialDomainProps& spatial_props, Real Lx, Real Ly, Real Lz,
int n_ghost_pot_offset, Parameters *P)
{
// Set Box Size
Lbox_x = Lx;
Lbox_y = Ly;
Lbox_z = Lz;

// Set Box Left boundary positions
xMin = x_min;
yMin = y_min;
zMin = z_min;
xMin = spatial_props.xMin;
yMin = spatial_props.yMin;
zMin = spatial_props.zMin;

// Set Box Right boundary positions
xMax = x_max;
yMax = y_max;
zMax = z_max;
xMax = spatial_props.xMax;
yMax = spatial_props.yMax;
zMax = spatial_props.zMax;

// Set uniform ( dx, dy, dz )
dx = dx_real;
dy = dy_real;
dz = dz_real;
dx = spatial_props.dx;
dy = spatial_props.dy;
dz = spatial_props.dz;

// Set Box Total number of cells
nx_total = nx;
ny_total = ny;
nz_total = nz;
nx_total = spatial_props.nx_total;
ny_total = spatial_props.ny_total;
nz_total = spatial_props.nz_total;

// Set Box local domain number of cells
nx_local = nx_real;
ny_local = ny_real;
nz_local = nz_real;
nx_local = spatial_props.nx_local;
ny_local = spatial_props.ny_local;
nz_local = spatial_props.nz_local;

// Local n_cells without ghost cells
n_cells = nx_local * ny_local * nz_local;
Expand Down
6 changes: 3 additions & 3 deletions src/gravity/grav3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
#include <stdio.h>

#include "../global/global.h"
#include "../grid/spatial_domain_props.h"

#ifdef SOR
#include "../gravity/potential_SOR_3D.h"
Expand Down Expand Up @@ -191,9 +192,8 @@ class Grav3D

/*! \fn void Initialize(int nx_in, int ny_in, int nz_in)
* \brief Initialize the grid. */
void Initialize(Real x_min, Real y_min, Real z_min, Real x_max, Real y_max, Real z_max, Real Lx, Real Ly, Real Lz,
int nx_total, int ny_total, int nz_total, int nx_real, int ny_real, int nz_real, Real dx_real,
Real dy_real, Real dz_real, int n_ghost_pot_offset, struct Parameters *P);
void Initialize(const SpatialDomainProps& spatial_props, Real Lx, Real Ly, Real Lz,
int n_ghost_pot_offset, struct Parameters *P);

void AllocateMemory_CPU(void);
void Initialize_values_CPU();
Expand Down
5 changes: 2 additions & 3 deletions src/gravity/gravity_functions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -358,9 +358,8 @@ static void printDiff(const Real *p, const Real *q, const int nx, const int ny,
void Grid3D::Initialize_Gravity(struct Parameters *P)
{
chprintf("\nInitializing Gravity... \n");
Grav.Initialize(H.xblocal, H.yblocal, H.zblocal, H.xblocal_max, H.yblocal_max, H.zblocal_max, H.xdglobal, H.ydglobal,
H.zdglobal, P->nx, P->ny, P->nz, H.nx_real, H.ny_real, H.nz_real, H.dx, H.dy, H.dz,
H.n_ghost_potential_offset, P);
SpatialDomainProps spatial_props = SpatialDomainProps::From_Grid3D(*this, P);
Grav.Initialize(spatial_props, H.xdglobal, H.ydglobal, H.zdglobal, H.n_ghost_potential_offset, P);
chprintf("Gravity Successfully Initialized. \n\n");

if (P->bc_potential_type == 1) {
Expand Down
64 changes: 64 additions & 0 deletions src/grid/spatial_domain_props.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
#include "../grid/spatial_domain_props.h"
#include "../gravity/grav3D.h"
#include "../grid/grid3D.h"

SpatialDomainProps SpatialDomainProps::From_Grav3D(Grav3D& grav)
{
SpatialDomainProps out;

out.nx_local = grav.nx_local;
out.ny_local = grav.ny_local;
out.nz_local = grav.nz_local;

out.nx_total = grav.nx_total;
out.ny_total = grav.ny_total;
out.nz_total = grav.nz_total;

out.dx = grav.dx;
out.dy = grav.dy;
out.dz = grav.dz;

// Left boundaries of the local domain
out.xMin = grav.xMin;
out.yMin = grav.yMin;
out.zMin = grav.zMin;

// Right boundaries of the local domain
out.xMax = grav.xMax;
out.yMax = grav.yMax;
out.zMax = grav.zMax;

return out;
}

SpatialDomainProps SpatialDomainProps::From_Grid3D(Grid3D& grid, struct Parameters * P)
{

SpatialDomainProps out;

// Set Box Left boundary positions
out.xMin = grid.H.xblocal; // x_min
out.yMin = grid.H.yblocal; // y_min
out.zMin = grid.H.zblocal; // z_min

// Set Box Right boundary positions
out.xMax = grid.H.xblocal_max; //x_max;
out.yMax = grid.H.yblocal_max; //y_max;
out.zMax = grid.H.zblocal_max; //z_max;

// Set uniform ( dx, dy, dz )
out.dx = grid.H.dx; // dx_real;
out.dy = grid.H.dy; // dy_real;
out.dz = grid.H.dz; // dz_real;

// Set Box Total number of cells
out.nx_total = P->nx; // nx;
out.ny_total = P->ny; // ny;
out.nz_total = P->nz; // nz;

// Set Box local domain number of cells
out.nx_local = grid.H.nx_real; // nx_real;
out.ny_local = grid.H.ny_real; // ny_real;
out.nz_local = grid.H.nz_real; // nz_real;
return out;
};
32 changes: 32 additions & 0 deletions src/grid/spatial_domain_props.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#pragma once

#include "../global/global.h"

class Grav3D;
class Grid3D;
struct Parameters;

/* This is a collection of 15 quantities that appear in 3 other locations throughout the codebase.
*
* The struct primarily exists to simplify the process of copying these values from one place to
* another. (But it may make sense to refactor other parts of the code in terms of this object)
*/
struct SpatialDomainProps {
// number of cells in the local domain
int nx_local, ny_local, nz_local;

// total number of cells in the entire (global) domain
int nx_total, ny_total, nz_total;

// Left boundaries of the local domain
Real xMin, yMin, zMin;

// Right boundaries of the local domain
Real xMax, yMax, zMax;

// cell widths
Real dx, dy, dz;

static SpatialDomainProps From_Grav3D(Grav3D&);
static SpatialDomainProps From_Grid3D(Grid3D&, Parameters* P);
};
52 changes: 33 additions & 19 deletions src/particles/particles_3D.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,13 @@ void Grid3D::Initialize_Particles(struct Parameters *P)
{
chprintf("\nInitializing Particles...\n");

Particles.Initialize(P, Grav, H.xbound, H.ybound, H.zbound, H.xdglobal, H.ydglobal, H.zdglobal);
#ifdef GRAVITY
SpatialDomainProps spatial_props = SpatialDomainProps::From_Grav3D(Grav);
#else
SpatialDomainProps spatial_props = SpatialDomainProps::From_Grid3D(*this, P);
#endif

Particles.Initialize(P, spatial_props, H.xbound, H.ybound, H.zbound, H.xdglobal, H.ydglobal, H.zdglobal);

#if defined(PARTICLES_GPU) && defined(GRAVITY_GPU)
// Set the GPU array for the particles potential equal to the Gravity GPU
Expand All @@ -47,7 +53,7 @@ void Grid3D::Initialize_Particles(struct Parameters *P)
chprintf("Particles Initialized Successfully. \n\n");
}

void Particles3D::Initialize(struct Parameters *P, Grav3D &Grav, Real xbound, Real ybound, Real zbound, Real xdglobal,
void Particles3D::Initialize(struct Parameters *P, const SpatialDomainProps& spatial_props, Real xbound, Real ybound, Real zbound, Real xdglobal,
Real ydglobal, Real zdglobal)
{
// Initialize local and total number of particles to 0
Expand Down Expand Up @@ -108,27 +114,27 @@ void Particles3D::Initialize(struct Parameters *P, Grav3D &Grav, Real xbound, Re

// Initialize Grid Values
// Local and total number of cells
G.nx_local = Grav.nx_local;
G.ny_local = Grav.ny_local;
G.nz_local = Grav.nz_local;
G.nx_total = Grav.nx_total;
G.ny_total = Grav.ny_total;
G.nz_total = Grav.nz_total;
G.nx_local = spatial_props.nx_local;
G.ny_local = spatial_props.ny_local;
G.nz_local = spatial_props.nz_local;
G.nx_total = spatial_props.nx_total;
G.ny_total = spatial_props.ny_total;
G.nz_total = spatial_props.nz_total;

// Uniform (dx, dy, dz)
G.dx = Grav.dx;
G.dy = Grav.dy;
G.dz = Grav.dz;
G.dx = spatial_props.dx;
G.dy = spatial_props.dy;
G.dz = spatial_props.dz;

// Left boundaries of the local domain
G.xMin = Grav.xMin;
G.yMin = Grav.yMin;
G.zMin = Grav.zMin;
G.xMin = spatial_props.xMin;
G.yMin = spatial_props.yMin;
G.zMin = spatial_props.zMin;

// Right boundaries of the local domain
G.xMax = Grav.xMax;
G.yMax = Grav.yMax;
G.zMax = Grav.zMax;
G.xMax = spatial_props.xMax;
G.yMax = spatial_props.yMax;
G.zMax = spatial_props.zMax;

// Left boundaries of the global domain
G.domainMin_x = xbound;
Expand Down Expand Up @@ -176,8 +182,16 @@ void Particles3D::Initialize(struct Parameters *P, Grav3D &Grav, Real xbound, Re
#endif

G.size_blocks_array = 1024 * 128;
G.n_cells_potential = (G.nx_local + 2 * N_GHOST_POTENTIAL) * (G.ny_local + 2 * N_GHOST_POTENTIAL) *
(G.nz_local + 2 * N_GHOST_POTENTIAL);
{
#ifdef N_GHOST_POTENTIAL
int n_ghost_pot = N_GHOST_POTENTIAL;
#else
int n_ghost_pot = 0;
#endif

G.n_cells_potential = (G.nx_local + 2 * n_ghost_pot) * (G.ny_local + 2 * n_ghost_pot) *
(G.nz_local + 2 * n_ghost_pot);
}

#ifdef SINGLE_PARTICLE_MASS
mass_dev = NULL; // This array won't be used
Expand Down
3 changes: 2 additions & 1 deletion src/particles/particles_3D.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@

#include "../global/global.h"
#include "../gravity/grav3D.h"
#include "../grid/spatial_domain_props.h"

#ifdef PARTICLES_GPU
#define TPB_PARTICLES 1024
Expand Down Expand Up @@ -218,7 +219,7 @@ class Particles3D

Particles3D(void);

void Initialize(struct Parameters *P, Grav3D &Grav, Real xbound, Real ybound, Real zbound, Real xdglobal,
void Initialize(struct Parameters *P, const SpatialDomainProps& spatial_props, Real xbound, Real ybound, Real zbound, Real xdglobal,
Real ydglobal, Real zdglobal);

void Allocate_Particles_Grid_Field_Real(Real **array_dev, int size);
Expand Down

0 comments on commit 4e40aef

Please sign in to comment.