petsc / src / ts / examples / tutorials / ex22f_mf.F90

  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
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
!     Time-dependent advection-reaction PDE in 1d. Demonstrates IMEX methods
!
!     u_t + a1*u_x = -k1*u + k2*v + s1
!     v_t + a2*v_x = k1*u - k2*v + s2
!     0 < x < 1;
!     a1 = 1, k1 = 10^6, s1 = 0,
!     a2 = 0, k2 = 2*k1, s2 = 1
!
!     Initial conditions:
!     u(x,0) = 1 + s2*x
!     v(x,0) = k0/k1*u(x,0) + s1/k1
!
!     Upstream boundary conditions:
!     u(0,t) = 1-sin(12*t)^4
!

#define MF_EX22F_MF
!#undef  MF_EX22F_MF


#ifdef MF_EX22F_MF
  module PETScShiftMod
#include <finclude/petscsys.h>
#include <finclude/petscts.h>
#include <finclude/petscmat.h>
    PetscScalar::PETSC_SHIFT
    TS::tscontext
    Mat::Jmat
    PetscReal::MFuser(6)
  end module PETScShiftMod
#endif

program main
#ifdef MF_EX22F_MF
  use PETScShiftMod, only :  tscontext,Jmat
#endif
  implicit none
#include <finclude/petscsys.h>
#include <finclude/petscvec.h>
#include <finclude/petscmat.h>
#include <finclude/petscsnes.h>
#include <finclude/petscts.h>
#include <petscdm.h>
#include <finclude/petscdmda.h>
  !
  !     Create an application context to contain data needed by the
  !     application-provided call-back routines, FormJacobian() and
  !     FormFunction(). We use a double precision array with six
  !     entries, two for each problem parameter a, k, s.
  !
  PetscReal user(6)
  integer user_a,user_k,user_s
  parameter (user_a = 0,user_k = 2,user_s = 4)

  external FormRHSFunction,FormIFunction
  external FormInitialSolution

#ifndef MF_EX22F_MF
  external FormIJacobian
#endif

#ifdef MF_EX22F_MF
  external MyMult,FormIJacobianMF
#endif

  TS             ts
  Vec            X
  Mat            J
  PetscInt       steps,maxsteps,mx
  PetscBool      OptionSaveToDisk
  PetscErrorCode ierr
  DM             da
  PetscReal      ftime,dt
  PetscReal      zero,one,pone
  PetscInt       im11,i2
  PetscBool      flg


  PetscInt       xs,xe,gxs,gxe,dof,gdof
  PetscScalar    shell_shift
  Mat            A

  im11 = -11
  i2   = 2
  zero = 0.0
  one = 1.0
  pone = one / 10

  call PetscInitialize(PETSC_NULL_CHARACTER,ierr)

  ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  !  Create distributed array (DMDA) to manage parallel grid and vectors
  ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  call DMDACreate1d(PETSC_COMM_WORLD,DM_BOUNDARY_NONE,im11,i2,i2, &
       PETSC_NULL_INTEGER,da,ierr)

  ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  !    Extract global vectors from DMDA;
  ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  call DMCreateGlobalVector(da,X,ierr)

  ! Initialize user application context
  ! Use zero-based indexing for command line parameters to match ex22.c
  user(user_a+1) = 1.0
  call PetscOptionsGetReal(PETSC_NULL_CHARACTER,'-a0',              &
       user(user_a+1),flg,ierr)
  user(user_a+2) = 0.0
  call PetscOptionsGetReal(PETSC_NULL_CHARACTER,'-a1',              &
       user(user_a+2),flg,ierr)
  user(user_k+1) = 1000000.0
  call PetscOptionsGetReal(PETSC_NULL_CHARACTER,'-k0',              &
       user(user_k+1),flg,ierr)
  user(user_k+2) = 2*user(user_k+1)
  call PetscOptionsGetReal(PETSC_NULL_CHARACTER,'-k1',              &
       user(user_k+2),flg,ierr)
  user(user_s+1) = 0.0
  call PetscOptionsGetReal(PETSC_NULL_CHARACTER,'-s0',              &
       user(user_s+1),flg,ierr)
  user(user_s+2) = 1.0
  call PetscOptionsGetReal(PETSC_NULL_CHARACTER,'-s1',              &
       user(user_s+2),flg,ierr)

  OptionSaveToDisk=.FALSE.
  call PetscOptionsGetBool(PETSC_NULL_CHARACTER,'-sdisk',           &
       OptionSaveToDisk,flg,ierr)
  ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  !    Create timestepping solver context
  !     - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  call TSCreate(PETSC_COMM_WORLD,ts,ierr)
