Skip to content

Commit

Permalink
Merge pull request #48 from streeve/fixup_es
Browse files Browse the repository at this point in the history
Fix ES solver
  • Loading branch information
guangyechen committed Mar 28, 2023
2 parents 3d22919 + 09f97ec commit f556411
Show file tree
Hide file tree
Showing 6 changed files with 106 additions and 63 deletions.
22 changes: 20 additions & 2 deletions .github/workflows/CI.yml
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ jobs:
cmake --install build
- name: Checkout CabanaPIC
uses: actions/checkout@v2.2.0
- name: Build CabanaPIC
- name: Build CabanaPIC EM
run: |
cmake -B build \
-DCMAKE_INSTALL_PREFIX=$HOME/CabanaPIC \
Expand All @@ -53,10 +53,28 @@ jobs:
-DCMAKE_SHARED_LINKER_FLAGS="--coverage" \
-DCMAKE_PREFIX_PATH="$HOME/Cabana" \
-DENABLE_TESTS=ON \
-DSOLVER_TYPE=EM \
-DREAL_TYPE=double
cmake --build build --parallel 2
cmake --install build
- name: Test CabanaPIC
- name: Test CabanaPIC EM
run: |
CTEST_OUTPUT_ON_FAILURE=1 cmake --build build --target test
- name: Build CabanaPIC ES
run: |
cmake -B build \
-DCMAKE_INSTALL_PREFIX=$HOME/CabanaPIC \
-DMPIEXEC_MAX_NUMPROCS=2 -DMPIEXEC_PREFLAGS="--oversubscribe" \
-DCMAKE_CXX_FLAGS="--coverage -O0 -Wall -Wextra -pedantic" \
-DCMAKE_EXE_LINKER_FLAGS="--coverage" \
-DCMAKE_SHARED_LINKER_FLAGS="--coverage" \
-DCMAKE_PREFIX_PATH="$HOME/Cabana" \
-DENABLE_TESTS=ON \
-DSOLVER_TYPE=ES \
-DREAL_TYPE=double
cmake --build build --parallel 2
cmake --install build
- name: Test CabanaPIC ES
run: |
CTEST_OUTPUT_ON_FAILURE=1 cmake --build build --target test
- name: Upload Report to codecov.io
Expand Down
8 changes: 4 additions & 4 deletions decks/2stream-short.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
Input_Deck::Input_Deck()
{
// User puts initialization code here
nx = 1;
ny = 32;
nx = 32;
ny = 1;
nz = 1;

num_steps = 3000;
Expand All @@ -41,8 +41,8 @@ Input_Deck::Input_Deck()

const real_t default_grid_len = 1.0;

len_x_global = default_grid_len;
len_y_global = 3.14159265358979*0.5; // TODO: use proper PI?
len_x_global = 3.14159265358979*0.5; // TODO: use proper PI?
len_y_global = default_grid_len;
len_z_global = default_grid_len;

Npe = n0*len_x_global*len_y_global*len_z_global;
Expand Down
40 changes: 21 additions & 19 deletions decks/custom_init.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,11 @@ class Custom_Particle_Initializer : public Particle_Initializer {
real_ dxp,
size_t nppc,
real_ w,
real_ v0
)
real_ v0,
real_,
real_,
real_
) override
{
std::cout << "Using Custom Particle Initialization" << std::endl;

Expand Down Expand Up @@ -75,26 +78,25 @@ class Custom_Particle_Initializer : public Particle_Initializer {
real_ x = pic*dxp+0.5*dxp-1.0;
size_t pre_ghost = (2*pi/nppc); //pre_gohost ranges [0,nx*ny*nz).

size_t ix,iy,iz;
RANK_TO_INDEX(pre_ghost, ix, iy, iz, nx, ny);
ix += ng;
iy += ng;
iz += ng;

position_x.access(s,i) = 0.0;
position_y.access(s,i) = x;
position_x.access(s,i) = x;
position_y.access(s,i) = 0.0;
position_z.access(s,i) = 0.0;

weight.access(s,i) = w;

int ix,iy,iz;
ix = pre_ghost+1;
iy = 1;
iz = 1;
cell.access(s,i) = VOXEL(ix,iy,iz,nx,ny,nz,ng);

// Initialize velocity.(each cell length is 2)
real_t nax = 0.0001*sin(2.0*3.1415926*((x+1.0+ix*2)/(2*nx)));
real_ gam = 1.0/sqrt(1.0-v0*v0);
velocity_x.access(s,i) = sign * v0*gam; // *(1.0-na*sign); //0;
velocity_x.access(s,i) = sign * v0*gam*(1.0+nax); //0;
velocity_y.access(s,i) = 0;
velocity_z.access(s,i) = 0; //na*sign; //sign * v0 *gam*(1.0+na*sign);
velocity_z.access(s,i) = 1e-7*sign;
//if(pi<100) printf("%d %d %d pre-g %d putting particle at x=%e with ux = %e ix = %d, pi = %d \n", pic, s, i, pre_ghost, position_x.access(s,i), velocity_x.access(s,i), ix, cell.access(s,i) );
};

Cabana::SimdPolicy<particle_list_t::vector_length,ExecutionSpace>
Expand All @@ -107,26 +109,26 @@ Input_Deck::Input_Deck()
{
// User puts initialization code here

std::cout << "Input_Deck constructor" << std::endl;
std::cout << "Custom Input_Deck constructor" << std::endl;
// Tell the deck to use the custom initer in place of the default
particle_initer = new Custom_Particle_Initializer();

nx = 1;
ny = 32;
nx = 32;
ny = 1;
nz = 1;

num_steps = 30;
nppc = 100;

v0 = 0.2;
v0 = 0.0866025403784439;

// Can also create local temporaries
//real_ gam = 1.0 / sqrt(1.0 - v0*v0);
real_ gam = 1.0 / sqrt(1.0 - v0*v0);

const real_t default_grid_len = 1.0;

len_x_global = default_grid_len;
len_y_global = 3.14159265358979*0.5; // TODO: use proper PI?
len_x_global = 6.28318530717959*(gam*sqrt(gam));
len_y_global = default_grid_len;
len_z_global = default_grid_len;

Npe = n0*len_x_global*len_y_global*len_z_global;
Expand Down
10 changes: 8 additions & 2 deletions example/example.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ static auto make_field_solver(field_array_t &fields)
{
// TODO: make this support 1/2/3d
#ifdef ES_FIELD_SOLVER
std::cout << "Created ES Solver" << std::endl;
Field_Solver<ES_Field_Solver> field_solver(fields);
std::cout << "Created ES Solver (1D only)" << std::endl;
Field_Solver<ES_Field_Solver_1D> field_solver(fields);
#else // EM
std::cout << "Created EM Solver" << std::endl;
Field_Solver<EM_Field_Solver> field_solver(fields);
Expand Down Expand Up @@ -66,6 +66,12 @@ int main( int argc, char* argv[] )
const int nx = deck.nx;
const int ny = deck.ny;
const int nz = deck.nz;
#ifdef ES_FIELD_SOLVER
if(ny>1 || nz>1){
std::cerr << "Error: ES Field solver supports 1D only.\n";
return -1;
}
#endif
const int num_ghosts = deck.num_ghosts;
const size_t num_cells = deck.num_cells;
real_t dxp = 2.f / (npc);
Expand Down
85 changes: 50 additions & 35 deletions src/fields.h
Original file line number Diff line number Diff line change
Expand Up @@ -314,6 +314,41 @@ template<typename Solver_Type> class Field_Solver : public Solver_Type
Kokkos::parallel_for( "init_fields()", fields.size(), _init_fields );
}

// TODO: is this the right place for this vs in the helper?
void dump_fields(FILE * fp,
field_array_t& d_fields,
real_t xmin,
real_t,
real_t,
real_t dx,
real_t,
real_t,
size_t nx,
size_t ny,
size_t,
size_t ng
)
{
// Host
field_array_t::host_mirror_type fields("host_fields", d_fields.size());

// Copy device field to host
Cabana::deep_copy(fields, d_fields);

auto ex = Cabana::slice<FIELD_EX>(fields);

for( size_t i=1; i<nx+1; i++ )
{
real_t x = xmin + (i-0.5)*dx;
size_t ii = VOXEL(i,1,1,nx,ny,nz,ng);
// fprintf(fp,"%e %e %e %e %e %e %e\n",x,y,ey(ii),jfx(ii),jfy(ii),jfz(ii),cbz(ii));
fprintf(fp,"%e %e\n",x,ex(ii));
}

fprintf(fp,"\n\n");

}

void advance_b(
field_array_t& fields,
real_t px,
Expand Down Expand Up @@ -407,6 +442,7 @@ class ES_Field_Solver
real_t,
size_t,
size_t,
size_t,
size_t
)
{
Expand All @@ -431,6 +467,19 @@ class ES_Field_Solver
class ES_Field_Solver_1D
{
public:
void advance_b(
field_array_t&,
real_t,
real_t,
real_t,
size_t,
size_t,
size_t,
size_t
)
{
// No-op, becasue ES
}

real_t e_energy(
field_array_t& fields,
Expand All @@ -439,6 +488,7 @@ class ES_Field_Solver_1D
real_t,
size_t,
size_t,
size_t,
size_t
)
{
Expand Down Expand Up @@ -503,41 +553,6 @@ class EM_Field_Solver

//how to formalize/generalize this?

// TODO: is this the right place for this vs in the helper?
void dump_fields(FILE * fp,
field_array_t& d_fields,
real_t xmin,
real_t,
real_t,
real_t dx,
real_t,
real_t,
size_t nx,
size_t ny,
size_t,
size_t ng
)
{
// Host
field_array_t::host_mirror_type fields("host_fields", d_fields.size());

// Copy device field to host
Cabana::deep_copy(fields, d_fields);

auto ex = Cabana::slice<FIELD_EX>(fields);

for( size_t i=1; i<nx+1; i++ )
{
real_t x = xmin + (i-0.5)*dx;
size_t ii = VOXEL(i,1,1,nx,ny,nz,ng);
// fprintf(fp,"%e %e %e %e %e %e %e\n",x,y,ey(ii),jfx(ii),jfy(ii),jfz(ii),cbz(ii));
fprintf(fp,"%e %e\n",x,ex(ii));
}

fprintf(fp,"\n\n");

}

real_t e_energy(
field_array_t& fields,
real_t,
Expand Down
4 changes: 3 additions & 1 deletion tests/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
add_subdirectory(decks)
add_subdirectory(energy_comparison)
if (${SOLVER_TYPE} STREQUAL "EM")
add_subdirectory(energy_comparison)
endif()

#LIST(APPEND TESTS example)

Expand Down

0 comments on commit f556411

Please sign in to comment.