1c4762a1bSJed Brown /********************************************************************** 2c4762a1bSJed Brown American Put Options Pricing using the Black-Scholes Equation 3c4762a1bSJed Brown 4c4762a1bSJed Brown Background (European Options): 5c4762a1bSJed Brown The standard European option is a contract where the holder has the right 6c4762a1bSJed Brown to either buy (call option) or sell (put option) an underlying asset at 7c4762a1bSJed Brown a designated future time and price. 8c4762a1bSJed Brown 9c4762a1bSJed Brown The classic Black-Scholes model begins with an assumption that the 10c4762a1bSJed Brown price of the underlying asset behaves as a lognormal random walk. 11c4762a1bSJed Brown Using this assumption and a no-arbitrage argument, the following 12c4762a1bSJed Brown linear parabolic partial differential equation for the value of the 13c4762a1bSJed Brown option results: 14c4762a1bSJed Brown 15c4762a1bSJed Brown dV/dt + 0.5(sigma**2)(S**alpha)(d2V/dS2) + (r - D)S(dV/dS) - rV = 0. 16c4762a1bSJed Brown 17c4762a1bSJed Brown Here, sigma is the volatility of the underling asset, alpha is a 18c4762a1bSJed Brown measure of elasticity (typically two), D measures the dividend payments 19c4762a1bSJed Brown on the underling asset, and r is the interest rate. 20c4762a1bSJed Brown 21c4762a1bSJed Brown To completely specify the problem, we need to impose some boundary 22c4762a1bSJed Brown conditions. These are as follows: 23c4762a1bSJed Brown 24c4762a1bSJed Brown V(S, T) = max(E - S, 0) 25c4762a1bSJed Brown V(0, t) = E for all 0 <= t <= T 26c4762a1bSJed Brown V(s, t) = 0 for all 0 <= t <= T and s->infinity 27c4762a1bSJed Brown 28c4762a1bSJed Brown where T is the exercise time time and E the strike price (price paid 29c4762a1bSJed Brown for the contract). 30c4762a1bSJed Brown 31c4762a1bSJed Brown An explicit formula for the value of an European option can be 32c4762a1bSJed Brown found. See the references for examples. 33c4762a1bSJed Brown 34c4762a1bSJed Brown Background (American Options): 35c4762a1bSJed Brown The American option is similar to its European counterpart. The 36a5b23f4aSJose E. Roman difference is that the holder of the American option can exercise 37c4762a1bSJed Brown their right to buy or sell the asset at any time prior to the 38c4762a1bSJed Brown expiration. This additional ability introduce a free boundary into 39c4762a1bSJed Brown the Black-Scholes equation which can be modeled as a linear 40c4762a1bSJed Brown complementarity problem. 41c4762a1bSJed Brown 42c4762a1bSJed Brown 0 <= -(dV/dt + 0.5(sigma**2)(S**alpha)(d2V/dS2) + (r - D)S(dV/dS) - rV) 43c4762a1bSJed Brown complements 44c4762a1bSJed Brown V(S,T) >= max(E-S,0) 45c4762a1bSJed Brown 46c4762a1bSJed Brown where the variables are the same as before and we have the same boundary 47c4762a1bSJed Brown conditions. 48c4762a1bSJed Brown 49c4762a1bSJed Brown There is not explicit formula for calculating the value of an American 50c4762a1bSJed Brown option. Therefore, we discretize the above problem and solve the 51c4762a1bSJed Brown resulting linear complementarity problem. 52c4762a1bSJed Brown 53c4762a1bSJed Brown We will use backward differences for the time variables and central 54c4762a1bSJed Brown differences for the space variables. Crank-Nicholson averaging will 55c4762a1bSJed Brown also be used in the discretization. The algorithm used by the code 56c4762a1bSJed Brown solves for V(S,t) for a fixed t and then uses this value in the 57c4762a1bSJed Brown calculation of V(S,t - dt). The method stops when V(S,0) has been 58c4762a1bSJed Brown found. 59c4762a1bSJed Brown 60c4762a1bSJed Brown References: 61606c0280SSatish Balay + * - Huang and Pang, "Options Pricing and Linear Complementarity," 62c4762a1bSJed Brown Journal of Computational Finance, volume 2, number 3, 1998. 63606c0280SSatish Balay - * - Wilmott, "Derivatives: The Theory and Practice of Financial Engineering," 64c4762a1bSJed Brown John Wiley and Sons, New York, 1998. 65c4762a1bSJed Brown ***************************************************************************/ 66c4762a1bSJed Brown 67c4762a1bSJed Brown /* 68c4762a1bSJed Brown Include "petsctao.h" so we can use TAO solvers. 69c4762a1bSJed Brown Include "petscdmda.h" so that we can use distributed meshes (DMs) for managing 70c4762a1bSJed Brown the parallel mesh. 71c4762a1bSJed Brown */ 72c4762a1bSJed Brown 73c4762a1bSJed Brown #include <petscdmda.h> 74c4762a1bSJed Brown #include <petsctao.h> 75c4762a1bSJed Brown 76c4762a1bSJed Brown static char help[] = 77c4762a1bSJed Brown "This example demonstrates use of the TAO package to\n\ 78c4762a1bSJed Brown solve a linear complementarity problem for pricing American put options.\n\ 79c4762a1bSJed Brown The code uses backward differences in time and central differences in\n\ 80c4762a1bSJed Brown space. The command line options are:\n\ 81c4762a1bSJed Brown -rate <r>, where <r> = interest rate\n\ 82c4762a1bSJed Brown -sigma <s>, where <s> = volatility of the underlying\n\ 83c4762a1bSJed Brown -alpha <a>, where <a> = elasticity of the underlying\n\ 84c4762a1bSJed Brown -delta <d>, where <d> = dividend rate\n\ 85c4762a1bSJed Brown -strike <e>, where <e> = strike price\n\ 86c4762a1bSJed Brown -expiry <t>, where <t> = the expiration date\n\ 87c4762a1bSJed Brown -mt <tg>, where <tg> = number of grid points in time\n\ 88c4762a1bSJed Brown -ms <sg>, where <sg> = number of grid points in space\n\ 89c4762a1bSJed Brown -es <se>, where <se> = ending point of the space discretization\n\n"; 90c4762a1bSJed Brown 91c4762a1bSJed Brown /*T 92c4762a1bSJed Brown Concepts: TAO^Solving a complementarity problem 93c4762a1bSJed Brown Routines: TaoCreate(); TaoDestroy(); 94c4762a1bSJed Brown Routines: TaoSetJacobianRoutine(); TaoAppSetConstraintRoutine(); 95c4762a1bSJed Brown Routines: TaoSetFromOptions(); 96c4762a1bSJed Brown Routines: TaoSolveApplication(); 97c4762a1bSJed Brown Routines: TaoSetVariableBoundsRoutine(); TaoSetInitialSolutionVec(); 98c4762a1bSJed Brown Processors: 1 99c4762a1bSJed Brown T*/ 100c4762a1bSJed Brown 101c4762a1bSJed Brown /* 102c4762a1bSJed Brown User-defined application context - contains data needed by the 103c4762a1bSJed Brown application-provided call-back routines, FormFunction(), and FormJacobian(). 104c4762a1bSJed Brown */ 105c4762a1bSJed Brown 106c4762a1bSJed Brown typedef struct { 107c4762a1bSJed Brown PetscReal *Vt1; /* Value of the option at time T + dt */ 108c4762a1bSJed Brown PetscReal *c; /* Constant -- (r - D)S */ 109c4762a1bSJed Brown PetscReal *d; /* Constant -- -0.5(sigma**2)(S**alpha) */ 110c4762a1bSJed Brown 111c4762a1bSJed Brown PetscReal rate; /* Interest rate */ 112c4762a1bSJed Brown PetscReal sigma, alpha, delta; /* Underlying asset properties */ 113c4762a1bSJed Brown PetscReal strike, expiry; /* Option contract properties */ 114c4762a1bSJed Brown 115c4762a1bSJed Brown PetscReal es; /* Finite value used for maximum asset value */ 116c4762a1bSJed Brown PetscReal ds, dt; /* Discretization properties */ 117c4762a1bSJed Brown PetscInt ms, mt; /* Number of elements */ 118c4762a1bSJed Brown 119c4762a1bSJed Brown DM dm; 120c4762a1bSJed Brown } AppCtx; 121c4762a1bSJed Brown 122c4762a1bSJed Brown /* -------- User-defined Routines --------- */ 123c4762a1bSJed Brown 124c4762a1bSJed Brown PetscErrorCode FormConstraints(Tao, Vec, Vec, void *); 125c4762a1bSJed Brown PetscErrorCode FormJacobian(Tao, Vec, Mat, Mat, void *); 126c4762a1bSJed Brown PetscErrorCode ComputeVariableBounds(Tao, Vec, Vec, void*); 127c4762a1bSJed Brown 128c4762a1bSJed Brown int main(int argc, char **argv) 129c4762a1bSJed Brown { 130c4762a1bSJed Brown Vec x; /* solution vector */ 131c4762a1bSJed Brown Vec c; /* Constraints function vector */ 132c4762a1bSJed Brown Mat J; /* jacobian matrix */ 133c4762a1bSJed Brown PetscBool flg; /* A return variable when checking for user options */ 134c4762a1bSJed Brown Tao tao; /* Tao solver context */ 135c4762a1bSJed Brown AppCtx user; /* user-defined work context */ 136c4762a1bSJed Brown PetscInt i, j; 137c4762a1bSJed Brown PetscInt xs,xm,gxs,gxm; 138c4762a1bSJed Brown PetscReal sval = 0; 139c4762a1bSJed Brown PetscReal *x_array; 140c4762a1bSJed Brown Vec localX; 141c4762a1bSJed Brown 142c4762a1bSJed Brown /* Initialize PETSc, TAO */ 143*9566063dSJacob Faibussowitsch PetscCall(PetscInitialize(&argc, &argv, (char *)0, help)); 144c4762a1bSJed Brown 145c4762a1bSJed Brown /* 146c4762a1bSJed Brown Initialize the user-defined application context with reasonable 147c4762a1bSJed Brown values for the American option to price 148c4762a1bSJed Brown */ 149c4762a1bSJed Brown user.rate = 0.04; 150c4762a1bSJed Brown user.sigma = 0.40; 151c4762a1bSJed Brown user.alpha = 2.00; 152c4762a1bSJed Brown user.delta = 0.01; 153c4762a1bSJed Brown user.strike = 10.0; 154c4762a1bSJed Brown user.expiry = 1.0; 155c4762a1bSJed Brown user.mt = 10; 156c4762a1bSJed Brown user.ms = 150; 157c4762a1bSJed Brown user.es = 100.0; 158c4762a1bSJed Brown 159c4762a1bSJed Brown /* Read in alternative values for the American option to price */ 160*9566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetReal(NULL,NULL, "-alpha", &user.alpha, &flg)); 161*9566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetReal(NULL,NULL, "-delta", &user.delta, &flg)); 162*9566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetReal(NULL,NULL, "-es", &user.es, &flg)); 163*9566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetReal(NULL,NULL, "-expiry", &user.expiry, &flg)); 164*9566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetInt(NULL,NULL, "-ms", &user.ms, &flg)); 165*9566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetInt(NULL,NULL, "-mt", &user.mt, &flg)); 166*9566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetReal(NULL,NULL, "-rate", &user.rate, &flg)); 167*9566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetReal(NULL,NULL, "-sigma", &user.sigma, &flg)); 168*9566063dSJacob Faibussowitsch PetscCall(PetscOptionsGetReal(NULL,NULL, "-strike", &user.strike, &flg)); 169c4762a1bSJed Brown 170c4762a1bSJed Brown /* Check that the options set are allowable (needs to be done) */ 171c4762a1bSJed Brown 172c4762a1bSJed Brown user.ms++; 173*9566063dSJacob Faibussowitsch PetscCall(DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,user.ms,1,1,NULL,&user.dm)); 174*9566063dSJacob Faibussowitsch PetscCall(DMSetFromOptions(user.dm)); 175*9566063dSJacob Faibussowitsch PetscCall(DMSetUp(user.dm)); 176c4762a1bSJed Brown /* Create appropriate vectors and matrices */ 177c4762a1bSJed Brown 178*9566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(user.dm,&xs,NULL,NULL,&xm,NULL,NULL)); 179*9566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(user.dm,&gxs,NULL,NULL,&gxm,NULL,NULL)); 180c4762a1bSJed Brown 181*9566063dSJacob Faibussowitsch PetscCall(DMCreateGlobalVector(user.dm,&x)); 182c4762a1bSJed Brown /* 183c4762a1bSJed Brown Finish filling in the user-defined context with the values for 184c4762a1bSJed Brown dS, dt, and allocating space for the constants 185c4762a1bSJed Brown */ 186c4762a1bSJed Brown user.ds = user.es / (user.ms-1); 187c4762a1bSJed Brown user.dt = user.expiry / user.mt; 188c4762a1bSJed Brown 189*9566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(gxm,&(user.Vt1))); 190*9566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(gxm,&(user.c))); 191*9566063dSJacob Faibussowitsch PetscCall(PetscMalloc1(gxm,&(user.d))); 192c4762a1bSJed Brown 193c4762a1bSJed Brown /* 194c4762a1bSJed Brown Calculate the values for the constant. Vt1 begins with the ending 195c4762a1bSJed Brown boundary condition. 196c4762a1bSJed Brown */ 197c4762a1bSJed Brown for (i=0; i<gxm; i++) { 198c4762a1bSJed Brown sval = (gxs+i)*user.ds; 199c4762a1bSJed Brown user.Vt1[i] = PetscMax(user.strike - sval, 0); 200c4762a1bSJed Brown user.c[i] = (user.delta - user.rate)*sval; 201c4762a1bSJed Brown user.d[i] = -0.5*user.sigma*user.sigma*PetscPowReal(sval, user.alpha); 202c4762a1bSJed Brown } 203c4762a1bSJed Brown if (gxs+gxm==user.ms) { 204c4762a1bSJed Brown user.Vt1[gxm-1] = 0; 205c4762a1bSJed Brown } 206*9566063dSJacob Faibussowitsch PetscCall(VecDuplicate(x, &c)); 207c4762a1bSJed Brown 208c4762a1bSJed Brown /* 209c4762a1bSJed Brown Allocate the matrix used by TAO for the Jacobian. Each row of 210c4762a1bSJed Brown the Jacobian matrix will have at most three elements. 211c4762a1bSJed Brown */ 212*9566063dSJacob Faibussowitsch PetscCall(DMCreateMatrix(user.dm,&J)); 213c4762a1bSJed Brown 214c4762a1bSJed Brown /* The TAO code begins here */ 215c4762a1bSJed Brown 216c4762a1bSJed Brown /* Create TAO solver and set desired solution method */ 217*9566063dSJacob Faibussowitsch PetscCall(TaoCreate(PETSC_COMM_WORLD, &tao)); 218*9566063dSJacob Faibussowitsch PetscCall(TaoSetType(tao,TAOSSILS)); 219c4762a1bSJed Brown 220c4762a1bSJed Brown /* Set routines for constraints function and Jacobian evaluation */ 221*9566063dSJacob Faibussowitsch PetscCall(TaoSetConstraintsRoutine(tao, c, FormConstraints, (void *)&user)); 222*9566063dSJacob Faibussowitsch PetscCall(TaoSetJacobianRoutine(tao, J, J, FormJacobian, (void *)&user)); 223c4762a1bSJed Brown 224c4762a1bSJed Brown /* Set the variable bounds */ 225*9566063dSJacob Faibussowitsch PetscCall(TaoSetVariableBoundsRoutine(tao,ComputeVariableBounds,(void*)&user)); 226c4762a1bSJed Brown 227c4762a1bSJed Brown /* Set initial solution guess */ 228*9566063dSJacob Faibussowitsch PetscCall(VecGetArray(x,&x_array)); 229c4762a1bSJed Brown for (i=0; i< xm; i++) 230c4762a1bSJed Brown x_array[i] = user.Vt1[i-gxs+xs]; 231*9566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(x,&x_array)); 232c4762a1bSJed Brown /* Set data structure */ 233*9566063dSJacob Faibussowitsch PetscCall(TaoSetSolution(tao, x)); 234c4762a1bSJed Brown 235c4762a1bSJed Brown /* Set routines for function and Jacobian evaluation */ 236*9566063dSJacob Faibussowitsch PetscCall(TaoSetFromOptions(tao)); 237c4762a1bSJed Brown 238c4762a1bSJed Brown /* Iteratively solve the linear complementarity problems */ 239c4762a1bSJed Brown for (i = 1; i < user.mt; i++) { 240c4762a1bSJed Brown 241c4762a1bSJed Brown /* Solve the current version */ 242*9566063dSJacob Faibussowitsch PetscCall(TaoSolve(tao)); 243c4762a1bSJed Brown 244c4762a1bSJed Brown /* Update Vt1 with the solution */ 245*9566063dSJacob Faibussowitsch PetscCall(DMGetLocalVector(user.dm,&localX)); 246*9566063dSJacob Faibussowitsch PetscCall(DMGlobalToLocalBegin(user.dm,x,INSERT_VALUES,localX)); 247*9566063dSJacob Faibussowitsch PetscCall(DMGlobalToLocalEnd(user.dm,x,INSERT_VALUES,localX)); 248*9566063dSJacob Faibussowitsch PetscCall(VecGetArray(localX,&x_array)); 249c4762a1bSJed Brown for (j = 0; j < gxm; j++) { 250c4762a1bSJed Brown user.Vt1[j] = x_array[j]; 251c4762a1bSJed Brown } 252*9566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(x,&x_array)); 253*9566063dSJacob Faibussowitsch PetscCall(DMRestoreLocalVector(user.dm,&localX)); 254c4762a1bSJed Brown } 255c4762a1bSJed Brown 256c4762a1bSJed Brown /* Free TAO data structures */ 257*9566063dSJacob Faibussowitsch PetscCall(TaoDestroy(&tao)); 258c4762a1bSJed Brown 259c4762a1bSJed Brown /* Free PETSc data structures */ 260*9566063dSJacob Faibussowitsch PetscCall(VecDestroy(&x)); 261*9566063dSJacob Faibussowitsch PetscCall(VecDestroy(&c)); 262*9566063dSJacob Faibussowitsch PetscCall(MatDestroy(&J)); 263*9566063dSJacob Faibussowitsch PetscCall(DMDestroy(&user.dm)); 264c4762a1bSJed Brown /* Free user-defined workspace */ 265*9566063dSJacob Faibussowitsch PetscCall(PetscFree(user.Vt1)); 266*9566063dSJacob Faibussowitsch PetscCall(PetscFree(user.c)); 267*9566063dSJacob Faibussowitsch PetscCall(PetscFree(user.d)); 268c4762a1bSJed Brown 269*9566063dSJacob Faibussowitsch PetscCall(PetscFinalize()); 270b122ec5aSJacob Faibussowitsch return 0; 271c4762a1bSJed Brown } 272c4762a1bSJed Brown 273c4762a1bSJed Brown /* -------------------------------------------------------------------- */ 274c4762a1bSJed Brown PetscErrorCode ComputeVariableBounds(Tao tao, Vec xl, Vec xu, void*ctx) 275c4762a1bSJed Brown { 276c4762a1bSJed Brown AppCtx *user = (AppCtx *) ctx; 277c4762a1bSJed Brown PetscInt i; 278c4762a1bSJed Brown PetscInt xs,xm; 279c4762a1bSJed Brown PetscInt ms = user->ms; 280c4762a1bSJed Brown PetscReal sval=0.0,*xl_array,ub= PETSC_INFINITY; 281c4762a1bSJed Brown 282c4762a1bSJed Brown /* Set the variable bounds */ 283*9566063dSJacob Faibussowitsch PetscCall(VecSet(xu, ub)); 284*9566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(user->dm,&xs,NULL,NULL,&xm,NULL,NULL)); 285c4762a1bSJed Brown 286*9566063dSJacob Faibussowitsch PetscCall(VecGetArray(xl,&xl_array)); 287c4762a1bSJed Brown for (i = 0; i < xm; i++) { 288c4762a1bSJed Brown sval = (xs+i)*user->ds; 289c4762a1bSJed Brown xl_array[i] = PetscMax(user->strike - sval, 0); 290c4762a1bSJed Brown } 291*9566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(xl,&xl_array)); 292c4762a1bSJed Brown 293c4762a1bSJed Brown if (xs==0) { 294*9566063dSJacob Faibussowitsch PetscCall(VecGetArray(xu,&xl_array)); 295c4762a1bSJed Brown xl_array[0] = PetscMax(user->strike, 0); 296*9566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(xu,&xl_array)); 297c4762a1bSJed Brown } 298c4762a1bSJed Brown if (xs+xm==ms) { 299*9566063dSJacob Faibussowitsch PetscCall(VecGetArray(xu,&xl_array)); 300c4762a1bSJed Brown xl_array[xm-1] = 0; 301*9566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(xu,&xl_array)); 302c4762a1bSJed Brown } 303c4762a1bSJed Brown 304c4762a1bSJed Brown return 0; 305c4762a1bSJed Brown } 306c4762a1bSJed Brown /* -------------------------------------------------------------------- */ 307c4762a1bSJed Brown 308c4762a1bSJed Brown /* 309c4762a1bSJed Brown FormFunction - Evaluates gradient of f. 310c4762a1bSJed Brown 311c4762a1bSJed Brown Input Parameters: 312c4762a1bSJed Brown . tao - the Tao context 313c4762a1bSJed Brown . X - input vector 314c4762a1bSJed Brown . ptr - optional user-defined context, as set by TaoAppSetConstraintRoutine() 315c4762a1bSJed Brown 316c4762a1bSJed Brown Output Parameters: 317c4762a1bSJed Brown . F - vector containing the newly evaluated gradient 318c4762a1bSJed Brown */ 319c4762a1bSJed Brown PetscErrorCode FormConstraints(Tao tao, Vec X, Vec F, void *ptr) 320c4762a1bSJed Brown { 321c4762a1bSJed Brown AppCtx *user = (AppCtx *) ptr; 322c4762a1bSJed Brown PetscReal *x, *f; 323c4762a1bSJed Brown PetscReal *Vt1 = user->Vt1, *c = user->c, *d = user->d; 324c4762a1bSJed Brown PetscReal rate = user->rate; 325c4762a1bSJed Brown PetscReal dt = user->dt, ds = user->ds; 326c4762a1bSJed Brown PetscInt ms = user->ms; 327c4762a1bSJed Brown PetscInt i, xs,xm,gxs,gxm; 328c4762a1bSJed Brown Vec localX,localF; 329c4762a1bSJed Brown PetscReal zero=0.0; 330c4762a1bSJed Brown 331*9566063dSJacob Faibussowitsch PetscCall(DMGetLocalVector(user->dm,&localX)); 332*9566063dSJacob Faibussowitsch PetscCall(DMGetLocalVector(user->dm,&localF)); 333*9566063dSJacob Faibussowitsch PetscCall(DMGlobalToLocalBegin(user->dm,X,INSERT_VALUES,localX)); 334*9566063dSJacob Faibussowitsch PetscCall(DMGlobalToLocalEnd(user->dm,X,INSERT_VALUES,localX)); 335*9566063dSJacob Faibussowitsch PetscCall(DMDAGetCorners(user->dm,&xs,NULL,NULL,&xm,NULL,NULL)); 336*9566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(user->dm,&gxs,NULL,NULL,&gxm,NULL,NULL)); 337*9566063dSJacob Faibussowitsch PetscCall(VecSet(F, zero)); 338c4762a1bSJed Brown /* 339c4762a1bSJed Brown The problem size is smaller than the discretization because of the 340c4762a1bSJed Brown two fixed elements (V(0,T) = E and V(Send,T) = 0. 341c4762a1bSJed Brown */ 342c4762a1bSJed Brown 343c4762a1bSJed Brown /* Get pointers to the vector data */ 344*9566063dSJacob Faibussowitsch PetscCall(VecGetArray(localX, &x)); 345*9566063dSJacob Faibussowitsch PetscCall(VecGetArray(localF, &f)); 346c4762a1bSJed Brown 347c4762a1bSJed Brown /* Left Boundary */ 348c4762a1bSJed Brown if (gxs==0) { 349c4762a1bSJed Brown f[0] = x[0]-user->strike; 350c4762a1bSJed Brown } else { 351c4762a1bSJed Brown f[0] = 0; 352c4762a1bSJed Brown } 353c4762a1bSJed Brown 354c4762a1bSJed Brown /* All points in the interior */ 355c4762a1bSJed Brown /* for (i=gxs+1;i<gxm-1;i++) { */ 356c4762a1bSJed Brown for (i=1;i<gxm-1;i++) { 357c4762a1bSJed Brown f[i] = (1.0/dt + rate)*x[i] - Vt1[i]/dt + (c[i]/(4*ds))*(x[i+1] - x[i-1] + Vt1[i+1] - Vt1[i-1]) + 358c4762a1bSJed Brown (d[i]/(2*ds*ds))*(x[i+1] -2*x[i] + x[i-1] + Vt1[i+1] - 2*Vt1[i] + Vt1[i-1]); 359c4762a1bSJed Brown } 360c4762a1bSJed Brown 361c4762a1bSJed Brown /* Right boundary */ 362c4762a1bSJed Brown if (gxs+gxm==ms) { 363c4762a1bSJed Brown f[xm-1]=x[gxm-1]; 364c4762a1bSJed Brown } else { 365c4762a1bSJed Brown f[xm-1]=0; 366c4762a1bSJed Brown } 367c4762a1bSJed Brown 368c4762a1bSJed Brown /* Restore vectors */ 369*9566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(localX, &x)); 370*9566063dSJacob Faibussowitsch PetscCall(VecRestoreArray(localF, &f)); 371*9566063dSJacob Faibussowitsch PetscCall(DMLocalToGlobalBegin(user->dm,localF,INSERT_VALUES,F)); 372*9566063dSJacob Faibussowitsch PetscCall(DMLocalToGlobalEnd(user->dm,localF,INSERT_VALUES,F)); 373*9566063dSJacob Faibussowitsch PetscCall(DMRestoreLocalVector(user->dm,&localX)); 374*9566063dSJacob Faibussowitsch PetscCall(DMRestoreLocalVector(user->dm,&localF)); 375*9566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(24.0*(gxm-2))); 376c4762a1bSJed Brown /* 377c4762a1bSJed Brown info=VecView(F,PETSC_VIEWER_STDOUT_WORLD); 378c4762a1bSJed Brown */ 379c4762a1bSJed Brown return 0; 380c4762a1bSJed Brown } 381c4762a1bSJed Brown 382c4762a1bSJed Brown /* ------------------------------------------------------------------- */ 383c4762a1bSJed Brown /* 384c4762a1bSJed Brown FormJacobian - Evaluates Jacobian matrix. 385c4762a1bSJed Brown 386c4762a1bSJed Brown Input Parameters: 387c4762a1bSJed Brown . tao - the Tao context 388c4762a1bSJed Brown . X - input vector 389c4762a1bSJed Brown . ptr - optional user-defined context, as set by TaoSetJacobian() 390c4762a1bSJed Brown 391c4762a1bSJed Brown Output Parameters: 392c4762a1bSJed Brown . J - Jacobian matrix 393c4762a1bSJed Brown */ 394c4762a1bSJed Brown PetscErrorCode FormJacobian(Tao tao, Vec X, Mat J, Mat tJPre, void *ptr) 395c4762a1bSJed Brown { 396c4762a1bSJed Brown AppCtx *user = (AppCtx *) ptr; 397c4762a1bSJed Brown PetscReal *c = user->c, *d = user->d; 398c4762a1bSJed Brown PetscReal rate = user->rate; 399c4762a1bSJed Brown PetscReal dt = user->dt, ds = user->ds; 400c4762a1bSJed Brown PetscInt ms = user->ms; 401c4762a1bSJed Brown PetscReal val[3]; 402c4762a1bSJed Brown PetscInt col[3]; 403c4762a1bSJed Brown PetscInt i; 404c4762a1bSJed Brown PetscInt gxs,gxm; 405c4762a1bSJed Brown PetscBool assembled; 406c4762a1bSJed Brown 407c4762a1bSJed Brown /* Set various matrix options */ 408*9566063dSJacob Faibussowitsch PetscCall(MatSetOption(J,MAT_IGNORE_OFF_PROC_ENTRIES,PETSC_TRUE)); 409*9566063dSJacob Faibussowitsch PetscCall(MatAssembled(J,&assembled)); 410*9566063dSJacob Faibussowitsch if (assembled) PetscCall(MatZeroEntries(J)); 411c4762a1bSJed Brown 412*9566063dSJacob Faibussowitsch PetscCall(DMDAGetGhostCorners(user->dm,&gxs,NULL,NULL,&gxm,NULL,NULL)); 413c4762a1bSJed Brown 414c4762a1bSJed Brown if (gxs==0) { 415c4762a1bSJed Brown i = 0; 416c4762a1bSJed Brown col[0] = 0; 417c4762a1bSJed Brown val[0]=1.0; 418*9566063dSJacob Faibussowitsch PetscCall(MatSetValues(J,1,&i,1,col,val,INSERT_VALUES)); 419c4762a1bSJed Brown } 420c4762a1bSJed Brown for (i=1; i < gxm-1; i++) { 421c4762a1bSJed Brown col[0] = gxs + i - 1; 422c4762a1bSJed Brown col[1] = gxs + i; 423c4762a1bSJed Brown col[2] = gxs + i + 1; 424c4762a1bSJed Brown val[0] = -c[i]/(4*ds) + d[i]/(2*ds*ds); 425c4762a1bSJed Brown val[1] = 1.0/dt + rate - d[i]/(ds*ds); 426c4762a1bSJed Brown val[2] = c[i]/(4*ds) + d[i]/(2*ds*ds); 427*9566063dSJacob Faibussowitsch PetscCall(MatSetValues(J,1,&col[1],3,col,val,INSERT_VALUES)); 428c4762a1bSJed Brown } 429c4762a1bSJed Brown if (gxs+gxm==ms) { 430c4762a1bSJed Brown i = ms-1; 431c4762a1bSJed Brown col[0] = i; 432c4762a1bSJed Brown val[0]=1.0; 433*9566063dSJacob Faibussowitsch PetscCall(MatSetValues(J,1,&i,1,col,val,INSERT_VALUES)); 434c4762a1bSJed Brown } 435c4762a1bSJed Brown 436c4762a1bSJed Brown /* Assemble the Jacobian matrix */ 437*9566063dSJacob Faibussowitsch PetscCall(MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY)); 438*9566063dSJacob Faibussowitsch PetscCall(MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY)); 439*9566063dSJacob Faibussowitsch PetscCall(PetscLogFlops(18.0*(gxm)+5)); 440c4762a1bSJed Brown return 0; 441c4762a1bSJed Brown } 442c4762a1bSJed Brown 443c4762a1bSJed Brown /*TEST 444c4762a1bSJed Brown 445c4762a1bSJed Brown build: 446c4762a1bSJed Brown requires: !complex 447c4762a1bSJed Brown 448c4762a1bSJed Brown test: 449c4762a1bSJed Brown args: -tao_monitor -tao_type ssils -tao_gttol 1.e-5 450c4762a1bSJed Brown requires: !single 451c4762a1bSJed Brown 452c4762a1bSJed Brown test: 453c4762a1bSJed Brown suffix: 2 454c4762a1bSJed Brown args: -tao_monitor -tao_type ssfls -tao_max_it 10 -tao_gttol 1.e-5 455c4762a1bSJed Brown requires: !single 456c4762a1bSJed Brown 457c4762a1bSJed Brown test: 458c4762a1bSJed Brown suffix: 3 459c4762a1bSJed Brown args: -tao_monitor -tao_type asils -tao_subset_type subvec -tao_gttol 1.e-5 460c4762a1bSJed Brown requires: !single 461c4762a1bSJed Brown 462c4762a1bSJed Brown test: 463c4762a1bSJed Brown suffix: 4 464c4762a1bSJed Brown args: -tao_monitor -tao_type asils -tao_subset_type mask -tao_gttol 1.e-5 465c4762a1bSJed Brown requires: !single 466c4762a1bSJed Brown 467c4762a1bSJed Brown test: 468c4762a1bSJed Brown suffix: 5 469c4762a1bSJed Brown args: -tao_monitor -tao_type asils -tao_subset_type matrixfree -pc_type jacobi -tao_max_it 6 -tao_gttol 1.e-5 470c4762a1bSJed Brown requires: !single 471c4762a1bSJed Brown 472c4762a1bSJed Brown test: 473c4762a1bSJed Brown suffix: 6 474c4762a1bSJed Brown args: -tao_monitor -tao_type asfls -tao_subset_type subvec -tao_max_it 10 -tao_gttol 1.e-5 475c4762a1bSJed Brown requires: !single 476c4762a1bSJed Brown 477c4762a1bSJed Brown test: 478c4762a1bSJed Brown suffix: 7 479c4762a1bSJed Brown args: -tao_monitor -tao_type asfls -tao_subset_type mask -tao_max_it 10 -tao_gttol 1.e-5 480c4762a1bSJed Brown requires: !single 481c4762a1bSJed Brown 482c4762a1bSJed Brown TEST*/ 483