#ifdef MF_EX22F_MF
  tscontext=ts
#endif
  call TSSetDM(ts,da,ierr)
  call TSSetType(ts,TSARKIMEX,ierr)
  call TSSetRHSFunction(ts,PETSC_NULL_OBJECT,FormRHSFunction,       &
       user,ierr)

#ifdef MF_EX22F_MF
  ! - - - - - - - - -- - - - -
  !   Matrix free setup
  call GetLayout(da,mx,xs,xe,gxs,gxe,ierr)
  dof=i2*(xe-xs+1)
  gdof=i2*(gxe-gxs+1)
  call MatCreateShell(PETSC_COMM_WORLD,                             &
       dof,dof,gdof,gdof,                                           &
       shell_shift,A,ierr);

  call MatShellSetOperation(A,MATOP_MULT,MyMult,ierr)
  ! - - - - - - - - - - - -
#endif

  call TSSetIFunction(ts,PETSC_NULL_OBJECT,FormIFunction,user,ierr)
  call DMSetMatType(da,MATAIJ,ierr)
  call DMCreateMatrix(da,J,ierr)

#ifdef MF_EX22F_MF
  Jmat=J
#endif

#ifndef MF_EX22F_MF
  call TSSetIJacobian(ts,J,J,FormIJacobian,user,ierr)
#endif
#ifdef MF_EX22F_MF
  call TSSetIJacobian(ts,A,A,FormIJacobianMF,user,ierr)
#endif

  ftime = 1.0
  maxsteps = 10000
  call TSSetDuration(ts,maxsteps,ftime,ierr)

  ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  !  Set initial conditions
  ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  call FormInitialSolution(ts,X,user,ierr)
  call TSSetSolution(ts,X,ierr)
  call VecGetSize(X,mx,ierr)
  !  Advective CFL, I don't know why it needs so much safety factor.
  dt = pone * max(user(user_a+1),user(user_a+2)) / mx;
  call TSSetInitialTimeStep(ts,zero,dt,ierr)

  ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  !   Set runtime options
  ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  call TSSetFromOptions(ts,ierr)

  ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  !  Solve nonlinear system
  ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  call TSSolve(ts,X,ierr)

  if (OptionSaveToDisk) then
     call GetLayout(da,mx,xs,xe,gxs,gxe,ierr)
     dof=i2*(xe-xs+1)
     gdof=i2*(gxe-gxs+1)
     call SaveSolutionToDisk(da,X,gdof,xs,xe)
  end if

  ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  !  Free work space.
  ! - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
  call MatDestroy(J,ierr)
  call VecDestroy(X,ierr)
  call TSDestroy(ts,ierr)
  call DMDestroy(da,ierr)
  call PetscFinalize(ierr)
end program main

! Small helper to extract the layout, result uses 1-based indexing.
subroutine GetLayout(da,mx,xs,xe,gxs,gxe,ierr)
  implicit none
