1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495
|
/*-----------------------------------------------------------------
* Programmer(s): Cody J. Balos @ LLNL
*-----------------------------------------------------------------
* Acknowledgement: This example is based on the PETSc TS ex25.c.
*-----------------------------------------------------------------
* SUNDIALS Copyright Start
* Copyright (c) 2002-2022, Lawrence Livermore National Security
* and Southern Methodist University.
* All rights reserved.
*
* See the top-level LICENSE and NOTICE files for details.
*
* SPDX-License-Identifier: BSD-3-Clause
* SUNDIALS Copyright End
*----------------------------------------------------------------
u_t - alpha u_xx = A + u^2 v - (B+1) u
v_t - alpha v_xx = B u - u^2 v
0 < x < 1;
A = 1, B = 3, alpha = 1/50
Initial conditions:
u(x,0) = 1 + sin(2 pi x)
v(x,0) = 3
Boundary conditions:
u(0,t) = u(1,t) = 1
v(0,t) = v(1,t) = 3
-----------------------------------------------------------------*/
static const char help[] = "ARKode example based on PETSc TS ex25.c.\nTime-dependent Brusselator reaction-diffusion PDE in 1d. Demonstrates IMEX methods.\n";
#include <mpi.h>
#include <petscdm.h>
#include <petscdmda.h>
#include <arkode/arkode_arkstep.h>
#include <nvector/nvector_petsc.h>
#include <sunnonlinsol/sunnonlinsol_petscsnes.h>
typedef struct {
PetscScalar u,v;
} Field;
typedef struct _User *User;
struct _User {
PetscReal A,B; /* Reaction coefficients */
PetscReal alpha; /* Diffusion coefficient */
PetscReal uleft,uright; /* Dirichlet boundary conditions */
PetscReal vleft,vright; /* Dirichlet boundary conditions */
DM da; /* PETSc DM for the problem */
void* arkode_mem; /* ARKode memory structure */
};
static PetscErrorCode FormRHSFunction(PetscReal,Vec,Vec,void*);
static PetscErrorCode FormIFunction(PetscReal,Vec,Vec,Vec,void*);
static PetscErrorCode FormIJacobian(SNES,Vec,Mat,Mat,void*);
static PetscErrorCode FormInitialSolution(Vec,void*);
/* User-supplied Functions called by ARKStep */
static int f_I(PetscReal,N_Vector,N_Vector,void*);
static int f_E(PetscReal,N_Vector,N_Vector,void*);
/* Private function to check function return values */
static int check_retval(void *retvalvalue, const char *funcname, int opt);
int main(int argc, char **argv)
{
long int steps=0;
MPI_Comm comm=PETSC_COMM_WORLD;
/* SUNDIALS data structures */
void *arkode_mem; /* integrator memory */
N_Vector nvecx; /* SUNDIALS N_Vector wrapper of X */
SUNNonlinearSolver NLS; /* SUNDIALS nonlinear solver */
SUNContext ctx; /* SUNDIALS context */
/* PETSc data structures */
SNES snes; /* nonlinear solver */
Vec X; /* solution, residual vectors */
Field *xdata; /* underlying x data */
Mat J; /* Jacobian matrix */
PetscInt mx;
PetscErrorCode ierr;
PetscReal T0,t,tstop,dtout,ftime,hx,dt;
PetscReal rtol,atol;
struct _User user; /* user-defined work context */
/* Initialize PETSc */
ierr = PetscInitialize(&argc,&argv,(char*)0,help);if (ierr) return ierr;
/* Create SUNDIALS context */
ierr = SUNContext_Create(&comm, &ctx);
if (ierr) return ierr;
/* Solution start and end time */
T0 = 0.0;
ftime = 10.0;
tstop = T0;
t = T0;
dtout = 1.0;
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Create distributed array (DMDA) to manage parallel grid and vectors
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
ierr = DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,11,2,2,NULL,&user.da);CHKERRQ(ierr);
ierr = DMSetFromOptions(user.da);CHKERRQ(ierr);
ierr = DMSetUp(user.da);CHKERRQ(ierr);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Extract global vectors from DMDA;
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
ierr = DMCreateGlobalVector(user.da,&X);CHKERRQ(ierr);
nvecx = N_VMake_Petsc(X, ctx);
if (check_retval((void *)nvecx,"N_VMake_Petsc",0)) return 1;
/* Initialize user application context */
#if PETSC_VERSION_GE(3,17,99)
PetscOptionsBegin(PETSC_COMM_WORLD,NULL,"Advection-reaction options","");
#else
ierr = PetscOptionsBegin(PETSC_COMM_WORLD,NULL,"Advection-reaction options","");CHKERRQ(ierr);
#endif
{
rtol = 1e-4;
atol = 1e-4;
user.A = 1;
user.B = 3;
user.alpha = 0.02;
user.uleft = 1;
user.uright = 1;
user.vleft = 3;
user.vright = 3;
ierr = PetscOptionsReal("-rtol","Relative tolerance","",rtol,&rtol,NULL);CHKERRQ(ierr);
ierr = PetscOptionsReal("-atol","Absolute tolerance","",atol,&atol,NULL);CHKERRQ(ierr);
ierr = PetscOptionsReal("-A","Reaction rate","",user.A,&user.A,NULL);CHKERRQ(ierr);
ierr = PetscOptionsReal("-B","Reaction rate","",user.B,&user.B,NULL);CHKERRQ(ierr);
ierr = PetscOptionsReal("-alpha","Diffusion coefficient","",user.alpha,&user.alpha,NULL);CHKERRQ(ierr);
ierr = PetscOptionsReal("-uleft","Dirichlet boundary condition","",user.uleft,&user.uleft,NULL);CHKERRQ(ierr);
ierr = PetscOptionsReal("-uright","Dirichlet boundary condition","",user.uright,&user.uright,NULL);CHKERRQ(ierr);
ierr = PetscOptionsReal("-vleft","Dirichlet boundary condition","",user.vleft,&user.vleft,NULL);CHKERRQ(ierr);
ierr = PetscOptionsReal("-vright","Dirichlet boundary condition","",user.vright,&user.vright,NULL);CHKERRQ(ierr);
}
#if PETSC_VERSION_GE(3,17,99)
PetscOptionsEnd();
#else
ierr = PetscOptionsEnd();CHKERRQ(ierr);
#endif
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Set initial conditions
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
ierr = FormInitialSolution(X,&user);CHKERRQ(ierr);
ierr = VecGetSize(X,&mx);CHKERRQ(ierr);
hx = 1.0/(PetscReal)(mx/2-1);
dt = 0.4 * PetscSqr(hx) / user.alpha; /* Diffusive stability limit */
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Create ARKStep time stepper
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Call ARKStepCreate to initialize the ARK timestepper module and
specify the right-hand side function in y'=f(t,y),the inital time
T0,and the initial dependent variable vector y. */
arkode_mem = ARKStepCreate(f_E,f_I,T0,nvecx,ctx);
if (check_retval((void *)arkode_mem,"ARKStepCreate",0)) return 1;
/* Store the arkode mem in the user data so we can access it in the Jacobian routine */
user.arkode_mem = arkode_mem;
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Create the nonlinear solver
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
ierr = SNESCreate(PETSC_COMM_WORLD,&snes);CHKERRQ(ierr);
/* Create SUNNonlinearSolver object which interfaces to SNES */
NLS = SUNNonlinSol_PetscSNES(nvecx,snes,ctx); /* this will call SNESSetFunction appropriately */
if (check_retval((void *)NLS,"SUNNonlinSol_PetscSNES",0)) return 1;
/* Set the Jacobian routine */
ierr = DMSetMatType(user.da,MATAIJ);CHKERRQ(ierr);
ierr = DMCreateMatrix(user.da,&J);CHKERRQ(ierr);
ierr = SNESSetJacobian(snes,J,J,FormIJacobian,&user);CHKERRQ(ierr);
/* Allow SNES/KSP/PC runtime options */
ierr = SNESSetFromOptions(snes);CHKERRQ(ierr);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Set ARKStep options
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
ierr = ARKStepSStolerances(arkode_mem,rtol,atol);
if (check_retval(&ierr,"ARKStepSStolerances",1)) return 1;
ierr = ARKStepSetOrder(arkode_mem,3);
if (check_retval(&ierr,"ARKStepSetOrder",1)) return 1;
ierr = ARKStepSetUserData(arkode_mem,(void *) &user);
if (check_retval(&ierr,"ARKStepSetUserData",1)) return 1;
ierr = ARKStepSetNonlinearSolver(arkode_mem,NLS);
if (check_retval(&ierr,"ARKStepSetNonlinearSolver",1)) return 1;
ierr = ARKStepSetAdaptivityMethod(arkode_mem,2,1,0,NULL);
if (check_retval(&ierr,"ARKStepSetAdaptivity",1)) return 1;
ierr = ARKStepSetInitStep(arkode_mem,dt);
if (check_retval(&ierr,"ARKStepSetInitStep",1)) return 1;
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Perform the integration
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
printf("%s\n",help);
/* Extract underlying data of x for printing */
ierr = DMDAVecGetArrayRead(user.da,X,&xdata);CHKERRQ(ierr);
/* Print out the solution every dt */
while(tstop<=ftime) {
printf("%ld TS dt %.6e time %.6f\n",steps,dt,t);
/* Advance time */
tstop+=dtout;
ierr = ARKStepSetStopTime(arkode_mem,tstop);
if (check_retval(&ierr,"ARKStepSetStopTime",1)) return 1;
/* Evolve solution in time */
ierr = ARKStepEvolve(arkode_mem,ftime,nvecx,&t,ARK_NORMAL);
if (check_retval(&ierr,"ARKStepEvolve",1)) return 1;
/* Get statistics */
ierr = ARKStepGetCurrentStep(arkode_mem,&dt);
if (check_retval(&ierr,"ARKStepGetCurrntStep",1)) return 1;
ierr = ARKStepGetNumSteps(arkode_mem,&steps);
if (check_retval(&ierr,"ARKStepGetNumSteps",1)) return 1;
}
printf("Converged at time %g after %ld steps.\n",t,steps);
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Free work space.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Free SUNDIALS data structures */
N_VDestroy(nvecx); /* Free x nvector */
SUNNonlinSolFree(NLS); /* Free nonlinear solver */
ARKStepFree(&arkode_mem); /* Free integrator memory */
/* Free petsc data structures */
ierr = MatDestroy(&J);CHKERRQ(ierr);
ierr = VecDestroy(&X);CHKERRQ(ierr);
ierr = DMDestroy(&user.da);CHKERRQ(ierr);
ierr = SUNContext_Free(&ctx);
ierr = PetscFinalize();
return ierr;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
User provided functions in ARKStep format
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
/* Implicit component of RHS */
static int f_I(PetscReal t,N_Vector x,N_Vector xdot,void *ptr)
{
PetscErrorCode ierr;
ierr = FormIFunction(t,N_VGetVector_Petsc(x),NULL,N_VGetVector_Petsc(xdot),ptr);
return ierr;
}
/* Explicit component of RHS */
static int f_E(PetscReal t,N_Vector x,N_Vector xdot,void *ptr)
{
PetscErrorCode ierr;
ierr = FormRHSFunction(t,N_VGetVector_Petsc(x),N_VGetVector_Petsc(xdot),ptr);
return ierr;
}
/* - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
User provided functions in Petsc TS format (minus the TS context)
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - */
static PetscErrorCode FormIFunction(PetscReal t,Vec X,Vec Xdot,Vec F,void *ptr)
{
User user = (User)ptr;
DMDALocalInfo info;
PetscInt i;
Field *x,*f;
PetscReal hx;
Vec Xloc;
PetscErrorCode ierr;
PetscFunctionBeginUser;
ierr = DMDAGetLocalInfo(user->da,&info);CHKERRQ(ierr);
hx = 1.0/(PetscReal)(info.mx-1);
/*
Scatter ghost points to local vector,using the 2-step process
DMGlobalToLocalBegin(), DMGlobalToLocalEnd().
By placing code between these two statements,computations can be
done while messages are in transition.
*/
ierr = DMGetLocalVector(user->da,&Xloc);CHKERRQ(ierr);
ierr = DMGlobalToLocalBegin(user->da,X,INSERT_VALUES,Xloc);CHKERRQ(ierr);
ierr = DMGlobalToLocalEnd(user->da,X,INSERT_VALUES,Xloc);CHKERRQ(ierr);
/* Get pointers to vector data */
ierr = DMDAVecGetArrayRead(user->da,Xloc,&x);CHKERRQ(ierr);
ierr = DMDAVecGetArray(user->da,F,&f);CHKERRQ(ierr);
/* Compute function over the locally owned part of the grid */
for (i=info.xs; i<info.xs+info.xm; i++) {
if (i == 0) {
f[i].u = 0.;
f[i].v = 0.;
} else if (i == info.mx-1) {
f[i].u = 0.;
f[i].v = 0.;
} else {
f[i].u = user->alpha * (x[i-1].u - 2.*x[i].u + x[i+1].u) / (hx*hx);
f[i].v = user->alpha * (x[i-1].v - 2.*x[i].v + x[i+1].v) / (hx*hx);
}
}
/* Restore vectors */
ierr = DMDAVecRestoreArrayRead(user->da,Xloc,&x);CHKERRQ(ierr);
ierr = DMDAVecRestoreArray(user->da,F,&f);CHKERRQ(ierr);
ierr = DMRestoreLocalVector(user->da,&Xloc);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
static PetscErrorCode FormRHSFunction(PetscReal t,Vec X,Vec F,void *ptr)
{
User user = (User)ptr;
DMDALocalInfo info;
PetscInt i;
Field *x,*f;
PetscErrorCode ierr;
PetscFunctionBeginUser;
ierr = DMDAGetLocalInfo(user->da,&info);CHKERRQ(ierr);
/* Get pointers to vector data */
ierr = DMDAVecGetArrayRead(user->da,X,&x);CHKERRQ(ierr);
ierr = DMDAVecGetArray(user->da,F,&f);CHKERRQ(ierr);
/* Compute function over the locally owned part of the grid */
for (i=info.xs; i<info.xs+info.xm; i++) {
PetscScalar u = x[i].u,v = x[i].v;
if (i == 0) {
f[i].u = 0.;
f[i].v = 0.;
} else if (i == info.mx-1) {
f[i].u = 0.;
f[i].v = 0.;
} else {
f[i].u = user->A + u*u*v - (user->B+1)*u;
f[i].v = user->B*u - u*u*v;
}
}
/* Restore vectors */
ierr = DMDAVecRestoreArrayRead(user->da,X,&x);CHKERRQ(ierr);
ierr = DMDAVecRestoreArray(user->da,F,&f);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
/* --------------------------------------------------------------------- */
/*
IJacobian - Compute J = I - gamma * df_i/dx
*/
PetscErrorCode FormIJacobian(SNES snes,Vec X,Mat J,Mat Jpre,void *ptr)
{
User user = (User)ptr;
PetscErrorCode ierr;
DMDALocalInfo info;
PetscInt i;
PetscReal hx,gamma,a;
Field *x;
PetscFunctionBeginUser;
ierr = DMDAGetLocalInfo(user->da,&info);CHKERRQ(ierr);
hx = 1.0/(PetscReal)(info.mx-1);
/* Get current gamma value from ARKode */
ierr = ARKStepGetCurrentGamma(user->arkode_mem,&gamma);
/* Get pointers to vector data */
ierr = DMDAVecGetArrayRead(user->da,X,&x);CHKERRQ(ierr);
/* Set shortcut value */
a = user->alpha/hx/hx * gamma;
/* Compute function over the locally owned part of the grid */
for (i=info.xs; i<info.xs+info.xm; i++) {
if (i == 0 || i == info.mx-1) {
const PetscInt row = i,col = i;
const PetscScalar vals[2][2] = {{1.,0},{0,1.}};
ierr = MatSetValuesBlocked(Jpre,1,&row,1,&col,&vals[0][0],INSERT_VALUES);CHKERRQ(ierr);
} else {
const PetscInt row = i,col[] = {i-1,i,i+1};
const PetscScalar dxxL = -a,dxx0 = 2.*a,dxxR = -a;
const PetscScalar vals[2][3][2] = {{{dxxL,0},{1.+dxx0,0},{dxxR,0}},
{{0,dxxL},{0,1.+dxx0},{0,dxxR}}};
ierr = MatSetValuesBlocked(Jpre,1,&row,3,col,&vals[0][0][0],INSERT_VALUES);CHKERRQ(ierr);
}
}
/* Restore vectors */
ierr = DMDAVecRestoreArrayRead(user->da,X,&x);CHKERRQ(ierr);
/* Assemble matrix */
ierr = MatAssemblyBegin(Jpre,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatAssemblyEnd(Jpre,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
if (J != Jpre) {
ierr = MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
ierr = MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY);CHKERRQ(ierr);
}
/* Add I */
ierr = MatShift(Jpre,1.0);
PetscFunctionReturn(0);
}
PetscErrorCode FormInitialSolution(Vec X,void *ctx)
{
User user = (User)ctx;
PetscInt i;
DMDALocalInfo info;
Field *x;
PetscReal hx;
PetscErrorCode ierr;
PetscFunctionBeginUser;
ierr = DMDAGetLocalInfo(user->da,&info);CHKERRQ(ierr);
hx = 1.0/(PetscReal)(info.mx-1);
/* Get pointers to vector data */
ierr = DMDAVecGetArray(user->da,X,&x);CHKERRQ(ierr);
/* Compute function over the locally owned part of the grid */
for (i=info.xs; i<info.xs+info.xm; i++) {
PetscReal xi = i*hx;
x[i].u = user->uleft*(1.-xi) + user->uright*xi + PetscSinReal(2.*PETSC_PI*xi);
x[i].v = user->vleft*(1.-xi) + user->vright*xi;
}
ierr = DMDAVecRestoreArray(user->da,X,&x);CHKERRQ(ierr);
PetscFunctionReturn(0);
}
/*-------------------------------
* Private helper functions
*-------------------------------*/
/* Check function return value...
opt == 0 means SUNDIALS function allocates memory so check if
returned NULL pointer
opt == 1 means SUNDIALS function returns a retval so check if
retval >= 0
opt == 2 means function allocates memory so check if returned
NULL pointer
*/
static int check_retval(void *value,const char *funcname,int opt)
{
int *errretval;
/* Check if SUNDIALS function returned NULL pointer - no memory allocated */
if (opt == 0 && value == NULL) {
fprintf(stderr,"\nSUNDIALS_ERROR: %s() failed - returned NULL pointer\n\n",
funcname);
return 1; }
/* Check if retval < 0 */
else if (opt == 1) {
errretval = (int *) value;
if (*errretval < 0) {
fprintf(stderr,"\nSUNDIALS_ERROR: %s() failed with retval = %d\n\n",
funcname,*errretval);
return 1; }}
/* Check if function returned NULL pointer - no memory allocated */
else if (opt == 2 && value == NULL) {
fprintf(stderr,"\nMEMORY_ERROR: %s() failed - returned NULL pointer\n\n",
funcname);
return 1; }
return 0;
}
|