/*@C MatFinalizePackage - This function destroys everything in the Petsc interface to the Mat package. It is called from PetscFinalize(). Level: developer .keywords: Petsc, destroy, package, mathematica .seealso: PetscFinalize() @*/ PetscErrorCode MatFinalizePackage(void) { MatBaseName nnames,names = MatBaseNameList; PetscErrorCode ierr; PetscFunctionBegin; ierr = MatSolverPackageDestroy();CHKERRQ(ierr); while (names) { nnames = names->next; ierr = PetscFree(names->bname);CHKERRQ(ierr); ierr = PetscFree(names->sname);CHKERRQ(ierr); ierr = PetscFree(names->mname);CHKERRQ(ierr); ierr = PetscFree(names);CHKERRQ(ierr); names = nnames; } ierr = PetscFunctionListDestroy(&MatList);CHKERRQ(ierr); ierr = PetscFunctionListDestroy(&MatOrderingList);CHKERRQ(ierr); ierr = PetscFunctionListDestroy(&MatColoringList);CHKERRQ(ierr); ierr = PetscFunctionListDestroy(&MatPartitioningList);CHKERRQ(ierr); ierr = PetscFunctionListDestroy(&MatCoarsenList);CHKERRQ(ierr); MatBaseNameList = NULL; MatPackageInitialized = PETSC_FALSE; MatRegisterAllCalled = PETSC_FALSE; MatOrderingRegisterAllCalled = PETSC_FALSE; MatColoringRegisterAllCalled = PETSC_FALSE; MatPartitioningRegisterAllCalled = PETSC_FALSE; MatCoarsenRegisterAllCalled = PETSC_FALSE; PetscFunctionReturn(0); }
/*@C TSFinalizePackage - This function destroys everything in the Petsc interface to Mathematica. It is called from PetscFinalize(). Level: developer .keywords: Petsc, destroy, package, mathematica .seealso: PetscFinalize() @*/ PetscErrorCode TSFinalizePackage(void) { PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscFunctionListDestroy(&TSList);CHKERRQ(ierr); ierr = PetscFunctionListDestroy(&TSTrajectoryList);CHKERRQ(ierr); TSPackageInitialized = PETSC_FALSE; TSRegisterAllCalled = PETSC_FALSE; PetscFunctionReturn(0); }
/*@C DMFinalizePackage - This function finalizes everything in the DM package. It is called from PetscFinalize(). Level: developer .keywords: AO, initialize, package .seealso: PetscInitialize() @*/ PetscErrorCode DMFinalizePackage(void) { PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscFunctionListDestroy(&PetscPartitionerList);CHKERRQ(ierr); ierr = PetscFunctionListDestroy(&DMList);CHKERRQ(ierr); DMPackageInitialized = PETSC_FALSE; DMRegisterAllCalled = PETSC_FALSE; PetscPartitionerRegisterAllCalled = PETSC_FALSE; PetscFunctionReturn(0); }
/*@C PetscFVFinalizePackage - This function finalizes everything in the PetscFV package. It is called from PetscFinalize(). Level: developer .keywords: PetscFV, initialize, package .seealso: PetscInitialize() @*/ PetscErrorCode PetscFVFinalizePackage(void) { PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscFunctionListDestroy(&PetscLimiterList);CHKERRQ(ierr); ierr = PetscFunctionListDestroy(&PetscFVList);CHKERRQ(ierr); PetscFVPackageInitialized = PETSC_FALSE; PetscFVRegisterAllCalled = PETSC_FALSE; PetscLimiterRegisterAllCalled = PETSC_FALSE; PetscFunctionReturn(0); }
/*@C SNESFinalizePackage - This function destroys everything in the Petsc interface to the SNES package. It is called from PetscFinalize(). Level: developer .keywords: Petsc, destroy, package, mathematica .seealso: PetscFinalize() @*/ PetscErrorCode SNESFinalizePackage(void) { PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscFunctionListDestroy(&SNESList);CHKERRQ(ierr); ierr = PetscFunctionListDestroy(&SNESLineSearchList);CHKERRQ(ierr); SNESPackageInitialized = PETSC_FALSE; SNESRegisterAllCalled = PETSC_FALSE; SNESLineSearchRegisterAllCalled = PETSC_FALSE; PetscFunctionReturn(0); }
/*@C PetscFEFinalizePackage - This function finalizes everything in the PetscFE package. It is called from PetscFinalize(). Level: developer .keywords: PetscFE, initialize, package .seealso: PetscInitialize() @*/ PetscErrorCode PetscFEFinalizePackage(void) { PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscFunctionListDestroy(&PetscSpaceList);CHKERRQ(ierr); ierr = PetscFunctionListDestroy(&PetscDualSpaceList);CHKERRQ(ierr); ierr = PetscFunctionListDestroy(&PetscFEList);CHKERRQ(ierr); PetscFEPackageInitialized = PETSC_FALSE; PetscSpaceRegisterAllCalled = PETSC_FALSE; PetscDualSpaceRegisterAllCalled = PETSC_FALSE; PetscFERegisterAllCalled = PETSC_FALSE; PetscFunctionReturn(0); }
/*@C PCSetType - Builds PC for a particular preconditioner type Collective on PC Input Parameter: + pc - the preconditioner context. - type - a known method Options Database Key: . -pc_type <type> - Sets PC type Use -help for a list of available methods (for instance, jacobi or bjacobi) Notes: See "petsc/include/petscpc.h" for available methods (for instance, PCJACOBI, PCILU, or PCBJACOBI). Normally, it is best to use the KSPSetFromOptions() command and then set the PC type from the options database rather than by using this routine. Using the options database provides the user with maximum flexibility in evaluating the many different preconditioners. The PCSetType() routine is provided for those situations where it is necessary to set the preconditioner independently of the command line or options database. This might be the case, for example, when the choice of preconditioner changes during the execution of the program, and the user's application is taking responsibility for choosing the appropriate preconditioner. In other words, this routine is not for beginners. Level: intermediate Developer Note: PCRegister() is used to add preconditioner types to PCList from which they are accessed by PCSetType(). .keywords: PC, set, method, type .seealso: KSPSetType(), PCType, PCRegister(), PCCreate(), KSPGetPC() @*/ PetscErrorCode PCSetType(PC pc,PCType type) { PetscErrorCode ierr,(*r)(PC); PetscBool match; PetscFunctionBegin; PetscValidHeaderSpecific(pc,PC_CLASSID,1); PetscValidCharPointer(type,2); ierr = PetscObjectTypeCompare((PetscObject)pc,type,&match);CHKERRQ(ierr); if (match) PetscFunctionReturn(0); ierr = PetscFunctionListFind(PCList,type,&r);CHKERRQ(ierr); if (!r) SETERRQ1(PetscObjectComm((PetscObject)pc),PETSC_ERR_ARG_UNKNOWN_TYPE,"Unable to find requested PC type %s",type); /* Destroy the previous private PC context */ if (pc->ops->destroy) { ierr = (*pc->ops->destroy)(pc);CHKERRQ(ierr); pc->ops->destroy = NULL; pc->data = 0; } ierr = PetscFunctionListDestroy(&((PetscObject)pc)->qlist);CHKERRQ(ierr); /* Reinitialize function pointers in PCOps structure */ ierr = PetscMemzero(pc->ops,sizeof(struct _PCOps));CHKERRQ(ierr); /* XXX Is this OK?? */ pc->modifysubmatrices = 0; pc->modifysubmatricesP = 0; /* Call the PCCreate_XXX routine for this particular preconditioner */ pc->setupcalled = 0; ierr = PetscObjectChangeTypeName((PetscObject)pc,type);CHKERRQ(ierr); ierr = (*r)(pc);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@C TaoLineSearchRegisterDestroy - Frees the list of line-search algorithms that were registered by TaoLineSearchRegister(). Not Collective Level: developer .seealso: TaoLineSearchRegister() @*/ PetscErrorCode TaoLineSearchRegisterDestroy(void) { PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscFunctionListDestroy(&TaoLineSearchList);CHKERRQ(ierr); TaoLineSearchInitialized = PETSC_FALSE; PetscFunctionReturn(0); }
PetscErrorCode MatHeaderMerge(Mat A,Mat C) { PetscErrorCode ierr; PetscInt refct; PetscOps *Abops; MatOps Aops; char *mtype,*mname; void *spptr; PetscFunctionBegin; /* save the parts of A we need */ Abops = ((PetscObject)A)->bops; Aops = A->ops; refct = ((PetscObject)A)->refct; mtype = ((PetscObject)A)->type_name; mname = ((PetscObject)A)->name; spptr = A->spptr; /* zero these so the destroy below does not free them */ ((PetscObject)A)->type_name = 0; ((PetscObject)A)->name = 0; /* free all the interior data structures from mat */ ierr = (*A->ops->destroy)(A); CHKERRQ(ierr); ierr = PetscFree(C->spptr); CHKERRQ(ierr); ierr = PetscLayoutDestroy(&A->rmap); CHKERRQ(ierr); ierr = PetscLayoutDestroy(&A->cmap); CHKERRQ(ierr); ierr = PetscFunctionListDestroy(&((PetscObject)A)->qlist); CHKERRQ(ierr); ierr = PetscObjectListDestroy(&((PetscObject)A)->olist); CHKERRQ(ierr); /* copy C over to A */ ierr = PetscMemcpy(A,C,sizeof(struct _p_Mat)); CHKERRQ(ierr); /* return the parts of A we saved */ ((PetscObject)A)->bops = Abops; A->ops = Aops; ((PetscObject)A)->refct = refct; ((PetscObject)A)->type_name = mtype; ((PetscObject)A)->name = mname; A->spptr = spptr; /* since these two are copied into A we do not want them destroyed in C */ ((PetscObject)C)->qlist = 0; ((PetscObject)C)->olist = 0; ierr = PetscHeaderDestroy(&C); CHKERRQ(ierr); PetscFunctionReturn(0); }
PetscErrorCode OpFinalizePackage() { PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscFunctionListDestroy(&OpList);CHKERRQ(ierr); OpPackageInitialized = PETSC_FALSE; PetscFunctionReturn(0); }
/*@C TSRegisterDestroy - Frees the list of timestepping routines that were registered by TSRegister()/TSRegisterDynamic(). Not Collective Level: advanced .keywords: TS, timestepper, register, destroy .seealso: TSRegister(), TSRegisterAll(), TSRegisterDynamic() @*/ PetscErrorCode TSRegisterDestroy(void) { PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscFunctionListDestroy(&TSList);CHKERRQ(ierr); TSRegisterAllCalled = PETSC_FALSE; PetscFunctionReturn(0); }
PetscErrorCode PCGAMGClassicalFinalizePackage(void) { PetscErrorCode ierr; PetscFunctionBegin; PCGAMGClassicalPackageInitialized = PETSC_FALSE; ierr = PetscFunctionListDestroy(&PCGAMGClassicalProlongatorList);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@C PetscDrawFinalizePackage - This function destroys everything in the Petsc interface to the Draw package. It is called from PetscFinalize(). Level: developer .keywords: Petsc, destroy, package, mathematica .seealso: PetscFinalize() @*/ PetscErrorCode PetscDrawFinalizePackage(void) { PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscFunctionListDestroy(&PetscDrawList);CHKERRQ(ierr); PetscDrawPackageInitialized = PETSC_FALSE; PetscFunctionReturn(0); }
/*@C TSSSPFinalizePackage - This function destroys everything in the TSSSP package. It is called from PetscFinalize(). Level: developer .keywords: Petsc, destroy, package .seealso: PetscFinalize() @*/ PetscErrorCode TSSSPFinalizePackage(void) { PetscErrorCode ierr; PetscFunctionBegin; TSSSPPackageInitialized = PETSC_FALSE; ierr = PetscFunctionListDestroy(&TSSSPList);CHKERRQ(ierr); PetscFunctionReturn(0); }
/*@C MatColoringRegisterDestroy - Frees the list of coloringing routines. Not Collective Level: developer .keywords: matrix, register, destroy .seealso: MatColoringRegisterDynamic(), MatColoringRegisterAll() @*/ PetscErrorCode MatColoringRegisterDestroy(void) { PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscFunctionListDestroy(&MatColoringList);CHKERRQ(ierr); MatColoringRegisterAllCalled = PETSC_FALSE; PetscFunctionReturn(0); }
/*@C TSGLLEFinalizePackage - This function destroys everything in the TSGLLE package. It is called from PetscFinalize(). Level: developer .keywords: Petsc, destroy, package .seealso: PetscFinalize() @*/ PetscErrorCode TSGLLEAdaptFinalizePackage(void) { PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscFunctionListDestroy(&TSGLLEAdaptList);CHKERRQ(ierr); TSGLLEAdaptPackageInitialized = PETSC_FALSE; TSGLLEAdaptRegisterAllCalled = PETSC_FALSE; PetscFunctionReturn(0); }
/*@C CharacteristicFinalizePackage - This function destroys everything in the Petsc interface to the characteristics package. It is called from PetscFinalize(). Level: developer .keywords: Petsc, destroy, package, finalize .seealso: PetscFinalize() @*/ PetscErrorCode CharacteristicFinalizePackage(void) { PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscFunctionListDestroy(&CharacteristicList);CHKERRQ(ierr); CharacteristicPackageInitialized = PETSC_FALSE; CharacteristicRegisterAllCalled = PETSC_FALSE; PetscFunctionReturn(0); }
/*@C RGFinalizePackage - This function destroys everything in the Slepc interface to the RG package. It is called from SlepcFinalize(). Level: developer .seealso: SlepcFinalize() @*/ PetscErrorCode RGFinalizePackage(void) { PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscFunctionListDestroy(&RGList);CHKERRQ(ierr); RGPackageInitialized = PETSC_FALSE; RGRegisterAllCalled = PETSC_FALSE; PetscFunctionReturn(0); }
/*@C MatMFFDFinalizePackage - This function destroys everything in the MatMFFD package. It is called from PetscFinalize(). Level: developer .keywords: Petsc, destroy, package .seealso: PetscFinalize(), MatCreateMFFD(), MatCreateSNESMF() @*/ PetscErrorCode MatMFFDFinalizePackage(void) { PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscFunctionListDestroy(&MatMFFDList);CHKERRQ(ierr); MatMFFDPackageInitialized = PETSC_FALSE; MatMFFDRegisterAllCalled = PETSC_FALSE; PetscFunctionReturn(0); }
/* PetscHeaderDestroy_Private - Destroys a base PETSc object header. Called by the macro PetscHeaderDestroy(). */ PetscErrorCode PetscHeaderDestroy_Private(PetscObject h) { PetscErrorCode ierr; PetscFunctionBegin; PetscValidHeader(h,1); ierr = PetscLogObjectDestroy(h);CHKERRQ(ierr); ierr = PetscComposedQuantitiesDestroy(h);CHKERRQ(ierr); if (PetscMemoryCollectMaximumUsage) { PetscLogDouble usage; ierr = PetscMemoryGetCurrentUsage(&usage);CHKERRQ(ierr); if (usage > PetscMemoryMaximumUsage) PetscMemoryMaximumUsage = usage; } /* first destroy things that could execute arbitrary code */ if (h->python_destroy) { void *python_context = h->python_context; PetscErrorCode (*python_destroy)(void*) = h->python_destroy; h->python_context = 0; h->python_destroy = 0; ierr = (*python_destroy)(python_context);CHKERRQ(ierr); } ierr = PetscObjectDestroyOptionsHandlers(h);CHKERRQ(ierr); ierr = PetscObjectListDestroy(&h->olist);CHKERRQ(ierr); ierr = PetscCommDestroy(&h->comm);CHKERRQ(ierr); /* next destroy other things */ h->classid = PETSCFREEDHEADER; ierr = PetscFunctionListDestroy(&h->qlist);CHKERRQ(ierr); ierr = PetscFree(h->type_name);CHKERRQ(ierr); ierr = PetscFree(h->name);CHKERRQ(ierr); ierr = PetscFree(h->prefix);CHKERRQ(ierr); ierr = PetscFree(h->fortran_func_pointers);CHKERRQ(ierr); ierr = PetscFree(h->fortrancallback[PETSC_FORTRAN_CALLBACK_CLASS]);CHKERRQ(ierr); ierr = PetscFree(h->fortrancallback[PETSC_FORTRAN_CALLBACK_SUBTYPE]);CHKERRQ(ierr); #if defined(PETSC_USE_LOG) if (PetscObjectsLog) { PetscInt i; /* Record object removal from list of all objects */ for (i=0; i<PetscObjectsMaxCounts; i++) { if (PetscObjects[i] == h) { PetscObjects[i] = 0; PetscObjectsCounts--; break; } } if (!PetscObjectsCounts) { ierr = PetscFree(PetscObjects);CHKERRQ(ierr); PetscObjectsMaxCounts = 0; } } #endif PetscFunctionReturn(0); }
/*@C VecFinalizePackage - This function finalizes everything in the Vec package. It is called from PetscFinalize(). Level: developer .keywords: Vec, initialize, package .seealso: PetscInitialize() @*/ PetscErrorCode VecFinalizePackage(void) { PetscErrorCode ierr; PetscFunctionBegin; ierr = PetscFunctionListDestroy(&VecList);CHKERRQ(ierr); ierr = MPI_Op_free(&PetscSplitReduction_Op);CHKERRQ(ierr); ierr = MPI_Op_free(&VecMax_Local_Op);CHKERRQ(ierr); ierr = MPI_Op_free(&VecMin_Local_Op);CHKERRQ(ierr); VecPackageInitialized = PETSC_FALSE; VecRegisterAllCalled = PETSC_FALSE; PetscFunctionReturn(0); }
int main(int argc, char **argv) { MPI_Comm comm; PetscMPIInt rank; PetscErrorCode ierr; User user; PetscLogDouble v1, v2; PetscInt nplot = 0; char filename1[2048], fileName[2048]; PetscBool set = PETSC_FALSE; PetscInt steps_output; ierr = PetscInitialize(&argc, &argv, (char*) 0, help);CHKERRQ(ierr); comm = PETSC_COMM_WORLD; ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); ierr = PetscNew(&user);CHKERRQ(ierr); ierr = PetscNew(&user->algebra);CHKERRQ(ierr); ierr = PetscNew(&user->model);CHKERRQ(ierr); ierr = PetscNew(&user->model->physics);CHKERRQ(ierr); Algebra algebra = user->algebra; ierr = LoadOptions(comm, user);CHKERRQ(ierr); ierr = PetscTime(&v1);CHKERRQ(ierr); ierr = CreateMesh(comm, user);CHKERRQ(ierr); ierr = PetscTime(&v2);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD, "Read and Distribute mesh takes %f sec \n", v2 - v1);CHKERRQ(ierr); ierr = SetUpLocalSpace(user);CHKERRQ(ierr); //Set up the dofs of each element ierr = ConstructGeometryFVM(&user->facegeom, &user->cellgeom, user);CHKERRQ(ierr); ierr = LimiterSetup(user);CHKERRQ(ierr); if(user->output_solution){ // the output file options ierr = PetscOptionsBegin(PETSC_COMM_WORLD,0,"Options for output solution",0);CHKERRQ(ierr); ierr = PetscOptionsString("-solutionfile", "solution file", "AeroSim.c", filename1,filename1, 2048, &set);CHKERRQ(ierr); if(!set){SETERRQ(PETSC_COMM_SELF, PETSC_ERR_ARG_NULL,"please use option -solutionfile to specify solution file name \n");} ierr = PetscOptionsInt("-steps_output", "the number of time steps between two outputs", "", steps_output, &steps_output, &set);CHKERRQ(ierr); if(!set){ steps_output = 1;} ierr = PetscOptionsEnd();CHKERRQ(ierr); } if (user->TimeIntegralMethod == EXPLICITMETHOD) { if(user->myownexplicitmethod){ ierr = PetscPrintf(PETSC_COMM_WORLD,"Using the fully explicit method based on my own routing\n");CHKERRQ(ierr); user->current_time = user->initial_time; user->current_step = 1; ierr = DMCreateGlobalVector(user->dm, &algebra->solution);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) algebra->solution, "solution");CHKERRQ(ierr); ierr = SetInitialCondition(user->dm, algebra->solution, user);CHKERRQ(ierr); ierr = VecDuplicate(algebra->solution, &algebra->fn);CHKERRQ(ierr); ierr = VecDuplicate(algebra->solution, &algebra->oldsolution);CHKERRQ(ierr); if(user->Explicit_RK2){ ierr = PetscPrintf(PETSC_COMM_WORLD,"Use the second order Runge Kutta method \n");CHKERRQ(ierr); }else{ ierr = PetscPrintf(PETSC_COMM_WORLD,"Use the first order forward Euler method \n");CHKERRQ(ierr); } nplot = 0; //the plot step while(user->current_time < (user->final_time - 0.05 * user->dt)){ user->current_time = user->current_time + user->dt; ierr = FormTimeStepFunction(user, algebra, algebra->solution, algebra->fn);CHKERRQ(ierr); PetscReal fnnorm; ierr = VecNorm(algebra->fn,NORM_INFINITY,&fnnorm);CHKERRQ(ierr); if(0){ PetscViewer viewer; ierr = OutputVTK(user->dm, "function.vtk", &viewer);CHKERRQ(ierr); ierr = VecView(algebra->fn, viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Step %D at time %g with founction norm = %g \n", user->current_step, user->current_time, fnnorm);CHKERRQ(ierr); //break; } if(user->Explicit_RK2){ ierr = VecCopy(algebra->solution, algebra->oldsolution);CHKERRQ(ierr);//U^n ierr = VecAXPY(algebra->solution, user->dt, algebra->fn);CHKERRQ(ierr);//U^{(1)} ierr = FormTimeStepFunction(user, algebra, algebra->solution, algebra->fn);CHKERRQ(ierr);//f(U^{(1)}) ierr = VecAXPY(algebra->solution, 1.0, algebra->oldsolution);CHKERRQ(ierr);//U^n + U^{(1)} ierr = VecAXPY(algebra->solution, user->dt, algebra->fn);CHKERRQ(ierr);// + dt*f(U^{(1)}) ierr = VecScale(algebra->solution, 0.5);CHKERRQ(ierr); }else{ ierr = VecCopy(algebra->solution, algebra->oldsolution);CHKERRQ(ierr); ierr = VecAXPY(algebra->solution, user->dt, algebra->fn);CHKERRQ(ierr); } {// Monitor the solution and function norms PetscReal norm; PetscLogDouble space =0; PetscInt size; ierr = VecNorm(algebra->solution,NORM_INFINITY,&norm);CHKERRQ(ierr); ierr = VecGetSize(algebra->solution, &size);CHKERRQ(ierr); norm = norm/size; if (norm>1.e5) { SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_LIB, "The norm of the solution is: %f (current time: %f). The explicit method is going to DIVERGE!!!", norm, user->current_time); } if (user->current_step%10==0) { ierr = PetscPrintf(PETSC_COMM_WORLD,"Step %D at time %g with solution norm = %g and founction norm = %g \n", user->current_step, user->current_time, norm, fnnorm);CHKERRQ(ierr); } ierr = PetscMallocGetCurrentUsage(&space);CHKERRQ(ierr); // if (user->current_step%10==0) { // ierr = PetscPrintf(PETSC_COMM_WORLD,"Current space PetscMalloc()ed %g M\n", // space/(1024*1024));CHKERRQ(ierr); // } } { // Monitor the difference of two steps' solution PetscReal norm; ierr = VecAXPY(algebra->oldsolution, -1, algebra->solution);CHKERRQ(ierr); ierr = VecNorm(algebra->oldsolution,NORM_INFINITY,&norm);CHKERRQ(ierr); if (user->current_step%10==0) { ierr = PetscPrintf(PETSC_COMM_WORLD,"Step %D at time %g with ||u_k-u_{k-1}|| = %g \n", user->current_step, user->current_time, norm);CHKERRQ(ierr); } if((norm<1.e-6)||(user->current_step > user->max_time_its)) break; } // output the solution if (user->output_solution && (user->current_step%steps_output==0)){ PetscViewer viewer; // update file name for the current time step ierr = PetscSNPrintf(fileName, sizeof(fileName),"%s_%d.vtk",filename1, nplot);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Outputing solution %s (current time %f)\n", fileName, user->current_time);CHKERRQ(ierr); ierr = OutputVTK(user->dm, fileName, &viewer);CHKERRQ(ierr); ierr = VecView(algebra->solution, viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); nplot++; } user->current_step++; } ierr = VecDestroy(&algebra->fn);CHKERRQ(ierr); }else{ PetscReal ftime; TS ts; TSConvergedReason reason; PetscInt nsteps; ierr = PetscPrintf(PETSC_COMM_WORLD,"Using the fully explicit method based on the PETSC TS routing\n");CHKERRQ(ierr); ierr = DMCreateGlobalVector(user->dm, &algebra->solution);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) algebra->solution, "solution");CHKERRQ(ierr); ierr = SetInitialCondition(user->dm, algebra->solution, user);CHKERRQ(ierr); ierr = TSCreate(comm, &ts);CHKERRQ(ierr); ierr = TSSetType(ts, TSEULER);CHKERRQ(ierr); ierr = TSSetDM(ts, user->dm);CHKERRQ(ierr); ierr = TSMonitorSet(ts,TSMonitorFunctionError,&user,NULL);CHKERRQ(ierr); ierr = TSSetRHSFunction(ts, NULL, MyRHSFunction, user);CHKERRQ(ierr); ierr = TSSetDuration(ts, 1000, user->final_time);CHKERRQ(ierr); ierr = TSSetInitialTimeStep(ts, user->initial_time, user->dt);CHKERRQ(ierr); ierr = TSSetFromOptions(ts);CHKERRQ(ierr); ierr = TSSolve(ts, algebra->solution);CHKERRQ(ierr); ierr = TSGetSolveTime(ts, &ftime);CHKERRQ(ierr); ierr = TSGetTimeStepNumber(ts, &nsteps);CHKERRQ(ierr); ierr = TSGetConvergedReason(ts, &reason);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"%s at time %g after %D steps\n",TSConvergedReasons[reason],ftime,nsteps);CHKERRQ(ierr); ierr = TSDestroy(&ts);CHKERRQ(ierr); } if(user->benchmark_couette) { ierr = DMCreateGlobalVector(user->dm, &algebra->exactsolution);CHKERRQ(ierr); ierr = ComputeExactSolution(user->dm, user->final_time, algebra->exactsolution, user);CHKERRQ(ierr); } if (user->output_solution){ PetscViewer viewer; ierr = OutputVTK(user->dm, "solution.vtk", &viewer);CHKERRQ(ierr); ierr = VecView(algebra->solution, viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); } if(user->benchmark_couette) { PetscViewer viewer; PetscReal norm; ierr = OutputVTK(user->dm, "exact_solution.vtk", &viewer);CHKERRQ(ierr); ierr = VecView(algebra->exactsolution, viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); ierr = VecAXPY(algebra->exactsolution, -1, algebra->solution);CHKERRQ(ierr); ierr = VecNorm(algebra->exactsolution,NORM_INFINITY,&norm);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Final time at %f, Error: ||u_k-u|| = %g \n", user->final_time, norm);CHKERRQ(ierr); ierr = OutputVTK(user->dm, "Error.vtk", &viewer);CHKERRQ(ierr); ierr = VecView(algebra->exactsolution, viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); } ierr = VecDestroy(&algebra->solution);CHKERRQ(ierr); ierr = VecDestroy(&algebra->oldsolution);CHKERRQ(ierr); ierr = DMDestroy(&user->dm);CHKERRQ(ierr); } else if (user->TimeIntegralMethod == IMPLICITMETHOD) { ierr = PetscPrintf(PETSC_COMM_WORLD,"Using the fully implicit method\n");CHKERRQ(ierr); ierr = SNESCreate(comm,&user->snes);CHKERRQ(ierr); ierr = SNESSetDM(user->snes,user->dm);CHKERRQ(ierr); ierr = DMCreateGlobalVector(user->dm, &algebra->solution);CHKERRQ(ierr); ierr = VecDuplicate(algebra->solution, &algebra->oldsolution);CHKERRQ(ierr); ierr = VecDuplicate(algebra->solution, &algebra->f);CHKERRQ(ierr); ierr = VecDuplicate(algebra->solution, &algebra->fn);CHKERRQ(ierr); ierr = VecDuplicate(algebra->solution, &algebra->oldfn);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) algebra->solution, "solution");CHKERRQ(ierr); ierr = SetInitialCondition(user->dm, algebra->solution, user);CHKERRQ(ierr); ierr = DMSetMatType(user->dm, MATAIJ);CHKERRQ(ierr); // ierr = DMCreateMatrix(user->dm, &algebra->A);CHKERRQ(ierr); ierr = DMCreateMatrix(user->dm, &algebra->J);CHKERRQ(ierr); if (user->JdiffP) { /*Set up the preconditioner matrix*/ ierr = DMCreateMatrix(user->dm, &algebra->P);CHKERRQ(ierr); }else{ algebra->P = algebra->J; } ierr = MatSetOption(algebra->J, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);CHKERRQ(ierr); /*set nonlinear function */ ierr = SNESSetFunction(user->snes, algebra->f, FormFunction, (void*)user);CHKERRQ(ierr); /* compute Jacobian */ ierr = SNESSetJacobian(user->snes, algebra->J, algebra->P, FormJacobian, (void*)user);CHKERRQ(ierr); ierr = SNESSetFromOptions(user->snes);CHKERRQ(ierr); /* do the solve */ if (user->timestep == TIMESTEP_STEADY_STATE) { ierr = SolveSteadyState(user);CHKERRQ(ierr); } else { ierr = SolveTimeDependent(user);CHKERRQ(ierr); } if (user->output_solution){ PetscViewer viewer; ierr = OutputVTK(user->dm, "solution.vtk", &viewer);CHKERRQ(ierr); ierr = VecView(algebra->solution, viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); } if(user->benchmark_couette) { PetscViewer viewer; PetscReal norm; ierr = OutputVTK(user->dm, "exact_solution.vtk", &viewer);CHKERRQ(ierr); ierr = VecView(algebra->exactsolution, viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); ierr = VecAXPY(algebra->exactsolution, -1, algebra->solution);CHKERRQ(ierr); ierr = VecNorm(algebra->exactsolution,NORM_INFINITY,&norm);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Error: ||u_k-u|| = %g \n", norm);CHKERRQ(ierr); ierr = OutputVTK(user->dm, "Error.vtk", &viewer);CHKERRQ(ierr); ierr = VecView(algebra->exactsolution, viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); } ierr = VecDestroy(&algebra->solution);CHKERRQ(ierr); ierr = VecDestroy(&algebra->f);CHKERRQ(ierr); ierr = VecDestroy(&algebra->oldsolution);CHKERRQ(ierr); ierr = VecDestroy(&algebra->fn);CHKERRQ(ierr); ierr = VecDestroy(&algebra->oldfn);CHKERRQ(ierr); ierr = SNESDestroy(&user->snes);CHKERRQ(ierr); ierr = DMDestroy(&user->dm);CHKERRQ(ierr); } else { SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"WRONG option for the time integral method. Using the option '-time_integral_method 0 or 1'"); } ierr = VecDestroy(&user->cellgeom);CHKERRQ(ierr); ierr = VecDestroy(&user->facegeom);CHKERRQ(ierr); ierr = DMDestroy(&user->dmGrad);CHKERRQ(ierr); ierr = PetscFunctionListDestroy(&LimitList);CHKERRQ(ierr); ierr = PetscFree(user->model->physics);CHKERRQ(ierr); ierr = PetscFree(user->algebra);CHKERRQ(ierr); ierr = PetscFree(user->model);CHKERRQ(ierr); ierr = PetscFree(user);CHKERRQ(ierr); { PetscLogDouble space =0; ierr = PetscMallocGetCurrentUsage(&space);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Unfreed space at the End %g M\n", space/(1024*1024));CHKERRQ(ierr); } ierr = PetscFinalize(); return(0); }
int main(int argc, char **argv) { MPI_Comm comm; PetscMPIInt rank; PetscErrorCode ierr; User user; PetscLogDouble v1, v2; PetscInt nplot = 0; char fileName[2048]; ierr = PetscInitialize(&argc, &argv, (char*) 0, help);CHKERRQ(ierr); comm = PETSC_COMM_WORLD; ierr = MPI_Comm_rank(comm, &rank);CHKERRQ(ierr); ierr = PetscNew(&user);CHKERRQ(ierr); ierr = PetscNew(&user->algebra);CHKERRQ(ierr); ierr = PetscNew(&user->model);CHKERRQ(ierr); ierr = PetscNew(&user->model->physics);CHKERRQ(ierr); Algebra algebra = user->algebra; ierr = LoadOptions(comm, user);CHKERRQ(ierr); ierr = PetscTime(&v1);CHKERRQ(ierr); ierr = CreateMesh(comm, user);CHKERRQ(ierr); ierr = PetscTime(&v2);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD, "Read and Distribute mesh takes %f sec \n", v2 - v1);CHKERRQ(ierr); ierr = SetUpLocalSpace(user);CHKERRQ(ierr); //Set up the dofs of each element ierr = ConstructGeometryFVM(&user->facegeom, &user->cellgeom, user);CHKERRQ(ierr); ierr = LimiterSetup(user);CHKERRQ(ierr); if (user->TimeIntegralMethod == EXPLICITMETHOD) { // explicit method if(user->myownexplicitmethod){// Using the fully explicit method based on my own routing ierr = PetscPrintf(PETSC_COMM_WORLD,"Using the fully explicit method based on my own routing\n");CHKERRQ(ierr); user->current_time = user->initial_time; user->current_step = 1; ierr = DMCreateGlobalVector(user->dm, &algebra->solution);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) algebra->solution, "solution");CHKERRQ(ierr); ierr = VecSet(algebra->solution, 0);CHKERRQ(ierr); ierr = SetInitialCondition(user->dm, algebra->solution, user);CHKERRQ(ierr); if(1){ PetscViewer viewer; ierr = OutputVTK(user->dm, "intialcondition.vtk", &viewer);CHKERRQ(ierr); ierr = VecView(algebra->solution, viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Outputing the initial condition intialcondition.vtk!!! \n");CHKERRQ(ierr); } ierr = VecDuplicate(algebra->solution, &algebra->fn);CHKERRQ(ierr); ierr = VecDuplicate(algebra->solution, &algebra->oldsolution);CHKERRQ(ierr); if(user->Explicit_RK2||user->Explicit_RK4){ ierr = PetscPrintf(PETSC_COMM_WORLD,"Use the second order Runge Kutta method \n");CHKERRQ(ierr); }else{ ierr = PetscPrintf(PETSC_COMM_WORLD,"Use the first order forward Euler method \n");CHKERRQ(ierr); } nplot = 0; //the plot step while(user->current_time < (user->final_time - 0.05 * user->dt)){ user->current_time = user->current_time + user->dt; ierr = FormTimeStepFunction(user, algebra, algebra->solution, algebra->fn);CHKERRQ(ierr); if(0){ PetscViewer viewer; ierr = OutputVTK(user->dm, "function.vtk", &viewer);CHKERRQ(ierr); ierr = VecView(algebra->fn, viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); } if(user->Explicit_RK2){ /* U^n_1 = U^n + 0.5*dt*f(U^n) U^{n+1} = U^n + dt*f(U^n_1) */ ierr = VecCopy(algebra->solution, algebra->oldsolution);CHKERRQ(ierr); //note that algebra->oldsolution and algebra->solution are both U^n ierr = VecAXPY(algebra->solution, 0.5*user->dt, algebra->fn);CHKERRQ(ierr); //U^n_1 = U^n + 0.5*dt*f(U^n), now algebra->solution is U^n_1, and algebra->fn is f(U^n) ierr = FormTimeStepFunction(user, algebra, algebra->solution, algebra->fn);CHKERRQ(ierr); //algebra->fn is f(U^n_1) // reset the algebra->solution to U^n ierr = VecCopy(algebra->oldsolution, algebra->solution);CHKERRQ(ierr); ierr = VecAXPY(algebra->solution, user->dt, algebra->fn);CHKERRQ(ierr); // now algebra->solution is U^{n+1} = U^n + dt*f(U^n_1) }else if(user->Explicit_RK4){ /* refer to https://en.wikipedia.org/wiki/Runge%E2%80%93Kutta_methods k_1 = f(U^n) U^n_1 = U^n + 0.5*dt*k_1 k_2 = f(U^n_1) U^n_2 = U^n + 0.5*dt*k_2 k_3 = f(U^n_2) U^n_3 = U^n + 0.5*dt*k_3 k_4 = f(U^n_3) U^{n+1} = U^n + dt/6*(k_1 + 2*k_2 + 2*k_3 + k_4) */ Vec VecTemp; // store the U^n_1 Vec k1, k2, k3, k4; ierr = VecDuplicate(algebra->solution, &k1);CHKERRQ(ierr); ierr = VecDuplicate(algebra->solution, &k2);CHKERRQ(ierr); ierr = VecDuplicate(algebra->solution, &k3);CHKERRQ(ierr); ierr = VecDuplicate(algebra->solution, &k4);CHKERRQ(ierr); ierr = VecCopy(algebra->solution, algebra->oldsolution);CHKERRQ(ierr); ierr = VecCopy(algebra->fn, k1);CHKERRQ(ierr); //note that algebra->oldsolution and algebra->solution are both U^n ierr = VecAXPY(algebra->solution, 0.5*user->dt, k1);CHKERRQ(ierr); //U^n_1 = U^n + 0.5*dt*k1, now algebra->solution is U^n_1, and algebra->fn is f(U^n) ierr = FormTimeStepFunction(user, algebra, algebra->solution, algebra->fn);CHKERRQ(ierr); //algebra->fn is f(U^n_1) ierr = VecCopy(algebra->fn, k2);CHKERRQ(ierr); // reset the algebra->solution to U^n ierr = VecCopy(algebra->oldsolution, algebra->solution);CHKERRQ(ierr); ierr = VecAXPY(algebra->solution, 0.5*user->dt, k2);CHKERRQ(ierr); //U^n_2 = U^n + 0.5*dt*k2, now algebra->solution is U^n_2, and algebra->fn is f(U^n_1) ierr = FormTimeStepFunction(user, algebra, algebra->solution, algebra->fn);CHKERRQ(ierr); //algebra->fn is f(U^n_2) ierr = VecCopy(algebra->fn, k3);CHKERRQ(ierr); // reset the algebra->solution to U^n ierr = VecCopy(algebra->oldsolution, algebra->solution);CHKERRQ(ierr); ierr = VecAXPY(algebra->solution, 0.5*user->dt, k3);CHKERRQ(ierr); //U^n_3 = U^n + 0.5*dt*k3, now algebra->solution is U^n_3, and algebra->fn is f(U^n_2) ierr = FormTimeStepFunction(user, algebra, algebra->solution, algebra->fn);CHKERRQ(ierr); //algebra->fn is f(U^n_3) ierr = VecCopy(algebra->fn, k4);CHKERRQ(ierr); //U^{n+1} = U^n + dt/6*(k_1 + 2*k_2 + 2*k_3 + k_4) PetscReal temp; temp = user->dt/6; // reset the algebra->solution to U^n ierr = VecCopy(algebra->oldsolution, algebra->solution);CHKERRQ(ierr); ierr = VecAXPY(algebra->solution, temp, k1);CHKERRQ(ierr); // now algebra->solution is U^n + dt/6*k_1 ierr = VecAXPY(algebra->solution, 2*temp, k2);CHKERRQ(ierr); // now algebra->solution is U^n + dt/6*k_1 + 2*dt/6*k_2 ierr = VecAXPY(algebra->solution, 2*temp, k3);CHKERRQ(ierr); // now algebra->solution is U^n + dt/6*k_1 + 2*dt/6*k_2 + 2*dt/6*k_3 ierr = VecAXPY(algebra->solution, temp, k4);CHKERRQ(ierr); // now algebra->solution is U^n + dt/6*k_1 + 2*dt/6*k_2 + 2*dt/6*k_3 + dt/6*k_4 ierr = VecDestroy(&k1);CHKERRQ(ierr); ierr = VecDestroy(&k2);CHKERRQ(ierr); ierr = VecDestroy(&k3);CHKERRQ(ierr); ierr = VecDestroy(&k4);CHKERRQ(ierr); }else{ ierr = VecCopy(algebra->solution, algebra->oldsolution);CHKERRQ(ierr); ierr = VecAXPY(algebra->solution, user->dt, algebra->fn);CHKERRQ(ierr); } {// Monitor the solution and function norms PetscReal norm; PetscLogDouble space =0; PetscInt size; PetscReal fnnorm; ierr = VecNorm(algebra->fn,NORM_2,&fnnorm);CHKERRQ(ierr); //ierr = VecView(algebra->fn, PETSC_VIEWER_STDOUT_WORLD);CHKERRQ(ierr); ierr = VecNorm(algebra->solution,NORM_2,&norm);CHKERRQ(ierr); ierr = VecGetSize(algebra->solution, &size);CHKERRQ(ierr); norm = norm/size; fnnorm = fnnorm/size; if (norm>1.e5) { SETERRQ2(PETSC_COMM_WORLD, PETSC_ERR_LIB, "The norm of the solution is: %f (current time: %f). The explicit method is going to DIVERGE!!!", norm, user->current_time); } if (user->current_step%10==0) { ierr = PetscPrintf(PETSC_COMM_WORLD,"Step %D at time %g with solution norm = %g and founction norm = %g \n", user->current_step, user->current_time, norm, fnnorm);CHKERRQ(ierr); } // ierr = PetscMallocGetCurrentUsage(&space);CHKERRQ(ierr); // if (user->current_step%10==0) { // ierr = PetscPrintf(PETSC_COMM_WORLD,"Current space PetscMalloc()ed %g M\n", // space/(1024*1024));CHKERRQ(ierr); // } } { // Monitor the difference of two steps' solution PetscReal norm; ierr = VecAXPY(algebra->oldsolution, -1, algebra->solution);CHKERRQ(ierr); ierr = VecNorm(algebra->oldsolution,NORM_2,&norm);CHKERRQ(ierr); if (user->current_step%10==0) { ierr = PetscPrintf(PETSC_COMM_WORLD,"Step %D at time %g with ||u_k-u_{k-1}|| = %g \n", user->current_step, user->current_time, norm);CHKERRQ(ierr); } if((norm<1.e-6)||(user->current_step > user->max_time_its)){ if(norm<1.e-6) ierr = PetscPrintf(PETSC_COMM_WORLD,"\n Convergence with ||u_k-u_{k-1}|| = %g < 1.e-6\n\n", norm);CHKERRQ(ierr); if(user->current_step > user->max_time_its) ierr = PetscPrintf(PETSC_COMM_WORLD,"\n Convergence with reaching the max time its\n\n");CHKERRQ(ierr); break; } } // output the solution if (user->output_solution && (user->current_step%user->steps_output==0)){ PetscViewer viewer; Vec solution_unscaled; // Note the the algebra->solution is scaled by the density, so this is for the unscaled solution nplot = user->current_step/user->steps_output; // update file name for the current time step ierr = VecDuplicate(algebra->solution, &solution_unscaled);CHKERRQ(ierr); ierr = ReformatSolution(algebra->solution, solution_unscaled, user);CHKERRQ(ierr); ierr = PetscSNPrintf(fileName, sizeof(fileName),"%s_%d.vtk",user->solutionfile, nplot);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Outputing solution %s (current time %f)\n", fileName, user->current_time);CHKERRQ(ierr); ierr = OutputVTK(user->dm, fileName, &viewer);CHKERRQ(ierr); ierr = VecView(solution_unscaled, viewer);CHKERRQ(ierr); ierr = VecDestroy(&solution_unscaled);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); } user->current_step++; } ierr = VecDestroy(&algebra->fn);CHKERRQ(ierr); }else{ // Using the fully explicit method based on the PETSC TS routing PetscReal ftime; TS ts; TSConvergedReason reason; PetscInt nsteps; //PetscReal minRadius; //ierr = DMPlexTSGetGeometry(user->dm, NULL, NULL, &minRadius);CHKERRQ(ierr); //user->dt = 0.9*4 * minRadius / 1.0; ierr = PetscPrintf(PETSC_COMM_WORLD,"Using the fully explicit method based on the PETSC TS routing\n");CHKERRQ(ierr); ierr = DMCreateGlobalVector(user->dm, &algebra->solution);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) algebra->solution, "solution");CHKERRQ(ierr); ierr = VecSet(algebra->solution, 0.0);CHKERRQ(ierr); ierr = SetInitialCondition(user->dm, algebra->solution, user);CHKERRQ(ierr); ierr = TSCreate(comm, &ts);CHKERRQ(ierr); ierr = TSSetType(ts, TSEULER);CHKERRQ(ierr); ierr = TSSetDM(ts, user->dm);CHKERRQ(ierr); ierr = TSMonitorSet(ts,TSMonitorFunctionError,(void*)user,NULL);CHKERRQ(ierr); ierr = TSSetRHSFunction(ts, NULL, MyRHSFunction, user);CHKERRQ(ierr); ierr = TSSetDuration(ts, 1000, user->final_time);CHKERRQ(ierr); ierr = TSSetInitialTimeStep(ts, user->initial_time, user->dt);CHKERRQ(ierr); ierr = TSSetFromOptions(ts);CHKERRQ(ierr); ierr = TSSolve(ts, algebra->solution);CHKERRQ(ierr); ierr = TSGetSolveTime(ts, &ftime);CHKERRQ(ierr); ierr = TSGetTimeStepNumber(ts, &nsteps);CHKERRQ(ierr); ierr = TSGetConvergedReason(ts, &reason);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"%s at time %g after %D steps\n",TSConvergedReasons[reason],ftime,nsteps);CHKERRQ(ierr); ierr = TSDestroy(&ts);CHKERRQ(ierr); } if(user->benchmark_couette) { ierr = DMCreateGlobalVector(user->dm, &algebra->exactsolution);CHKERRQ(ierr); ierr = ComputeExactSolution(user->dm, user->current_time, algebra->exactsolution, user);CHKERRQ(ierr); } if(user->benchmark_couette) { PetscViewer viewer; PetscReal norm; ierr = OutputVTK(user->dm, "exact_solution.vtk", &viewer);CHKERRQ(ierr); ierr = VecView(algebra->exactsolution, viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); ierr = VecAXPY(algebra->exactsolution, -1, algebra->solution);CHKERRQ(ierr); ierr = VecNorm(algebra->exactsolution,NORM_INFINITY,&norm);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Final time at %f, Error: ||u_k-u|| = %g \n", user->current_time, norm);CHKERRQ(ierr); ierr = OutputVTK(user->dm, "Error.vtk", &viewer);CHKERRQ(ierr); ierr = VecView(algebra->exactsolution, viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); } ierr = VecDestroy(&algebra->solution);CHKERRQ(ierr); if(user->myownexplicitmethod){ierr = VecDestroy(&algebra->oldsolution);CHKERRQ(ierr);} ierr = VecDestroy(&algebra->exactsolution);CHKERRQ(ierr); ierr = DMDestroy(&user->dm);CHKERRQ(ierr); } else if (user->TimeIntegralMethod == IMPLICITMETHOD) { // Using the fully implicit method ierr = PetscPrintf(PETSC_COMM_WORLD,"Using the fully implicit method\n");CHKERRQ(ierr); ierr = SNESCreate(comm,&user->snes);CHKERRQ(ierr); ierr = SNESSetDM(user->snes,user->dm);CHKERRQ(ierr); ierr = DMCreateGlobalVector(user->dm, &algebra->solution);CHKERRQ(ierr); ierr = VecDuplicate(algebra->solution, &algebra->oldsolution);CHKERRQ(ierr); ierr = VecDuplicate(algebra->solution, &algebra->f);CHKERRQ(ierr); ierr = VecDuplicate(algebra->solution, &algebra->fn);CHKERRQ(ierr); ierr = VecDuplicate(algebra->solution, &algebra->oldfn);CHKERRQ(ierr); ierr = PetscObjectSetName((PetscObject) algebra->solution, "solution");CHKERRQ(ierr); ierr = SetInitialCondition(user->dm, algebra->solution, user);CHKERRQ(ierr); ierr = DMSetMatType(user->dm, MATAIJ);CHKERRQ(ierr); // ierr = DMCreateMatrix(user->dm, &algebra->A);CHKERRQ(ierr); ierr = DMCreateMatrix(user->dm, &algebra->J);CHKERRQ(ierr); if (user->JdiffP) { /*Set up the preconditioner matrix*/ ierr = DMCreateMatrix(user->dm, &algebra->P);CHKERRQ(ierr); }else{ algebra->P = algebra->J; } ierr = MatSetOption(algebra->J, MAT_NEW_NONZERO_ALLOCATION_ERR, PETSC_FALSE);CHKERRQ(ierr); /*set nonlinear function */ ierr = SNESSetFunction(user->snes, algebra->f, FormFunction, (void*)user);CHKERRQ(ierr); /* compute Jacobian */ ierr = SNESSetJacobian(user->snes, algebra->J, algebra->P, FormJacobian, (void*)user);CHKERRQ(ierr); ierr = SNESSetFromOptions(user->snes);CHKERRQ(ierr); /* do the solve */ if (user->timestep == TIMESTEP_STEADY_STATE) { ierr = SolveSteadyState(user);CHKERRQ(ierr); } else { ierr = SolveTimeDependent(user);CHKERRQ(ierr); } if (user->output_solution){ PetscViewer viewer; Vec solution_unscaled; // Note the the algebra->solution is scaled by the density, so this is for the unscaled solution ierr = VecDuplicate(algebra->solution, &solution_unscaled);CHKERRQ(ierr); ierr = ReformatSolution(algebra->solution, solution_unscaled, user);CHKERRQ(ierr); ierr = OutputVTK(user->dm, "solution.vtk", &viewer);CHKERRQ(ierr); ierr = VecView(solution_unscaled, viewer);CHKERRQ(ierr); ierr = VecDestroy(&solution_unscaled);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); } if(user->benchmark_couette) { PetscViewer viewer; PetscReal norm; ierr = OutputVTK(user->dm, "exact_solution.vtk", &viewer);CHKERRQ(ierr); ierr = VecView(algebra->exactsolution, viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); ierr = VecAXPY(algebra->exactsolution, -1, algebra->solution);CHKERRQ(ierr); ierr = VecNorm(algebra->exactsolution,NORM_INFINITY,&norm);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Error: ||u_k-u|| = %g \n", norm);CHKERRQ(ierr); ierr = OutputVTK(user->dm, "Error.vtk", &viewer);CHKERRQ(ierr); ierr = VecView(algebra->exactsolution, viewer);CHKERRQ(ierr); ierr = PetscViewerDestroy(&viewer);CHKERRQ(ierr); } ierr = VecDestroy(&algebra->solution);CHKERRQ(ierr); ierr = VecDestroy(&algebra->f);CHKERRQ(ierr); ierr = VecDestroy(&algebra->oldsolution);CHKERRQ(ierr); ierr = VecDestroy(&algebra->fn);CHKERRQ(ierr); ierr = VecDestroy(&algebra->oldfn);CHKERRQ(ierr); ierr = SNESDestroy(&user->snes);CHKERRQ(ierr); ierr = DMDestroy(&user->dm);CHKERRQ(ierr); } else { SETERRQ(PETSC_COMM_SELF,PETSC_ERR_USER,"WRONG option for the time integral method. Using the option '-time_integral_method 0 or 1'"); } ierr = VecDestroy(&user->cellgeom);CHKERRQ(ierr); ierr = VecDestroy(&user->facegeom);CHKERRQ(ierr); ierr = DMDestroy(&user->dmGrad);CHKERRQ(ierr); ierr = PetscFunctionListDestroy(&LimitList);CHKERRQ(ierr); ierr = PetscFree(user->model->physics);CHKERRQ(ierr); ierr = PetscFree(user->algebra);CHKERRQ(ierr); ierr = PetscFree(user->model);CHKERRQ(ierr); ierr = PetscFree(user);CHKERRQ(ierr); { PetscLogDouble space =0; ierr = PetscMallocGetCurrentUsage(&space);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"Unfreed space at the End %g M\n", space/(1024*1024));CHKERRQ(ierr); } ierr = PetscFinalize(); return(0); }
int main(int argc,char **argv) { PetscFunctionList plist = NULL; char pname[256]; TS ts; /* nonlinear solver */ Vec x,r; /* solution, residual vectors */ Mat A; /* Jacobian matrix */ Problem problem; PetscBool use_monitor; PetscInt steps,maxsteps = 1000,nonlinits,linits,snesfails,rejects; PetscReal ftime; MonitorCtx mon; PetscErrorCode ierr; PetscMPIInt size; /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Initialize program - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ PetscInitialize(&argc,&argv,(char*)0,help); ierr = MPI_Comm_size(PETSC_COMM_WORLD,&size);CHKERRQ(ierr); if (size > 1) SETERRQ(PETSC_COMM_WORLD,PETSC_ERR_SUP,"Only for sequential runs"); /* Register the available problems */ ierr = PetscFunctionListAdd(&plist,"rober",&RoberCreate);CHKERRQ(ierr); ierr = PetscFunctionListAdd(&plist,"ce",&CECreate);CHKERRQ(ierr); ierr = PetscFunctionListAdd(&plist,"orego",&OregoCreate);CHKERRQ(ierr); ierr = PetscStrcpy(pname,"ce");CHKERRQ(ierr); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Set runtime options - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = PetscOptionsBegin(PETSC_COMM_WORLD,NULL,"Timestepping benchmark options","");CHKERRQ(ierr); { ierr = PetscOptionsFList("-problem_type","Name of problem to run","",plist,pname,pname,sizeof(pname),NULL);CHKERRQ(ierr); use_monitor = PETSC_FALSE; ierr = PetscOptionsBool("-monitor_error","Display errors relative to exact solutions","",use_monitor,&use_monitor,NULL);CHKERRQ(ierr); } ierr = PetscOptionsEnd();CHKERRQ(ierr); /* Create the new problem */ ierr = PetscNew(&problem);CHKERRQ(ierr); problem->comm = MPI_COMM_WORLD; { PetscErrorCode (*pcreate)(Problem); ierr = PetscFunctionListFind(plist,pname,&pcreate);CHKERRQ(ierr); if (!pcreate) SETERRQ1(PETSC_COMM_SELF,1,"No problem '%s'",pname); ierr = (*pcreate)(problem);CHKERRQ(ierr); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Create necessary matrix and vectors - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = MatCreate(PETSC_COMM_WORLD,&A);CHKERRQ(ierr); ierr = MatSetSizes(A,problem->n,problem->n,PETSC_DETERMINE,PETSC_DETERMINE);CHKERRQ(ierr); ierr = MatSetFromOptions(A);CHKERRQ(ierr); ierr = MatSetUp(A);CHKERRQ(ierr); ierr = MatGetVecs(A,&x,NULL);CHKERRQ(ierr); ierr = VecDuplicate(x,&r);CHKERRQ(ierr); mon.comm = PETSC_COMM_WORLD; mon.problem = problem; ierr = VecDuplicate(x,&mon.x);CHKERRQ(ierr); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Create timestepping solver context - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = TSCreate(PETSC_COMM_WORLD,&ts);CHKERRQ(ierr); ierr = TSSetProblemType(ts,TS_NONLINEAR);CHKERRQ(ierr); ierr = TSSetType(ts,TSROSW);CHKERRQ(ierr); /* Rosenbrock-W */ ierr = TSSetIFunction(ts,NULL,problem->function,problem->data);CHKERRQ(ierr); ierr = TSSetIJacobian(ts,A,A,problem->jacobian,problem->data);CHKERRQ(ierr); ierr = TSSetDuration(ts,maxsteps,problem->final_time);CHKERRQ(ierr); ierr = TSSetMaxStepRejections(ts,10);CHKERRQ(ierr); ierr = TSSetMaxSNESFailures(ts,-1);CHKERRQ(ierr); /* unlimited */ if (use_monitor) { ierr = TSMonitorSet(ts,&MonitorError,&mon,NULL);CHKERRQ(ierr); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Set initial conditions - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = (*problem->solution)(0,x,problem->data);CHKERRQ(ierr); ierr = TSSetInitialTimeStep(ts,0.0,.001);CHKERRQ(ierr); ierr = TSSetSolution(ts,x);CHKERRQ(ierr); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Set runtime options - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = TSSetFromOptions(ts);CHKERRQ(ierr); /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Solve nonlinear system - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = TSSolve(ts,x);CHKERRQ(ierr); ierr = TSGetSolveTime(ts,&ftime);CHKERRQ(ierr); ierr = TSGetTimeStepNumber(ts,&steps);CHKERRQ(ierr); ierr = TSGetSNESFailures(ts,&snesfails);CHKERRQ(ierr); ierr = TSGetStepRejections(ts,&rejects);CHKERRQ(ierr); ierr = TSGetSNESIterations(ts,&nonlinits);CHKERRQ(ierr); ierr = TSGetKSPIterations(ts,&linits);CHKERRQ(ierr); ierr = PetscPrintf(PETSC_COMM_WORLD,"steps %D (%D rejected, %D SNES fails), ftime %G, nonlinits %D, linits %D\n",steps,rejects,snesfails,ftime,nonlinits,linits);CHKERRQ(ierr); if (problem->hasexact) { ierr = MonitorError(ts,steps,ftime,x,&mon);CHKERRQ(ierr); } /* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - Free work space. All PETSc objects should be destroyed when they are no longer needed. - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */ ierr = MatDestroy(&A);CHKERRQ(ierr); ierr = VecDestroy(&x);CHKERRQ(ierr); ierr = VecDestroy(&r);CHKERRQ(ierr); ierr = VecDestroy(&mon.x);CHKERRQ(ierr); ierr = TSDestroy(&ts);CHKERRQ(ierr); if (problem->destroy) { ierr = (*problem->destroy)(problem);CHKERRQ(ierr); } ierr = PetscFree(problem);CHKERRQ(ierr); ierr = PetscFunctionListDestroy(&plist);CHKERRQ(ierr); ierr = PetscFinalize(); PetscFunctionReturn(0); }
void PETSC_STDCALL petscfunctionlistdestroy_(PetscFunctionList *fl, int *__ierr ){ *__ierr = PetscFunctionListDestroy( (PetscFunctionList* )PetscToPointer((fl) )); }