#include <finclude/petscsys.h>
#include <petscdm.h>
#include <finclude/petscdmda.h>
  DM da
  PetscInt mx,xs,xe,gxs,gxe
  PetscErrorCode ierr
  PetscInt xm,gxm
  call DMDAGetInfo(da,PETSC_NULL_INTEGER,                     &
       mx,PETSC_NULL_INTEGER,PETSC_NULL_INTEGER,                    &
       PETSC_NULL_INTEGER,PETSC_NULL_INTEGER,PETSC_NULL_INTEGER,    &
       PETSC_NULL_INTEGER,PETSC_NULL_INTEGER,                       &
       PETSC_NULL_INTEGER,PETSC_NULL_INTEGER,PETSC_NULL_INTEGER,    &
       PETSC_NULL_INTEGER,PETSC_NULL_INTEGER,ierr)
  call DMDAGetCorners(da,xs,PETSC_NULL_INTEGER,PETSC_NULL_INTEGER,  &
       xm,PETSC_NULL_INTEGER,PETSC_NULL_INTEGER,ierr)
  call DMDAGetGhostCorners(da,                                      &
       gxs,PETSC_NULL_INTEGER,PETSC_NULL_INTEGER,                   &
       gxm,PETSC_NULL_INTEGER,PETSC_NULL_INTEGER,ierr)
  xs = xs + 1
  gxs = gxs + 1
  xe = xs + xm - 1
  gxe = gxs + gxm - 1
end subroutine GetLayout

subroutine FormIFunctionLocal(mx,xs,xe,gxs,gxe,x,xdot,f,          &
     a,k,s,ierr)
  implicit none
  PetscInt mx,xs,xe,gxs,gxe
  PetscScalar x(2,xs:xe)
  PetscScalar xdot(2,xs:xe)
  PetscScalar f(2,xs:xe)
  PetscReal a(2),k(2),s(2)
  PetscErrorCode ierr
  PetscInt i
  do  i = xs,xe
     f(1,i) = xdot(1,i) + k(1)*x(1,i) - k(2)*x(2,i) - s(1)
     f(2,i) = xdot(2,i) - k(1)*x(1,i) + k(2)*x(2,i) - s(2)
  end do
end subroutine FormIFunctionLocal

subroutine FormIFunction(ts,t,X,Xdot,F,user,ierr)
  implicit none
#include <finclude/petscsys.h>
#include <finclude/petscvec.h>
#include <finclude/petscmat.h>
#include <finclude/petscsnes.h>
#include <finclude/petscts.h>
#include <petscdm.h>
#include <finclude/petscdmda.h>
  TS ts
  PetscReal t
  Vec X,Xdot,F
  PetscReal user(6)
  PetscErrorCode ierr
  integer user_a,user_k,user_s
  parameter (user_a = 1,user_k = 3,user_s = 5)

  DM             da
  PetscInt       mx,xs,xe,gxs,gxe
  Vec            Xloc
  PetscOffset    ixx,ixxdot,iff
  PetscScalar    xx(0:1),xxdot(0:1),ff(0:1)

  call TSGetDM(ts,da,ierr)
  call GetLayout(da,mx,xs,xe,gxs,gxe,ierr)

  ! Get access to vector data
  call VecGetArray(X,xx,ixx,ierr)
  call VecGetArray(Xdot,xxdot,ixxdot,ierr)
  call VecGetArray(F,ff,iff,ierr)

  call FormIFunctionLocal(mx,xs,xe,gxs,gxe,                         &
       xx(ixx),xxdot(ixxdot),ff(iff),                               &
       user(user_a),user(user_k),user(user_s),ierr)

  call VecRestoreArray(X,xx,ixx,ierr)
  call VecRestoreArray(Xdot,xxdot,ixxdot,ierr)
  call VecRestoreArray(F,ff,iff,ierr)
end subroutine FormIFunction

