-
Notifications
You must be signed in to change notification settings - Fork 0
/
fc2d_hps_interface.cpp
1385 lines (1117 loc) · 53.3 KB
/
fc2d_hps_interface.cpp
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
#include "fc2d_hps_interface.hpp"
/*************************************************/
// fc2d_hps
/*************************************************/
static fc2d_hps_vtable_t s_hps_vt;
fc2d_hps_app_context_t hps_app_context;
fc2d_hps_vtable_t* fc2d_hps_vt() {
FCLAW_ASSERT(s_hps_vt.is_set != 0);
return &s_hps_vt;
}
void fc2d_hps_solver_initialize(fclaw2d_global_t* glob) {
int claw_version = 4; /* solution data is organized as (i,j,m) */
fclaw2d_clawpatch_vtable_initialize(glob, claw_version);
/* Patch : These could be over-written by user specific settings */
fclaw2d_patch_vtable_t* patch_vt = fclaw2d_patch_vt(glob);
patch_vt->rhs = hps_rhs; /* Calls FORTRAN routine */
patch_vt->initialize = hps_rhs; /* Get an initial refinement */
patch_vt->setup = NULL;
/* Tagging functions : Base refinement on the right hand side */
patch_vt->tag4refinement = hps_tag4refinement;
patch_vt->tag4coarsening = hps_tag4coarsening;
/* Clawpatch and ForestClaw : Output functions */
fclaw2d_vtable_t* fclaw_vt = fclaw2d_vt(glob);
fclaw_vt->output_frame = hps_output;
fclaw_vt->hook_regrid = hps_regrid_hook;
/* Elliptic specific functions */
fclaw2d_elliptic_vtable_t *elliptic_vt = fclaw2d_elliptic_vt(glob);
elliptic_vt->setup = hps_setup_solver;
elliptic_vt->factor = hps_factor_solver;
/* Solver doesn't do anything so far */
elliptic_vt->solve = hps_solve;
elliptic_vt->apply_bc = fc2d_hps_physical_bc;
/* BCs : Homogeneous BCs by default */
fc2d_hps_vtable_t* hps_vt = hps_vt_init();
// hps_vt->fort_apply_bc = &FC2D_HPS_FORT_APPLY_BC_DEFAULT;
// hps_vt->fort_eval_bc = &FC2D_HPS_FORT_EVAL_BC_DEFAULT;
/* Diagnostics : Error, conservation */
fclaw2d_clawpatch_vtable_t *clawpatch_vt = fclaw2d_clawpatch_vt(glob);
clawpatch_vt->compute_error = hps_compute_error; /* calls user-defined fortran routine */
/* Conservation check : Compares sum(rhs) with sum of normal fluxes around the boundary
of the solution. (uses divergence theorem) */
clawpatch_vt->conservation_check = hps_conservation_check;
/* These are specialized for the elliptic problem */
fclaw2d_diagnostics_vtable_t *diag_vt = fclaw2d_diagnostics_vt(glob);
diag_vt->patch_init_diagnostics = fc2d_hps_diagnostics_initialize;
diag_vt->patch_reset_diagnostics = fc2d_hps_diagnostics_reset;
diag_vt->patch_compute_diagnostics = fc2d_hps_diagnostics_compute;
diag_vt->patch_gather_diagnostics = fc2d_hps_diagnostics_gather;
diag_vt->patch_finalize_diagnostics = fc2d_hps_diagnostics_finalize;
hps_vt->is_set = 1;
}
void fc2d_hps_setprob(fclaw2d_global_t* glob) {
return;
}
void fc2d_hps_rhs(fclaw2d_global_t* glob, fclaw2d_patch_t *patch, int blockno, int patchno) {
return;
}
static double lambdaGlobal = 0.0;
void fc2d_hps_heat_set_lambda(fclaw2d_global_t* glob, double lambda) {
// EllipticForest::EllipticForestApp& app = EllipticForest::EllipticForestApp::getInstance();
// app.options.setOption("lambda", lambda);
lambdaGlobal = lambda;
return;
}
double fc2d_hps_heat_get_lambda() {
return lambdaGlobal;
}
void hps_setup_solver(fclaw2d_global_t *glob) {
// Get EllipticForest app
EllipticForest::EllipticForestApp& app = EllipticForest::EllipticForestApp::getInstance();
app.logHead("Setting up EllipticForest...");
app.options["homogeneous-rhs"] = false;
app.options["cache-operators"] = false;
// Get glob from user and get options
fclaw2d_clawpatch_options_t *clawpatch_opt = fclaw2d_clawpatch_get_options(glob);
fclaw_options_t* fclaw_opt = fclaw2d_get_options(glob);
fclaw2d_domain_t* domain = glob->domain;
p4est_wrap_t* p4est_wrap = (p4est_wrap_t*) domain->pp;
p4est_t* p4est = p4est_wrap->p4est;
// Create root patch
int nx = clawpatch_opt->mx;
int ny = clawpatch_opt->my;
double x_lower = fclaw_opt->ax;
double x_upper = fclaw_opt->bx;
double y_lower = fclaw_opt->ay;
double y_upper = fclaw_opt->by;
EllipticForest::Petsc::PetscGrid root_grid(nx, ny, x_lower, x_upper, y_lower, y_upper);
EllipticForest::Petsc::PetscPatch root_patch(root_grid);
// Create node factory
// EllipticForest::Petsc::PetscPatchNodeFactory node_factory{};
hps_app_context.node_factory = new EllipticForest::Petsc::PetscPatchNodeFactory{};
auto& node_factory = *hps_app_context.node_factory;
// Create mesh from p4est
// EllipticForest::Mesh<EllipticForest::Petsc::PetscPatch> mesh(MPI_COMM_WORLD, p4est, root_patch, node_factory);
hps_app_context.mesh = new EllipticForest::Mesh<EllipticForest::Petsc::PetscPatch>(MPI_COMM_WORLD, p4est, root_patch, node_factory);
auto& mesh = *hps_app_context.mesh;
// Create patch solver
double lambda = fc2d_hps_heat_get_lambda();
hps_app_context.solver = new EllipticForest::Petsc::PetscPatchSolver{};
auto& solver = *hps_app_context.solver;
// TODO: Should these be moved to the user/problem stuff?
solver.setAlphaFunction([&](double x, double y){
return 1.0;
});
solver.setBetaFunction([&](double x, double y){
return 1.0;
});
solver.setLambdaFunction([&](double x, double y){
return lambda;
});
// Create new HPS algorithm
// TODO: delete HPS in clean up function (where...?)
hps_app_context.HPS = new HPSAlgorithm(MPI_COMM_WORLD, *hps_app_context.mesh, solver);
auto& HPS = *hps_app_context.HPS;
// Save HPS into ForestClaw glob
// TODO: Should I put this somewhere else?
// glob->user = (HPSAlgorithm*) HPS;
// Call setup stage
// TODO: Does this do anything anymore...?
hps_app_context.HPS->setupStage();
}
void hps_factor_solver(fclaw2d_global_t* glob) {
// HPSAlgorithm* HPS = (HPSAlgorithm*) glob->user;
// HPS->patchSolver = EllipticForest::FISHPACK::FISHPACKFVSolver(fc2d_hps_heat_get_lambda());
auto* HPS = hps_app_context.HPS;
HPS->buildStage();
}
void hps_rhs(fclaw2d_global_t *glob, fclaw2d_patch_t *patch, int blockno, int patchno) {
int mx,my,mbc;
double dx,dy,xlower,ylower;
fclaw2d_clawpatch_grid_data(glob,patch,&mx,&my,&mbc,
&xlower,&ylower,&dx,&dy);
int mfields;
double *rhs;
fclaw2d_clawpatch_rhs_data(glob,patch,&rhs,&mfields);
FCLAW_ASSERT(mfields == 1);
/* Compute right hand side */
fc2d_hps_vtable_t* hps_vt = fc2d_hps_vt();
FCLAW_ASSERT(hps_vt->fort_rhs != NULL); /* Must be initialized */
hps_vt->fort_rhs(&blockno,&mbc,&mx,&my,&mfields,
&xlower,&ylower,&dx,&dy,rhs);
}
void hps_solve(fclaw2d_global_t *glob) {
// Get EllipticForest app
EllipticForest::EllipticForestApp& app = EllipticForest::EllipticForestApp::getInstance();
app.logHead("Beginning HPS solve...");
// Get fc2d_hps virtual table
fc2d_hps_vtable_t* hps_vt = fc2d_hps_vt();
fc2d_hps_options_t* hps_opt = fc2d_hps_get_options(glob);
fclaw2d_global_set_global(glob);
// Get HPS algorithm from glob
// TODO: Should I get this from somewhere else?
// HPSAlgorithm* HPS = (HPSAlgorithm*) glob->user;
auto* HPS = hps_app_context.HPS;
// Update solver with lambda
// TODO: There's gotta be a better way to do this...
// app.log("Setting lambda to: %e", fc2d_hps_heat_get_lambda());
// HPS->patchSolver.setLambdaFunction([&](double x, double y){
// return fc2d_hps_heat_get_lambda();
// });
// Call build stage
// HPS->buildStage();
// Iterate through tree and update leaf IDs on patches
p4est_iterate(
HPS->mesh.quadtree.p4est,
NULL,
HPS,
[](p4est_iter_volume_info_t* info, void* user_data){
auto* HPS = (HPSAlgorithm*) user_data;
auto* quadrant = info->quad;
auto path = EllipticForest::p4est::p4est_quadrant_path(quadrant);
auto& map = HPS->mesh.quadtree.map;
auto& patch = map[path]->data;
patch.leafID = (int) info->quadid;
patch.isLeaf = true;
},
NULL,
NULL
);
// Call upwards stage
switch (hps_opt->rhs_function_type) {
case fc2d_hps_function_interface_types::COPY_FROM_FC:
HPS->upwardsStage([&](EllipticForest::Petsc::PetscPatch& leafPatch){
auto& grid = leafPatch.grid();
auto* clawpatch = &(glob->domain->blocks->patches[leafPatch.leafID]);
int mx, my;
int mbc;
double x_lower, y_lower, dx, dy;
int mfields;
double* rhs;
fclaw2d_clawpatch_grid_data(glob, clawpatch, &mx, &my, &mbc, &x_lower, &y_lower, &dx, &dy);
fclaw2d_clawpatch_rhs_data(glob, clawpatch, &rhs, &mfields);
int nx = mx + 2*mbc;
int ny = mx + 2*mbc;
leafPatch.vectorF() = EllipticForest::Vector<double>(grid.nPointsX() * grid.nPointsY());
for (auto i = 0; i < nx; i++) {
for (auto j = 0; j < ny; j++) {
if (i > mbc-1 && i < nx-mbc && j > mbc-1 && j < ny-mbc) {
int ii = i - mbc;
int jj = j - mbc;
int idx_ef = jj + ii*mx;
int idx_ef_T = ii + jj*my;
int idx_fc = j + i*nx;
int idx_fc_T = i + j*ny;
// q[idx_fc] = patch.vectorU()[idx_ef];
// rhs[idx_fc] = patch.vectorU()[idx_ef];
leafPatch.vectorF()[idx_ef] = rhs[idx_fc];
}
}
}
return;
});
break;
case fc2d_hps_function_interface_types::ANALYTIC:
HPS->upwardsStage([&](double x, double y){
return hps_vt->cb_rhs_analytic(x, y, glob->curr_time);
});
break;
case fc2d_hps_function_interface_types::EXTENDED:
HPS->upwardsStage([&](EllipticForest::Petsc::PetscPatch& leafPatch){
auto* clawpatch = &(glob->domain->blocks->patches[leafPatch.leafID]);
int mbc, meqn, mrhs, maux, mx, my;
double xlower, ylower, dx, dy;
double* q;
double* rhs;
double* aux;
fclaw2d_clawpatch_grid_data(glob, clawpatch, &mx, &my, &mbc, &xlower, &ylower, &dx, &dy);
fclaw2d_clawpatch_soln_data(glob, clawpatch, &q, &meqn);
fclaw2d_clawpatch_rhs_data(glob, clawpatch, &rhs, &mrhs);
fclaw2d_clawpatch_aux_data(glob, clawpatch, &aux, &maux);
hps_vt->cb_rhs_extended(leafPatch, mbc, meqn, mrhs, maux, mx, my, xlower, ylower, dx, dy, q, rhs, aux);
return;
});
break;
default:
// TODO: Better error handling
std::cerr << "Invalid RHS function type." << std::endl;
break;
}
// Call solve stage; provide Dirichlet data via function
// std::vector<int> boundaryTypes = {
// // std::get<int>(app.options["boundary-type-west"]),
// // std::get<int>(app.options["boundary-type-east"]),
// // std::get<int>(app.options["boundary-type-south"]),
// // std::get<int>(app.options["boundary-type-north"])
// 0, 0, 0, 0
// };
std::vector<std::vector<int>> boundary_patch_numbers(4);
switch (hps_opt->bc_function_type) {
case fc2d_hps_function_interface_types::COPY_FROM_FC:
/* code */
break;
case fc2d_hps_function_interface_types::ANALYTIC:
HPS->solveStage([&](int side, double x, double y, double* a, double* b){
double time = glob->curr_time;
if (hps_opt->boundary_condition_types[side] == 0) {
*a = 1.0;
*b = 0.0;
}
else if (hps_opt->boundary_condition_types[side] == 1) {
*a = 0.0;
*b = 1.0;
}
else {
std::cerr << "INVALID BC TYPE" << std::endl;
}
return hps_vt->cb_bc_analytic(side, x, y, time);
});
break;
case fc2d_hps_function_interface_types::EXTENDED:
// Get list of patches that intersect the boundary; stored in boundary_patch_numbers
// Ordering is assured
fclaw2d_global_iterate_patches(
glob,
[](fclaw2d_domain_t* domain, fclaw2d_patch_t* patch, int blockno, int patchno, void* user){
fclaw2d_global_iterate_t *g = (fclaw2d_global_iterate_t *) user;
auto* glob = g->glob;
auto& boundary_patch_numbers = *(std::vector<std::vector<int>>*) g->user;
int intersects_boundary[4];
fclaw2d_physical_get_bc(glob, blockno, patchno, intersects_boundary);
for (int s = 0; s < 4; s++) {
if (intersects_boundary[s]) {
boundary_patch_numbers[s].push_back(patchno);
}
}
return;
},
&boundary_patch_numbers
);
HPS->solveStage([&](EllipticForest::Petsc::PetscPatch& root_patch){
// auto& grid = root_patch.grid();
// root_patch.vectorG() = EllipticForest::Vector<double>(2*grid.nPointsX() + 2*grid.nPointsY(), 0.0);
hps_vt->cb_bc_extended(root_patch, boundary_patch_numbers);
});
// int intersects_bc[4];
// fclaw2d_physical_get_bc(glob,blockno,patchno,intersects_bc);
break;
default:
// TODO: Better error handling
std::cerr << "Invalid BC function type." << std::endl;
break;
}
// if (!hps_opt->use_ext_bc) {
// }
// else {
// // Can I create an interpolation function by iterating through forestclaw patches, then call that interpolation function for the actual solveStage call?
// // Use Numerical Recipies algorithms!
// fc2d_hps_boundary_vectors_t boundary_vectors;
// fclaw2d_global_iterate_patches(
// glob,
// [](fclaw2d_domain_t* domain, fclaw2d_patch_t* patch, int blockno, int patchno, void* user){
// auto* s = (fclaw2d_global_iterate_t*) user;
// auto* boundary_vectors = (fc2d_hps_boundary_vectors_t*) s->user;
// },
// (void*) &boundary_vectors
// );
// HPS->solveStage([&](EllipticForest::Petsc::PetscPatch& rootPatch){
// // Iterate through patches; get boundary intersections; call extended boundary function
// fclaw2d_global_iterate_patches(
// fclaw2d_global_t* glob,
// [](fclaw2d_domain_t* domain, fclaw2d_patch_t* patch, int blockno, int patchno, void* user){
// //
// },
// NULL);
// });
// }
// HPS->solveStage([&](EllipticForest::FISHPACK::FISHPACKPatch& rootPatch){
// fclaw_options_t* fclaw_opt = fclaw2d_get_options(glob);
// fc2d_hps_vtable_t* hps_vt = fc2d_hps_vt();
// EllipticForest::FISHPACK::FISHPACKFVGrid& grid = rootPatch.grid();
// rootPatch.vectorG() = EllipticForest::Vector<double>(2*grid.nPointsX() + 2*grid.nPointsY());
// // std::vector<int> boundaryTypes = {
// // std::get<int>(app.options["boundary-type-west"]),
// // std::get<int>(app.options["boundary-type-east"]),
// // std::get<int>(app.options["boundary-type-south"]),
// // std::get<int>(app.options["boundary-type-north"])
// // };
// // for (auto s = 0; s < 4; s++) {
// // double x, y;
// // int nPointsSide;
// // if (s == 0 || s == 1) nPointsSide = grid.nPointsY();
// // else nPointsSide = grid.nPointsX();
// // switch (s) {
// // case 0:
// // x = grid.xLower();
// // break;
// // case 1:
// // x = grid.xUpper();
// // break;
// // case 2:
// // y = grid.yLower();
// // break;
// // case 3:
// // y = grid.yUpper();
// // break;
// // }
// // EllipticForest::Vector<double> boundaryDataSide(nPointsSide);
// // for (auto i = 0; i < nPointsSide; i++) {
// // if (s == 0 || s == 1) y = grid(1, i);
// // else x = grid(0, i);
// // boundaryDataSide[i] = hps_vt->fort_eval_bc(&boundaryTypes[s], &glob->curr_time, &x, &y);
// // }
// // if (boundaryTypes[s] == 0) {
// // // Dirichlet BC
// // rootPatch.vectorG().setSegment(s*nPointsSide, boundaryDataSide);
// // }
// // else if (boundaryTypes[s] == 1) {
// // // Neumann BC
// // EllipticForest::Matrix<double> T = rootPatch.matrixT()(s*nPointsSide, (s+1)*nPointsSide-1, s*nPointsSide, (s+1)*nPointsSide-1);
// // EllipticForest::Vector<double> g = EllipticForest::solve(T, boundaryDataSide);
// // rootPatch.vectorG().setSegment(s*nPointsSide, g);
// // }
// // }
// EllipticForest::Vector<double> gWest(grid.nPointsY());
// EllipticForest::Vector<double> gEast(grid.nPointsY());
// EllipticForest::Vector<double> gSouth(grid.nPointsX());
// EllipticForest::Vector<double> gNorth(grid.nPointsX());
// int dirichletBC = 1;
// for (auto j = 0; j < grid.nPointsY(); j++) {
// double y = grid(1, j);
// double x_lower = grid.xLower();
// double x_upper = grid.xUpper();
// gWest[j] = hps_vt->fort_eval_bc(&dirichletBC, &glob->curr_time, &x_lower, &y);
// gEast[j] = hps_vt->fort_eval_bc(&dirichletBC, &glob->curr_time, &x_upper, &y);
// }
// for (auto i = 0; i < grid.nPointsX(); i++) {
// double x = grid(0, i);
// double y_lower = grid.yLower();
// double y_upper = grid.yUpper();
// gSouth[i] = hps_vt->fort_eval_bc(&dirichletBC, &glob->curr_time, &x, &y_lower);
// gNorth[i] = hps_vt->fort_eval_bc(&dirichletBC, &glob->curr_time, &x, &y_upper);
// }
// rootPatch.vectorG().setSegment(0*grid.nPointsX(), gWest);
// rootPatch.vectorG().setSegment(1*grid.nPointsX(), gEast);
// rootPatch.vectorG().setSegment(2*grid.nPointsX(), gSouth);
// rootPatch.vectorG().setSegment(3*grid.nPointsX(), gNorth);
// // std::cout << rootPatch.str() << std::endl;
// // std::cout << "g = " << rootPatch.vectorG() << std::endl;
// return;
// });
// Copy data to ForestClaw patch
HPS->mesh.quadtree.traversePreOrder([&](EllipticForest::Petsc::PetscPatch& patch){
if (patch.isLeaf) {
fclaw2d_patch_t* fc_patch = &(glob->domain->blocks->patches[patch.leafID]);
int mbc;
int mx, my;
double x_lower, y_lower, dx, dy;
double* q;
int meqn, mfields;
double* rhs;
fclaw2d_clawpatch_grid_data(glob, fc_patch, &mx, &my, &mbc, &x_lower, &y_lower, &dx, &dy);
fclaw2d_clawpatch_soln_data(glob, fc_patch, &q, &meqn);
fclaw2d_clawpatch_rhs_data(glob, fc_patch, &rhs, &mfields);
EllipticForest::Petsc::PetscGrid& grid = patch.grid();
int nx = mx + 2*mbc;
int ny = mx + 2*mbc;
for (auto i = 0; i < nx; i++) {
for (auto j = 0; j < ny; j++) {
if (i > mbc-1 && i < nx-mbc && j > mbc-1 && j < ny-mbc) {
int ii = i - mbc;
int jj = j - mbc;
int idx_ef = jj + ii*mx;
int idx_ef_T = ii + jj*my;
int idx_fc = j + i*nx;
int idx_fc_T = i + j*ny;
// q[idx_fc] = patch.vectorU()[idx_ef];
rhs[idx_fc] = patch.vectorU()[idx_ef_T];
}
}
}
// std::cout << patch.str() << std::endl;
// std::cout << "g = " << patch.vectorG() << std::endl;
// std::cout << "u = " << patch.vectorU() << std::endl;
// std::cout << "f = " << patch.vectorF() << std::endl;
}
return;
});
return;
}
void hps_output(fclaw2d_global_t *glob, int iframe) {
const fc2d_hps_options_t* hps_opt;
hps_opt = fc2d_hps_get_options(glob);
// if (hps_opt->ascii_out != 0)
// fclaw2d_clawpatch_output_ascii(glob,iframe);
// if (hps_opt->vtk_out != 0)
// fclaw2d_clawpatch_output_vtk(glob,iframe);
fclaw2d_clawpatch_output_vtk(glob, iframe);
}
int hps_tag4refinement(fclaw2d_global_t *glob, fclaw2d_patch_t *this_patch, int blockno, int patchno, int initflag) {
const fclaw_options_t *fclaw_opt = fclaw2d_get_options(glob);
int tag_patch;
double refine_threshold;
refine_threshold = fclaw_opt->refine_threshold;
int mx,my,mbc;
double xlower,ylower,dx,dy;
fclaw2d_clawpatch_grid_data(glob,this_patch,&mx,&my,&mbc,
&xlower,&ylower,&dx,&dy);
double *rhs;
int mfields;
fclaw2d_clawpatch_rhs_data(glob,this_patch,&rhs,&mfields);
fclaw2d_clawpatch_vtable_t* clawpatch_vt = fclaw2d_clawpatch_vt(glob);
FCLAW_ASSERT(clawpatch_vt->fort_tag4refinement != NULL);
/* Use default fortran tagging routines. Choose refinement based on criteria
set in configuration files (clawpatch:refinement-criteria) */
tag_patch = 0;
clawpatch_vt->fort_tag4refinement(&mx,&my,&mbc,&mfields,&xlower,&ylower,&dx,&dy,
&blockno, rhs, &refine_threshold,
&initflag, &tag_patch);
return tag_patch;
}
int hps_tag4coarsening(fclaw2d_global_t *glob, fclaw2d_patch_t *fine_patches, int blockno, int patchno, int initflag) {
fclaw2d_patch_t *patch0 = &fine_patches[0];
int mx,my,mbc;
double xlower,ylower,dx,dy;
fclaw2d_clawpatch_grid_data(glob,patch0,&mx,&my,&mbc,
&xlower,&ylower,&dx,&dy);
double *rhs[4];
int mfields;
for (int igrid = 0; igrid < 4; igrid++)
{
fclaw2d_clawpatch_rhs_data(glob,&fine_patches[igrid],&rhs[igrid],&mfields);
}
fclaw2d_clawpatch_vtable_t* clawpatch_vt = fclaw2d_clawpatch_vt(glob);
FCLAW_ASSERT(clawpatch_vt->fort_tag4coarsening != NULL);
const fclaw_options_t *fclaw_opt = fclaw2d_get_options(glob);
double coarsen_threshold = fclaw_opt->coarsen_threshold;
int tag_patch = 0;
clawpatch_vt->fort_tag4coarsening(&mx,&my,&mbc,&mfields,&xlower,&ylower,&dx,&dy,
&blockno, rhs[0],rhs[1],rhs[2],rhs[3],
&coarsen_threshold,&initflag,&tag_patch);
return tag_patch == 1;
}
void hps_compute_error(fclaw2d_global_t *glob, fclaw2d_patch_t *patch, int blockno, int patchno, void *user) {
fc2d_hps_error_info_t* error_data = (fc2d_hps_error_info_t*) user;
fclaw_options_t *fclaw_opt = fclaw2d_get_options(glob);
if (fclaw_opt->compute_error)
{
fclaw2d_clawpatch_vtable_t *clawpatch_vt = fclaw2d_clawpatch_vt(glob);
FCLAW_ASSERT(clawpatch_vt->fort_compute_patch_error != NULL);
int mx, my, mbc;
double xlower,ylower,dx,dy;
fclaw2d_clawpatch_grid_data(glob,patch,&mx,&my,&mbc,&xlower,
&ylower,&dx,&dy);
double *area = fclaw2d_clawpatch_get_area(glob,patch); /* Might be null */
/* Computing solution is stored in the RHS; true solution is stored in soln */
int mfields;
/* Computed solution */
double *rhs;
fclaw2d_clawpatch_rhs_data(glob,patch,&rhs,&mfields);
double *err;
fclaw2d_clawpatch_elliptic_error_data(glob,patch,&err,&mfields);
/* True solution */
double *soln;
fclaw2d_clawpatch_elliptic_soln_data(glob,patch,&soln,&mfields);
double t = glob->curr_time;
clawpatch_vt->fort_compute_patch_error(&blockno, &mx,&my,&mbc,
&mfields,&dx,&dy,
&xlower,&ylower, &t, rhs, err, soln);
/* Accumulate sums and maximums needed to compute error norms */
FCLAW_ASSERT(clawpatch_vt->fort_compute_error_norm != NULL);
clawpatch_vt->fort_compute_error_norm(&blockno, &mx, &my, &mbc, &mfields,
&dx,&dy, area, err,
error_data->local_error);
}
}
void hps_conservation_check(fclaw2d_global_t *glob, fclaw2d_patch_t *patch, int blockno, int patchno, void *user) {
fc2d_hps_error_info_t* error_data = (fc2d_hps_error_info_t*) user;
int mx, my, mbc;
double xlower,ylower,dx,dy;
fclaw2d_clawpatch_grid_data(glob,patch,&mx,&my,&mbc,
&xlower,&ylower,&dx,&dy);
int mfields;
double *rhs; /* Solution is stored in the right hand side */
fclaw2d_clawpatch_rhs_data(glob,patch,&rhs,&mfields);
fclaw2d_clawpatch_vtable_t *clawpatch_vt = fclaw2d_clawpatch_vt(glob);
FCLAW_ASSERT(clawpatch_vt->fort_conservation_check != NULL);
/* Need a better way to determine which diagnostic to do */
double* area = fclaw2d_clawpatch_get_area(glob,patch);
clawpatch_vt->fort_conservation_check(&mx, &my, &mbc, &mfields, &dx,&dy,
area, rhs, error_data->rhs,
error_data->c_kahan);
fc2d_hps_options_t *hps_opt = fc2d_hps_get_options(glob);
int intersects_bc[4];
fclaw2d_physical_get_bc(glob,blockno,patchno,intersects_bc);
double t = glob->curr_time;
int cons_check = 1;
fc2d_hps_vtable_t* hps_vt = fc2d_hps_vt();
FCLAW_ASSERT(hps_vt->fort_apply_bc != NULL);
/* Sum up the normal derivative around the boundary */
hps_vt->fort_apply_bc(&blockno, &mx, &my, &mbc, &mfields,
&xlower, &ylower, &dx,&dy,&t, intersects_bc,
hps_opt->boundary_condition_types,rhs, hps_vt->fort_eval_bc,
&cons_check, error_data->boundary);
}
fc2d_hps_vtable_t* hps_vt_init() {
FCLAW_ASSERT(s_hps_vt.is_set == 0);
return &s_hps_vt;
}
void hps_regrid_hook(fclaw2d_domain_t * old_domain,
fclaw2d_patch_t * old_patch,
fclaw2d_domain_t * new_domain,
fclaw2d_patch_t * new_patch,
fclaw2d_patch_relation_t newsize,
int blockno,
int old_patchno,
int new_patchno,
void *user) {
#if 0 // Commented out because new interface doesn't work with this format...
// Get the EF app, HPS, and quadtree
EllipticForest::EllipticForestApp& app = EllipticForest::EllipticForestApp::getInstance();
// app.log("REGRID HOOK");
// app.log(" OLD_PATCH # = %i", old_patchno);
// app.log(" NEW_PATCH # = %i", new_patchno);
fclaw2d_global_t* glob = fclaw2d_global_get_global();
HPSAlgorithm* HPS = (HPSAlgorithm*) glob->user;
auto& quadtree = HPS->mesh.quadtree;
auto& solver = HPS->patchSolver;
// Get current quadtree node
int target_globalID = 0;
quadtree.traversePreOrder([&](EllipticForest::Quadtree<EllipticForest::FISHPACK::FISHPACKPatch>::QuadtreeNode node){
auto& patch = node.data;
if (newsize == FCLAW2D_PATCH_HALFSIZE) {
if (node.leafID == new_patchno) {
// app.log("\tFOUND NODE");
target_globalID = node.globalID;
return false;
}
}
else if (newsize == FCLAW2D_PATCH_DOUBLESIZE) {
if (node.leafID == new_patchno) {
// app.log("\tFOUND NODE");
target_globalID = node.parentID;
return false;
}
}
return true;
});
auto target_node = quadtree.getNode(target_globalID);
if (newsize == FCLAW2D_PATCH_SAMESIZE) {
// Old patch has same refinement as new patch; copy data
// app.log("\tNO REGRID");
}
else if (newsize == FCLAW2D_PATCH_HALFSIZE) {
// Old patch is more coarse than new patch; interpolate 1 patch to 4
// app.log("\tINTERPOLATE 1 TO 4");
quadtree.refineNode(target_node.globalID, [&](EllipticForest::FISHPACK::FISHPACKPatch& parentPatch){
std::vector<EllipticForest::FISHPACK::FISHPACKPatch> childrenPatches(4);
// Build children grids
auto& parentGrid = parentPatch.grid();
int nx = parentGrid.nPointsX();
int ny = parentGrid.nPointsY();
double xLower = parentGrid.xLower();
double xUpper = parentGrid.xUpper();
double xMid = (xLower + xUpper) / 2.0;
double yLower = parentGrid.yLower();
double yUpper = parentGrid.yUpper();
double yMid = (yLower + yUpper) / 2.0;
EllipticForest::FISHPACK::FISHPACKFVGrid child0Grid(nx, ny, xLower, xMid, yLower, yMid);
EllipticForest::FISHPACK::FISHPACKFVGrid child1Grid(nx, ny, xMid, xUpper, yLower, yMid);
EllipticForest::FISHPACK::FISHPACKFVGrid child2Grid(nx, ny, xLower, xMid, yMid, yUpper);
EllipticForest::FISHPACK::FISHPACKFVGrid child3Grid(nx, ny, xMid, xUpper, yMid, yUpper);
// Build child patches
childrenPatches[0] = EllipticForest::FISHPACK::FISHPACKPatch(child0Grid);
childrenPatches[1] = EllipticForest::FISHPACK::FISHPACKPatch(child1Grid);
childrenPatches[2] = EllipticForest::FISHPACK::FISHPACKPatch(child2Grid);
childrenPatches[3] = EllipticForest::FISHPACK::FISHPACKPatch(child3Grid);
int globalIDCounter = 1;
int leafIDCounter = 0;
for (auto& patch : childrenPatches) {
patch.globalID = parentPatch.globalID + globalIDCounter++; // ??? Maybe...
patch.leafID = parentPatch.leafID + leafIDCounter++; // ??? Maybe...
patch.level = parentPatch.level + 1;
patch.isLeaf = true;
patch.nCoarsens = 0;
}
parentPatch.leafID = -1;
// parentPatch.nCoarsens = 0;
// Compute child data
for (auto& patch : childrenPatches) {
// Compute T
patch.matrixT() = solver.buildD2N(patch.grid());
// Compute f
auto& grid = patch.grid();
fclaw2d_patch_t* fc_patch = &(new_domain->blocks->patches[patch.leafID]);
int mx,my,mbc;
double dx,dy,xlower,ylower;
fclaw2d_clawpatch_grid_data(glob,fc_patch,&mx,&my,&mbc,
&xlower,&ylower,&dx,&dy);
int mfields;
double *rhs;
fclaw2d_clawpatch_rhs_data(glob,fc_patch,&rhs,&mfields);
int meqn;
double *q;
fclaw2d_clawpatch_soln_data(glob,fc_patch,&q,&meqn);
double lambda = fc2d_hps_heat_get_lambda();
// int mfields;
// double* rhs;
// global_heat_rhs(glob, fc_patch, blockno, new_patchno);
// NOTE: Need to call heat_rhs (heat_user.cpp) in order to init this data
// fclaw2d_clawpatch_rhs_data(glob, fc_patch, &rhs, &mfields);
// NOTE: Check if rhs is a null pointer
int nx = mx + 2*mbc;
int ny = mx + 2*mbc;
patch.vectorF() = EllipticForest::Vector<double>(grid.nPointsX() * grid.nPointsY());
for (auto i = 0; i < nx; i++) {
for (auto j = 0; j < ny; j++) {
if (i > mbc-1 && i < nx-mbc && j > mbc-1 && j < ny-mbc) {
int ii = i - mbc;
int jj = j - mbc;
int idx_ef = jj + ii*mx;
int idx_ef_T = ii + jj*my;
int idx_fc = j + i*nx;
int idx_fc_T = i + j*ny;
// rhs[idx_fc] = lambda*q[idx_fc];
// printf("rhs[%i] = %10.4f\n", idx_fc, rhs[idx_fc]);
// patch.vectorF()[idx_ef] = rhs[idx_fc];
patch.vectorF()[idx_ef] = lambda*q[idx_fc];
// q[idx_fc] = patch.vectorU()[idx_ef];
// rhs[idx_fc] = patch.vectorU()[idx_ef];
}
// int idx = j + i*grid.nPointsY();
// int idx_T = i + j*grid.nPointsX();
// patch.vectorF()[idx] = rhs[idx];
}
}
// Compute h
patch.vectorH() = solver.particularNeumannData(patch.grid(), patch.vectorF());
}
// Merge child data to get new parent data; updates parent node with appropiate data
HPS->merge4to1(parentPatch, childrenPatches[0], childrenPatches[1], childrenPatches[2], childrenPatches[3]);
HPS->upwards4to1(parentPatch, childrenPatches[0], childrenPatches[1], childrenPatches[2], childrenPatches[3]);
// HPS->split1to4(parentPatch, childrenPatches[0], childrenPatches[1], childrenPatches[2], childrenPatches[3]);
return childrenPatches;
});
}
else if (newsize == FCLAW2D_PATCH_DOUBLESIZE) {
// Old patch is more fine than new patch; average 4 patches to 1
// app.log("\tAVERAGE 4 TO 1");
quadtree.coarsenNode(target_node.globalID, [&](EllipticForest::FISHPACK::FISHPACKPatch& child0Patch, EllipticForest::FISHPACK::FISHPACKPatch& child1Patch, EllipticForest::FISHPACK::FISHPACKPatch& child2Patch, EllipticForest::FISHPACK::FISHPACKPatch& child3Patch){
EllipticForest::FISHPACK::FISHPACKPatch parentPatch;
// Build parent grid
int nx = child0Patch.grid().nPointsX();
int ny = child0Patch.grid().nPointsY();
double xLower = child0Patch.grid().xLower();
double xUpper = child1Patch.grid().xUpper();
double yLower = child0Patch.grid().yLower();
double yUpper = child2Patch.grid().yUpper();
EllipticForest::FISHPACK::FISHPACKFVGrid parentGrid(nx, ny, xLower, xUpper, yLower, yUpper);
parentPatch.grid() = parentGrid;
parentPatch.globalID = target_node.globalID;
parentPatch.leafID = child0Patch.leafID;
parentPatch.level = target_node.level;
parentPatch.isLeaf = true;
// Compute parent data
// Compute T
parentPatch.matrixT() = solver.buildD2N(parentPatch.grid());
// Compute f
fclaw2d_patch_t* fc_patch = &(new_domain->blocks->patches[new_patchno]);
int mfields;
double* rhs;
fclaw2d_clawpatch_rhs_data(glob, fc_patch, &rhs, &mfields);
parentPatch.vectorF() = EllipticForest::Vector<double>(parentPatch.grid().nPointsX() * parentPatch.grid().nPointsY());
for (auto i = 0; i < parentPatch.grid().nPointsX(); i++) {
for (auto j = 0; j < parentPatch.grid().nPointsY(); j++) {
int idx = j + i*parentPatch.grid().nPointsY();
int idx_T = i + j*parentPatch.grid().nPointsX();
parentPatch.vectorF()[idx] = rhs[idx];
}
}
// Compute h
parentPatch.vectorH() = solver.particularNeumannData(parentPatch.grid(), parentPatch.vectorF());
return parentPatch;
});
}
// For debugging: Check if quadtree is valid
// if (!quadtree.isValid()) {
// std::cerr << "[EllipticForest] Quadtree is invalid." << std::endl;
// exit;
// }
return;
#endif
}
/*************************************************/
// fc2d_hps_physical_bc
/*************************************************/
void fc2d_hps_physical_bc(struct fclaw2d_global *glob) {
fc2d_hps_time_info_t tinfo;
tinfo.t = glob->curr_time;
fclaw2d_global_iterate_patches(glob,
cb_fc2d_hps_physical_bc,
(void *) &tinfo);
}
void cb_fc2d_hps_physical_bc(fclaw2d_domain_t *domain, fclaw2d_patch_t *patch, int blockno, int patchno, void *user) {
fclaw2d_global_iterate_t* s = (fclaw2d_global_iterate_t*) user;
fc2d_hps_time_info_t *tinfo = (fc2d_hps_time_info_t*) s->user;
double t = tinfo->t;
/* Determine which faces are at the physical boundary */
int intersects_bc[4];
fclaw2d_physical_get_bc(s->glob,blockno,patchno,intersects_bc);
int mx, my, mbc;
double xlower, ylower, dx, dy;
fclaw2d_clawpatch_grid_data(s->glob,patch,&mx,&my,&mbc,
&xlower,&ylower,&dx,&dy);
const fc2d_hps_options_t* hps_opt = fc2d_hps_get_options(s->glob);
int mfields;
double *rhs;
fclaw2d_clawpatch_rhs_data(s->glob,patch,&rhs,&mfields);
fc2d_hps_vtable_t* hps_vt = fc2d_hps_vt();
int cons_check = 0;
double flux_sum[4];
hps_vt->fort_apply_bc(&blockno, &mx, &my, &mbc, &mfields,
&xlower, &ylower, &dx,&dy,&t, intersects_bc,
hps_opt->boundary_condition_types,rhs, hps_vt->fort_eval_bc,
&cons_check, flux_sum);
}
void fc2d_hps_physical_get_bc(fclaw2d_global_t *glob, int blockno, int patchno, int *intersects_bdry) {
// const int numfaces = get_faces_per_patch(domain);
int bdry[4];
fclaw2d_patch_boundary_type(glob->domain,blockno,patchno,bdry);
int i;
for(i = 0; i < 4; i++)
{
// Physical boundary conditions
intersects_bdry[i] = bdry[i] == 1;
}
}
/*************************************************/
// fc2d_hps_output_ascii
/*************************************************/
void fc2d_hps_time_header_ascii(fclaw2d_global_t* glob, int iframe) {
const fclaw2d_clawpatch_options_t *clawpatch_opt =
fclaw2d_clawpatch_get_options(glob);
char matname1[20];
sprintf(matname1,"fort.q%04d",iframe);
FILE *f1 = fopen(matname1,"w");
fclose(f1);
char matname2[20];
sprintf(matname2,"fort.t%04d",iframe);
double time = glob->curr_time;
int ngrids = glob->domain->global_num_patches;
int mfields = clawpatch_opt->rhs_fields;
int maux = clawpatch_opt->maux;
int mf;
const fclaw_options_t *fclaw_opt = fclaw2d_get_options(glob);
if (fclaw_opt->compute_error)
mf = mfields + 2; /* Print out error and true solution */
else
mf = mfields; /* Only print out computed solution */
FILE *f2 = fopen(matname2,"w");
fprintf(f2,"%12.6f %23s\n%5d %30s\n%5d %30s\n%5d %30s\n%5d %30s\n",time,"time",
mf,"mfields",ngrids,"ngrids",maux,"num_aux",2,"num_dim");
fclose(f2);
}
void cb_hps_output_ascii(fclaw2d_domain_t * domain, fclaw2d_patch_t * patch, int blockno, int patchno, void *user) {
fclaw2d_global_iterate_t* s = (fclaw2d_global_iterate_t*) user;
fclaw2d_global_t *glob = (fclaw2d_global_t*) s->glob;
int iframe = *((int *) s->user);
/* Get info not readily available to user */
int global_num, local_num;
int level;
fclaw2d_patch_get_info(glob->domain,patch,
blockno,patchno,
&global_num,&local_num, &level);
int mx,my,mbc;