Actual source code: ex75.c

  1: static char help[] = "Variable-Viscosity Stokes Problem in 2d.\n\
  2: Exact solutions provided by Mirko Velic.\n\n\n";

  4: #include <petsc.h>

  6: #include "ex75.h"

  8: typedef struct {
  9:   PetscBool fem; /* Flag for FEM tests */
 10: } AppCtx;

 12: PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options)
 13: {
 14:   PetscFunctionBeginUser;
 15:   options->fem = PETSC_FALSE;
 16:   PetscOptionsBegin(comm, "", "Stokes Problem Options", "DMPLEX");
 17:   PetscCall(PetscOptionsBool("-fem", "Run FEM tests", "ex75.c", options->fem, &options->fem, NULL));
 18:   PetscOptionsEnd();
 19:   PetscFunctionReturn(PETSC_SUCCESS);
 20: }

 22: /*
 23:   SolKxSolution - Exact Stokes solutions for exponentially varying viscosity

 25:  Input Parameters:
 26: + x  - The x coordinate at which to evaluate the solution
 27: . z  - The z coordinate at which to evaluate the solution
 28: . kn - The constant defining the x-dependence of the forcing function
 29: . km - The constant defining the z-dependence of the forcing function
 30: - B  - The viscosity coefficient

 32:   Output Parameters:
 33: + vx - The x-velocity at (x,z)
 34: . vz - The z-velocity at (x,z)
 35: . p - The pressure at (x,z)
 36: . sxx - The stress sigma_xx at (x,z)
 37: . sxz - The stress sigma_xz at (x,z)
 38: - szz - The stress sigma_zz at (x,z)

 40:   Note:
 41: $  The domain is the square 0 <= x,z <= 1. We solve the Stokes equation for incompressible flow with free-slip boundary
 42: $  conditions everywhere. The forcing term f is given by
 43: $
 44: $    fx = 0
 45: $    fz = sigma*sin(km*z)*cos(kn*x)
 46: $
 47: $  where
 48: $
 49: $    km = m*Pi (m may be non-integral)
 50: $    kn = n*Pi
 51: $
 52: $  meaning that the density rho is -sigma*sin(km*z)*cos(kn*x). The viscosity eta is exp(2*B*x).
 53: */
 54: PetscErrorCode SolKxSolution(PetscReal x, PetscReal z, PetscReal kn, PetscReal km, PetscReal B, PetscScalar *vx, PetscScalar *vz, PetscScalar *p, PetscScalar *sxx, PetscScalar *sxz, PetscScalar *szz)
 55: {
 56:   PetscScalar sigma;
 57:   PetscScalar _C1, _C2, _C3, _C4;
 58:   PetscScalar Rp, UU, VV;
 59:   PetscScalar a, b, r, _aa, _bb, AA, BB, Rm;
 60:   PetscScalar num1, num2, num3, num4, den1;

 62:   PetscScalar t1, t2, t3, t4, t5, t6, t7, t8, t9, t10;
 63:   PetscScalar t11, t12, t13, t14, t15, t16, t17, t18, t19, t20, t21;
 64:   PetscScalar t22, t23, t24, t25, t26, t28, t29, t30, t31, t32;
 65:   PetscScalar t33, t34, t35, t36, t37, t38, t39, t40, t41, t42;
 66:   PetscScalar t44, t45, t46, t47, t48, t49, t51, t52, t53, t54;
 67:   PetscScalar t56, t58, t61, t62, t63, t64, t65, t66, t67, t68;
 68:   PetscScalar t69, t70, t71, t72, t73, t74, t75, t76, t77, t78;
 69:   PetscScalar t79, t80, t81, t82, t83, t84, t85, t86, t87, t88;
 70:   PetscScalar t89, t90, t91, t92, t93, t94, t95, t96, t97, t98;
 71:   PetscScalar t99, t100, t101, t103, t104, t105, t106, t107, t108, t109;
 72:   PetscScalar t110, t111, t112, t113, t114, t115, t116, t117, t118, t119;
 73:   PetscScalar t120, t121, t123, t125, t127, t128, t130, t131, t132, t133;
 74:   PetscScalar t135, t136, t138, t140, t141, t142, t143, t152, t160, t162;

 76:   PetscFunctionBegin;
 77:   /*************************************************************************/
 78:   /*************************************************************************/
 79:   /* rho = -sin(km*z)*cos(kn*x) */
 80:   /* viscosity  Z= exp(2*B*z)  */
 81:   /* solution valid for km not zero -- should get trivial solution if km=0 */
 82:   sigma = 1.0;
 83:   /*************************************************************************/
 84:   /*************************************************************************/
 85:   a  = B * B + km * km;
 86:   b  = 2.0 * km * B;
 87:   r  = sqrt(a * a + b * b);
 88:   Rp = sqrt((r + a) / 2.0);
 89:   Rm = sqrt((r - a) / 2.0);
 90:   UU = Rp - B;
 91:   VV = Rp + B;

 93:   /*******************************************/
 94:   /*         calculate the constants         */
 95:   /*******************************************/
 96:   t1  = kn * kn;
 97:   t4  = km * km;
 98:   t6  = t4 * t4;
 99:   t7  = B * B;
100:   t9  = 0.4e1 * t7 * t4;
101:   t12 = 0.8e1 * t7 * kn * km;
102:   t14 = 0.4e1 * t7 * t1;
103:   t16 = 0.2e1 * t4 * t1;
104:   t17 = t1 * t1;
105:   _aa = -0.4e1 * B * t1 * sigma * (t4 + t1) / (t6 + t9 + t12 + t14 + t16 + t17) / (t6 + t9 - t12 + t14 + t16 + t17);

107:   t2  = kn * kn;
108:   t3  = t2 * t2;
109:   t4  = B * B;
110:   t6  = 0.4e1 * t4 * t2;
111:   t7  = km * km;
112:   t9  = 0.4e1 * t7 * t4;
113:   t10 = t7 * t7;
114:   t12 = 0.2e1 * t7 * t2;
115:   t16 = 0.8e1 * t4 * kn * km;
116:   _bb = sigma * kn * (t3 - t6 + t9 + t10 + t12) / (t10 + t9 + t16 + t6 + t12 + t3) / (t10 + t9 - t16 + t6 + t12 + t3);

118:   AA = _aa;
119:   BB = _bb;

121:   t1   = Rm * Rm;
122:   t2   = B - Rp;
123:   t4   = Rp + B;
124:   t6   = UU * x;
125:   t9   = exp(t6 - 0.4e1 * Rp);
126:   t13  = kn * kn;
127:   t15  = B * B;
128:   t18  = Rp * Rp;
129:   t19  = t18 * B;
130:   t20  = t15 * Rp;
131:   t22  = t1 * Rp;
132:   t24  = B * t1;
133:   t32  = 0.8e1 * t15 * BB * kn * Rp;
134:   t34  = 0.2e1 * Rm;
135:   t35  = cos(t34);
136:   t37  = Rm * Rp;
137:   t49  = sin(t34);
138:   t63  = exp(t6 - 0.2e1 * Rp);
139:   t65  = Rm * t2;
140:   t67  = 0.2e1 * B * kn;
141:   t68  = B * Rm;
142:   t69  = t67 + t68 + t37;
143:   t73  = 0.3e1 * t15;
144:   t75  = 0.2e1 * B * Rp;
145:   t76  = t73 - t75 + t1 - t13 - t18;
146:   t78  = t65 * t76 * BB;
147:   t80  = Rm - kn;
148:   t81  = cos(t80);
149:   t83  = t68 - t67 + t37;
150:   t88  = Rm + kn;
151:   t89  = cos(t88);
152:   t92  = t65 * t76 * AA;
153:   t97  = sin(t80);
154:   t103 = sin(t88);
155:   t108 = exp(t6 - 0.3e1 * Rp - B);
156:   t110 = Rm * t4;
157:   t111 = t67 + t68 - t37;
158:   t115 = t73 + t75 + t1 - t13 - t18;
159:   t117 = t110 * t115 * BB;
160:   t120 = -t67 + t68 - t37;
161:   t127 = t110 * t115 * AA;
162:   t140 = exp(t6 - Rp - B);
163:   num1 = -0.4e1 * t1 * t2 * t4 * AA * t9 + ((0.2e1 * Rp * (-B * t13 + 0.3e1 * t15 * B - t19 - 0.2e1 * t20 - 0.2e1 * t22 - t24) * AA - t32) * t35 + (0.2e1 * t37 * (t1 - t13 + 0.5e1 * t15 - t18) * AA - 0.8e1 * B * BB * kn * Rm * Rp) * t49 - 0.2e1 * B * (0.3e1 * t20 - t18 * Rp - 0.2e1 * t19 - Rp * t13 - t22 - 0.2e1 * t24) * AA + t32) * t63 + ((0.2e1 * t65 * t69 * AA + t78) * t81 + (0.2e1 * t65 * t83 * AA - t78) * t89 + (t92 - 0.2e1 * t65 * t69 * BB) * t97 + (t92 + 0.2e1 * t65 * t83 * BB) * t103) * t108 + ((-0.2e1 * t110 * t111 * AA - t117) * t81 + (-0.2e1 * t110 * t120 * AA + t117) * t89 + (-t127 + 0.2e1 * t110 * t111 * BB) * t97 + (-t127 - 0.2e1 * t110 * t120 * BB) * t103) * t140;

165:   t1   = Rp + B;
166:   t2   = Rm * t1;
167:   t3   = B * B;
168:   t4   = 0.3e1 * t3;
169:   t5   = B * Rp;
170:   t7   = Rm * Rm;
171:   t8   = kn * kn;
172:   t9   = Rp * Rp;
173:   t10  = t4 + 0.2e1 * t5 + t7 - t8 - t9;
174:   t12  = t2 * t10 * AA;
175:   t14  = B * Rm;
176:   t20  = UU * x;
177:   t23  = exp(t20 - 0.4e1 * Rp);
178:   t25  = Rm * Rp;
179:   t32  = Rm * kn;
180:   t37  = 0.2e1 * Rm;
181:   t38  = cos(t37);
182:   t41  = t3 * B;
183:   t44  = t3 * Rp;
184:   t48  = B * t7;
185:   t53  = t3 * BB;
186:   t54  = kn * Rp;
187:   t58  = sin(t37);
188:   t69  = exp(t20 - 0.2e1 * Rp);
189:   t71  = t9 * Rp;
190:   t72  = Rm * t71;
191:   t73  = t3 * Rm;
192:   t75  = 0.5e1 * t73 * Rp;
193:   t77  = 0.8e1 * t44 * kn;
194:   t78  = t25 * t8;
195:   t79  = t7 * Rm;
196:   t80  = B * t79;
197:   t81  = t14 * t8;
198:   t82  = t79 * Rp;
199:   t84  = 0.3e1 * t41 * Rm;
200:   t85  = t14 * t9;
201:   t86  = -t72 + t75 + t77 - t78 + t80 - t81 + t82 + t84 + t85;
202:   t88  = t7 * t9;
203:   t89  = t5 * t8;
204:   t90  = t7 * t3;
205:   t91  = B * t71;
206:   t92  = t48 * Rp;
207:   t94  = 0.2e1 * t14 * t54;
208:   t96  = 0.3e1 * Rp * t41;
209:   t98  = 0.2e1 * t73 * kn;
210:   t100 = 0.2e1 * t9 * t3;
211:   t101 = -t88 - t89 - t90 - t91 - t92 - t94 + t96 - t98 - t100;
212:   t105 = Rm - kn;
213:   t106 = cos(t105);
214:   t108 = t75 - t77 - t78 + t85 - t72 - t81 + t80 + t84 + t82;
215:   t110 = -t100 + t96 - t91 + t94 + t98 - t92 - t89 - t88 - t90;
216:   t114 = Rm + kn;
217:   t115 = cos(t114);
218:   t121 = sin(t105);
219:   t127 = sin(t114);
220:   t132 = exp(t20 - 0.3e1 * Rp - B);
221:   t135 = 0.2e1 * B * kn;
222:   t136 = t135 + t14 - t25;
223:   t142 = -t135 + t14 - t25;
224:   t152 = t2 * t10 * BB;
225:   t162 = exp(t20 - Rp - B);
226:   num2 = (0.2e1 * t12 - 0.8e1 * t14 * kn * t1 * BB) * t23 + ((-0.2e1 * t25 * (t7 - t8 + 0.5e1 * t3 - t9) * AA + 0.8e1 * B * BB * t32 * Rp) * t38 + (0.2e1 * Rp * (-B * t8 + 0.3e1 * t41 - t9 * B - 0.2e1 * t44 - 0.2e1 * t7 * Rp - t48) * AA - 0.8e1 * t53 * t54) * t58 - 0.2e1 * t14 * (t4 + t9 - t8 + t7) * AA + 0.8e1 * t53 * t32) * t69 + ((-t86 * AA - 0.2e1 * t101 * BB) * t106 + (-t108 * AA + 0.2e1 * t110 * BB) * t115 + (-0.2e1 * t101 * AA + t86 * BB) * t121 + (-0.2e1 * t110 * AA - t108 * BB) * t127) * t132 + ((t12 - 0.2e1 * t2 * t136 * BB) * t106 + (t12 + 0.2e1 * t2 * t142 * BB) * t115 + (-0.2e1 * t2 * t136 * AA - t152) * t121 + (-0.2e1 * t2 * t142 * AA + t152) * t127) * t162;

228:   t1   = Rm * Rm;
229:   t2   = B - Rp;
230:   t4   = Rp + B;
231:   t6   = VV * x;
232:   t7   = exp(-t6);
233:   t11  = B * t1;
234:   t12  = Rp * Rp;
235:   t13  = t12 * B;
236:   t14  = B * B;
237:   t15  = t14 * Rp;
238:   t19  = kn * kn;
239:   t21  = t1 * Rp;
240:   t30  = 0.8e1 * t14 * BB * kn * Rp;
241:   t32  = 0.2e1 * Rm;
242:   t33  = cos(t32);
243:   t35  = Rm * Rp;
244:   t47  = sin(t32);
245:   t61  = exp(-t6 - 0.2e1 * Rp);
246:   t63  = Rm * t2;
247:   t65  = 0.2e1 * B * kn;
248:   t66  = B * Rm;
249:   t67  = t65 + t66 + t35;
250:   t71  = 0.3e1 * t14;
251:   t73  = 0.2e1 * B * Rp;
252:   t74  = t71 - t73 + t1 - t19 - t12;
253:   t76  = t63 * t74 * BB;
254:   t78  = Rm - kn;
255:   t79  = cos(t78);
256:   t81  = t66 - t65 + t35;
257:   t86  = Rm + kn;
258:   t87  = cos(t86);
259:   t90  = t63 * t74 * AA;
260:   t95  = sin(t78);
261:   t101 = sin(t86);
262:   t106 = exp(-t6 - 0.3e1 * Rp - B);
263:   t108 = Rm * t4;
264:   t109 = t65 + t66 - t35;
265:   t113 = t71 + t73 + t1 - t19 - t12;
266:   t115 = t108 * t113 * BB;
267:   t118 = -t65 + t66 - t35;
268:   t125 = t108 * t113 * AA;
269:   t138 = exp(-t6 - Rp - B);
270:   num3 = -0.4e1 * t1 * t2 * t4 * AA * t7 + ((-0.2e1 * Rp * (-t11 - t13 + 0.2e1 * t15 + 0.3e1 * t14 * B - B * t19 + 0.2e1 * t21) * AA + t30) * t33 + (-0.2e1 * t35 * (t1 - t19 + 0.5e1 * t14 - t12) * AA + 0.8e1 * B * BB * kn * Rm * Rp) * t47 + 0.2e1 * B * (-t12 * Rp + 0.2e1 * t11 + 0.3e1 * t15 + 0.2e1 * t13 - t21 - Rp * t19) * AA - t30) * t61 + ((-0.2e1 * t63 * t67 * AA - t76) * t79 + (-0.2e1 * t63 * t81 * AA + t76) * t87 + (-t90 + 0.2e1 * t63 * t67 * BB) * t95 + (-t90 - 0.2e1 * t63 * t81 * BB) * t101) * t106 + ((0.2e1 * t108 * t109 * AA + t115) * t79 + (0.2e1 * t108 * t118 * AA - t115) * t87 + (t125 - 0.2e1 * t108 * t109 * BB) * t95 + (t125 + 0.2e1 * t108 * t118 * BB) * t101) * t138;

272:   t1   = B - Rp;
273:   t2   = Rm * t1;
274:   t3   = B * B;
275:   t4   = 0.3e1 * t3;
276:   t5   = B * Rp;
277:   t7   = Rm * Rm;
278:   t8   = kn * kn;
279:   t9   = Rp * Rp;
280:   t10  = t4 - 0.2e1 * t5 + t7 - t8 - t9;
281:   t12  = t2 * t10 * AA;
282:   t14  = B * Rm;
283:   t20  = VV * x;
284:   t21  = exp(-t20);
285:   t23  = Rm * Rp;
286:   t30  = Rm * kn;
287:   t35  = 0.2e1 * Rm;
288:   t36  = cos(t35);
289:   t38  = B * t7;
290:   t40  = t3 * Rp;
291:   t42  = t3 * B;
292:   t51  = t3 * BB;
293:   t52  = kn * Rp;
294:   t56  = sin(t35);
295:   t67  = exp(-t20 - 0.2e1 * Rp);
296:   t70  = 0.2e1 * B * kn;
297:   t71  = t70 + t14 + t23;
298:   t76  = Rm - kn;
299:   t77  = cos(t76);
300:   t79  = t14 - t70 + t23;
301:   t84  = Rm + kn;
302:   t85  = cos(t84);
303:   t91  = t2 * t10 * BB;
304:   t93  = sin(t76);
305:   t99  = sin(t84);
306:   t104 = exp(-t20 - 0.3e1 * Rp - B);
307:   t106 = t9 * Rp;
308:   t107 = Rm * t106;
309:   t108 = t3 * Rm;
310:   t110 = 0.5e1 * t108 * Rp;
311:   t112 = 0.8e1 * t40 * kn;
312:   t113 = t23 * t8;
313:   t114 = t7 * Rm;
314:   t115 = B * t114;
315:   t116 = t14 * t8;
316:   t117 = t114 * Rp;
317:   t119 = 0.3e1 * t42 * Rm;
318:   t120 = t14 * t9;
319:   t121 = t107 - t110 - t112 + t113 + t115 - t116 - t117 + t119 + t120;
320:   t123 = t38 * Rp;
321:   t125 = 0.2e1 * t14 * t52;
322:   t127 = 0.3e1 * Rp * t42;
323:   t128 = t7 * t3;
324:   t130 = 0.2e1 * t9 * t3;
325:   t131 = t7 * t9;
326:   t132 = B * t106;
327:   t133 = t5 * t8;
328:   t135 = 0.2e1 * t108 * kn;
329:   t136 = -t123 - t125 + t127 + t128 + t130 + t131 - t132 - t133 + t135;
330:   t141 = -t110 + t112 + t113 + t120 + t107 - t116 + t115 + t119 - t117;
331:   t143 = t125 - t132 + t130 - t135 + t127 + t131 - t123 + t128 - t133;
332:   t160 = exp(-t20 - Rp - B);
333:   num4 = (0.2e1 * t12 - 0.8e1 * t14 * kn * t1 * BB) * t21 + ((0.2e1 * t23 * (t7 - t8 + 0.5e1 * t3 - t9) * AA - 0.8e1 * B * BB * t30 * Rp) * t36 + (-0.2e1 * Rp * (-t38 - t9 * B + 0.2e1 * t40 + 0.3e1 * t42 - B * t8 + 0.2e1 * t7 * Rp) * AA + 0.8e1 * t51 * t52) * t56 - 0.2e1 * t14 * (t4 + t9 - t8 + t7) * AA + 0.8e1 * t51 * t30) * t67 + ((t12 - 0.2e1 * t2 * t71 * BB) * t77 + (t12 + 0.2e1 * t2 * t79 * BB) * t85 + (-0.2e1 * t2 * t71 * AA - t91) * t93 + (-0.2e1 * t2 * t79 * AA + t91) * t99) * t104 + ((-t121 * AA + 0.2e1 * t136 * BB) * t77 + (-t141 * AA - 0.2e1 * t143 * BB) * t85 + (0.2e1 * t136 * AA + t121 * BB) * t93 + (0.2e1 * t143 * AA - t141 * BB) * t99) * t160;

335:   t1   = Rm * Rm;
336:   t2   = Rp * Rp;
337:   t3   = t1 * t2;
338:   t4   = B * B;
339:   t5   = t1 * t4;
340:   t9   = exp(-0.4e1 * Rp);
341:   t15  = cos(0.2e1 * Rm);
342:   t22  = exp(-0.2e1 * Rp);
343:   den1 = (-0.4e1 * t3 + 0.4e1 * t5) * t9 + ((0.8e1 * t1 + 0.8e1 * t4) * t2 * t15 - 0.8e1 * t5 - 0.8e1 * t2 * t4) * t22 - 0.4e1 * t3 + 0.4e1 * t5;

345:   _C1 = num1 / den1;
346:   _C2 = num2 / den1;
347:   _C3 = num3 / den1;
348:   _C4 = num4 / den1;

350:   /*******************************************/
351:   /*         calculate solution         */
352:   /*******************************************/
353:   t1  = Rm * x;
354:   t2  = cos(t1);
355:   t4  = sin(t1);
356:   t10 = exp(-0.2e1 * x * B);
357:   t12 = kn * x;
358:   t13 = cos(t12);
359:   t16 = sin(t12);
360:   *vx = -km * (_C1 * t2 + _C2 * t4 + _C3 * t2 + _C4 * t4 + t10 * AA * t13 + t10 * BB * t16);

362:   t2  = Rm * x;
363:   t3  = cos(t2);
364:   t6  = sin(t2);
365:   t22 = exp(-0.2e1 * x * B);
366:   t23 = B * t22;
367:   t24 = kn * x;
368:   t25 = cos(t24);
369:   t29 = sin(t24);
370:   *vz = UU * _C1 * t3 + UU * _C2 * t6 - _C1 * t6 * Rm + _C2 * t3 * Rm - VV * _C3 * t3 - VV * _C4 * t6 - _C3 * t6 * Rm + _C4 * t3 * Rm - 0.2e1 * t23 * AA * t25 - 0.2e1 * t23 * BB * t29 - t22 * AA * t29 * kn + t22 * BB * t25 * kn;

372:   t3   = exp(0.2e1 * x * B);
373:   t4   = t3 * B;
374:   t8   = km * km;
375:   t9   = t3 * t8;
376:   t11  = 0.3e1 * t9 * Rm;
377:   t12  = Rm * Rm;
378:   t14  = t3 * t12 * Rm;
379:   t15  = UU * UU;
380:   t19  = 0.4e1 * t4 * UU * Rm - t11 - t14 + 0.3e1 * t3 * t15 * Rm;
381:   t20  = Rm * x;
382:   t21  = sin(t20);
383:   t26  = 0.2e1 * t9 * B;
384:   t33  = 0.2e1 * t4 * t12;
385:   t36  = -t3 * t15 * UU - t26 + 0.3e1 * t9 * UU + 0.3e1 * t3 * UU * t12 + t33 - 0.2e1 * t4 * t15;
386:   t37  = cos(t20);
387:   t46  = VV * VV;
388:   t53  = -t11 - t14 + 0.3e1 * t3 * t46 * Rm - 0.4e1 * t4 * VV * Rm;
389:   t64  = -t26 + t33 + t3 * t46 * VV - 0.3e1 * t9 * VV - 0.2e1 * t4 * t46 - 0.3e1 * t3 * VV * t12;
390:   t73  = kn * kn;
391:   t74  = t73 * kn;
392:   t79  = B * B;
393:   t86  = B * t8;
394:   t90  = kn * x;
395:   t91  = sin(t90);
396:   t106 = cos(t90);
397:   *sxx = -((t19 * t21 + t36 * t37) * _C1 + (t36 * t21 - t19 * t37) * _C2 + (t53 * t21 + t64 * t37) * _C3 + (t64 * t21 - t53 * t37) * _C4 + (-AA * t74 - 0.4e1 * BB * t73 * B + 0.4e1 * t79 * AA * kn - 0.3e1 * t8 * AA * kn - 0.8e1 * t86 * BB) * t91 + (-0.8e1 * t86 * AA - 0.4e1 * AA * t73 * B - 0.4e1 * t79 * BB * kn + 0.3e1 * t8 * BB * kn + BB * t74) * t106) / km;

399:   t3  = exp(0.2e1 * x * B);
400:   t4  = km * km;
401:   t5  = t3 * t4;
402:   t6  = Rm * x;
403:   t7  = cos(t6);
404:   t8  = _C1 * t7;
405:   t10 = sin(t6);
406:   t11 = _C2 * t10;
407:   t13 = _C3 * t7;
408:   t15 = _C4 * t10;
409:   t18 = kn * x;
410:   t19 = cos(t18);
411:   t22 = sin(t18);
412:   t24 = UU * UU;
413:   t25 = t3 * t24;
414:   t28 = t3 * UU;
415:   t38 = Rm * Rm;
416:   t39 = t7 * t38;
417:   t42 = t10 * t38;
418:   t44 = t5 * t8 + t5 * t11 + t5 * t13 + t5 * t15 + t4 * AA * t19 + t4 * BB * t22 + t25 * t8 + t25 * t11 - 0.2e1 * t28 * _C1 * t10 * Rm + 0.2e1 * t28 * _C2 * t7 * Rm - t3 * _C1 * t39 - t3 * _C2 * t42;
419:   t45 = VV * VV;
420:   t46 = t3 * t45;
421:   t49 = t3 * VV;
422:   t62 = B * B;
423:   t78 = kn * kn;
424:   t82 = t46 * t13 + t46 * t15 + 0.2e1 * t49 * _C3 * t10 * Rm - 0.2e1 * t49 * _C4 * t7 * Rm - t3 * _C3 * t39 - t3 * _C4 * t42 + 0.4e1 * t62 * AA * t19 + 0.4e1 * t62 * BB * t22 + 0.4e1 * B * AA * t22 * kn - 0.4e1 * B * BB * t19 * kn - AA * t19 * t78 - BB * t22 * t78;
425:   *sxz = t44 + t82;

427:   t3   = exp(0.2e1 * x * B);
428:   t4   = t3 * B;
429:   t8   = km * km;
430:   t9   = t3 * t8;
431:   t10  = t9 * Rm;
432:   t11  = Rm * Rm;
433:   t13  = t3 * t11 * Rm;
434:   t14  = UU * UU;
435:   t18  = 0.4e1 * t4 * UU * Rm - t10 - t13 + 0.3e1 * t3 * t14 * Rm;
436:   t19  = Rm * x;
437:   t20  = sin(t19);
438:   t25  = 0.2e1 * t9 * B;
439:   t31  = 0.2e1 * t4 * t11;
440:   t34  = -t3 * t14 * UU - t25 + t9 * UU + 0.3e1 * t3 * UU * t11 + t31 - 0.2e1 * t4 * t14;
441:   t35  = cos(t19);
442:   t44  = VV * VV;
443:   t51  = -t10 - t13 + 0.3e1 * t3 * t44 * Rm - 0.4e1 * t4 * VV * Rm;
444:   t61  = -t25 + t31 + t3 * t44 * VV - t9 * VV - 0.2e1 * t4 * t44 - 0.3e1 * t3 * VV * t11;
445:   t70  = kn * kn;
446:   t71  = t70 * kn;
447:   t76  = B * B;
448:   t82  = B * t8;
449:   t86  = kn * x;
450:   t87  = sin(t86);
451:   t101 = cos(t86);
452:   *p = ((t18 * t20 + t34 * t35) * _C1 + (t34 * t20 - t18 * t35) * _C2 + (t51 * t20 + t61 * t35) * _C3 + (t61 * t20 - t51 * t35) * _C4 + (-AA * t71 - 0.4e1 * BB * t70 * B + 0.4e1 * t76 * AA * kn - t8 * AA * kn - 0.4e1 * t82 * BB) * t87 + (-0.4e1 * t82 * AA - 0.4e1 * AA * t70 * B - 0.4e1 * t76 * BB * kn + t8 * BB * kn + BB * t71) * t101) / km;

454:   t3  = exp(0.2e1 * x * B);
455:   t4  = UU * UU;
456:   t8  = km * km;
457:   t9  = t3 * t8;
458:   t10 = t9 * Rm;
459:   t11 = Rm * Rm;
460:   t13 = t3 * t11 * Rm;
461:   t14 = t3 * B;
462:   t18 = 0.3e1 * t3 * t4 * Rm + t10 - t13 + 0.4e1 * t14 * UU * Rm;
463:   t19 = Rm * x;
464:   t20 = sin(t19);
465:   t23 = 0.2e1 * t9 * B;
466:   t33 = 0.2e1 * t14 * t11;
467:   t34 = -t23 + 0.3e1 * t3 * UU * t11 - t9 * UU - t3 * t4 * UU - 0.2e1 * t4 * t14 + t33;
468:   t35 = cos(t19);
469:   t47 = VV * VV;
470:   t51 = t10 - 0.4e1 * t14 * VV * Rm + 0.3e1 * t3 * t47 * Rm - t13;
471:   t61 = t9 * VV - t23 + t3 * t47 * VV - 0.2e1 * t14 * t47 + t33 - 0.3e1 * t3 * VV * t11;
472:   t70 = B * B;
473:   t74 = kn * kn;
474:   t75 = t74 * kn;
475:   t83 = kn * x;
476:   t84 = sin(t83);
477:   t96 = cos(t83);
478:   *szz = -((t18 * t20 + t34 * t35) * _C1 + (t34 * t20 - t18 * t35) * _C2 + (t51 * t20 + t61 * t35) * _C3 + (t61 * t20 - t51 * t35) * _C4 + (0.4e1 * t70 * AA * kn - AA * t75 - 0.4e1 * BB * t74 * B + t8 * AA * kn) * t84 + (-t8 * BB * kn - 0.4e1 * AA * t74 * B - 0.4e1 * t70 * BB * kn + BB * t75) * t96) / km;

480:   /* vx = Vx, vz = Vz, sxx = xx-component of stress tensor, sxz = xz-component of stress tensor, p = pressure, szz = zz-component of stress tensor */
481:   *vx *= cos(km * z);  /* Vx */
482:   *vz *= sin(km * z);  /* Vz */
483:   *p *= cos(km * z);   /* p */
484:   *sxx *= cos(km * z); /* sxx total stress */
485:   *sxz *= sin(km * z); /* tzx stress */
486:   *szz *= cos(km * z); /* szz total stress */

488:   /* rho = -sigma*sin(km*z)*cos(kn*x); */ /* density */
489:   PetscFunctionReturn(PETSC_SUCCESS);
490: }