subroutine FormRHSFunctionLocal(mx,xs,xe,gxs,gxe,t,x,f,           &
     a,k,s,ierr)
  implicit none
  PetscInt mx,xs,xe,gxs,gxe
  PetscReal t
  PetscScalar x(2,gxs:gxe),f(2,xs:xe)
  PetscReal a(2),k(2),s(2)
  PetscErrorCode ierr
  PetscInt i,j
  PetscReal hx,u0t(2)
  PetscReal one,two,three,four,six,twelve
  PetscReal half,third,twothird,sixth
  PetscReal twelfth

  one = 1.0
  two = 2.0
  three = 3.0
  four = 4.0
  six = 6.0
  twelve = 12.0
  hx = one / mx
  u0t(1) = one - sin(twelve*t)**four
  u0t(2) = 0.0
  half = one/two
  third = one / three
  twothird = two / three
  sixth = one / six
  twelfth = one / twelve
  do  i = xs,xe
     do  j = 1,2
        if (i .eq. 1) then
           f(j,i) = a(j)/hx*(third*u0t(j) + half*x(j,i) - x(j,i+1)  &
                + sixth*x(j,i+2))
        else if (i .eq. 2) then
           f(j,i) = a(j)/hx*(-twelfth*u0t(j) + twothird*x(j,i-1)    &
                - twothird*x(j,i+1) + twelfth*x(j,i+2))
        else if (i .eq. mx-1) then
           f(j,i) = a(j)/hx*(-sixth*x(j,i-2) + x(j,i-1)             &
                - half*x(j,i) -third*x(j,i+1))
        else if (i .eq. mx) then
           f(j,i) = a(j)/hx*(-x(j,i) + x(j,i-1))
        else
           f(j,i) = a(j)/hx*(-twelfth*x(j,i-2)                      &
                + twothird*x(j,i-1)                                 &
                - twothird*x(j,i+1) + twelfth*x(j,i+2))
        end if
     end do
  end do

#ifdef EXPLICIT_INTEGRATOR22
  do  i = xs,xe
     f(1,i) = f(1,i) -( k(1)*x(1,i) - k(2)*x(2,i) - s(1))
     f(2,i) = f(2,i) -(- k(1)*x(1,i) + k(2)*x(2,i) - s(2))
  end do
#endif

end subroutine FormRHSFunctionLocal

subroutine FormRHSFunction(ts,t,X,F,user,ierr)
  implicit none
#include <finclude/petscsys.h>
#include <finclude/petscvec.h>
#include <finclude/petscmat.h>
#include <finclude/petscsnes.h>
#include <finclude/petscts.h>
#include <petscdm.h>
#include <finclude/petscdmda.h>
  TS ts
  PetscReal t
  Vec X,F
  PetscReal user(6)
  PetscErrorCode ierr
  integer user_a,user_k,user_s
  parameter (user_a = 1,user_k = 3,user_s = 5)
  DM             da
  Vec            Xloc
  PetscInt       mx,xs,xe,gxs,gxe
  PetscOffset    ixx,iff
  PetscScalar    xx(0:1),ff(0:1)

  call TSGetDM(ts,da,ierr)
  call GetLayout(da,mx,xs,xe,gxs,gxe,ierr)

  !     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.
  call DMGetLocalVector(da,Xloc,ierr)
  call DMGlobalToLocalBegin(da,X,INSERT_VALUES,Xloc,ierr)
  call DMGlobalToLocalEnd(da,X,INSERT_VALUES,Xloc,ierr)

  ! Get access to vector data
  call VecGetArray(Xloc,xx,ixx,ierr)
  call VecGetArray(F,ff,iff,ierr)

  call FormRHSFunctionLocal(mx,xs,xe,gxs,gxe,                       &
       t,xx(ixx),ff(iff),                                           &
       user(user_a),user(user_k),user(user_s),ierr)

  call VecRestoreArray(Xloc,xx,ixx,ierr)
  call VecRestoreArray(F,ff,iff,ierr)
  call DMRestoreLocalVector(da,Xloc,ierr)
end subroutine FormRHSFunction

#ifndef MF_EX22F_MF
! ---------------------------------------------------------------------
!
!  IJacobian - Compute IJacobian = dF/dU + shift*dF/dUdot
!
subroutine FormIJacobian(ts,t,X,Xdot,shift,J,Jpre,mstr,user,ierr)
  implicit none
