void PetscDMNonlinearSolver<T>::init() { PetscErrorCode ierr; DM dm; this->PetscNonlinearSolver<T>::init(); // Attaching a DM with the function and Jacobian callbacks to SNES. ierr = DMCreateLibMesh(libMesh::COMM_WORLD, this->system(), &dm); CHKERRABORT(libMesh::COMM_WORLD, ierr); ierr = DMSetFromOptions(dm); CHKERRABORT(libMesh::COMM_WORLD, ierr); ierr = DMSetUp(dm); CHKERRABORT(libMesh::COMM_WORLD, ierr); ierr = SNESSetDM(this->_snes, dm); CHKERRABORT(libMesh::COMM_WORLD, ierr); // SNES now owns the reference to dm. ierr = DMDestroy(&dm); CHKERRABORT(libMesh::COMM_WORLD, ierr); KSP ksp; ierr = SNESGetKSP (this->_snes, &ksp); CHKERRABORT(libMesh::COMM_WORLD,ierr); // Set the tolerances for the iterative solver. Use the user-supplied // tolerance for the relative residual & leave the others at default values ierr = KSPSetTolerances (ksp, this->initial_linear_tolerance, PETSC_DEFAULT,PETSC_DEFAULT, this->max_linear_iterations); CHKERRABORT(libMesh::COMM_WORLD,ierr); // Set the tolerances for the non-linear solver. ierr = SNESSetTolerances(this->_snes, this->absolute_residual_tolerance, this->relative_residual_tolerance, this->absolute_step_tolerance, this->max_nonlinear_iterations, this->max_function_evaluations); CHKERRABORT(libMesh::COMM_WORLD,ierr); //Pull in command-line options KSPSetFromOptions(ksp); SNESSetFromOptions(this->_snes); }
PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user, DM *dm) { PetscInt dim = user->dim; PetscBool interpolate = user->interpolate; PetscReal refinementLimit = user->refinementLimit; const char *partitioner = user->partitioner; PetscErrorCode ierr; PetscFunctionBeginUser; ierr = PetscLogEventBegin(user->createMeshEvent,0,0,0,0);CHKERRQ(ierr); ierr = DMPlexCreateBoxMesh(comm, dim, interpolate, dm);CHKERRQ(ierr); { DM refinedMesh = NULL; DM distributedMesh = NULL; /* Refine mesh using a volume constraint */ ierr = DMPlexSetRefinementLimit(*dm, refinementLimit);CHKERRQ(ierr); ierr = DMRefine(*dm, comm, &refinedMesh);CHKERRQ(ierr); if (refinedMesh) { ierr = DMDestroy(dm);CHKERRQ(ierr); *dm = refinedMesh; } /* Distribute mesh over processes */ ierr = DMPlexDistribute(*dm, partitioner, 0, &distributedMesh);CHKERRQ(ierr); if (distributedMesh) { ierr = DMDestroy(dm);CHKERRQ(ierr); *dm = distributedMesh; } } ierr = DMSetFromOptions(*dm);CHKERRQ(ierr); ierr = PetscLogEventEnd(user->createMeshEvent,0,0,0,0);CHKERRQ(ierr); user->dm = *dm; PetscFunctionReturn(0); }
int main(int argc, char **argv) { DM dm; char typeString[256] = {'\0'}; PetscViewer viewer = NULL; PetscBool flg; PetscErrorCode ierr; ierr = PetscInitialize(&argc, &argv, NULL,help);if (ierr) return ierr; ierr = DMCreate(PETSC_COMM_WORLD, &dm);CHKERRQ(ierr); ierr = PetscStrncpy(typeString,DMFOREST,256);CHKERRQ(ierr); ierr = PetscOptionsBegin(PETSC_COMM_WORLD,NULL,"DM Forest example options",NULL);CHKERRQ(ierr); ierr = PetscOptionsString("-dm_type","The type of the dm",NULL,DMFOREST,typeString,sizeof(typeString),NULL);CHKERRQ(ierr); ierr = PetscOptionsEnd();CHKERRQ(ierr); ierr = DMSetType(dm,(DMType) typeString);CHKERRQ(ierr); ierr = DMSetFromOptions(dm);CHKERRQ(ierr); ierr = DMSetUp(dm);CHKERRQ(ierr); ierr = PetscOptionsGetViewer(PETSC_COMM_WORLD,NULL,"-dm_view",&viewer,NULL,&flg);CHKERRQ(ierr); if (flg) { ierr = DMView(dm,viewer);CHKERRQ(ierr); } ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); ierr = DMDestroy(&dm);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }
void petscSetupDM (NonlinearSystem & nl) { #if !PETSC_VERSION_LESS_THAN(3,3,0) PetscErrorCode ierr; // Initialize the part of the DM package that's packaged with Moose; in the PETSc source tree this call would be in DMInitializePackage() ierr = DMMooseRegisterAll(); CHKERRABORT(nl.comm().get(),ierr); // Create and set up the DM that will consume the split options and deal with block matrices. PetscNonlinearSolver<Number> *petsc_solver = dynamic_cast<PetscNonlinearSolver<Number> *>(nl.sys().nonlinear_solver.get()); SNES snes = petsc_solver->snes(); /* FIXME: reset the DM, do not recreate it anew every time? */ DM dm = PETSC_NULL; ierr = DMCreateMoose(nl.comm().get(), nl, &dm); CHKERRABORT(nl.comm().get(),ierr); ierr = DMSetFromOptions(dm); CHKERRABORT(nl.comm().get(),ierr); ierr = DMSetUp(dm); CHKERRABORT(nl.comm().get(),ierr); ierr = SNESSetDM(snes,dm); CHKERRABORT(nl.comm().get(),ierr); ierr = DMDestroy(&dm); CHKERRABORT(nl.comm().get(),ierr); ierr = SNESSetUpdate(snes,SNESUpdateDMMoose); CHKERRABORT(nl.comm().get(),ierr); #endif }
int main(int argc,char **argv) { PetscErrorCode ierr; KSP ksp; DM da,shell; PetscInt levels; ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr; ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRQ(ierr); ierr = DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,129,1,1,0,&da);CHKERRQ(ierr); ierr = DMSetFromOptions(da);CHKERRQ(ierr); ierr = DMSetUp(da);CHKERRQ(ierr); ierr = MyDMShellCreate(PETSC_COMM_WORLD,da,&shell);CHKERRQ(ierr); /* these two lines are not needed but allow PCMG to automatically know how many multigrid levels the user wants */ ierr = DMGetRefineLevel(da,&levels);CHKERRQ(ierr); ierr = DMSetRefineLevel(shell,levels);CHKERRQ(ierr); ierr = KSPSetDM(ksp,shell);CHKERRQ(ierr); ierr = KSPSetComputeRHS(ksp,ComputeRHS,NULL);CHKERRQ(ierr); ierr = KSPSetComputeOperators(ksp,ComputeMatrix,NULL);CHKERRQ(ierr); ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr); ierr = KSPSolve(ksp,NULL,NULL);CHKERRQ(ierr); ierr = KSPDestroy(&ksp);CHKERRQ(ierr); ierr = DMDestroy(&shell);CHKERRQ(ierr); ierr = DMDestroy(&da);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }
int main(int argc,char **argv) { DM dm; Vec X,Y; PetscErrorCode ierr; PetscInt dof = 10; ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr; ierr = PetscOptionsGetInt(NULL,NULL,"-dof",&dof,NULL);CHKERRQ(ierr); ierr = DMDACreate3d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DMDA_STENCIL_STAR,-128,-128,-128,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,dof,1,NULL,NULL,NULL,&dm);CHKERRQ(ierr); ierr = DMSetFromOptions(dm);CHKERRQ(ierr); ierr = DMSetUp(dm);CHKERRQ(ierr); ierr = PetscMemoryTrace("DMDACreate3d ");CHKERRQ(ierr); ierr = DMCreateGlobalVector(dm,&X);CHKERRQ(ierr); ierr = PetscMemoryTrace("DMCreateGlobalVector"); ierr = DMCreateGlobalVector(dm,&Y);CHKERRQ(ierr); ierr = PetscMemoryTrace("DMCreateGlobalVector");CHKERRQ(ierr); ierr = VecDestroy(&X);CHKERRQ(ierr); ierr = VecDestroy(&Y);CHKERRQ(ierr); ierr = DMDestroy(&dm);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }
PetscErrorCode DMDACreate2d(MPI_Comm comm,DMDABoundaryType bx,DMDABoundaryType by,DMDAStencilType stencil_type, PetscInt M,PetscInt N,PetscInt m,PetscInt n,PetscInt dof,PetscInt s,const PetscInt lx[],const PetscInt ly[],DM *da) { PetscErrorCode ierr; PetscFunctionBegin; ierr = DMDACreate(comm, da); CHKERRQ(ierr); ierr = DMDASetDim(*da, 2); CHKERRQ(ierr); ierr = DMDASetSizes(*da, M, N, 1); CHKERRQ(ierr); ierr = DMDASetNumProcs(*da, m, n, PETSC_DECIDE); CHKERRQ(ierr); ierr = DMDASetBoundaryType(*da, bx, by, DMDA_BOUNDARY_NONE); CHKERRQ(ierr); ierr = DMDASetDof(*da, dof); CHKERRQ(ierr); ierr = DMDASetStencilType(*da, stencil_type); CHKERRQ(ierr); ierr = DMDASetStencilWidth(*da, s); CHKERRQ(ierr); ierr = DMDASetOwnershipRanges(*da, lx, ly, NULL); CHKERRQ(ierr); /* This violates the behavior for other classes, but right now users expect negative dimensions to be handled this way */ ierr = DMSetFromOptions(*da); CHKERRQ(ierr); ierr = DMSetUp(*da); CHKERRQ(ierr); ierr = DMViewFromOptions(*da,"-dm_view"); CHKERRQ(ierr); PetscFunctionReturn(0); }
int main(int argc, char **argv) { Mat A; KSP ksp; DM shell; Vec *left, *right; MPI_Comm c; PetscErrorCode ierr; ierr = PetscInitialize(&argc, &argv, NULL, NULL);if (ierr) return ierr; c = PETSC_COMM_WORLD; ierr = MatCreate(c, &A); CHKERRQ(ierr); ierr = MatSetSizes(A, 1, 1, PETSC_DECIDE, PETSC_DECIDE); CHKERRQ(ierr); ierr = MatSetFromOptions(A); CHKERRQ(ierr); ierr = MatSetUp(A); CHKERRQ(ierr); ierr = KSPCreate(c, &ksp); CHKERRQ(ierr); ierr = KSPSetOperators(ksp, A, A); CHKERRQ(ierr); ierr = KSPSetFromOptions(ksp); CHKERRQ(ierr); ierr = DMShellCreate(c, &shell); CHKERRQ(ierr); ierr = DMSetFromOptions(shell); CHKERRQ(ierr); ierr = DMSetUp(shell); CHKERRQ(ierr); ierr = KSPSetDM(ksp, shell); CHKERRQ(ierr); ierr = KSPCreateVecs(ksp, 1, &right, 1, &left); CHKERRQ(ierr); ierr = VecView(right[0], PETSC_VIEWER_STDOUT_(c));CHKERRQ(ierr); ierr = VecDestroyVecs(1,&right); CHKERRQ(ierr); ierr = VecDestroyVecs(1,&left); CHKERRQ(ierr); ierr = DMDestroy(&shell); CHKERRQ(ierr); ierr = KSPDestroy(&ksp); CHKERRQ(ierr); ierr = MatDestroy(&A); CHKERRQ(ierr); PetscFinalize(); return 0; }
PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user, DM *dm) { const char *filename = user->filename; PetscInt dim = user->dim; size_t len; PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscStrlen(filename, &len);CHKERRQ(ierr); if (!len) {ierr = DMPlexCreateBoxMesh(comm, dim, PETSC_TRUE, dm);CHKERRQ(ierr);} else {ierr = DMPlexCreateFromFile(comm, filename, PETSC_TRUE, dm);CHKERRQ(ierr);} ierr = DMViewFromOptions(*dm, NULL, "-orig_dm_view");CHKERRQ(ierr); { DM distributedMesh = NULL; ierr = DMPlexDistribute(*dm, 0, NULL, &distributedMesh);CHKERRQ(ierr); if (distributedMesh) { ierr = DMDestroy(dm);CHKERRQ(ierr); *dm = distributedMesh; ierr = DMViewFromOptions(*dm, NULL, "-dist_dm_view");CHKERRQ(ierr); } } ierr = DMSetFromOptions(*dm);CHKERRQ(ierr); if (user->uninterpolate) { DM udm = NULL; ierr = DMPlexUninterpolate(*dm, &udm);CHKERRQ(ierr); ierr = DMDestroy(dm);CHKERRQ(ierr); *dm = udm; ierr = DMViewFromOptions(*dm, NULL, "-un_dm_view");CHKERRQ(ierr); } ierr = PetscObjectSetName((PetscObject) *dm, "Test Mesh");CHKERRQ(ierr); ierr = DMViewFromOptions(*dm, NULL, "-dm_view");CHKERRQ(ierr); PetscFunctionReturn(0); }
int main(int argc,char **argv) { KSP ksp; DM da; UserContext user; PetscErrorCode ierr; ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr; ierr = KSPCreate(PETSC_COMM_WORLD,&ksp);CHKERRQ(ierr); ierr = DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_PERIODIC, DM_BOUNDARY_MIRROR,DMDA_STENCIL_STAR,11,11,PETSC_DECIDE,PETSC_DECIDE,1,1,NULL,NULL,&da);CHKERRQ(ierr); ierr = DMSetFromOptions(da);CHKERRQ(ierr); ierr = DMSetUp(da);CHKERRQ(ierr); ierr = KSPSetDM(ksp,(DM)da);CHKERRQ(ierr); ierr = DMSetApplicationContext(da,&user);CHKERRQ(ierr); user.uu = 1.0; user.tt = 1.0; ierr = KSPSetComputeRHS(ksp,ComputeRHS,&user);CHKERRQ(ierr); ierr = KSPSetComputeOperators(ksp,ComputeJacobian,&user);CHKERRQ(ierr); ierr = KSPSetFromOptions(ksp);CHKERRQ(ierr); ierr = KSPSolve(ksp,NULL,NULL);CHKERRQ(ierr); ierr = DMDestroy(&da);CHKERRQ(ierr); ierr = KSPDestroy(&ksp);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }
static PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *ctx, DM *dm) { PetscInt dim = ctx->dim; PetscBool cellSimplex = ctx->cellSimplex; const char *filename = ctx->filename; const PetscInt cells[3] = {1, 1, 1}; size_t len; PetscMPIInt rank, size; PetscErrorCode ierr; PetscFunctionBegin; ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr); ierr = PetscStrlen(filename, &len);CHKERRQ(ierr); if (len) {ierr = DMPlexCreateFromFile(comm, filename, PETSC_TRUE, dm);CHKERRQ(ierr);} else {ierr = DMPlexCreateBoxMesh(comm, dim, cellSimplex, cells, NULL, NULL, NULL, PETSC_TRUE, dm);CHKERRQ(ierr);} { DM distributedMesh = NULL; PetscPartitioner part; ierr = DMPlexGetPartitioner(*dm, &part);CHKERRQ(ierr); ierr = PetscPartitionerSetFromOptions(part);CHKERRQ(ierr); /* Distribute mesh over processes */ ierr = DMPlexDistribute(*dm, 0, NULL, &distributedMesh);CHKERRQ(ierr); if (distributedMesh) { ierr = DMDestroy(dm);CHKERRQ(ierr); *dm = distributedMesh; } } ierr = PetscObjectSetName((PetscObject) *dm, "Mesh");CHKERRQ(ierr); ierr = DMSetFromOptions(*dm);CHKERRQ(ierr); ierr = DMViewFromOptions(*dm, NULL, "-dm_view");CHKERRQ(ierr); PetscFunctionReturn(0); }
int main(int argc,char *argv[]) { Mat M; Vec x,y; PetscErrorCode ierr; DM da,daf; ierr = PetscInitialize(&argc,&argv,0,help);if (ierr) return ierr; ierr = DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_STAR,4,5,PETSC_DECIDE,PETSC_DECIDE,41,1,0,0,&da);CHKERRQ(ierr); ierr = DMSetFromOptions(da);CHKERRQ(ierr); ierr = DMSetUp(da);CHKERRQ(ierr); ierr = DMRefine(da,PETSC_COMM_WORLD,&daf);CHKERRQ(ierr); ierr = DMCreateInterpolation(da,daf,&M,NULL);CHKERRQ(ierr); ierr = DMCreateGlobalVector(da,&x);CHKERRQ(ierr); ierr = DMCreateGlobalVector(daf,&y);CHKERRQ(ierr); ierr = MatMult(M,x,y);CHKERRQ(ierr); ierr = MatMultTranspose(M,y,x);CHKERRQ(ierr); ierr = DMDestroy(&da);CHKERRQ(ierr); ierr = DMDestroy(&daf);CHKERRQ(ierr); ierr = VecDestroy(&x);CHKERRQ(ierr); ierr = VecDestroy(&y);CHKERRQ(ierr); ierr = MatDestroy(&M);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }
PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user, DM *dm) { PetscInt dim = user->dim; PetscBool interpolate = user->interpolate; PetscReal refinementLimit = user->refinementLimit; PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscLogEventBegin(user->createMeshEvent,0,0,0,0);CHKERRQ(ierr); ierr = DMMeshCreateBoxMesh(comm, dim, interpolate, dm);CHKERRQ(ierr); { DM refinedMesh = PETSC_NULL; /* Refine mesh using a volume constraint */ ierr = DMMeshRefine(*dm, refinementLimit, interpolate, &refinedMesh);CHKERRQ(ierr); if (refinedMesh) { ierr = DMDestroy(dm);CHKERRQ(ierr); *dm = refinedMesh; } } ierr = PetscObjectSetName((PetscObject) *dm, "Serial Mesh");CHKERRQ(ierr); ierr = DMSetFromOptions(*dm);CHKERRQ(ierr); ierr = PetscLogEventEnd(user->createMeshEvent,0,0,0,0);CHKERRQ(ierr); user->dm = *dm; PetscFunctionReturn(0); }
PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user, DM *dm) { PetscInt dim = user->dim; PetscBool refinementUniform = user->refinementUniform; PetscBool cellSimplex = user->cellSimplex; const char *partitioner = "chaco"; PetscMPIInt rank; PetscErrorCode ierr; PetscFunctionBegin; ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); ierr = DMCreate(comm, dm);CHKERRQ(ierr); ierr = DMSetType(*dm, DMPLEX);CHKERRQ(ierr); ierr = DMPlexSetDimension(*dm, dim);CHKERRQ(ierr); switch(dim) { case 2: if (cellSimplex) { ierr = CreateSimplexHybrid_2D(comm, *dm);CHKERRQ(ierr); } else { SETERRQ(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make hybrid meshes for quadrilaterals"); } break; default: SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make hybrid meshes for dimension %d", dim); } { DM refinedMesh = PETSC_NULL; DM distributedMesh = PETSC_NULL; /* Distribute mesh over processes */ ierr = DMPlexDistribute(*dm, partitioner, 0, &distributedMesh);CHKERRQ(ierr); if (distributedMesh) { PetscInt cMax = PETSC_DETERMINE, fMax = PETSC_DETERMINE; /* Do not know how to preserve this after distribution */ if (rank) { cMax = 1; fMax = 11; } ierr = DMPlexSetHybridBounds(distributedMesh, cMax, PETSC_DETERMINE, fMax, PETSC_DETERMINE);CHKERRQ(ierr); ierr = DMDestroy(dm);CHKERRQ(ierr); *dm = distributedMesh; } if (refinementUniform) { ierr = DMPlexSetRefinementUniform(*dm, refinementUniform);CHKERRQ(ierr); ierr = DMRefine(*dm, comm, &refinedMesh);CHKERRQ(ierr); if (refinedMesh) { ierr = DMDestroy(dm);CHKERRQ(ierr); *dm = refinedMesh; } } } ierr = PetscObjectSetName((PetscObject) *dm, "Hybrid Mesh");CHKERRQ(ierr); ierr = DMSetFromOptions(*dm);CHKERRQ(ierr); user->dm = *dm; PetscFunctionReturn(0); }
int main(int argc,char **argv) { PetscErrorCode ierr; SNES snes; KSP ksp; Vec u, uexact; FishCtx user; DMDALocalInfo info; double errnorm; PetscInitialize(&argc,&argv,NULL,help); user.printevals = PETSC_FALSE; ierr = PetscOptionsBegin(PETSC_COMM_WORLD, "f3_", "options for fish3", ""); CHKERRQ(ierr); ierr = PetscOptionsBool("-printevals","residual and Jacobian routines report grid", "fish3.c",user.printevals,&user.printevals,NULL);CHKERRQ(ierr); ierr = PetscOptionsEnd(); CHKERRQ(ierr); ierr = DMDACreate3d(COMM, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DMDA_STENCIL_STAR, 3,3,3,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE, 1,1, NULL,NULL,NULL,&(user.da)); CHKERRQ(ierr); ierr = DMSetFromOptions(user.da); CHKERRQ(ierr); ierr = DMSetUp(user.da); CHKERRQ(ierr); ierr = DMSetApplicationContext(user.da,&user);CHKERRQ(ierr); ierr = DMDAGetLocalInfo(user.da,&info); CHKERRQ(ierr); ierr = DMCreateGlobalVector(user.da,&u);CHKERRQ(ierr); ierr = VecDuplicate(u,&uexact);CHKERRQ(ierr); ierr = VecDuplicate(u,&(user.b));CHKERRQ(ierr); ierr = formExactRHS(&info,uexact,user.b,&user); CHKERRQ(ierr); ierr = SNESCreate(COMM,&snes); CHKERRQ(ierr); ierr = SNESSetDM(snes,user.da); CHKERRQ(ierr); ierr = DMDASNESSetFunctionLocal(user.da,INSERT_VALUES, (DMDASNESFunction)FormFunctionLocal,&user); CHKERRQ(ierr); ierr = DMDASNESSetJacobianLocal(user.da, (DMDASNESJacobian)FormJacobianLocal,&user); CHKERRQ(ierr); ierr = SNESGetKSP(snes,&ksp); CHKERRQ(ierr); ierr = KSPSetType(ksp,KSPCG); CHKERRQ(ierr); ierr = SNESSetFromOptions(snes); CHKERRQ(ierr); ierr = VecSet(u,0.0); CHKERRQ(ierr); ierr = SNESSolve(snes,NULL,u); CHKERRQ(ierr); ierr = VecAXPY(u,-1.0,uexact); CHKERRQ(ierr); // u <- u + (-1.0) uexact ierr = VecNorm(u,NORM_INFINITY,&errnorm); CHKERRQ(ierr); ierr = PetscPrintf(COMM,"on %d x %d x %d grid: error |u-uexact|_inf = %g\n", info.mx,info.my,info.mz,errnorm); CHKERRQ(ierr); VecDestroy(&u); VecDestroy(&uexact); VecDestroy(&(user.b)); SNESDestroy(&snes); DMDestroy(&(user.da)); PetscFinalize(); return 0; }
PetscErrorCode CreateTensorProductHybrid_2D(MPI_Comm comm, PetscInt testNum, DM *dm) { DM idm, hdm = NULL; DMLabel faultLabel, hybridLabel; PetscInt p; PetscMPIInt rank; PetscErrorCode ierr; PetscFunctionBegin; ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); if (!rank) { PetscInt numPoints[2] = {6, 2}; PetscInt coneSize[8] = {4, 4, 0, 0, 0, 0, 0, 0}; PetscInt cones[8] = {2, 3, 4, 5, 3, 6, 7, 4,}; PetscInt coneOrientations[8] = {0, 0, 0, 0, 0, 0, 0, 0}; PetscScalar vertexCoords[12] = {-1.0, -0.5, 0.0, -0.5, 0.0, 0.5, -1.0, 0.5, 1.0, -0.5, 1.0, 0.5}; PetscInt faultPoints[2] = {3, 4}; ierr = DMPlexCreateFromDAG(*dm, 1, numPoints, coneSize, cones, coneOrientations, vertexCoords);CHKERRQ(ierr); for(p = 0; p < 2; ++p) {ierr = DMSetLabelValue(*dm, "fault", faultPoints[p], 1);CHKERRQ(ierr);} ierr = PetscObjectSetOptionsPrefix((PetscObject) *dm, "orig_");CHKERRQ(ierr); ierr = DMSetFromOptions(*dm);CHKERRQ(ierr); ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr); ierr = PetscObjectSetOptionsPrefix((PetscObject) idm, "in_");CHKERRQ(ierr); ierr = DMSetFromOptions(idm);CHKERRQ(ierr); ierr = DMViewFromOptions(idm, NULL, "-dm_view");CHKERRQ(ierr); ierr = DMGetLabel(*dm, "fault", &faultLabel);CHKERRQ(ierr); ierr = DMPlexCreateHybridMesh(idm, faultLabel, NULL, &hybridLabel, NULL, NULL, &hdm);CHKERRQ(ierr); ierr = DMLabelDestroy(&hybridLabel);CHKERRQ(ierr); } else { PetscInt numPoints[3] = {0, 0, 0}; ierr = DMPlexCreateFromDAG(*dm, 1, numPoints, NULL, NULL, NULL, NULL);CHKERRQ(ierr); ierr = DMPlexInterpolate(*dm, &idm);CHKERRQ(ierr); ierr = DMViewFromOptions(idm, NULL, "-in_dm_view");CHKERRQ(ierr); ierr = DMPlexCreateHybridMesh(idm, NULL, NULL, NULL, NULL, NULL, &hdm);CHKERRQ(ierr); } ierr = DMDestroy(&idm);CHKERRQ(ierr); ierr = DMDestroy(dm);CHKERRQ(ierr); *dm = hdm; PetscFunctionReturn(0); }
static PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user, DM *dm) { PetscErrorCode ierr; PetscFunctionBeginUser; /* Create box mesh */ ierr = DMPlexCreateBoxMesh(comm, user->dim, user->simplex, user->cells, NULL, NULL, NULL, PETSC_TRUE, dm);CHKERRQ(ierr); /* Distribute mesh over processes */ { DM dmDist = NULL; PetscPartitioner part; ierr = DMPlexGetPartitioner(*dm, &part);CHKERRQ(ierr); ierr = PetscPartitionerSetFromOptions(part);CHKERRQ(ierr); ierr = DMPlexDistribute(*dm, 0, NULL, &dmDist);CHKERRQ(ierr); if (dmDist) { ierr = DMDestroy(dm);CHKERRQ(ierr); *dm = dmDist; } } /* TODO: This should be pulled into the library */ { char convType[256]; PetscBool flg; ierr = PetscOptionsBegin(comm, "", "Mesh conversion options", "DMPLEX");CHKERRQ(ierr); ierr = PetscOptionsFList("-dm_plex_convert_type","Convert DMPlex to another format","ex12",DMList,DMPLEX,convType,256,&flg);CHKERRQ(ierr); ierr = PetscOptionsEnd(); if (flg) { DM dmConv; ierr = DMConvert(*dm,convType,&dmConv);CHKERRQ(ierr); if (dmConv) { ierr = DMDestroy(dm);CHKERRQ(ierr); *dm = dmConv; } } } /* TODO: This should be pulled into the library */ ierr = DMLocalizeCoordinates(*dm);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) *dm, "Mesh");CHKERRQ(ierr); ierr = DMSetApplicationContext(*dm, user);CHKERRQ(ierr); ierr = DMSetFromOptions(*dm);CHKERRQ(ierr); ierr = DMViewFromOptions(*dm, NULL, "-dm_view");CHKERRQ(ierr); /* TODO: Add a hierachical viewer */ if (user->spectral) { PetscInt planeDir[2] = {0, 1}; PetscReal planeCoord[2] = {0., 1.}; ierr = CreateSpectralPlanes(*dm, 2, planeDir, planeCoord, user);CHKERRQ(ierr); } PetscFunctionReturn(0); }
PetscErrorCode PETScExternalSolverCreate(MPI_Comm comm, TS * ts) { DM da; PetscErrorCode ierr; PetscFunctionBeginUser; /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Create distributed array (DMDA) to manage parallel grid and vectors - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = DMDACreate2d(comm, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE, DMDA_STENCIL_STAR, 11, 11, PETSC_DECIDE, PETSC_DECIDE, 1, 1, NULL, NULL, &da); CHKERRQ(ierr); ierr = DMSetFromOptions(da); CHKERRQ(ierr); ierr = DMSetUp(da); CHKERRQ(ierr); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Create timestepping solver context - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = TSCreate(comm, ts); CHKERRQ(ierr); ierr = TSSetProblemType(*ts, TS_NONLINEAR); CHKERRQ(ierr); ierr = TSSetType(*ts, TSBEULER); CHKERRQ(ierr); ierr = TSSetDM(*ts, da); CHKERRQ(ierr); ierr = DMDestroy(&da); CHKERRQ(ierr); ierr = TSSetIFunction(*ts, NULL, FormIFunction, nullptr); CHKERRQ(ierr); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Set Jacobian evaluation routine - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = TSSetIJacobian(*ts, NULL, NULL, FormIJacobian, NULL); CHKERRQ(ierr); PetscFunctionReturn(0); }
static PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user, DM *newdm) { PetscInt dim = user->dim; PetscBool cellSimplex = user->cellSimplex; PetscErrorCode ierr; PetscFunctionBegin; switch (dim) { case 2: if (cellSimplex) { ierr = CreateSimplex_2D(comm, newdm);CHKERRQ(ierr); } else { ierr = CreateQuad_2D(comm, newdm);CHKERRQ(ierr); } break; case 3: if (cellSimplex) { ierr = CreateSimplex_3D(comm, newdm);CHKERRQ(ierr); } else { ierr = CreateHex_3D(comm, newdm);CHKERRQ(ierr); } break; default: SETERRQ1(comm, PETSC_ERR_ARG_OUTOFRANGE, "Cannot make meshes for dimension %d", dim); } if (user->refinementLimit > 0.0) { DM rdm; const char *name; ierr = DMPlexSetRefinementUniform(*newdm, PETSC_FALSE);CHKERRQ(ierr); ierr = DMPlexSetRefinementLimit(*newdm, user->refinementLimit);CHKERRQ(ierr); ierr = DMRefine(*newdm, PETSC_COMM_SELF, &rdm);CHKERRQ(ierr); ierr = PetscObjectGetName((PetscObject) *newdm, &name);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) rdm, name);CHKERRQ(ierr); ierr = DMDestroy(newdm);CHKERRQ(ierr); *newdm = rdm; } if (user->interpolate) { DM idm = NULL; const char *name; ierr = DMPlexInterpolate(*newdm, &idm);CHKERRQ(ierr); ierr = PetscObjectGetName((PetscObject) *newdm, &name);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) idm, name);CHKERRQ(ierr); ierr = DMPlexCopyCoordinates(*newdm, idm);CHKERRQ(ierr); ierr = DMDestroy(newdm);CHKERRQ(ierr); *newdm = idm; } ierr = DMSetFromOptions(*newdm);CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode DMLibMeshCreateDomainDecompositionDM(DM dm, PetscInt dnumber, PetscInt* dsizes, char*** dblocklists, DM* ddm) { PetscErrorCode ierr; PetscBool islibmesh; PetscFunctionBegin; PetscValidHeaderSpecific(dm,DM_CLASSID,1); ierr = PetscObjectTypeCompare((PetscObject)dm, DMLIBMESH,&islibmesh); if(!islibmesh) SETERRQ2(((PetscObject)dm)->comm, PETSC_ERR_ARG_WRONG, "Got DM oftype %s, not of type %s", ((PetscObject)dm)->type_name, DMLIBMESH); if(dnumber < 0) SETERRQ1(((PetscObject)dm)->comm, PETSC_ERR_ARG_WRONG, "Negative number %D of decomposition parts", dnumber); PetscValidPointer(ddm,5); DM_libMesh *dlm = (DM_libMesh *)(dm->data); ierr = DMCreate(((PetscObject)dm)->comm, ddm); CHKERRQ(ierr); ierr = DMSetType(*ddm, DMLIBMESH); CHKERRQ(ierr); DM_libMesh *ddlm = (DM_libMesh *)((*ddm)->data); ddlm->sys = dlm->sys; ddlm->varids = dlm->varids; ddlm->varnames = dlm->varnames; ddlm->blockids = dlm->blockids; ddlm->blocknames = dlm->blocknames; ddlm->decomposition = new(std::vector<std::set<unsigned int> >); ddlm->decomposition_type = DMLIBMESH_DOMAIN_DECOMPOSITION; if(dnumber) { for(PetscInt d = 0; d < dnumber; ++d) { if(dsizes[d] < 0) SETERRQ2(((PetscObject)dm)->comm, PETSC_ERR_ARG_WRONG, "Negative size %D of decomposition part %D", dsizes[d],d); ddlm->decomposition->push_back(std::set<unsigned int>()); for(PetscInt b = 0; b < dsizes[d]; ++b) { std::string bname(dblocklists[d][b]); std::map<std::string, unsigned int>::const_iterator bit = dlm->blockids->find(bname); if(bit == dlm->blockids->end()) SETERRQ3(((PetscObject)dm)->comm, PETSC_ERR_ARG_WRONG, "Block %D on the %D-th list with name %s is not owned by this DM", b, d, dblocklists[d][b]); unsigned int bid = bit->second; (*ddlm->decomposition)[d].insert(bid); } } } else { /* Empty splits indicate default: split all blocks with one per split. */ PetscInt d = 0; for(std::map<std::string, unsigned int>::const_iterator bit = ddlm->blockids->begin(); bit != ddlm->blockids->end(); ++bit) { ddlm->decomposition->push_back(std::set<unsigned int>()); unsigned int bid = bit->second; std::string bname = bit->first; (*ddlm->decomposition)[d].insert(bid); ++d; } } ierr = DMLibMeshSetUpName_Private(*ddm); CHKERRQ(ierr); ierr = DMSetFromOptions(*ddm); CHKERRQ(ierr); ierr = DMSetUp(*ddm); CHKERRQ(ierr); PetscFunctionReturn(0); }
int main(int argc,char **argv) { PetscInt M = 5,N = 4,P = 3, m = PETSC_DECIDE,n = PETSC_DECIDE,p = PETSC_DECIDE,dim = 1; PetscErrorCode ierr; DM dac,daf; DMBoundaryType bx = DM_BOUNDARY_NONE,by=DM_BOUNDARY_NONE,bz=DM_BOUNDARY_NONE; DMDAStencilType stype = DMDA_STENCIL_BOX; Mat A; ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr; ierr = PetscOptionsGetInt(NULL,NULL,"-M",&M,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,NULL,"-N",&N,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,NULL,"-P",&P,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,NULL,"-p",&p,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,NULL,"-dim",&dim,NULL);CHKERRQ(ierr); /* Create distributed array and get vectors */ if (dim == 1) { ierr = DMDACreate1d(PETSC_COMM_WORLD,bx,M,1,1,NULL,&dac);CHKERRQ(ierr); } else if (dim == 2) { ierr = DMDACreate2d(PETSC_COMM_WORLD,bx,by,stype,M,N,PETSC_DECIDE,PETSC_DECIDE,1,1,NULL,NULL,&dac);CHKERRQ(ierr); } else if (dim == 3) { ierr = DMDACreate3d(PETSC_COMM_WORLD,bx,by,bz,stype,M,N,P,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,1,1,NULL,NULL,NULL,&dac);CHKERRQ(ierr); } else SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"dim must be 1,2, or 3"); ierr = DMSetFromOptions(dac);CHKERRQ(ierr); ierr = DMSetUp(dac);CHKERRQ(ierr); ierr = DMRefine(dac,PETSC_COMM_WORLD,&daf);CHKERRQ(ierr); ierr = DMDASetUniformCoordinates(dac,0.0,1.0,0.0,1.0,0.0,1.0);CHKERRQ(ierr); if (dim == 1) { ierr = SetCoordinates1d(daf);CHKERRQ(ierr); } else if (dim == 2) { ierr = SetCoordinates2d(daf);CHKERRQ(ierr); } else if (dim == 3) { ierr = SetCoordinates3d(daf);CHKERRQ(ierr); } ierr = DMCreateInterpolation(dac,daf,&A,0);CHKERRQ(ierr); /* Free memory */ ierr = DMDestroy(&dac);CHKERRQ(ierr); ierr = DMDestroy(&daf);CHKERRQ(ierr); ierr = MatDestroy(&A);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }
int main(int argc,char **argv) { PetscMPIInt rank; PetscInt M = 10,N = 8,m = PETSC_DECIDE,n = PETSC_DECIDE; PetscErrorCode ierr; DM da; PetscViewer viewer; Vec local,global; PetscScalar value; ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr; ierr = PetscViewerDrawOpen(PETSC_COMM_WORLD,0,"",300,0,300,300,&viewer);CHKERRQ(ierr); /* Read options */ ierr = PetscOptionsGetInt(NULL,NULL,"-M",&M,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,NULL,"-N",&N,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,NULL,"-m",&m,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,NULL,"-n",&n,NULL);CHKERRQ(ierr); /* Create distributed array and get vectors */ ierr = DMDACreate2d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE,DMDA_STENCIL_BOX,M,N,m,n,1,1,NULL,NULL,&da);CHKERRQ(ierr); ierr = DMSetFromOptions(da);CHKERRQ(ierr); ierr = DMSetUp(da);CHKERRQ(ierr); ierr = DMCreateGlobalVector(da,&global);CHKERRQ(ierr); ierr = DMCreateLocalVector(da,&local);CHKERRQ(ierr); value = -3.0; ierr = VecSet(global,value);CHKERRQ(ierr); ierr = DMGlobalToLocalBegin(da,global,INSERT_VALUES,local);CHKERRQ(ierr); ierr = DMGlobalToLocalEnd(da,global,INSERT_VALUES,local);CHKERRQ(ierr); ierr = MPI_Comm_rank(PETSC_COMM_WORLD,&rank);CHKERRQ(ierr); value = rank+1; ierr = VecScale(local,value);CHKERRQ(ierr); ierr = DMLocalToGlobalBegin(da,local,ADD_VALUES,global);CHKERRQ(ierr); ierr = DMLocalToGlobalEnd(da,local,ADD_VALUES,global);CHKERRQ(ierr); ierr = VecView(global,PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = DMView(da,viewer);CHKERRQ(ierr); /* Free memory */ ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); ierr = VecDestroy(&local);CHKERRQ(ierr); ierr = VecDestroy(&global);CHKERRQ(ierr); ierr = DMDestroy(&da);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }
PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user, DM *dm) { PetscInt dim = user->dim; PetscErrorCode ierr; PetscFunctionBeginUser; ierr = DMPlexCreateBoxMesh(comm, dim, user->simplex, user->cells, NULL, NULL, NULL, PETSC_TRUE, dm);CHKERRQ(ierr); { Parameter *param; Vec coordinates; PetscScalar *coords; PetscReal alpha; PetscInt cdim, N, bs, i; ierr = DMGetCoordinateDim(*dm, &cdim);CHKERRQ(ierr); ierr = DMGetCoordinates(*dm, &coordinates);CHKERRQ(ierr); ierr = VecGetLocalSize(coordinates, &N);CHKERRQ(ierr); ierr = VecGetBlockSize(coordinates, &bs);CHKERRQ(ierr); if (bs != cdim) SETERRQ2(comm, PETSC_ERR_ARG_WRONG, "Invalid coordinate blocksize %D != embedding dimension %D", bs, cdim); ierr = VecGetArray(coordinates, &coords);CHKERRQ(ierr); ierr = PetscBagGetData(user->bag, (void **) ¶m);CHKERRQ(ierr); alpha = param->alpha; for (i = 0; i < N; i += cdim) { PetscScalar x = coords[i+0]; PetscScalar y = coords[i+1]; coords[i+0] = PetscCosReal(alpha)*x - PetscSinReal(alpha)*y; coords[i+1] = PetscSinReal(alpha)*x + PetscCosReal(alpha)*y; } ierr = VecRestoreArray(coordinates, &coords);CHKERRQ(ierr); ierr = DMSetCoordinates(*dm, coordinates);CHKERRQ(ierr); } { DM pdm = NULL; PetscPartitioner part; ierr = DMPlexGetPartitioner(*dm, &part);CHKERRQ(ierr); ierr = PetscPartitionerSetFromOptions(part);CHKERRQ(ierr); ierr = DMPlexDistribute(*dm, 0, NULL, &pdm);CHKERRQ(ierr); if (pdm) { ierr = DMDestroy(dm);CHKERRQ(ierr); *dm = pdm; } } ierr = DMSetFromOptions(*dm);CHKERRQ(ierr); ierr = DMViewFromOptions(*dm, NULL, "-dm_view");CHKERRQ(ierr); PetscFunctionReturn(0); }
static PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user, DM *dm) { DM distributedMesh = NULL; PetscErrorCode ierr; PetscFunctionBeginUser; ierr = DMPlexCreateBoxMesh(comm, 2, PETSC_TRUE, dm);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) *dm, "Mesh");CHKERRQ(ierr); ierr = DMPlexDistribute(*dm, 0, NULL, &distributedMesh);CHKERRQ(ierr); if (distributedMesh) { ierr = DMDestroy(dm);CHKERRQ(ierr); *dm = distributedMesh; } ierr = DMSetFromOptions(*dm);CHKERRQ(ierr); ierr = DMViewFromOptions(*dm, NULL, "-dm_view");CHKERRQ(ierr); PetscFunctionReturn(0); }
int main(int argc, char **argv) { DM dm; AppCtx user; /* user-defined work context */ PetscErrorCode ierr; ierr = PetscInitialize(&argc, &argv, PETSC_NULL, help);CHKERRQ(ierr); ierr = ProcessOptions(PETSC_COMM_WORLD, &user);CHKERRQ(ierr); ierr = DMPatchCreateGrid(PETSC_COMM_WORLD, user.dim, user.patchSize, user.commSize, user.gridSize, &dm);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) dm, "Patch Mesh");CHKERRQ(ierr); ierr = DMSetFromOptions(dm);CHKERRQ(ierr); ierr = DMSetUp(dm);CHKERRQ(ierr); ierr = DMView(dm, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = DMPatchSolve(dm);CHKERRQ(ierr); ierr = DMDestroy(&dm);CHKERRQ(ierr); ierr = PetscFinalize(); return 0; }
void petscSetupDM (NonlinearSystem & nl) { #if !PETSC_VERSION_LESS_THAN(3,3,0) PetscErrorCode ierr; PetscBool ismoose; DM dm = PETSC_NULL; // Initialize the part of the DM package that's packaged with Moose; in the PETSc source tree this call would be in DMInitializePackage() ierr = DMMooseRegisterAll(); CHKERRABORT(nl.comm().get(),ierr); // Create and set up the DM that will consume the split options and deal with block matrices. PetscNonlinearSolver<Number> *petsc_solver = dynamic_cast<PetscNonlinearSolver<Number> *>(nl.sys().nonlinear_solver.get()); SNES snes = petsc_solver->snes(); // if there exists a DMMoose object, not to recreate a new one ierr = SNESGetDM(snes, &dm); CHKERRABORT(nl.comm().get(), ierr); if (dm) { ierr = PetscObjectTypeCompare((PetscObject)dm, DMMOOSE, &ismoose); CHKERRABORT(nl.comm().get(), ierr); if (ismoose) return; } ierr = DMCreateMoose(nl.comm().get(), nl, &dm); CHKERRABORT(nl.comm().get(),ierr); ierr = DMSetFromOptions(dm); CHKERRABORT(nl.comm().get(),ierr); ierr = DMSetUp(dm); CHKERRABORT(nl.comm().get(),ierr); ierr = SNESSetDM(snes,dm); CHKERRABORT(nl.comm().get(),ierr); ierr = DMDestroy(&dm); CHKERRABORT(nl.comm().get(),ierr); // We temporarily comment out this updating function because // we lack an approach to check if the problem // structure has been changed from the last iteration. // The indices will be rebuilt for every timestep. // TODO: figure out a way to check the structure changes of the // matrix // ierr = SNESSetUpdate(snes,SNESUpdateDMMoose); // CHKERRABORT(nl.comm().get(),ierr); #endif }
example is used for testing the subroutine MatLoad_MPI_DA\n\n"; #include <petscdm.h> #include <petscdmda.h> int main(int argc,char **argv) { PetscInt X = 10,Y = 8,Z=8; PetscErrorCode ierr; DM da; PetscViewer viewer; Mat A; ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr; ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,"temp.dat",FILE_MODE_WRITE,&viewer);CHKERRQ(ierr); /* Read options */ ierr = PetscOptionsGetInt(NULL,NULL,"-X",&X,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,NULL,"-Y",&Y,NULL);CHKERRQ(ierr); ierr = PetscOptionsGetInt(NULL,NULL,"-Z",&Z,NULL);CHKERRQ(ierr); /* Create distributed array and get vectors */ ierr = DMDACreate3d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DM_BOUNDARY_NONE,DMDA_STENCIL_STAR,X,Y,Z,PETSC_DECIDE,PETSC_DECIDE,PETSC_DECIDE,1,1,NULL,NULL,NULL,&da);CHKERRQ(ierr); ierr = DMSetFromOptions(da);CHKERRQ(ierr); ierr = DMSetUp(da);CHKERRQ(ierr); ierr = DMSetMatType(da,MATMPIAIJ);CHKERRQ(ierr); ierr = DMCreateMatrix(da,&A);CHKERRQ(ierr); ierr = MatShift(A,X);CHKERRQ(ierr); ierr = MatView(A,viewer);CHKERRQ(ierr); ierr = MatDestroy(&A);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); ierr = PetscViewerBinaryOpen(PETSC_COMM_WORLD,"temp.dat",FILE_MODE_READ,&viewer);CHKERRQ(ierr); ierr = DMCreateMatrix(da,&A);CHKERRQ(ierr); ierr = MatLoad(A,viewer);CHKERRQ(ierr); /* Free memory */ ierr = MatDestroy(&A);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); ierr = DMDestroy(&da);CHKERRQ(ierr); ierr = PetscFinalize(); return ierr; }
PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user, DM *dm) { PetscInt dim = user->dim; PetscBool interpolate = user->interpolate; PetscReal refinementLimit = user->refinementLimit; const char *partitioner = "chaco"; PetscErrorCode ierr; PetscFunctionBegin; if (user->simplex) { DM refinedMesh = NULL; DM distributedMesh = NULL; ierr = DMPlexCreateBoxMesh(comm, dim, interpolate, dm);CHKERRQ(ierr); /* Refine mesh using a volume constraint */ ierr = DMPlexSetRefinementLimit(*dm, refinementLimit);CHKERRQ(ierr); ierr = DMRefine(*dm, comm, &refinedMesh);CHKERRQ(ierr); if (refinedMesh) { ierr = DMDestroy(dm);CHKERRQ(ierr); *dm = refinedMesh; } /* Distribute mesh over processes */ ierr = DMPlexDistribute(*dm, partitioner, 0, NULL, &distributedMesh);CHKERRQ(ierr); if (distributedMesh) { ierr = DMDestroy(dm);CHKERRQ(ierr); *dm = distributedMesh; } ierr = PetscObjectSetName((PetscObject) *dm, "Simplical Mesh");CHKERRQ(ierr); } else { switch (user->dim) { case 2: ierr = DMDACreate2d(comm, DMDA_BOUNDARY_NONE, DMDA_BOUNDARY_NONE, DMDA_STENCIL_BOX, -3, -3, PETSC_DETERMINE, PETSC_DETERMINE, 1, 1, NULL, NULL, dm);CHKERRQ(ierr); ierr = DMDASetVertexCoordinates(*dm, 0.0, 1.0, 0.0, 1.0, 0.0, 1.0);CHKERRQ(ierr); break; default: SETERRQ1(PETSC_COMM_SELF, PETSC_ERR_ARG_OUTOFRANGE, "Cannot create structured mesh of dimension %d", dim); } ierr = PetscObjectSetName((PetscObject) *dm, "Hexahedral Mesh");CHKERRQ(ierr); } ierr = DMSetFromOptions(*dm);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@C DMDACreate1d - Creates an object that will manage the communication of one-dimensional regular array data that is distributed across some processors. Collective on MPI_Comm Input Parameters: + comm - MPI communicator . bx - type of ghost cells at the boundary the array should have, if any. Use DM_BOUNDARY_NONE, DM_BOUNDARY_GHOSTED, or DM_BOUNDARY_PERIODIC. . M - global dimension of the array (use -M to indicate that it may be set to a different value from the command line with -da_grid_x <M>) . dof - number of degrees of freedom per node . s - stencil width - lx - array containing number of nodes in the X direction on each processor, or NULL. If non-null, must be of length as the number of processes in the MPI_Comm. Output Parameter: . da - the resulting distributed array object Options Database Key: + -dm_view - Calls DMView() at the conclusion of DMDACreate1d() . -da_grid_x <nx> - number of grid points in x direction; can set if M < 0 . -da_refine_x <rx> - refinement factor - -da_refine <n> - refine the DMDA n times before creating it, if M < 0 Level: beginner Notes: The array data itself is NOT stored in the DMDA, it is stored in Vec objects; The appropriate vector objects can be obtained with calls to DMCreateGlobalVector() and DMCreateLocalVector() and calls to VecDuplicate() if more are needed. .keywords: distributed array, create, one-dimensional .seealso: DMDestroy(), DMView(), DMDACreate2d(), DMDACreate3d(), DMGlobalToLocalBegin(), DMDASetRefinementFactor(), DMGlobalToLocalEnd(), DMLocalToGlobalBegin(), DMLocalToLocalBegin(), DMLocalToLocalEnd(), DMDAGetRefinementFactor(), DMDAGetInfo(), DMCreateGlobalVector(), DMCreateLocalVector(), DMDACreateNaturalVector(), DMLoad(), DMDAGetOwnershipRanges() @*/ PetscErrorCode DMDACreate1d(MPI_Comm comm, DMBoundaryType bx, PetscInt M, PetscInt dof, PetscInt s, const PetscInt lx[], DM *da) { PetscErrorCode ierr; PetscMPIInt size; PetscFunctionBegin; ierr = DMDACreate(comm, da);CHKERRQ(ierr); ierr = DMSetDimension(*da, 1);CHKERRQ(ierr); ierr = DMDASetSizes(*da, M, 1, 1);CHKERRQ(ierr); ierr = MPI_Comm_size(comm, &size);CHKERRQ(ierr); ierr = DMDASetNumProcs(*da, size, PETSC_DECIDE, PETSC_DECIDE);CHKERRQ(ierr); ierr = DMDASetBoundaryType(*da, bx, DM_BOUNDARY_NONE, DM_BOUNDARY_NONE);CHKERRQ(ierr); ierr = DMDASetDof(*da, dof);CHKERRQ(ierr); ierr = DMDASetStencilWidth(*da, s);CHKERRQ(ierr); ierr = DMDASetOwnershipRanges(*da, lx, NULL, NULL);CHKERRQ(ierr); /* This violates the behavior for other classes, but right now users expect negative dimensions to be handled this way */ ierr = DMSetFromOptions(*da);CHKERRQ(ierr); ierr = DMSetUp(*da);CHKERRQ(ierr); PetscFunctionReturn(0); }
int main(int argc,char **args) { PetscErrorCode ierr; DM da; KSP ksp; Mat A; Vec b,u,uexact; PetscReal errnorm; DMDALocalInfo info; PetscInitialize(&argc,&args,(char*)0,help); ierr = DMDACreate1d(PETSC_COMM_WORLD, DM_BOUNDARY_NONE, 9,1,1,NULL, &da); CHKERRQ(ierr); ierr = DMSetFromOptions(da); CHKERRQ(ierr); ierr = DMSetUp(da); CHKERRQ(ierr); ierr = DMDASetUniformCoordinates(da,0.0,1.0,-1.0,-1.0,-1.0,-1.0); CHKERRQ(ierr); ierr = DMCreateMatrix(da,&A);CHKERRQ(ierr); ierr = MatSetOptionsPrefix(A,"a_"); CHKERRQ(ierr); ierr = MatSetFromOptions(A); CHKERRQ(ierr); ierr = DMCreateGlobalVector(da,&b);CHKERRQ(ierr); ierr = VecDuplicate(b,&u); CHKERRQ(ierr); ierr = VecDuplicate(b,&uexact); CHKERRQ(ierr); ierr = formExactAndRHS(da,uexact,b); CHKERRQ(ierr); ierr = formdirichletlaplacian(da,A); CHKERRQ(ierr); ierr = KSPCreate(PETSC_COMM_WORLD,&ksp); CHKERRQ(ierr); ierr = KSPSetOperators(ksp,A,A); CHKERRQ(ierr); ierr = KSPSetFromOptions(ksp); CHKERRQ(ierr); ierr = KSPSolve(ksp,b,u); CHKERRQ(ierr); ierr = VecAXPY(u,-1.0,uexact); CHKERRQ(ierr); // u <- u + (-1.0) uxact ierr = VecNorm(u,NORM_INFINITY,&errnorm); CHKERRQ(ierr); ierr = DMDAGetLocalInfo(da,&info);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD, "on %d point grid: error |u-uexact|_inf = %g\n", info.mx,errnorm); CHKERRQ(ierr); VecDestroy(&u); VecDestroy(&uexact); VecDestroy(&b); MatDestroy(&A); KSPDestroy(&ksp); DMDestroy(&da); PetscFinalize(); return 0; }