forked from mmcgurn/petscDebug
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex9.c
2148 lines (1931 loc) · 91.3 KB
/
ex9.c
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
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
static char help[] = "Landau Damping test using Vlasov-Poisson equations\n";
/*
To run the code with particles sinusoidally perturbed in x space use the test "pp_poisson_bsi_1d_4" or "pp_poisson_bsi_2d_4"
According to Lukas, good damping results come at ~16k particles per cell
To visualize the efield use
-monitor_efield
To visualize the swarm distribution use
-ts_monitor_hg_swarm
To visualize the particles, we can use
-ts_monitor_sp_swarm -ts_monitor_sp_swarm_retain 0 -ts_monitor_sp_swarm_phase 1 -draw_size 500,500
*/
#include <petscts.h>
#include <petscdmplex.h>
#include <petscdmswarm.h>
#include <petscfe.h>
#include <petscds.h>
#include <petscbag.h>
#include <petscdraw.h>
#include <petsc/private/dmpleximpl.h> /* For norm and dot */
#include <petsc/private/petscfeimpl.h> /* For interpolation */
#include "petscdm.h"
#include "petscdmlabel.h"
const char *EMTypes[] = {"primal", "mixed", "coulomb", "none", "EMType", "EM_", NULL};
typedef enum {
EM_PRIMAL,
EM_MIXED,
EM_COULOMB,
EM_NONE
} EMType;
typedef enum {
V0,
X0,
T0,
M0,
Q0,
PHI0,
POISSON,
VLASOV,
SIGMA,
NUM_CONSTANTS
} ConstantType;
typedef struct {
PetscScalar v0; /* Velocity scale, often the thermal velocity */
PetscScalar t0; /* Time scale */
PetscScalar x0; /* Space scale */
PetscScalar m0; /* Mass scale */
PetscScalar q0; /* Charge scale */
PetscScalar kb;
PetscScalar epsi0;
PetscScalar phi0; /* Potential scale */
PetscScalar poissonNumber; /* Non-Dimensional Poisson Number */
PetscScalar vlasovNumber; /* Non-Dimensional Vlasov Number */
PetscReal sigma; /* Nondimensional charge per length in x */
} Parameter;
typedef struct {
PetscBag bag; /* Problem parameters */
PetscBool error; /* Flag for printing the error */
PetscBool efield_monitor; /* Flag to show electric field monitor */
PetscBool initial_monitor;
PetscBool periodic; /* Use periodic boundaries */
PetscBool fake_1D; /* Run simulation in 2D but zeroing second dimension */
PetscBool perturbed_weights; /* Uniformly sample x,v space with gaussian weights */
PetscBool poisson_monitor;
PetscInt ostep; /* print the energy at each ostep time steps */
PetscInt numParticles;
PetscReal timeScale; /* Nondimensionalizing time scale */
PetscReal charges[2]; /* The charges of each species */
PetscReal masses[2]; /* The masses of each species */
PetscReal thermal_energy[2]; /* Thermal Energy (used to get other constants)*/
PetscReal cosine_coefficients[2]; /*(alpha, k)*/
PetscReal totalWeight;
PetscReal stepSize;
PetscInt steps;
PetscReal initVel;
EMType em; /* Type of electrostatic model */
SNES snes;
PetscDraw drawef;
PetscDrawLG drawlg_ef;
PetscDraw drawic_x;
PetscDraw drawic_v;
PetscDraw drawic_w;
PetscDrawHG drawhgic_x;
PetscDrawHG drawhgic_v;
PetscDrawHG drawhgic_w;
PetscDraw EDraw;
PetscDraw RhoDraw;
PetscDraw PotDraw;
PetscDrawSP EDrawSP;
PetscDrawSP RhoDrawSP;
PetscDrawSP PotDrawSP;
PetscBool monitor_positions; /* Flag to show particle positins at each time step */
PetscDraw positionDraw;
PetscDrawSP positionDrawSP;
} AppCtx;
static PetscErrorCode ProcessOptions(MPI_Comm comm, AppCtx *options)
{
PetscFunctionBeginUser;
PetscInt d = 2;
options->error = PETSC_FALSE;
options->efield_monitor = PETSC_FALSE;
options->initial_monitor = PETSC_FALSE;
options->periodic = PETSC_FALSE;
options->fake_1D = PETSC_FALSE;
options->perturbed_weights = PETSC_FALSE;
options->poisson_monitor = PETSC_FALSE;
options->ostep = 100;
options->timeScale = 2.0e-14;
options->charges[0] = -1.0;
options->charges[1] = 1.0;
options->masses[0] = 1.0;
options->masses[1] = 1000.0;
options->thermal_energy[0] = 1.0;
options->thermal_energy[1] = 1.0;
options->cosine_coefficients[0] = 0.01;
options->cosine_coefficients[1] = 0.5;
options->initVel = 1;
options->totalWeight = 1.0;
options->drawef = NULL;
options->drawlg_ef = NULL;
options->drawic_x = NULL;
options->drawic_v = NULL;
options->drawic_w = NULL;
options->drawhgic_x = NULL;
options->drawhgic_v = NULL;
options->drawhgic_w = NULL;
options->EDraw = NULL;
options->RhoDraw = NULL;
options->PotDraw = NULL;
options->EDrawSP = NULL;
options->RhoDrawSP = NULL;
options->PotDrawSP = NULL;
options->em = EM_COULOMB;
options->numParticles = 32768;
options->monitor_positions = PETSC_FALSE;
options->positionDraw = NULL;
options->positionDrawSP = NULL;
PetscOptionsBegin(comm, "", "Central Orbit Options", "DMSWARM");
PetscCall(PetscOptionsBool("-error", "Flag to print the error", "ex9.c", options->error, &options->error, NULL));
PetscCall(PetscOptionsBool("-monitor_efield", "Flag to show efield plot", "ex9.c", options->efield_monitor, &options->efield_monitor, NULL));
PetscCall(PetscOptionsBool("-monitor_ics", "Flag to show initial condition histograms", "ex9.c", options->initial_monitor, &options->initial_monitor, NULL));
PetscCall(PetscOptionsBool("-monitor_positions", "The flag to show particle positions", "ex9.c", options->monitor_positions, &options->monitor_positions, NULL));
PetscCall(PetscOptionsBool("-monitor_poisson", "The flag to show charges, Efield and potential solve", "ex9.c", options->poisson_monitor, &options->poisson_monitor, NULL));
PetscCall(PetscOptionsBool("-periodic", "Flag to use periodic particle boundaries", "ex9.c", options->periodic, &options->periodic, NULL));
PetscCall(PetscOptionsBool("-fake_1D", "Flag to run a 1D simulation (but really in 2D)", "ex9.c", options->fake_1D, &options->fake_1D, NULL));
PetscCall(PetscOptionsBool("-perturbed_weights", "Flag to run uniform sampling with perturbed weights", "ex9.c", options->perturbed_weights, &options->perturbed_weights, NULL));
PetscCall(PetscOptionsInt("-output_step", "Number of time steps between output", "ex9.c", options->ostep, &options->ostep, NULL));
PetscCall(PetscOptionsReal("-timeScale", "Nondimensionalizing time scale", "ex9.c", options->timeScale, &options->timeScale, NULL));
PetscCall(PetscOptionsReal("-initial_velocity", "Initial velocity of perturbed particle", "ex9.c", options->initVel, &options->initVel, NULL));
PetscCall(PetscOptionsReal("-total_weight", "Total weight of all particles", "ex9.c", options->totalWeight, &options->totalWeight, NULL));
PetscCall(PetscOptionsRealArray("-cosine_coefficients", "Amplitude and frequency of cosine equation used in initialization", "ex9.c", options->cosine_coefficients, &d, NULL));
PetscCall(PetscOptionsRealArray("-charges", "Species charges", "ex9.c", options->charges, &d, NULL));
PetscCall(PetscOptionsEnum("-em_type", "Type of electrostatic solver", "ex9.c", EMTypes, (PetscEnum)options->em, (PetscEnum *)&options->em, NULL));
PetscOptionsEnd();
PetscFunctionReturn(PETSC_SUCCESS);
}
static PetscErrorCode SetupContext(DM dm, DM sw, AppCtx *user)
{
PetscFunctionBeginUser;
if (user->efield_monitor) {
PetscDrawAxis axis_ef;
PetscCall(PetscDrawCreate(PETSC_COMM_WORLD, NULL, "monitor_efield", 0, 300, 400, 300, &user->drawef));
PetscCall(PetscDrawSetSave(user->drawef, "ex9_Efield.png"));
PetscCall(PetscDrawSetFromOptions(user->drawef));
PetscCall(PetscDrawLGCreate(user->drawef, 1, &user->drawlg_ef));
PetscCall(PetscDrawLGGetAxis(user->drawlg_ef, &axis_ef));
PetscCall(PetscDrawAxisSetLabels(axis_ef, "Electron Electric Field", "time", "E_max"));
PetscCall(PetscDrawLGSetLimits(user->drawlg_ef, 0., user->steps * user->stepSize, -10., 0.));
PetscCall(PetscDrawAxisSetLimits(axis_ef, 0., user->steps * user->stepSize, -10., 0.));
}
if (user->initial_monitor) {
PetscDrawAxis axis1, axis2, axis3;
PetscReal dmboxlower[2], dmboxupper[2];
PetscInt dim, cStart, cEnd;
PetscCall(DMGetDimension(sw, &dim));
PetscCall(DMGetBoundingBox(dm, dmboxlower, dmboxupper));
PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
PetscCall(PetscDrawCreate(PETSC_COMM_WORLD, NULL, "monitor_initial_conditions_x", 0, 300, 400, 300, &user->drawic_x));
PetscCall(PetscDrawSetSave(user->drawic_x, "ex9_ic_x.png"));
PetscCall(PetscDrawSetFromOptions(user->drawic_x));
PetscCall(PetscDrawHGCreate(user->drawic_x, dim, &user->drawhgic_x));
PetscCall(PetscDrawHGGetAxis(user->drawhgic_x, &axis1));
PetscCall(PetscDrawHGSetNumberBins(user->drawhgic_x, cEnd - cStart));
PetscCall(PetscDrawAxisSetLabels(axis1, "Initial X Distribution", "X", "counts"));
PetscCall(PetscDrawAxisSetLimits(axis1, dmboxlower[0], dmboxupper[0], 0, 1500));
PetscCall(PetscDrawCreate(PETSC_COMM_WORLD, NULL, "monitor_initial_conditions_v", 400, 300, 400, 300, &user->drawic_v));
PetscCall(PetscDrawSetSave(user->drawic_v, "ex9_ic_v.png"));
PetscCall(PetscDrawSetFromOptions(user->drawic_v));
PetscCall(PetscDrawHGCreate(user->drawic_v, dim, &user->drawhgic_v));
PetscCall(PetscDrawHGGetAxis(user->drawhgic_v, &axis2));
PetscCall(PetscDrawHGSetNumberBins(user->drawhgic_v, 1000));
PetscCall(PetscDrawAxisSetLabels(axis2, "Initial V_x Distribution", "V", "counts"));
PetscCall(PetscDrawAxisSetLimits(axis2, -1, 1, 0, 1500));
PetscCall(PetscDrawCreate(PETSC_COMM_WORLD, NULL, "monitor_initial_conditions_w", 800, 300, 400, 300, &user->drawic_w));
PetscCall(PetscDrawSetSave(user->drawic_w, "ex9_ic_w.png"));
PetscCall(PetscDrawSetFromOptions(user->drawic_w));
PetscCall(PetscDrawHGCreate(user->drawic_w, dim, &user->drawhgic_w));
PetscCall(PetscDrawHGGetAxis(user->drawhgic_w, &axis3));
PetscCall(PetscDrawHGSetNumberBins(user->drawhgic_w, 10));
PetscCall(PetscDrawAxisSetLabels(axis3, "Initial W Distribution", "weight", "counts"));
PetscCall(PetscDrawAxisSetLimits(axis3, 0, 0.01, 0, 5000));
}
if (user->monitor_positions) {
PetscDrawAxis axis;
PetscCall(PetscDrawCreate(PETSC_COMM_WORLD, NULL, "position_monitor_species1", 0, 0, 400, 300, &user->positionDraw));
PetscCall(PetscDrawSetFromOptions(user->positionDraw));
PetscCall(PetscDrawSPCreate(user->positionDraw, 10, &user->positionDrawSP));
PetscCall(PetscDrawSPSetDimension(user->positionDrawSP, 1));
PetscCall(PetscDrawSPGetAxis(user->positionDrawSP, &axis));
PetscCall(PetscDrawSPReset(user->positionDrawSP));
PetscCall(PetscDrawAxisSetLabels(axis, "Particles", "x", "v"));
PetscCall(PetscDrawSetSave(user->positionDraw, "ex9_pos.png"));
}
if (user->poisson_monitor) {
PetscDrawAxis axis_E, axis_Rho, axis_Pot;
PetscCall(PetscDrawCreate(PETSC_COMM_WORLD, NULL, "Efield_monitor", 0, 0, 400, 300, &user->EDraw));
PetscCall(PetscDrawSetFromOptions(user->EDraw));
PetscCall(PetscDrawSPCreate(user->EDraw, 10, &user->EDrawSP));
PetscCall(PetscDrawSPSetDimension(user->EDrawSP, 1));
PetscCall(PetscDrawSPGetAxis(user->EDrawSP, &axis_E));
PetscCall(PetscDrawSPReset(user->EDrawSP));
PetscCall(PetscDrawAxisSetLabels(axis_E, "Particles", "x", "E"));
PetscCall(PetscDrawSetSave(user->EDraw, "ex9_E_spatial.png"));
PetscCall(PetscDrawCreate(PETSC_COMM_WORLD, NULL, "rho_monitor", 0, 0, 400, 300, &user->RhoDraw));
PetscCall(PetscDrawSetFromOptions(user->RhoDraw));
PetscCall(PetscDrawSPCreate(user->RhoDraw, 10, &user->RhoDrawSP));
PetscCall(PetscDrawSPSetDimension(user->RhoDrawSP, 1));
PetscCall(PetscDrawSPGetAxis(user->RhoDrawSP, &axis_Rho));
PetscCall(PetscDrawSPReset(user->RhoDrawSP));
PetscCall(PetscDrawAxisSetLabels(axis_Rho, "Particles", "x", "rho"));
PetscCall(PetscDrawSetSave(user->RhoDraw, "ex9_rho_spatial.png"));
PetscCall(PetscDrawCreate(PETSC_COMM_WORLD, NULL, "potential_monitor", 0, 0, 400, 300, &user->PotDraw));
PetscCall(PetscDrawSetFromOptions(user->PotDraw));
PetscCall(PetscDrawSPCreate(user->PotDraw, 10, &user->PotDrawSP));
PetscCall(PetscDrawSPSetDimension(user->PotDrawSP, 1));
PetscCall(PetscDrawSPGetAxis(user->PotDrawSP, &axis_Pot));
PetscCall(PetscDrawSPReset(user->PotDrawSP));
PetscCall(PetscDrawAxisSetLabels(axis_Pot, "Particles", "x", "potential"));
PetscCall(PetscDrawSetSave(user->PotDraw, "ex9_phi_spatial.png"));
}
PetscFunctionReturn(PETSC_SUCCESS);
}
static PetscErrorCode DestroyContext(AppCtx *user)
{
PetscFunctionBeginUser;
PetscCall(PetscDrawLGDestroy(&user->drawlg_ef));
PetscCall(PetscDrawDestroy(&user->drawef));
PetscCall(PetscDrawHGDestroy(&user->drawhgic_x));
PetscCall(PetscDrawDestroy(&user->drawic_x));
PetscCall(PetscDrawHGDestroy(&user->drawhgic_v));
PetscCall(PetscDrawDestroy(&user->drawic_v));
PetscCall(PetscDrawHGDestroy(&user->drawhgic_w));
PetscCall(PetscDrawDestroy(&user->drawic_w));
PetscCall(PetscDrawSPDestroy(&user->positionDrawSP));
PetscCall(PetscDrawDestroy(&user->positionDraw));
PetscCall(PetscDrawSPDestroy(&user->EDrawSP));
PetscCall(PetscDrawDestroy(&user->EDraw));
PetscCall(PetscDrawSPDestroy(&user->RhoDrawSP));
PetscCall(PetscDrawDestroy(&user->RhoDraw));
PetscCall(PetscDrawSPDestroy(&user->PotDrawSP));
PetscCall(PetscDrawDestroy(&user->PotDraw));
PetscCall(PetscBagDestroy(&user->bag));
PetscFunctionReturn(PETSC_SUCCESS);
}
static PetscErrorCode computeParticleMoments(DM sw, PetscReal moments[3], AppCtx *user)
{
DM dm;
const PetscReal *coords;
const PetscScalar *w;
PetscReal mom[3] = {0.0, 0.0, 0.0};
PetscInt cell, cStart, cEnd, dim;
PetscFunctionBeginUser;
PetscCall(DMGetDimension(sw, &dim));
PetscCall(DMSwarmGetCellDM(sw, &dm));
PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
PetscCall(DMSwarmSortGetAccess(sw));
PetscCall(DMSwarmGetField(sw, DMSwarmPICField_coor, NULL, NULL, (void **)&coords));
PetscCall(DMSwarmGetField(sw, "w_q", NULL, NULL, (void **)&w));
for (cell = cStart; cell < cEnd; ++cell) {
PetscInt *pidx;
PetscInt Np, p, d;
PetscCall(DMSwarmSortGetPointsPerCell(sw, cell, &Np, &pidx));
for (p = 0; p < Np; ++p) {
const PetscInt idx = pidx[p];
const PetscReal *c = &coords[idx * dim];
mom[0] += PetscRealPart(w[idx]);
mom[1] += PetscRealPart(w[idx]) * c[0];
for (d = 0; d < dim; ++d) mom[2] += PetscRealPart(w[idx]) * c[d] * c[d];
}
PetscCall(PetscFree(pidx));
}
PetscCall(DMSwarmRestoreField(sw, DMSwarmPICField_coor, NULL, NULL, (void **)&coords));
PetscCall(DMSwarmRestoreField(sw, "w_q", NULL, NULL, (void **)&w));
PetscCall(DMSwarmSortRestoreAccess(sw));
PetscCall(MPIU_Allreduce(mom, moments, 3, MPIU_REAL, MPI_SUM, PetscObjectComm((PetscObject)sw)));
PetscFunctionReturn(PETSC_SUCCESS);
}
static void f0_1(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
{
f0[0] = u[0];
}
static void f0_x(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
{
f0[0] = x[0] * u[0];
}
static void f0_r2(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
{
PetscInt d;
f0[0] = 0.0;
for (d = 0; d < dim; ++d) f0[0] += PetscSqr(x[d]) * u[0];
}
static PetscErrorCode computeFEMMoments(DM dm, Vec u, PetscReal moments[3], AppCtx *user)
{
PetscDS prob;
PetscScalar mom;
PetscInt field = 0;
PetscFunctionBeginUser;
PetscCall(DMGetDS(dm, &prob));
PetscCall(PetscDSSetObjective(prob, field, &f0_1));
PetscCall(DMPlexComputeIntegralFEM(dm, u, &mom, user));
moments[0] = PetscRealPart(mom);
PetscCall(PetscDSSetObjective(prob, field, &f0_x));
PetscCall(DMPlexComputeIntegralFEM(dm, u, &mom, user));
moments[1] = PetscRealPart(mom);
PetscCall(PetscDSSetObjective(prob, field, &f0_r2));
PetscCall(DMPlexComputeIntegralFEM(dm, u, &mom, user));
moments[2] = PetscRealPart(mom);
PetscFunctionReturn(PETSC_SUCCESS);
}
static PetscErrorCode MonitorEField(TS ts, PetscInt step, PetscReal t, Vec U, void *ctx)
{
AppCtx *user = (AppCtx *)ctx;
DM dm, sw;
PetscReal *E;
PetscReal Enorm = 0., lgEnorm, lgEmax, sum = 0., Emax = 0., temp = 0., *weight, chargesum = 0.;
PetscReal *x, *v;
PetscInt *species, dim, p, d, Np, cStart, cEnd;
PetscReal pmoments[3]; /* \int f, \int x f, \int r^2 f */
PetscReal fmoments[3]; /* \int \hat f, \int x \hat f, \int r^2 \hat f */
Vec rho;
PetscFunctionBeginUser;
if (step < 0) PetscFunctionReturn(PETSC_SUCCESS);
PetscCall(TSGetDM(ts, &sw));
PetscCall(DMSwarmGetCellDM(sw, &dm));
PetscCall(DMGetDimension(sw, &dim));
PetscCall(DMSwarmGetLocalSize(sw, &Np));
PetscCall(DMSwarmSortGetAccess(sw));
PetscCall(DMSwarmGetField(sw, DMSwarmPICField_coor, NULL, NULL, (void **)&x));
PetscCall(DMSwarmGetField(sw, "velocity", NULL, NULL, (void **)&v));
PetscCall(DMSwarmGetField(sw, "E_field", NULL, NULL, (void **)&E));
PetscCall(DMSwarmGetField(sw, "species", NULL, NULL, (void **)&species));
PetscCall(DMSwarmGetField(sw, "w_q", NULL, NULL, (void **)&weight));
PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
for (p = 0; p < Np; ++p) {
for (d = 0; d < 1; ++d) {
temp = PetscAbsReal(E[p * dim + d]);
if (temp > Emax) Emax = temp;
}
Enorm += PetscSqrtReal(E[p * dim] * E[p * dim]);
sum += E[p * dim];
chargesum += user->charges[0] * weight[p];
}
lgEnorm = Enorm != 0 ? PetscLog10Real(Enorm) : -16.;
lgEmax = Emax != 0 ? PetscLog10Real(Emax) : -16.;
PetscCall(DMSwarmRestoreField(sw, DMSwarmPICField_coor, NULL, NULL, (void **)&x));
PetscCall(DMSwarmRestoreField(sw, "velocity", NULL, NULL, (void **)&v));
PetscCall(DMSwarmRestoreField(sw, "w_q", NULL, NULL, (void **)&weight));
PetscCall(DMSwarmRestoreField(sw, "E_field", NULL, NULL, (void **)&E));
PetscCall(DMSwarmRestoreField(sw, "species", NULL, NULL, (void **)&species));
Parameter *param;
PetscCall(PetscBagGetData(user->bag, (void **)¶m));
PetscCall(DMSwarmCreateGlobalVectorFromField(sw, "charges", &rho));
if (user->em == EM_PRIMAL) {
PetscCall(computeParticleMoments(sw, pmoments, user));
PetscCall(computeFEMMoments(dm, rho, fmoments, user));
} else if (user->em == EM_MIXED) {
DM potential_dm;
IS potential_IS;
PetscInt fields = 1;
PetscCall(DMCreateSubDM(dm, 1, &fields, &potential_IS, &potential_dm));
PetscCall(computeParticleMoments(sw, pmoments, user));
PetscCall(computeFEMMoments(potential_dm, rho, fmoments, user));
PetscCall(DMDestroy(&potential_dm));
PetscCall(ISDestroy(&potential_IS));
}
PetscCall(DMSwarmDestroyGlobalVectorFromField(sw, "charges", &rho));
PetscCall(PetscPrintf(PETSC_COMM_WORLD, "%f\t%+e\t%e\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\t%f\n", (double)t, (double)sum, (double)Enorm, (double)lgEnorm, (double)Emax, (double)lgEmax, (double)chargesum, (double)pmoments[0], (double)pmoments[1], (double)pmoments[2], (double)fmoments[0], (double)fmoments[1], (double)fmoments[2]));
PetscCall(PetscDrawLGAddPoint(user->drawlg_ef, &t, &lgEmax));
PetscCall(PetscDrawLGDraw(user->drawlg_ef));
PetscCall(PetscDrawSave(user->drawef));
PetscFunctionReturn(PETSC_SUCCESS);
}
PetscErrorCode MonitorInitialConditions(TS ts, PetscInt step, PetscReal t, Vec U, void *ctx)
{
AppCtx *user = (AppCtx *)ctx;
DM dm, sw;
const PetscScalar *u;
PetscReal *weight, *pos, *vel;
PetscInt dim, p, Np, cStart, cEnd;
PetscFunctionBegin;
if (step < 0) PetscFunctionReturn(PETSC_SUCCESS); /* -1 indicates interpolated solution */
PetscCall(TSGetDM(ts, &sw));
PetscCall(DMSwarmGetCellDM(sw, &dm));
PetscCall(DMGetDimension(sw, &dim));
PetscCall(DMSwarmGetLocalSize(sw, &Np));
PetscCall(DMSwarmSortGetAccess(sw));
PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
if (step == 0) {
PetscCall(PetscDrawHGReset(user->drawhgic_x));
PetscCall(PetscDrawHGGetDraw(user->drawhgic_x, &user->drawic_x));
PetscCall(PetscDrawClear(user->drawic_x));
PetscCall(PetscDrawFlush(user->drawic_x));
PetscCall(PetscDrawHGReset(user->drawhgic_v));
PetscCall(PetscDrawHGGetDraw(user->drawhgic_v, &user->drawic_v));
PetscCall(PetscDrawClear(user->drawic_v));
PetscCall(PetscDrawFlush(user->drawic_v));
PetscCall(PetscDrawHGReset(user->drawhgic_w));
PetscCall(PetscDrawHGGetDraw(user->drawhgic_w, &user->drawic_w));
PetscCall(PetscDrawClear(user->drawic_w));
PetscCall(PetscDrawFlush(user->drawic_w));
PetscCall(VecGetArrayRead(U, &u));
PetscCall(DMSwarmGetField(sw, "velocity", NULL, NULL, (void **)&vel));
PetscCall(DMSwarmGetField(sw, "w_q", NULL, NULL, (void **)&weight));
PetscCall(DMSwarmGetField(sw, DMSwarmPICField_coor, NULL, NULL, (void **)&pos));
PetscCall(VecGetLocalSize(U, &Np));
Np /= dim * 2;
for (p = 0; p < Np; ++p) {
PetscCall(PetscDrawHGAddValue(user->drawhgic_x, pos[p * dim]));
PetscCall(PetscDrawHGAddValue(user->drawhgic_v, vel[p * dim]));
PetscCall(PetscDrawHGAddValue(user->drawhgic_w, weight[p]));
}
PetscCall(VecRestoreArrayRead(U, &u));
PetscCall(PetscDrawHGDraw(user->drawhgic_x));
PetscCall(PetscDrawHGSave(user->drawhgic_x));
PetscCall(PetscDrawHGDraw(user->drawhgic_v));
PetscCall(PetscDrawHGSave(user->drawhgic_v));
PetscCall(PetscDrawHGDraw(user->drawhgic_w));
PetscCall(PetscDrawHGSave(user->drawhgic_w));
PetscCall(DMSwarmRestoreField(sw, DMSwarmPICField_coor, NULL, NULL, (void **)&pos));
PetscCall(DMSwarmRestoreField(sw, "velocity", NULL, NULL, (void **)&vel));
PetscCall(DMSwarmRestoreField(sw, "w_q", NULL, NULL, (void **)&weight));
}
PetscFunctionReturn(PETSC_SUCCESS);
}
static PetscErrorCode MonitorPositions_2D(TS ts, PetscInt step, PetscReal t, Vec U, void *ctx)
{
AppCtx *user = (AppCtx *)ctx;
DM dm, sw;
PetscScalar *x, *v, *weight;
PetscReal lower[3], upper[3], speed;
const PetscInt *s;
PetscInt dim, cStart, cEnd, c;
PetscFunctionBeginUser;
if (step > 0 && step % user->ostep == 0) {
PetscCall(TSGetDM(ts, &sw));
PetscCall(DMSwarmGetCellDM(sw, &dm));
PetscCall(DMGetDimension(dm, &dim));
PetscCall(DMGetBoundingBox(dm, lower, upper));
PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
PetscCall(DMSwarmGetField(sw, DMSwarmPICField_coor, NULL, NULL, (void **)&x));
PetscCall(DMSwarmGetField(sw, "velocity", NULL, NULL, (void **)&v));
PetscCall(DMSwarmGetField(sw, "w_q", NULL, NULL, (void **)&weight));
PetscCall(DMSwarmGetField(sw, "species", NULL, NULL, (void **)&s));
PetscCall(DMSwarmSortGetAccess(sw));
PetscCall(PetscDrawSPReset(user->positionDrawSP));
PetscCall(PetscDrawSPSetLimits(user->positionDrawSP, lower[0], upper[0], lower[1], upper[1]));
PetscCall(PetscDrawSPSetLimits(user->positionDrawSP, lower[0], upper[0], -12, 12));
for (c = 0; c < cEnd - cStart; ++c) {
PetscInt *pidx, Npc, q;
PetscCall(DMSwarmSortGetPointsPerCell(sw, c, &Npc, &pidx));
for (q = 0; q < Npc; ++q) {
const PetscInt p = pidx[q];
if (s[p] == 0) {
speed = PetscSqrtReal(PetscSqr(v[p * dim]) + PetscSqr(v[p * dim + 1]));
if (dim == 1 || user->fake_1D) {
PetscCall(PetscDrawSPAddPointColorized(user->positionDrawSP, &x[p * dim], &x[p * dim + 1], &speed));
} else {
PetscCall(PetscDrawSPAddPointColorized(user->positionDrawSP, &x[p * dim], &v[p * dim], &speed));
}
} else if (s[p] == 1) {
PetscCall(PetscDrawSPAddPoint(user->positionDrawSP, &x[p * dim], &v[p * dim]));
}
}
PetscCall(PetscFree(pidx));
}
PetscCall(PetscDrawSPDraw(user->positionDrawSP, PETSC_TRUE));
PetscCall(PetscDrawSave(user->positionDraw));
PetscCall(DMSwarmSortRestoreAccess(sw));
PetscCall(DMSwarmRestoreField(sw, DMSwarmPICField_coor, NULL, NULL, (void **)&x));
PetscCall(DMSwarmRestoreField(sw, "w_q", NULL, NULL, (void **)&weight));
PetscCall(DMSwarmRestoreField(sw, "velocity", NULL, NULL, (void **)&v));
PetscCall(DMSwarmRestoreField(sw, "species", NULL, NULL, (void **)&s));
}
PetscFunctionReturn(PETSC_SUCCESS);
}
static PetscErrorCode MonitorPoisson(TS ts, PetscInt step, PetscReal t, Vec U, void *ctx)
{
AppCtx *user = (AppCtx *)ctx;
DM dm, sw;
PetscScalar *x, *E, *weight, *pot, *charges;
PetscReal lower[3], upper[3], xval;
PetscInt dim, cStart, cEnd, c;
PetscFunctionBeginUser;
if (step > 0 && step % user->ostep == 0) {
PetscCall(TSGetDM(ts, &sw));
PetscCall(DMSwarmGetCellDM(sw, &dm));
PetscCall(DMGetDimension(dm, &dim));
PetscCall(DMGetBoundingBox(dm, lower, upper));
PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
PetscCall(PetscDrawSPReset(user->RhoDrawSP));
PetscCall(PetscDrawSPReset(user->EDrawSP));
PetscCall(PetscDrawSPReset(user->PotDrawSP));
PetscCall(DMSwarmGetField(sw, DMSwarmPICField_coor, NULL, NULL, (void **)&x));
PetscCall(DMSwarmGetField(sw, "E_field", NULL, NULL, (void **)&E));
PetscCall(DMSwarmGetField(sw, "potential", NULL, NULL, (void **)&pot));
PetscCall(DMSwarmGetField(sw, "charges", NULL, NULL, (void **)&charges));
PetscCall(DMSwarmGetField(sw, "w_q", NULL, NULL, (void **)&weight));
PetscCall(DMSwarmSortGetAccess(sw));
for (c = 0; c < cEnd - cStart; ++c) {
PetscReal Esum = 0.0;
PetscInt *pidx, Npc, q;
PetscCall(DMSwarmSortGetPointsPerCell(sw, c, &Npc, &pidx));
for (q = 0; q < Npc; ++q) {
const PetscInt p = pidx[q];
Esum += E[p * dim];
}
xval = (c + 0.5) * ((upper - lower) / (cEnd - cStart));
PetscCall(PetscDrawSPAddPoint(user->EDrawSP, &xval, &Esum));
PetscCall(PetscFree(pidx));
}
for (c = 0; c < (cEnd - cStart); ++c) {
xval = (c + 0.5) * ((upper - lower) / (cEnd - cStart));
PetscCall(PetscDrawSPAddPoint(user->RhoDrawSP, &xval, &charges[c]));
PetscCall(PetscDrawSPAddPoint(user->PotDrawSP, &xval, &pot[c]));
}
PetscCall(PetscDrawSPDraw(user->RhoDrawSP, PETSC_TRUE));
PetscCall(PetscDrawSave(user->RhoDraw));
PetscCall(PetscDrawSPDraw(user->EDrawSP, PETSC_TRUE));
PetscCall(PetscDrawSave(user->EDraw));
PetscCall(PetscDrawSPDraw(user->PotDrawSP, PETSC_TRUE));
PetscCall(PetscDrawSave(user->PotDraw));
PetscCall(DMSwarmSortRestoreAccess(sw));
PetscCall(DMSwarmRestoreField(sw, DMSwarmPICField_coor, NULL, NULL, (void **)&x));
PetscCall(DMSwarmRestoreField(sw, "potential", NULL, NULL, (void **)&pot));
PetscCall(DMSwarmRestoreField(sw, "charges", NULL, NULL, (void **)&charges));
PetscCall(DMSwarmRestoreField(sw, "w_q", NULL, NULL, (void **)&weight));
PetscCall(DMSwarmRestoreField(sw, "E_field", NULL, NULL, (void **)&E));
}
PetscFunctionReturn(PETSC_SUCCESS);
}
static PetscErrorCode SetupParameters(MPI_Comm comm, AppCtx *ctx)
{
PetscBag bag;
Parameter *p;
PetscFunctionBeginUser;
/* setup PETSc parameter bag */
PetscCall(PetscBagGetData(ctx->bag, (void **)&p));
PetscCall(PetscBagSetName(ctx->bag, "par", "Vlasov-Poisson Parameters"));
bag = ctx->bag;
PetscCall(PetscBagRegisterScalar(bag, &p->v0, 1.0, "v0", "Velocity scale, m/s"));
PetscCall(PetscBagRegisterScalar(bag, &p->t0, 1.0, "t0", "Time scale, s"));
PetscCall(PetscBagRegisterScalar(bag, &p->x0, 1.0, "x0", "Space scale, m"));
PetscCall(PetscBagRegisterScalar(bag, &p->v0, 1.0, "phi0", "Potential scale, kg*m^2/A*s^3"));
PetscCall(PetscBagRegisterScalar(bag, &p->q0, 1.0, "q0", "Charge Scale, A*s"));
PetscCall(PetscBagRegisterScalar(bag, &p->m0, 1.0, "m0", "Mass Scale, kg"));
PetscCall(PetscBagRegisterScalar(bag, &p->epsi0, 1.0, "epsi0", "Permittivity of Free Space, kg"));
PetscCall(PetscBagRegisterScalar(bag, &p->kb, 1.0, "kb", "Boltzmann Constant, m^2 kg/s^2 K^1"));
PetscCall(PetscBagRegisterScalar(bag, &p->sigma, 1.0, "sigma", "Charge per unit area, C/m^3"));
PetscCall(PetscBagRegisterScalar(bag, &p->poissonNumber, 1.0, "poissonNumber", "Non-Dimensional Poisson Number"));
PetscCall(PetscBagRegisterScalar(bag, &p->vlasovNumber, 1.0, "vlasovNumber", "Non-Dimensional Vlasov Number"));
PetscCall(PetscBagSetFromOptions(bag));
{
PetscViewer viewer;
PetscViewerFormat format;
PetscBool flg;
PetscCall(PetscOptionsGetViewer(comm, NULL, NULL, "-param_view", &viewer, &format, &flg));
if (flg) {
PetscCall(PetscViewerPushFormat(viewer, format));
PetscCall(PetscBagView(bag, viewer));
PetscCall(PetscViewerFlush(viewer));
PetscCall(PetscViewerPopFormat(viewer));
PetscCall(PetscViewerDestroy(&viewer));
}
}
PetscFunctionReturn(PETSC_SUCCESS);
}
static PetscErrorCode CreateMesh(MPI_Comm comm, AppCtx *user, DM *dm)
{
PetscFunctionBeginUser;
PetscCall(DMCreate(comm, dm));
PetscCall(DMSetType(*dm, DMPLEX));
PetscCall(DMSetFromOptions(*dm));
PetscCall(DMViewFromOptions(*dm, NULL, "-dm_view"));
PetscFunctionReturn(PETSC_SUCCESS);
}
static void ion_f0(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
{
f0[0] = -constants[SIGMA];
}
static void laplacian_f1(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f1[])
{
PetscInt d;
for (d = 0; d < dim; ++d) f1[d] = u_x[d];
}
static void laplacian_g3(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g3[])
{
PetscInt d;
for (d = 0; d < dim; ++d) g3[d * dim + d] = 1.0;
}
static PetscErrorCode zero(PetscInt dim, PetscReal time, const PetscReal x[], PetscInt Nc, PetscScalar *u, void *ctx)
{
*u = 0.0;
return PETSC_SUCCESS;
}
/*
/ I -grad\ / q \ = /0\
\-div 0 / \phi/ \f/
*/
static void f0_q(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
{
for (PetscInt d = 0; d < dim; ++d) f0[d] += u[uOff[0] + d];
}
static void f1_q(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f1[])
{
for (PetscInt d = 0; d < dim; ++d) f1[d * dim + d] = u[uOff[1]];
}
static void f0_phi_backgroundCharge(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar f0[])
{
f0[0] += constants[SIGMA];
for (PetscInt d = 0; d < dim; ++d) f0[0] += u_x[uOff_x[0] + d * dim + d];
}
/* Boundary residual. Dirichlet boundary for u means u_bdy=p*n */
static void g0_qq(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g0[])
{
for (PetscInt d = 0; d < dim; ++d) g0[d * dim + d] = 1.0;
}
static void g2_qphi(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g2[])
{
for (PetscInt d = 0; d < dim; ++d) g2[d * dim + d] = 1.0;
}
static void g1_phiq(PetscInt dim, PetscInt Nf, PetscInt NfAux, const PetscInt uOff[], const PetscInt uOff_x[], const PetscScalar u[], const PetscScalar u_t[], const PetscScalar u_x[], const PetscInt aOff[], const PetscInt aOff_x[], const PetscScalar a[], const PetscScalar a_t[], const PetscScalar a_x[], PetscReal t, PetscReal u_tShift, const PetscReal x[], PetscInt numConstants, const PetscScalar constants[], PetscScalar g1[])
{
for (PetscInt d = 0; d < dim; ++d) g1[d * dim + d] = 1.0;
}
static PetscErrorCode CreateFEM(DM dm, AppCtx *user)
{
PetscFE fephi, feq;
PetscDS ds;
PetscBool simplex;
PetscInt dim;
PetscFunctionBeginUser;
PetscCall(DMGetDimension(dm, &dim));
PetscCall(DMPlexIsSimplex(dm, &simplex));
if (user->em == EM_MIXED) {
DMLabel label;
const PetscInt id = 1;
PetscCall(PetscFECreateDefault(PETSC_COMM_SELF, dim, dim, simplex, "field_", PETSC_DETERMINE, &feq));
PetscCall(PetscObjectSetName((PetscObject)feq, "field"));
PetscCall(PetscFECreateDefault(PETSC_COMM_SELF, dim, 1, simplex, "potential_", PETSC_DETERMINE, &fephi));
PetscCall(PetscObjectSetName((PetscObject)fephi, "potential"));
PetscCall(PetscFECopyQuadrature(feq, fephi));
PetscCall(DMSetField(dm, 0, NULL, (PetscObject)feq));
PetscCall(DMSetField(dm, 1, NULL, (PetscObject)fephi));
PetscCall(DMCreateDS(dm));
PetscCall(PetscFEDestroy(&fephi));
PetscCall(PetscFEDestroy(&feq));
PetscCall(DMGetLabel(dm, "marker", &label));
PetscCall(DMGetDS(dm, &ds));
PetscCall(PetscDSSetResidual(ds, 0, f0_q, f1_q));
PetscCall(PetscDSSetResidual(ds, 1, f0_phi_backgroundCharge, NULL));
PetscCall(PetscDSSetJacobian(ds, 0, 0, g0_qq, NULL, NULL, NULL));
PetscCall(PetscDSSetJacobian(ds, 0, 1, NULL, NULL, g2_qphi, NULL));
PetscCall(PetscDSSetJacobian(ds, 1, 0, NULL, g1_phiq, NULL, NULL));
PetscCall(DMAddBoundary(dm, DM_BC_ESSENTIAL, "wall", label, 1, &id, 0, 0, NULL, (void (*)(void))zero, NULL, NULL, NULL));
} else if (user->em == EM_PRIMAL) {
MatNullSpace nullsp;
PetscCall(PetscFECreateDefault(PETSC_COMM_SELF, dim, 1, simplex, NULL, PETSC_DETERMINE, &fephi));
PetscCall(PetscObjectSetName((PetscObject)fephi, "potential"));
PetscCall(DMSetField(dm, 0, NULL, (PetscObject)fephi));
PetscCall(DMCreateDS(dm));
PetscCall(DMGetDS(dm, &ds));
PetscCall(PetscDSSetResidual(ds, 0, ion_f0, laplacian_f1));
PetscCall(PetscDSSetJacobian(ds, 0, 0, NULL, NULL, NULL, laplacian_g3));
PetscCall(MatNullSpaceCreate(PetscObjectComm((PetscObject)dm), PETSC_TRUE, 0, NULL, &nullsp));
PetscCall(PetscObjectCompose((PetscObject)fephi, "nullspace", (PetscObject)nullsp));
PetscCall(MatNullSpaceDestroy(&nullsp));
PetscCall(PetscFEDestroy(&fephi));
}
PetscFunctionReturn(PETSC_SUCCESS);
}
static PetscErrorCode CreatePoisson(DM dm, AppCtx *user)
{
SNES snes;
Mat J;
MatNullSpace nullSpace;
PetscFunctionBeginUser;
PetscCall(CreateFEM(dm, user));
PetscCall(SNESCreate(PetscObjectComm((PetscObject)dm), &snes));
PetscCall(SNESSetOptionsPrefix(snes, "em_"));
PetscCall(SNESSetDM(snes, dm));
PetscCall(DMPlexSetSNESLocalFEM(dm, user, user, user));
PetscCall(SNESSetFromOptions(snes));
PetscCall(DMCreateMatrix(dm, &J));
PetscCall(MatNullSpaceCreate(PetscObjectComm((PetscObject)dm), PETSC_TRUE, 0, NULL, &nullSpace));
PetscCall(MatSetNullSpace(J, nullSpace));
PetscCall(MatNullSpaceDestroy(&nullSpace));
PetscCall(SNESSetJacobian(snes, J, J, NULL, NULL));
PetscCall(MatDestroy(&J));
user->snes = snes;
PetscFunctionReturn(PETSC_SUCCESS);
}
PetscErrorCode PetscPDFPertubedConstant2D(const PetscReal x[], const PetscReal dummy[], PetscReal p[])
{
p[0] = (1 + 0.01 * PetscCosReal(0.5 * x[0])) / (2 * PETSC_PI);
p[1] = (1 + 0.01 * PetscCosReal(0.5 * x[1])) / (2 * PETSC_PI);
return PETSC_SUCCESS;
}
PetscErrorCode PetscPDFPertubedConstant1D(const PetscReal x[], const PetscReal dummy[], PetscReal p[])
{
p[0] = (1. + 0.01 * PetscCosReal(0.5 * x[0])) / (2 * PETSC_PI);
return PETSC_SUCCESS;
}
PetscErrorCode PetscPDFCosine1D(const PetscReal x[], const PetscReal scale[], PetscReal p[])
{
const PetscReal alpha = scale ? scale[0] : 0.0;
const PetscReal k = scale ? scale[1] : 1.;
p[0] = (1 + alpha * PetscCosReal(k * x[0]));
return PETSC_SUCCESS;
}
PetscErrorCode PetscPDFCosine2D(const PetscReal x[], const PetscReal scale[], PetscReal p[])
{
const PetscReal alpha = scale ? scale[0] : 0.;
const PetscReal k = scale ? scale[0] : 1.;
p[0] = (1 + alpha * PetscCosReal(k * (x[0] + x[1])));
return PETSC_SUCCESS;
}
static PetscErrorCode InitializeParticles_PerturbedWeights(DM sw, AppCtx *user)
{
DM vdm, dm;
PetscScalar *weight;
PetscReal *x, *v, vmin[3], vmax[3], gmin[3], gmax[3], xi0[3];
PetscInt *N, Ns, dim, *cellid, *species, Np, cStart, cEnd, Npc, n;
PetscInt p, q, s, c, d, cv;
PetscBool flg;
PetscMPIInt size, rank;
Parameter *param;
PetscFunctionBegin;
PetscCallMPI(MPI_Comm_size(PetscObjectComm((PetscObject)sw), &size));
PetscCallMPI(MPI_Comm_rank(PetscObjectComm((PetscObject)sw), &rank));
PetscOptionsBegin(PetscObjectComm((PetscObject)sw), "", "DMSwarm Options", "DMSWARM");
PetscCall(DMSwarmGetNumSpecies(sw, &Ns));
PetscCall(PetscOptionsInt("-dm_swarm_num_species", "The number of species", "DMSwarmSetNumSpecies", Ns, &Ns, &flg));
if (flg) PetscCall(DMSwarmSetNumSpecies(sw, Ns));
PetscCall(PetscCalloc1(Ns, &N));
n = Ns;
PetscCall(PetscOptionsIntArray("-dm_swarm_num_particles", "The target number of particles", "", N, &n, NULL));
PetscOptionsEnd();
Np = N[0];
PetscCall(PetscPrintf(PETSC_COMM_WORLD, "Np = %" PetscInt_FMT "\n", Np));
PetscCall(DMGetDimension(sw, &dim));
PetscCall(DMSwarmGetCellDM(sw, &dm));
PetscCall(DMPlexGetHeightStratum(dm, 0, &cStart, &cEnd));
PetscCall(DMCreate(PETSC_COMM_WORLD, &vdm));
PetscCall(DMSetType(vdm, DMPLEX));
PetscCall(DMPlexSetOptionsPrefix(vdm, "v"));
PetscCall(DMSetFromOptions(vdm));
PetscCall(DMViewFromOptions(vdm, NULL, "-vdm_view"));
PetscCall(DMGetBoundingBox(dm, gmin, gmax));
PetscCall(PetscBagGetData(user->bag, (void **)¶m));
PetscCall(DMSwarmSetLocalSizes(sw, Np, 0));
Npc = Np / (cEnd - cStart);
PetscCall(DMSwarmGetField(sw, DMSwarmPICField_cellid, NULL, NULL, (void **)&cellid));
for (c = 0, p = 0; c < cEnd - cStart; ++c) {
for (s = 0; s < Ns; ++s) {
for (q = 0; q < Npc; ++q, ++p) cellid[p] = c;
}
}
PetscCall(DMSwarmRestoreField(sw, DMSwarmPICField_cellid, NULL, NULL, (void **)&cellid));
PetscCall(PetscFree(N));
PetscCall(DMSwarmGetField(sw, DMSwarmPICField_coor, NULL, NULL, (void **)&x));
PetscCall(DMSwarmGetField(sw, "velocity", NULL, NULL, (void **)&v));
PetscCall(DMSwarmGetField(sw, "w_q", NULL, NULL, (void **)&weight));
PetscCall(DMSwarmGetField(sw, "species", NULL, NULL, (void **)&species));
PetscCall(DMSwarmSortGetAccess(sw));
PetscInt vStart, vEnd;
PetscCall(DMPlexGetHeightStratum(vdm, 0, &vStart, &vEnd));
PetscCall(DMGetBoundingBox(vdm, vmin, vmax));
for (c = 0; c < cEnd - cStart; ++c) {
const PetscInt cell = c + cStart;
PetscInt *pidx, Npc;
PetscReal centroid[3], volume;
PetscCall(DMSwarmSortGetPointsPerCell(sw, c, &Npc, &pidx));
PetscCall(DMPlexComputeCellGeometryFVM(dm, cell, &volume, centroid, NULL));
for (q = 0; q < Npc; ++q) {
const PetscInt p = pidx[q];
for (d = 0; d < dim; ++d) {
x[p * dim + d] = centroid[d];
v[p * dim + d] = vmin[0] + (q + 0.5) * (vmax[0] - vmin[0]) / Npc;
if (user->fake_1D && d > 0) v[p * dim + d] = 0;
}
}
PetscCall(PetscFree(pidx));
}
PetscCall(DMGetCoordinatesLocalSetUp(vdm));
/* Setup Quadrature for spatial and velocity weight calculations*/
PetscQuadrature quad_x;
PetscInt Nq_x;
const PetscReal *wq_x, *xq_x;
PetscReal *xq_x_extended;
PetscReal weightsum = 0., totalcellweight = 0., *weight_x, *weight_v;
PetscReal scale[2] = {user->cosine_coefficients[0], user->cosine_coefficients[1]};
PetscCall(PetscCalloc2(cEnd - cStart, &weight_x, Np, &weight_v));
if (user->fake_1D) PetscCall(PetscDTGaussTensorQuadrature(1, 1, 5, -1.0, 1.0, &quad_x));
else PetscCall(PetscDTGaussTensorQuadrature(dim, 1, 5, -1.0, 1.0, &quad_x));
PetscCall(PetscQuadratureGetData(quad_x, NULL, NULL, &Nq_x, &xq_x, &wq_x));
if (user->fake_1D) {
PetscCall(PetscCalloc1(Nq_x * dim, &xq_x_extended));
for (PetscInt i = 0; i < Nq_x; ++i) xq_x_extended[i * dim] = xq_x[i];
}
/* Integrate the density function to get the weights of particles in each cell */
for (d = 0; d < dim; ++d) xi0[d] = -1.0;
for (c = cStart; c < cEnd; ++c) {
PetscReal v0_x[3], J_x[9], invJ_x[9], detJ_x, xr_x[3], den_x;
PetscInt *pidx, Npc, q;
PetscInt Ncx;
const PetscScalar *array_x;
PetscScalar *coords_x = NULL;
PetscBool isDGx;
weight_x[c] = 0.;
PetscCall(DMPlexGetCellCoordinates(dm, c, &isDGx, &Ncx, &array_x, &coords_x));
PetscCall(DMSwarmSortGetPointsPerCell(sw, c, &Npc, &pidx));
PetscCall(DMPlexComputeCellGeometryFEM(dm, c, NULL, v0_x, J_x, invJ_x, &detJ_x));
for (q = 0; q < Nq_x; ++q) {
/*Transform quadrature points from ref space to real space (0,12.5664)*/
if (user->fake_1D) CoordinatesRefToReal(dim, dim, xi0, v0_x, J_x, &xq_x_extended[q * dim], xr_x);
else CoordinatesRefToReal(dim, dim, xi0, v0_x, J_x, &xq_x[q * dim], xr_x);
/*Transform quadrature points from real space to ideal real space (0, 2PI/k)*/
if (user->fake_1D) {
PetscCall(PetscPDFCosine1D(xr_x, scale, &den_x));
detJ_x = J_x[0];
} else PetscCall(PetscPDFCosine2D(xr_x, scale, &den_x));
/*We have to transform the quadrature weights as well*/
weight_x[c] += den_x * (wq_x[q] * detJ_x);
}
PetscCall(PetscPrintf(PETSC_COMM_WORLD, "c:%" PetscInt_FMT " [x_a,x_b] = %1.15f,%1.15f -> cell weight = %1.15f\n", c, (double)PetscRealPart(coords_x[0]), (double)PetscRealPart(coords_x[2]), (double)weight_x[c]));
totalcellweight += weight_x[c];
PetscCheck(Npc / size == vEnd - vStart, PETSC_COMM_SELF, PETSC_ERR_ARG_WRONG, "Number of particles %" PetscInt_FMT " in cell (rank %d/%d) != %" PetscInt_FMT " number of velocity vertices", Npc, rank, size, vEnd - vStart);
/* Set weights to be gaussian in velocity cells (using exact solution) */
for (cv = 0; cv < vEnd - vStart; ++cv) {
PetscInt Nc;
const PetscScalar *array_v;
PetscScalar *coords_v = NULL;
PetscBool isDG;
PetscCall(DMPlexGetCellCoordinates(vdm, cv, &isDG, &Nc, &array_v, &coords_v));
const PetscInt p = pidx[cv];
weight_v[p] = 0.5 * (PetscErfReal(coords_v[1] / PetscSqrtReal(2.)) - PetscErfReal(coords_v[0] / PetscSqrtReal(2.)));
weight[p] = user->totalWeight * weight_v[p] * weight_x[c];
weightsum += weight[p];
PetscCall(DMPlexRestoreCellCoordinates(vdm, cv, &isDG, &Nc, &array_v, &coords_v));
}
PetscCall(DMPlexRestoreCellCoordinates(dm, c, &isDGx, &Ncx, &array_x, &coords_x));
PetscCall(PetscFree(pidx));
}
PetscCall(PetscPrintf(PETSC_COMM_WORLD, "particle weight sum = %1.10f cell weight sum = %1.10f\n", (double)totalcellweight, (double)weightsum));
if (user->fake_1D) PetscCall(PetscFree(xq_x_extended));
PetscCall(PetscFree2(weight_x, weight_v));
PetscCall(PetscQuadratureDestroy(&quad_x));
PetscCall(DMSwarmSortRestoreAccess(sw));
PetscCall(DMSwarmRestoreField(sw, DMSwarmPICField_coor, NULL, NULL, (void **)&x));
PetscCall(DMSwarmRestoreField(sw, "w_q", NULL, NULL, (void **)&weight));
PetscCall(DMSwarmRestoreField(sw, "species", NULL, NULL, (void **)&species));
PetscCall(DMSwarmRestoreField(sw, "velocity", NULL, NULL, (void **)&v));
PetscCall(DMDestroy(&vdm));
PetscFunctionReturn(PETSC_SUCCESS);
}
static PetscErrorCode InitializeConstants(DM sw, AppCtx *user)
{
DM dm;
PetscInt *species;
PetscReal *weight, totalCharge = 0., totalWeight = 0., gmin[3], gmax[3];
PetscInt Np, p, dim;
PetscFunctionBegin;
PetscCall(DMSwarmGetCellDM(sw, &dm));
PetscCall(DMGetDimension(sw, &dim));
PetscCall(DMSwarmGetLocalSize(sw, &Np));
PetscCall(DMGetBoundingBox(dm, gmin, gmax));
PetscCall(DMSwarmGetField(sw, "w_q", NULL, NULL, (void **)&weight));
PetscCall(DMSwarmGetField(sw, "species", NULL, NULL, (void **)&species));
for (p = 0; p < Np; ++p) {
totalWeight += weight[p];
totalCharge += user->charges[species[p]] * weight[p];
}