#include <finclude/petscsys.h>
#include <finclude/petscvec.h>
#include <finclude/petscmat.h>
#include <finclude/petscsnes.h>
#include <finclude/petscts.h>
#include <petscdm.h>
#include <finclude/petscdmda.h>
  TS ts
  PetscReal t,shift
  Vec X,Xdot
  Mat J,Jpre
  MatStructure mstr
  PetscReal user(6)
  PetscErrorCode ierr
  integer user_a,user_k,user_s
  parameter (user_a = 0,user_k = 2,user_s = 4)

  DM             da
  PetscInt       mx,xs,xe,gxs,gxe
  PetscInt       i,i1,row,col
  PetscReal      k1,k2;
  PetscScalar    val(4)

  call TSGetDM(ts,da,ierr)
  call GetLayout(da,mx,xs,xe,gxs,gxe,ierr)

  i1 = 1
  k1 = user(user_k+1)
  k2 = user(user_k+2)
  do i = xs,xe
     row = i-gxs
     col = i-gxs
     val(1) = shift + k1
     val(2) = -k2
     val(3) = -k1
     val(4) = shift + k2
     call MatSetValuesBlockedLocal(Jpre,i1,row,i1,col,val,          &
          INSERT_VALUES,ierr)
  end do
  call MatAssemblyBegin(Jpre,MAT_FINAL_ASSEMBLY,ierr)
  call MatAssemblyEnd(Jpre,MAT_FINAL_ASSEMBLY,ierr)
  if (J /= Jpre) then
     call MatAssemblyBegin(J,MAT_FINAL_ASSEMBLY,ierr)
     call MatAssemblyEnd(J,MAT_FINAL_ASSEMBLY,ierr)
  end if
  mstr = SAME_NONZERO_PATTERN
end subroutine FormIJacobian
#endif

subroutine FormInitialSolutionLocal(mx,xs,xe,gxs,gxe,x,a,k,s,ierr)
  implicit none
  PetscInt mx,xs,xe,gxs,gxe
  PetscScalar x(2,xs:xe)
  PetscReal a(2),k(2),s(2)
  PetscErrorCode ierr

  PetscInt i
  PetscReal one,hx,r,ik
  one = 1.0
  hx = one / mx
  do i=xs,xe
     r = i*hx
     if (k(2) .ne. 0.0) then
        ik = one/k(2)
     else
        ik = one
     end if
     x(1,i) = one + s(2)*r
     x(2,i) = k(1)*ik*x(1,i) + s(2)*ik
  end do
end subroutine FormInitialSolutionLocal

subroutine FormInitialSolution(ts,X,user,ierr)
  implicit none
#include <finclude/petscsys.h>
#include <finclude/petscvec.h>
#include <finclude/petscmat.h>
#include <finclude/petscsnes.h>
#include <finclude/petscts.h>
#include <petscdm.h>
#include <finclude/petscdmda.h>
  TS ts
  PetscReal t
  Vec X
  PetscReal user(6)
  PetscErrorCode ierr
  integer user_a,user_k,user_s
  parameter (user_a = 1,user_k = 3,user_s = 5)

  DM             da
  PetscInt       mx,xs,xe,gxs,gxe
  PetscOffset    ixx
  PetscScalar    xx(0:1)

  call TSGetDM(ts,da,ierr)
  call GetLayout(da,mx,xs,xe,gxs,gxe,ierr)

  ! Get access to vector data
  call VecGetArray(X,xx,ixx,ierr)

  call FormInitialSolutionLocal(mx,xs,xe,gxs,gxe,                       &
       xx(ixx),user(user_a),user(user_k),user(user_s),ierr)

  call VecRestoreArray(X,xx,ixx,ierr)
end subroutine FormInitialSolution


#ifdef MF_EX22F_MF
! ---------------------------------------------------------------------
!
!  IJacobian - Compute IJacobian = dF/dU + shift*dF/dUdot
!
subroutine FormIJacobianMF(ts,t,X,Xdot,shift,J,Jpre,mstr,user,ierr)
  use PETScShiftMod, only :  PETSC_SHIFT,MFuser
  implicit none
