#include #include #include #include #include #include #include typedef enum {RUN_STANDALONE, RUN_COUPLED} RunType; typedef enum {NONE, LABELS, BOUNDARIES, SOLUTION} Visualization; typedef struct /* The time context */ { PetscReal total; /* total time in seconds */ PetscReal dt; /* the current time step size */ PetscInt iter; /* number of iterations so far */ /* PetscBool halfstep; /\* Is the timestep a halfstep? *\/ */ } TSctx; typedef struct { PetscInt debug; RunType runType; /* definitions for mesh and problem setup */ PetscBool simplex; PetscBool interpolate; /* Additional options and parameters */ PetscBool verbose; TSctx time; PetscViewer viewer; }AppCtx; /* defining the kronecker delta */ const PetscInt delta3D[3*3] = {1,0,0,0,1,0,0,0,1}; /* defining the material parameters */ const PetscReal mu =76.923076923, lbda=115.384615385, rho_steel = 7850E-9; void f0_u_transient(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscScalar f0[]) { const PetscInt Ncomp = dim; PetscInt comp; for(comp=0;compsimplex; PetscQuadrature q; PetscInt order; PetscInt BSpaceOrder; PetscSpace space; /* Creating the FE */ ierr = PetscFECreateDefault(dm, dim, dim, simplex,"def_",PETSC_DEFAULT,&fe);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) fe, "deformation");CHKERRQ(ierr); ierr = PetscFEGetQuadrature(fe,&q);CHKERRQ(ierr); ierr = PetscQuadratureGetOrder(q,&order);CHKERRQ(ierr); /* Creating the FE field for the velocity */ ierr = PetscFECreateDefault(dm,dim,dim,simplex,"vel_",PETSC_DEFAULT,&fe_vel);CHKERRQ(ierr); ierr = PetscFESetQuadrature(fe_vel,q);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) fe_vel, "velocity");CHKERRQ(ierr); /* Discretization and boundary conditons: */ while (cdm) { ierr = DMGetDS(cdm, &prob);CHKERRQ(ierr); ierr = PetscDSSetDiscretization(prob, 0, (PetscObject) fe); CHKERRQ(ierr); ierr = PetscDSSetDiscretization(prob,1, (PetscObject) fe_vel);CHKERRQ(ierr); ierr = SetupProblem(cdm, user); CHKERRQ(ierr); /* Define the boundaries */ const PetscInt Ncomp = dim; PetscInt components[dim]; const PetscInt Nfid = 2; PetscInt d; PetscInt fid[Nfid]; /* fixed faces [numer of fixed faces] */ const PetscInt Npid = 1; /* number of pressure loaded faces */ PetscInt pid[Npid]; /* the ids of the pressure loaded faces */ const PetscInt Nsxid = 1; /* number of face ids for the symmetry in x * direction bc */ PetscInt sxid[Nsxid]; const PetscInt Nsyid = 1; PetscInt syid[Nsyid]; PetscInt restrictX[] = {0}; /* array of constricted components for the * dmaddboundary routine */ PetscInt restrictY[] = {1}; PetscInt restrictZ[] = {2}; PetscInt restrictall[3] = {0, 1, 2}; /* restricting all movement */ for (d=0;dverbose==PETSC_TRUE) PetscPrintf(PETSC_COMM_WORLD,"Writing the solution to the vtk file. \n"); ierr = VecView(u,viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); if (user->verbose==PETSC_TRUE)PetscPrintf(PETSC_COMM_WORLD,"Done creating the VTK file. \n"); return(0); } static PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options) { const char *runTypes[2]={"standalone","coupled"}; const char *visutype[4]={"none","labels","boundaries","solution"}; PetscInt vis; PetscInt run; PetscErrorCode ierr; options->debug = 0; options->runType = RUN_STANDALONE; options->interpolate = PETSC_TRUE; options->verbose = PETSC_FALSE; options->time.total = 0.001; options->time.dt = 0.000005; ierr = PetscOptionsBegin(comm,"", "Linear elasticity problem options", "DMPLEX"); CHKERRQ(ierr); ierr = PetscOptionsInt("-debug","The debugging level","cimply.c",options->debug, &options->debug,NULL);CHKERRQ(ierr); run = options->runType; options->runType = (RunType) run; ierr = PetscOptionsBool("-interpolate","Generate intermediate mesh elements", "cimply.c",options->interpolate,&options->interpolate,NULL);CHKERRQ(ierr); ierr = PetscOptionsBool("-simplex","use simplex or tensor product cells","cimply.c",options->simplex,&options->simplex,NULL);CHKERRQ(ierr); ierr = PetscOptionsBool("-verbose","Output additional information.","cimply.c",options->verbose,&options->verbose,NULL);CHKERRQ(ierr); ierr = PetscOptionsEnd(); CHKERRQ(ierr); return(0); } int main(int argc, char *argv[]) { SNES snes; /* nonlinear solver */ KSP ksp; /* the linear sovler context */ DM dm, distributeddm; /* problem definition */ Vec u,r; /* solution and residual vectors */ Mat A,J,P; /* Jacobian Matrix */ AppCtx user; /* user-defined work context */ PetscViewer viewer; TS ts; /* in case of transient analysis */ int dim; /* dimension of the anlysis */ PetscErrorCode ierr; PetscMPIInt rank, numProcs; PetscPartitioner part; ierr = PetscInitialize(&argc,&argv,NULL,NULL); ierr = ProcessOptions(PETSC_COMM_WORLD,&user); ierr = DMPlexCreateFromFile(PETSC_COMM_WORLD,"nakamura.msh", PETSC_TRUE,&(dm)); ierr = DMGetDimension(dm,&(dim)); /* distribute the mesh */ MPI_Comm_rank(PETSC_COMM_WORLD, &rank); MPI_Comm_size(PETSC_COMM_WORLD, &numProcs); DMPlexGetPartitioner(dm, &part); PetscPartitionerSetType(part, PETSCPARTITIONERPARMETIS); ierr = DMPlexDistribute(dm,0,NULL,&distributeddm); if (distributeddm) { ierr=DMDestroy(&(dm)); dm = distributeddm; ierr = DMDestroy(&distributeddm); } ierr = DMView(dm,PETSC_VIEWER_STDOUT_WORLD); PetscPrintf(PETSC_COMM_WORLD,"starting transient analysis. Total time: %g s \n",user.time.total); ierr = TSCreate(PETSC_COMM_WORLD, &ts); CHKERRQ(ierr); ierr = TSSetType(ts, TSBEULER); CHKERRQ(ierr); ierr = TSSetDM(ts,dm); CHKERRQ(ierr); ierr = DMSetApplicationContext(dm, &user);CHKERRQ(ierr); ierr = SetupDiscretization(dm,&user);CHKERRQ(ierr); ierr = DMPlexCreateClosureIndex(dm,NULL);CHKERRQ(ierr); ierr = DMSetVecType(dm,VECMPI);CHKERRQ(ierr); ierr = DMCreateGlobalVector(dm, &(u)); CHKERRQ(ierr); ierr = DMSetMatType(dm, MATAIJ);CHKERRQ(ierr); ierr = TSMonitorSet(ts,Monitor,&user,NULL);CHKERRQ(ierr); ierr = DMTSSetBoundaryLocal(dm, DMPlexTSComputeBoundary, &user); CHKERRQ(ierr); ierr = DMTSSetIFunctionLocal(dm, DMPlexTSComputeIFunctionFEM, &user); CHKERRQ(ierr); ierr = DMTSSetIJacobianLocal(dm, DMPlexTSComputeIJacobianFEM, &user); CHKERRQ(ierr); ierr = TSSetSolution(ts,u); CHKERRQ(ierr); ierr = TSSetDuration(ts,1000,user.time.total); CHKERRQ(ierr); ierr = TSSetExactFinalTime(ts, TS_EXACTFINALTIME_MATCHSTEP); CHKERRQ(ierr); ierr = TSSetEquationType(ts,TS_EQ_IMPLICIT);CHKERRQ(ierr); ierr = TSSetInitialTimeStep(ts,0.0, user.time.dt); CHKERRQ(ierr); ierr = TSSetFromOptions(ts); CHKERRQ(ierr); ierr = TSGetSNES(ts, &(snes)); CHKERRQ(ierr); ierr = SNESGetKSP(snes, &(ksp)); CHKERRQ(ierr); printf("solving ... .\n"); ierr = TSSolve(ts,u); CHKERRQ(ierr); MatDestroy(&(J)); VecDestroy(&(u)); VecDestroy(&(r)); TSDestroy(&(ts)); DMDestroy(&(dm)); PetscViewerDestroy(&(user.viewer)); PetscFinalize(); return 0; }