492: PetscErrorCode SolKxWrapperV(PetscInt dim, const PetscReal x[], PetscInt Nf, PetscScalar v[], void *ctx)
493: {
494:   PetscReal   B  = 100.0;
495:   PetscReal   kn = 100 * M_PI;
496:   PetscReal   km = 100 * M_PI;
497:   PetscScalar p, sxx, sxz, szz;

499:   PetscFunctionBeginUser;
500:   SolKxSolution(x[0], x[1], kn, km, B, &v[0], &v[1], &p, &sxx, &sxz, &szz);
501:   PetscFunctionReturn(PETSC_SUCCESS);
502: }

504: PetscErrorCode SolKxWrapperP(PetscInt dim, const PetscReal x[], PetscInt Nf, PetscScalar v[], void *ctx)
505: {
506:   PetscReal   B  = 100.0;
507:   PetscReal   kn = 100 * M_PI;
508:   PetscReal   km = 100 * M_PI;
509:   PetscScalar vx, vz, sxx, sxz, szz;

511:   PetscFunctionBeginUser;
512:   SolKxSolution(x[0], x[1], kn, km, B, &vx, &vz, &v[0], &sxx, &sxz, &szz);
513:   PetscFunctionReturn(PETSC_SUCCESS);
514: }

516: /*
517:   Compare the C implementation with generated data from Maple
518: */
519: PetscErrorCode MapleTest(MPI_Comm comm, AppCtx *ctx)
520: {
521:   const PetscInt n = 41;
522:   PetscScalar    vxMaple[41][41], vzMaple[41][41], pMaple[41][41], sxxMaple[41][41], sxzMaple[41][41], szzMaple[41][41];
523:   PetscReal      x[41], z[41];
524:   PetscReal      kn, km, B;
525:   PetscInt       i, j;

527:   PetscFunctionBegin;
528:   PetscCall(SolKxData5(x, z, &kn, &km, &B, vxMaple, vzMaple, pMaple, sxxMaple, sxzMaple, szzMaple));
529:   for (i = 0; i < n; ++i) {
530:     for (j = 0; j < n; ++j) {
531:       PetscScalar vx, vz, p, sxx, sxz, szz;
532:       PetscReal   norm;

534:       PetscCall(SolKxSolution(x[i], z[j], kn, km, B, &vx, &vz, &p, &sxx, &sxz, &szz));
535:       norm = PetscSqrt(PetscSqr(PetscAbsScalar(vx - vxMaple[i][j])) + PetscSqr(PetscAbsScalar(vz - vzMaple[i][j])));
536:       PetscCheck(norm > -1.0e-10, PETSC_COMM_SELF, PETSC_ERR_PLIB, "Invalid solution, %0.17e %0.17e %0.17e %0.17e %0.17e %0.17e %0.17e %0.17e %0.17e", (double)x[i], (double)z[j], (double)PetscAbsScalar(vx - vxMaple[i][j]), (double)PetscAbsScalar(vz - vzMaple[i][j]), (double)PetscAbsScalar(p - pMaple[i][j]), (double)PetscAbsScalar(sxx - sxxMaple[i][j]), (double)PetscAbsScalar(sxz - sxzMaple[i][j]), (double)PetscAbsScalar(szz - szzMaple[i][j]), (double)norm);
537:     }
538:   }
539: }
540: PetscCall(PetscPrintf(comm, "Verified Maple test 5\n"));
541: PetscFunctionReturn(PETSC_SUCCESS);
542: }