#include <finclude/petscsys.h>
#include <finclude/petscvec.h>
#include <finclude/petscmat.h>
#include <finclude/petscsnes.h>
#include <finclude/petscts.h>
#include <petscdm.h>
#include <finclude/petscdmda.h>
  TS ts
  PetscReal t,shift
  Vec X,Xdot
  Mat J,Jpre
  MatStructure mstr
  PetscReal user(6)
  PetscErrorCode ierr

  !  call MatShellSetContext(J,shift,ierr)
  PETSC_SHIFT=shift
  MFuser=user

  mstr = SAME_NONZERO_PATTERN
end subroutine FormIJacobianMF

! -------------------------------------------------------------------
!
!   MyMult - user provided matrix multiply
!
!   Input Parameters:
!.  X - input vector
!
!   Output Parameter:
!.  F - function vector
!
subroutine  MyMult(A,X,F,ierr)
  use PETScShiftMod, only :  PETSC_SHIFT,tscontext,Jmat,MFuser
  implicit none

#include <finclude/petscsys.h>
#include <finclude/petscvec.h>
#include <finclude/petscmat.h>
#include <finclude/petscpc.h>
#include <finclude/petscts.h>
#include <finclude/petscvec.h90>

  Mat     A
  Vec     X,F

  PetscErrorCode ierr
  PetscScalar shift

  Mat J,Jpre

  PetscReal user(6)

  integer user_a,user_k,user_s
  parameter (user_a = 0,user_k = 2,user_s = 4)

  DM             da
  PetscInt       mx,xs,xe,gxs,gxe
  PetscInt       i,i1,row,col
  PetscReal      k1,k2;
  PetscScalar    val(4)

  !call MatShellGetContext(A,shift,ierr)
  shift=PETSC_SHIFT
  user=MFuser

  call TSGetDM(tscontext,da,ierr)
  call GetLayout(da,mx,xs,xe,gxs,gxe,ierr)

  i1 = 1
  k1 = user(user_k+1)
  k2 = user(user_k+2)

  do i = xs,xe
     row = i-gxs
     col = i-gxs
     val(1) = shift + k1
     val(2) = -k2
     val(3) = -k1
     val(4) = shift + k2
     call MatSetValuesBlockedLocal(Jmat,i1,row,i1,col,val,          &
          INSERT_VALUES,ierr)
  end do

!  call MatAssemblyBegin(Jpre,MAT_FINAL_ASSEMBLY,ierr)
!  call MatAssemblyEnd(Jpre,MAT_FINAL_ASSEMBLY,ierr)
!  if (J /= Jpre) then
     call MatAssemblyBegin(Jmat,MAT_FINAL_ASSEMBLY,ierr)
     call MatAssemblyEnd(Jmat,MAT_FINAL_ASSEMBLY,ierr)
!  end if

  call MatMult(Jmat,X,F,ierr)

  return
end subroutine MyMult
#endif


!
subroutine SaveSolutionToDisk(da,X,gdof,xs,xe)
  implicit none
#include <finclude/petscsys.h>
#include <finclude/petscvec.h>
#include <finclude/petscmat.h>
#include <finclude/petscsnes.h>
#include <finclude/petscts.h>
#include <petscdm.h>
#include <finclude/petscdmda.h>

  Vec X,Xloc
  DM             da
  PetscInt xs,xe
  PetscInt gdof,i
  PetscErrorCode ierr
  PetscOffset    ixx
  PetscScalar data2(2,xs:xe),data(gdof)
  PetscScalar    xx(0:1)


  call VecGetArray(X,xx,ixx,ierr)

  data2=reshape(xx(ixx:ixx+gdof),(/2,xe-xs+1/))
  data=reshape(data2,(/gdof/))
  open(1020,file='solution_out_ex22f_mf.txt')
  do i=1,gdof
     write(1020,'(e24.16,1x)') data(i)
  end do
  close(1020)

  call VecRestoreArray(X,xx,ixx,ierr)
end subroutine SaveSolutionToDisk
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.