1 static char help[] = "Solves DAE with integrator only on non-algebraic terms \n"; 2 3 #include <petscts.h> 4 5 /* 6 \dot{U} = f(U,V) 7 F(U,V) = 0 8 9 Same as ex6.c except the user provided functions take input values as a single vector instead of two vectors 10 */ 11 12 /* 13 f(U,V) = U + V 14 15 */ 16 PetscErrorCode f(PetscReal t,Vec UV,Vec F) 17 { 18 const PetscScalar *u,*v; 19 PetscScalar *f; 20 PetscInt n,i; 21 22 PetscFunctionBeginUser; 23 PetscCall(VecGetLocalSize(UV,&n)); 24 n = n/2; 25 PetscCall(VecGetArrayRead(UV,&u)); 26 v = u + n; 27 PetscCall(VecGetArrayWrite(F,&f)); 28 for (i=0; i<n; i++) f[i] = u[i] + v[i]; 29 PetscCall(VecRestoreArrayRead(UV,&u)); 30 PetscCall(VecRestoreArrayWrite(F,&f)); 31 PetscFunctionReturn(0); 32 } 33 34 /* 35 F(U,V) = U - V 36 */ 37 PetscErrorCode F(PetscReal t,Vec UV,Vec F) 38 { 39 const PetscScalar *u,*v; 40 PetscScalar *f; 41 PetscInt n,i; 42 43 PetscFunctionBeginUser; 44 PetscCall(VecGetLocalSize(UV,&n)); 45 n = n/2; 46 PetscCall(VecGetArrayRead(UV,&u)); 47 v = u + n; 48 PetscCall(VecGetArrayWrite(F,&f)); 49 for (i=0; i<n; i++) f[i] = u[i] - v[i]; 50 PetscCall(VecRestoreArrayRead(UV,&u)); 51 PetscCall(VecRestoreArrayWrite(F,&f)); 52 PetscFunctionReturn(0); 53 } 54 55 typedef struct { 56 PetscReal t; 57 SNES snes; 58 Vec UV,V; 59 VecScatter scatterU,scatterV; 60 PetscErrorCode (*f)(PetscReal,Vec,Vec); 61 PetscErrorCode (*F)(PetscReal,Vec,Vec); 62 } AppCtx; 63 64 extern PetscErrorCode TSFunction(TS,PetscReal,Vec,Vec,void*); 65 extern PetscErrorCode SNESFunction(SNES,Vec,Vec,void*); 66 67 int main(int argc,char **argv) 68 { 69 AppCtx ctx; 70 TS ts; 71 Vec tsrhs,U; 72 IS is; 73 PetscInt i; 74 PetscMPIInt rank; 75 76 PetscCall(PetscInitialize(&argc,&argv,(char*)0,help)); 77 PetscCallMPI(MPI_Comm_rank(PETSC_COMM_WORLD,&rank)); 78 PetscCall(TSCreate(PETSC_COMM_WORLD,&ts)); 79 PetscCall(TSSetProblemType(ts,TS_NONLINEAR)); 80 PetscCall(TSSetType(ts,TSEULER)); 81 PetscCall(TSSetFromOptions(ts)); 82 PetscCall(VecCreateMPI(PETSC_COMM_WORLD,1,PETSC_DETERMINE,&tsrhs)); 83 PetscCall(VecDuplicate(tsrhs,&U)); 84 PetscCall(TSSetRHSFunction(ts,tsrhs,TSFunction,&ctx)); 85 PetscCall(TSSetMaxTime(ts,1.0)); 86 ctx.f = f; 87 88 PetscCall(SNESCreate(PETSC_COMM_WORLD,&ctx.snes)); 89 PetscCall(SNESSetFromOptions(ctx.snes)); 90 PetscCall(SNESSetFunction(ctx.snes,NULL,SNESFunction,&ctx)); 91 PetscCall(SNESSetJacobian(ctx.snes,NULL,NULL,SNESComputeJacobianDefault,&ctx)); 92 ctx.F = F; 93 PetscCall(VecCreateMPI(PETSC_COMM_WORLD,1,PETSC_DETERMINE,&ctx.V)); 94 95 /* Create scatters to move between separate U and V representation and UV representation of solution */ 96 PetscCall(VecCreateMPI(PETSC_COMM_WORLD,2,PETSC_DETERMINE,&ctx.UV)); 97 i = 2*rank; 98 PetscCall(ISCreateGeneral(PETSC_COMM_WORLD,1,&i,PETSC_COPY_VALUES,&is)); 99 PetscCall(VecScatterCreate(U,NULL,ctx.UV,is,&ctx.scatterU)); 100 PetscCall(ISDestroy(&is)); 101 i = 2*rank + 1; 102 PetscCall(ISCreateGeneral(PETSC_COMM_WORLD,1,&i,PETSC_COPY_VALUES,&is)); 103 PetscCall(VecScatterCreate(ctx.V,NULL,ctx.UV,is,&ctx.scatterV)); 104 PetscCall(ISDestroy(&is)); 105 106 PetscCall(VecSet(U,1.0)); 107 PetscCall(TSSolve(ts,U)); 108 109 PetscCall(VecDestroy(&ctx.V)); 110 PetscCall(VecDestroy(&ctx.UV)); 111 PetscCall(VecScatterDestroy(&ctx.scatterU)); 112 PetscCall(VecScatterDestroy(&ctx.scatterV)); 113 PetscCall(VecDestroy(&tsrhs)); 114 PetscCall(VecDestroy(&U)); 115 PetscCall(SNESDestroy(&ctx.snes)); 116 PetscCall(TSDestroy(&ts)); 117 PetscCall(PetscFinalize()); 118 return 0; 119 } 120 121 /* 122 Defines the RHS function that is passed to the time-integrator. 123 124 Solves F(U,V) for V and then computes f(U,V) 125 */ 126 PetscErrorCode TSFunction(TS ts,PetscReal t,Vec U,Vec F,void *actx) 127 { 128 AppCtx *ctx = (AppCtx*)actx; 129 130 PetscFunctionBeginUser; 131 ctx->t = t; 132 PetscCall(VecScatterBegin(ctx->scatterU,U,ctx->UV,INSERT_VALUES,SCATTER_FORWARD)); 133 PetscCall(VecScatterEnd(ctx->scatterU,U,ctx->UV,INSERT_VALUES,SCATTER_FORWARD)); 134 PetscCall(SNESSolve(ctx->snes,NULL,ctx->V)); 135 PetscCall(VecScatterBegin(ctx->scatterV,ctx->V,ctx->UV,INSERT_VALUES,SCATTER_FORWARD)); 136 PetscCall(VecScatterEnd(ctx->scatterV,ctx->V,ctx->UV,INSERT_VALUES,SCATTER_FORWARD)); 137 PetscCall((*ctx->f)(t,ctx->UV,F)); 138 PetscFunctionReturn(0); 139 } 140 141 /* 142 Defines the nonlinear function that is passed to the nonlinear solver 143 144 */ 145 PetscErrorCode SNESFunction(SNES snes,Vec V,Vec F,void *actx) 146 { 147 AppCtx *ctx = (AppCtx*)actx; 148 149 PetscFunctionBeginUser; 150 PetscCall(VecScatterBegin(ctx->scatterV,V,ctx->UV,INSERT_VALUES,SCATTER_FORWARD)); 151 PetscCall(VecScatterEnd(ctx->scatterV,V,ctx->UV,INSERT_VALUES,SCATTER_FORWARD)); 152 PetscCall((*ctx->F)(ctx->t,ctx->UV,F)); 153 PetscFunctionReturn(0); 154 } 155 156 /*TEST 157 158 test: 159 args: -ts_monitor -ts_view 160 161 TEST*/ 162