544: PetscErrorCode FEMTest(MPI_Comm comm, AppCtx *ctx)
545: {
546:   DM  dm;
547:   Vec u;
548:   PetscErrorCode (*funcs[2])(PetscInt, const PetscReal[], PetscInt, PetscScalar *, void *) = {SolKxWrapperV, SolKxWrapperP};
549:   PetscReal discError;

551:   PetscFunctionBegin;
552:   if (!ctx->fem) PetscFunctionReturn(PETSC_SUCCESS);
553:   /* Create DM */
554:   PetscCall(DMPlexCreateBoxMesh(comm, 2, PETSC_TRUE, NULL, NULL, NULL, NULL, PETSC_FALSE, 0, PETSC_TRUE, &dm));
555:   PetscCall(DMSetFromOptions(dm));
556:   /* Project solution into FE space */
557:   PetscCall(DMGetGlobalVector(dm, &u));
558:   PetscCall(DMProjectFunction(dm, 0.0, funcs, NULL, INSERT_VALUES, u));
559:   PetscCall(DMComputeL2Diff(dm, 0.0, funcs, NULL, u, &discError));
560:   PetscCall(VecViewFromOptions(u, NULL, "-vec_view"));
561:   /* Cleanup */
562:   PetscCall(DMRestoreGlobalVector(dm, &u));
563:   PetscCall(DMDestroy(&dm));
564:   PetscFunctionReturn(PETSC_SUCCESS);
565: }

567: int main(int argc, char **argv)
568: {
569:   AppCtx user; /* user-defined work context */

571:   PetscFunctionBeginUser;
572:   PetscCall(PetscInitialize(&argc, &argv, NULL, help));
573:   PetscCall(ProcessOptions(PETSC_COMM_WORLD, &user));
574:   PetscCall(MapleTest(PETSC_COMM_WORLD, &user));
575:   PetscCall(FEMTest(PETSC_COMM_WORLD, &user));
576:   PetscCall(PetscFinalize());
577:   return 0;
578: }