172d38120SMatthew G. Knepley static char help[] = "Test periodic DMDA for DMSwarm point location.\n";
272d38120SMatthew G. Knepley
372d38120SMatthew G. Knepley #include <petscdmda.h>
472d38120SMatthew G. Knepley #include <petscdmswarm.h>
572d38120SMatthew G. Knepley
672d38120SMatthew G. Knepley typedef struct {
772d38120SMatthew G. Knepley PetscInt dim; // Mesh dimension
872d38120SMatthew G. Knepley PetscInt Np; // Number of particles along each dimension
972d38120SMatthew G. Knepley } UserContext;
1072d38120SMatthew G. Knepley
ProcessOptions(UserContext * options)1172d38120SMatthew G. Knepley static PetscErrorCode ProcessOptions(UserContext *options)
1272d38120SMatthew G. Knepley {
1372d38120SMatthew G. Knepley PetscFunctionBeginUser;
1472d38120SMatthew G. Knepley options->dim = 3;
1572d38120SMatthew G. Knepley options->Np = -1;
1672d38120SMatthew G. Knepley
1772d38120SMatthew G. Knepley PetscCall(PetscOptionsGetInt(NULL, NULL, "-dim", &options->dim, NULL));
1872d38120SMatthew G. Knepley PetscCall(PetscOptionsGetInt(NULL, NULL, "-np", &options->Np, NULL));
1972d38120SMatthew G. Knepley PetscFunctionReturn(PETSC_SUCCESS);
2072d38120SMatthew G. Knepley }
2172d38120SMatthew G. Knepley
CreateMesh(DM * da,UserContext * user)2272d38120SMatthew G. Knepley static PetscErrorCode CreateMesh(DM *da, UserContext *user)
2372d38120SMatthew G. Knepley {
2472d38120SMatthew G. Knepley PetscReal gmin[3] = {0, 0., 0.}, gmax[3] = {0, 0., 0.};
2572d38120SMatthew G. Knepley
2672d38120SMatthew G. Knepley PetscFunctionBeginUser;
2772d38120SMatthew G. Knepley PetscCall(DMDACreate(PETSC_COMM_WORLD, da));
2872d38120SMatthew G. Knepley PetscCall(DMSetDimension(*da, user->dim));
2972d38120SMatthew G. Knepley PetscCall(DMDASetSizes(*da, 7, 7, 7));
3072d38120SMatthew G. Knepley PetscCall(DMDASetBoundaryType(*da, DM_BOUNDARY_GHOSTED, DM_BOUNDARY_GHOSTED, DM_BOUNDARY_GHOSTED));
3172d38120SMatthew G. Knepley PetscCall(DMDASetDof(*da, 2));
3272d38120SMatthew G. Knepley PetscCall(DMDASetStencilType(*da, DMDA_STENCIL_BOX));
3372d38120SMatthew G. Knepley PetscCall(DMDASetStencilWidth(*da, 1));
3472d38120SMatthew G. Knepley PetscCall(DMDASetElementType(*da, DMDA_ELEMENT_Q1));
3572d38120SMatthew G. Knepley PetscCall(DMSetFromOptions(*da));
3672d38120SMatthew G. Knepley PetscCall(DMSetUp(*da));
3772d38120SMatthew G. Knepley PetscCall(DMDASetUniformCoordinates(*da, 0., 1., 0., 1., 0., 1.));
3872d38120SMatthew G. Knepley PetscCall(DMSetApplicationContext(*da, user));
3972d38120SMatthew G. Knepley PetscCall(DMView(*da, PETSC_VIEWER_STDOUT_WORLD));
4072d38120SMatthew G. Knepley PetscCall(DMGetBoundingBox(*da, gmin, gmax));
4172d38120SMatthew G. Knepley PetscCall(PetscPrintf(PETSC_COMM_WORLD, "min: (%g, %g, %g) max: (%g, %g, %g)\n", gmin[0], gmin[1], gmin[2], gmax[0], gmax[1], gmax[2]));
4272d38120SMatthew G. Knepley PetscFunctionReturn(PETSC_SUCCESS);
4372d38120SMatthew G. Knepley }
4472d38120SMatthew G. Knepley
CreateSwarm(DM mesh,DM * swarm,UserContext * user)4572d38120SMatthew G. Knepley static PetscErrorCode CreateSwarm(DM mesh, DM *swarm, UserContext *user)
4672d38120SMatthew G. Knepley {
4772d38120SMatthew G. Knepley MPI_Comm comm;
4872d38120SMatthew G. Knepley PetscMPIInt size;
4972d38120SMatthew G. Knepley PetscInt dim;
5072d38120SMatthew G. Knepley
5172d38120SMatthew G. Knepley PetscFunctionBeginUser;
5272d38120SMatthew G. Knepley PetscCall(PetscObjectGetComm((PetscObject)mesh, &comm));
5372d38120SMatthew G. Knepley PetscCallMPI(MPI_Comm_size(comm, &size));
5472d38120SMatthew G. Knepley PetscCall(DMCreate(comm, swarm));
55*3a7d0413SPierre Jolivet PetscCall(PetscObjectSetOptionsPrefix((PetscObject)*swarm, "pic_"));
5672d38120SMatthew G. Knepley PetscCall(DMSetType(*swarm, DMSWARM));
5772d38120SMatthew G. Knepley PetscCall(PetscObjectSetName((PetscObject)*swarm, "ions"));
5872d38120SMatthew G. Knepley PetscCall(DMGetDimension(mesh, &dim));
5972d38120SMatthew G. Knepley PetscCall(DMSetDimension(*swarm, dim));
6072d38120SMatthew G. Knepley PetscCall(DMSwarmSetType(*swarm, DMSWARM_PIC));
6172d38120SMatthew G. Knepley PetscCall(DMSwarmSetCellDM(*swarm, mesh));
6272d38120SMatthew G. Knepley PetscCall(DMSwarmInitializeFieldRegister(*swarm));
6372d38120SMatthew G. Knepley PetscCall(DMSwarmFinalizeFieldRegister(*swarm));
6472d38120SMatthew G. Knepley PetscCall(DMSwarmSetLocalSizes(*swarm, user->Np / size, 0));
6572d38120SMatthew G. Knepley PetscFunctionReturn(PETSC_SUCCESS);
6672d38120SMatthew G. Knepley }
6772d38120SMatthew G. Knepley
InitializeParticles(DM sw,UserContext * user)6872d38120SMatthew G. Knepley static PetscErrorCode InitializeParticles(DM sw, UserContext *user)
6972d38120SMatthew G. Knepley {
7072d38120SMatthew G. Knepley DM da;
7172d38120SMatthew G. Knepley PetscReal gmin[3], gmax[3];
7272d38120SMatthew G. Knepley PetscInt ndir[3];
7372d38120SMatthew G. Knepley
7472d38120SMatthew G. Knepley PetscFunctionBeginUser;
7572d38120SMatthew G. Knepley PetscCall(DMSwarmGetCellDM(sw, &da));
7672d38120SMatthew G. Knepley PetscCall(DMGetBoundingBox(da, gmin, gmax));
7772d38120SMatthew G. Knepley ndir[0] = user->Np;
7872d38120SMatthew G. Knepley ndir[1] = user->Np;
7972d38120SMatthew G. Knepley ndir[2] = user->Np;
8072d38120SMatthew G. Knepley PetscCall(DMSwarmSetPointsUniformCoordinates(sw, gmin, gmax, ndir, INSERT_VALUES));
8172d38120SMatthew G. Knepley PetscFunctionReturn(PETSC_SUCCESS);
8272d38120SMatthew G. Knepley }
8372d38120SMatthew G. Knepley
main(int argc,char ** args)8472d38120SMatthew G. Knepley int main(int argc, char **args)
8572d38120SMatthew G. Knepley {
8672d38120SMatthew G. Knepley DM dm, sw;
8772d38120SMatthew G. Knepley UserContext user;
8872d38120SMatthew G. Knepley
8972d38120SMatthew G. Knepley PetscFunctionBeginUser;
9072d38120SMatthew G. Knepley PetscCall(PetscInitialize(&argc, &args, NULL, help));
9172d38120SMatthew G. Knepley PetscCall(ProcessOptions(&user));
9272d38120SMatthew G. Knepley PetscCall(CreateMesh(&dm, &user));
9372d38120SMatthew G. Knepley PetscCall(CreateSwarm(dm, &sw, &user));
9472d38120SMatthew G. Knepley
9572d38120SMatthew G. Knepley PetscCall(InitializeParticles(sw, &user));
9672d38120SMatthew G. Knepley PetscCall(DMSwarmMigrate(sw, PETSC_TRUE));
9772d38120SMatthew G. Knepley PetscCall(DMViewFromOptions(sw, NULL, "-sw_view"));
9872d38120SMatthew G. Knepley
9972d38120SMatthew G. Knepley PetscCall(DMDestroy(&dm));
10072d38120SMatthew G. Knepley PetscCall(DMDestroy(&sw));
10172d38120SMatthew G. Knepley PetscCall(PetscFinalize());
10272d38120SMatthew G. Knepley return 0;
10372d38120SMatthew G. Knepley }
10472d38120SMatthew G. Knepley
10572d38120SMatthew G. Knepley // ./dmswarm-coords -nx 8 -ny 8 -np 4 -pic_sw_view -dim 2 -periodic 0
10672d38120SMatthew G. Knepley // ./dmswarm-coords -nx 8 -ny 8 -np 4 -pic_sw_view -dim 2 -periodic 1
10772d38120SMatthew G. Knepley // ./dmswarm-coords -nx 8 -ny 8 -nz 8 -np 4 -pic_sw_view -dim 3 -periodic 0
10872d38120SMatthew G. Knepley // ./dmswarm-coords -nx 8 -ny 8 -nz 8 -np 4 -pic_sw_view -dim 3 -periodic 1
10972d38120SMatthew G. Knepley
11072d38120SMatthew G. Knepley /*TEST
11172d38120SMatthew G. Knepley
11272d38120SMatthew G. Knepley build:
11372d38120SMatthew G. Knepley requires: double !complex
11472d38120SMatthew G. Knepley
11572d38120SMatthew G. Knepley testset:
11672d38120SMatthew G. Knepley suffix: 2d
11772d38120SMatthew G. Knepley args: -dim 2 -da_grid_x 8 -da_grid_y 8 -np 4 -da_bd_all {{none periodic}} -pic_sw_view
11872d38120SMatthew G. Knepley
11972d38120SMatthew G. Knepley test:
12072d38120SMatthew G. Knepley suffix: p1
12172d38120SMatthew G. Knepley
12272d38120SMatthew G. Knepley test:
12372d38120SMatthew G. Knepley suffix: p2
12472d38120SMatthew G. Knepley nsize: 2
12572d38120SMatthew G. Knepley
12672d38120SMatthew G. Knepley test:
12772d38120SMatthew G. Knepley suffix: p4
12872d38120SMatthew G. Knepley nsize: 4
12972d38120SMatthew G. Knepley
13072d38120SMatthew G. Knepley testset:
13172d38120SMatthew G. Knepley suffix: 3d
13272d38120SMatthew G. Knepley args: -dim 3 -da_grid_x 8 -da_grid_y 8 -da_grid_z 8 -np 4 -da_bd_all {{none periodic}} -pic_sw_view
13372d38120SMatthew G. Knepley
13472d38120SMatthew G. Knepley test:
13572d38120SMatthew G. Knepley suffix: p1
13672d38120SMatthew G. Knepley
13772d38120SMatthew G. Knepley test:
13872d38120SMatthew G. Knepley suffix: p2
13972d38120SMatthew G. Knepley nsize: 2
14072d38120SMatthew G. Knepley
14172d38120SMatthew G. Knepley test:
14272d38120SMatthew G. Knepley suffix: p4
14372d38120SMatthew G. Knepley nsize: 4
14472d38120SMatthew G. Knepley
14572d38120SMatthew G. Knepley TEST*/
146