forked from idaholab/moose
/
SlepcSupport.C
1024 lines (823 loc) · 32.2 KB
/
SlepcSupport.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
//* This file is part of the MOOSE framework
//* https://www.mooseframework.org
//*
//* All rights reserved, see COPYRIGHT for full restrictions
//* https://github.com/idaholab/moose/blob/master/COPYRIGHT
//*
//* Licensed under LGPL 2.1, please see LICENSE for details
//* https://www.gnu.org/licenses/lgpl-2.1.html
#include "libmesh/libmesh_config.h"
#ifdef LIBMESH_HAVE_SLEPC
#include "SlepcSupport.h"
// MOOSE includes
#include "MultiMooseEnum.h"
#include "InputParameters.h"
#include "Conversion.h"
#include "EigenProblem.h"
#include "FEProblemBase.h"
#include "NonlinearEigenSystem.h"
#include "libmesh/petsc_vector.h"
#include "libmesh/petsc_matrix.h"
#include "libmesh/slepc_macro.h"
#include "libmesh/auto_ptr.h"
namespace Moose
{
namespace SlepcSupport
{
const int subspace_factor = 2;
InputParameters
getSlepcValidParams(InputParameters & params)
{
MooseEnum solve_type("POWER ARNOLDI KRYLOVSCHUR JACOBI_DAVIDSON "
"NONLINEAR_POWER NEWTON PJFNK JFNK",
"PJFNK");
params.set<MooseEnum>("solve_type") = solve_type;
params.setDocString("solve_type",
"POWER: Power / Inverse / RQI "
"ARNOLDI: Arnoldi "
"KRYLOVSCHUR: Krylov-Schur "
"JACOBI_DAVIDSON: Jacobi-Davidson "
"NONLINEAR_POWER: Nonlinear Power "
"NEWTON: Newton "
"PJFNK: Preconditioned Jacobian-free Newton-Kyrlov"
"JFNK: Jacobian-free Newton-Kyrlov");
// When the eigenvalue problems is reformed as a coupled nonlinear system,
// we use part of Jacobian as the preconditioning matrix.
// Because the difference between the Jacobian and the preconditioning matrix is not small,
// the linear solver KSP can not reduce the residual much. After several tests,
// we find 1e-2 is a reasonable choice.
params.set<Real>("l_tol") = 1e-2;
return params;
}
InputParameters
getSlepcEigenProblemValidParams()
{
InputParameters params = emptyInputParameters();
// We are solving a Non-Hermitian eigenvalue problem by default
MooseEnum eigen_problem_type("HERMITIAN NON_HERMITIAN GEN_HERMITIAN GEN_NON_HERMITIAN "
"GEN_INDEFINITE POS_GEN_NON_HERMITIAN SLEPC_DEFAULT");
params.addParam<MooseEnum>(
"eigen_problem_type",
eigen_problem_type,
"Type of the eigenvalue problem we are solving "
"HERMITIAN: Hermitian "
"NON_HERMITIAN: Non-Hermitian "
"GEN_HERMITIAN: Generalized Hermitian "
"GEN_NON_HERMITIAN: Generalized Non-Hermitian "
"GEN_INDEFINITE: Generalized indefinite Hermitian "
"POS_GEN_NON_HERMITIAN: Generalized Non-Hermitian with positive (semi-)definite B "
"SLEPC_DEFAULT: Use whatever SLEPC has by default ");
// Which eigenvalues are we interested in
MooseEnum which_eigen_pairs("LARGEST_MAGNITUDE SMALLEST_MAGNITUDE LARGEST_REAL SMALLEST_REAL "
"LARGEST_IMAGINARY SMALLEST_IMAGINARY TARGET_MAGNITUDE TARGET_REAL "
"TARGET_IMAGINARY ALL_EIGENVALUES SLEPC_DEFAULT");
params.addParam<MooseEnum>("which_eigen_pairs",
which_eigen_pairs,
"Which eigenvalue pairs to obtain from the solution "
"LARGEST_MAGNITUDE "
"SMALLEST_MAGNITUDE "
"LARGEST_REAL "
"SMALLEST_REAL "
"LARGEST_IMAGINARY "
"SMALLEST_IMAGINARY "
"TARGET_MAGNITUDE "
"TARGET_REAL "
"TARGET_IMAGINARY "
"ALL_EIGENVALUES "
"SLEPC_DEFAULT ");
params.addParam<unsigned int>("n_eigen_pairs", 1, "The number of eigen pairs");
params.addParam<unsigned int>("n_basis_vectors", 3, "The dimension of eigen subspaces");
params.addParam<Real>("eigen_tol", 1.0e-4, "Relative Tolerance for Eigen Solver");
params.addParam<unsigned int>("eigen_max_its", 10000, "Max Iterations for Eigen Solver");
params.addParam<Real>("l_abs_tol", 1e-50, "Absolute Tolerances for Linear Solver");
params.addParam<unsigned int>("free_power_iterations", 4, "The number of free power iterations");
params.addParam<unsigned int>("extra_power_iterations", 0, "The number of extra free power iterations");
return params;
}
void
setSlepcEigenSolverTolerances(EigenProblem & eigen_problem, const InputParameters & params)
{
Moose::PetscSupport::setSinglePetscOption("-eps_tol", stringify(params.get<Real>("eigen_tol")));
Moose::PetscSupport::setSinglePetscOption("-eps_max_it",
stringify(params.get<unsigned int>("eigen_max_its")));
// if it is a nonlinear eigenvalue solver, we need to set tolerances for nonlinear solver and
// linear solver
if (eigen_problem.isNonlinearEigenvalueSolver())
{
// nonlinear solver tolerances
Moose::PetscSupport::setSinglePetscOption("-eps_power_snes_max_it",
stringify(params.get<unsigned int>("nl_max_its")));
Moose::PetscSupport::setSinglePetscOption("-eps_power_snes_max_funcs",
stringify(params.get<unsigned int>("nl_max_funcs")));
Moose::PetscSupport::setSinglePetscOption("-eps_power_snes_atol",
stringify(params.get<Real>("nl_abs_tol")));
Moose::PetscSupport::setSinglePetscOption("-eps_power_snes_rtol",
stringify(params.get<Real>("nl_rel_tol")));
Moose::PetscSupport::setSinglePetscOption("-eps_power_snes_stol",
stringify(params.get<Real>("nl_rel_step_tol")));
// linear solver
Moose::PetscSupport::setSinglePetscOption("-eps_power_ksp_max_it",
stringify(params.get<unsigned int>("l_max_its")));
Moose::PetscSupport::setSinglePetscOption("-eps_power_ksp_rtol",
stringify(params.get<Real>("l_tol")));
Moose::PetscSupport::setSinglePetscOption("-eps_power_ksp_atol",
stringify(params.get<Real>("l_abs_tol")));
}
else
{ // linear eigenvalue problem
// linear solver
Moose::PetscSupport::setSinglePetscOption("-st_ksp_max_it",
stringify(params.get<unsigned int>("l_max_its")));
Moose::PetscSupport::setSinglePetscOption("-st_ksp_rtol", stringify(params.get<Real>("l_tol")));
Moose::PetscSupport::setSinglePetscOption("-st_ksp_atol",
stringify(params.get<Real>("l_abs_tol")));
}
}
void
storeSlepcEigenProblemOptions(EigenProblem & eigen_problem, const InputParameters & params)
{
const std::string & eigen_problem_type = params.get<MooseEnum>("eigen_problem_type");
if (!eigen_problem_type.empty())
eigen_problem.solverParams()._eigen_problem_type =
Moose::stringToEnum<Moose::EigenProblemType>(eigen_problem_type);
else
mooseError("Have to specify a valid eigen problem type");
const std::string & which_eigen_pairs = params.get<MooseEnum>("which_eigen_pairs");
if (!which_eigen_pairs.empty())
eigen_problem.solverParams()._which_eigen_pairs =
Moose::stringToEnum<Moose::WhichEigenPairs>(which_eigen_pairs);
// Set necessary parametrs used in EigenSystem::solve(),
// i.e. the number of requested eigenpairs nev and the number
// of basis vectors ncv used in the solution algorithm. Note that
// ncv >= nev must hold and ncv >= 2*nev is recommended
unsigned int n_eigen_pairs = params.get<unsigned int>("n_eigen_pairs");
unsigned int n_basis_vectors = params.get<unsigned int>("n_basis_vectors");
eigen_problem.setNEigenPairsRequired(n_eigen_pairs);
eigen_problem.es().parameters.set<unsigned int>("eigenpairs") = n_eigen_pairs;
// If the subspace dimension is too small, we increase it automatically
if (subspace_factor * n_eigen_pairs > n_basis_vectors)
{
n_basis_vectors = subspace_factor * n_eigen_pairs;
mooseWarning("Number of subspaces in Eigensolver is changed by moose because the value you set "
"is too small");
}
eigen_problem.es().parameters.set<unsigned int>("basis vectors") = n_basis_vectors;
// Operators A and B are formed as shell matrices
eigen_problem.solverParams()._eigen_matrix_free = params.get<bool>("matrix_free");
// Preconditioning is formed as a shell matrix
eigen_problem.solverParams()._precond_matrix_free = params.get<bool>("precond_matrix_free");
if (params.get<MooseEnum>("solve_type") == "PJFNK")
{
eigen_problem.solverParams()._eigen_matrix_free = true;
}
if (params.get<MooseEnum>("solve_type") == "JFNK")
{
eigen_problem.solverParams()._eigen_matrix_free = true;
eigen_problem.solverParams()._precond_matrix_free = true;
}
}
void
storeSlepcOptions(FEProblemBase & fe_problem, const InputParameters & params)
{
if (!(dynamic_cast<EigenProblem *>(&fe_problem)))
return;
if (params.isParamValid("solve_type"))
{
fe_problem.solverParams()._eigen_solve_type =
Moose::stringToEnum<Moose::EigenSolveType>(params.get<MooseEnum>("solve_type"));
}
}
void
setEigenProblemOptions(SolverParams & solver_params)
{
switch (solver_params._eigen_problem_type)
{
case Moose::EPT_HERMITIAN:
Moose::PetscSupport::setSinglePetscOption("-eps_hermitian");
break;
case Moose::EPT_NON_HERMITIAN:
Moose::PetscSupport::setSinglePetscOption("-eps_non_hermitian");
break;
case Moose::EPT_GEN_HERMITIAN:
Moose::PetscSupport::setSinglePetscOption("-eps_gen_hermitian");
break;
case Moose::EPT_GEN_INDEFINITE:
Moose::PetscSupport::setSinglePetscOption("-eps_gen_indefinite");
break;
case Moose::EPT_GEN_NON_HERMITIAN:
Moose::PetscSupport::setSinglePetscOption("-eps_gen_non_hermitian");
break;
case Moose::EPT_POS_GEN_NON_HERMITIAN:
Moose::PetscSupport::setSinglePetscOption("-eps_pos_gen_non_hermitian");
break;
case Moose::EPT_SLEPC_DEFAULT:
break;
default:
mooseError("Unknown eigen solver type \n");
}
}
void
setSlepcOutputOptions()
{
Moose::PetscSupport::setSinglePetscOption("-eps_monitor_conv");
Moose::PetscSupport::setSinglePetscOption("-eps_monitor");
}
void
setWhichEigenPairsOptions(SolverParams & solver_params)
{
switch (solver_params._which_eigen_pairs)
{
case Moose::WEP_LARGEST_MAGNITUDE:
Moose::PetscSupport::setSinglePetscOption("-eps_largest_magnitude");
break;
case Moose::WEP_SMALLEST_MAGNITUDE:
Moose::PetscSupport::setSinglePetscOption("-eps_smallest_magnitude");
break;
case Moose::WEP_LARGEST_REAL:
Moose::PetscSupport::setSinglePetscOption("-eps_largest_real");
break;
case Moose::WEP_SMALLEST_REAL:
Moose::PetscSupport::setSinglePetscOption("-eps_smallest_real");
break;
case Moose::WEP_LARGEST_IMAGINARY:
Moose::PetscSupport::setSinglePetscOption("-eps_largest_imaginary");
break;
case Moose::WEP_SMALLEST_IMAGINARY:
Moose::PetscSupport::setSinglePetscOption("-eps_smallest_imaginary");
break;
case Moose::WEP_TARGET_MAGNITUDE:
Moose::PetscSupport::setSinglePetscOption("-eps_target_magnitude");
break;
case Moose::WEP_TARGET_REAL:
Moose::PetscSupport::setSinglePetscOption("-eps_target_real");
break;
case Moose::WEP_TARGET_IMAGINARY:
Moose::PetscSupport::setSinglePetscOption("-eps_target_imaginary");
break;
case Moose::WEP_ALL_EIGENVALUES:
Moose::PetscSupport::setSinglePetscOption("-eps_all");
break;
case Moose::WEP_SLEPC_DEFAULT:
break;
default:
mooseError("Unknown type of WhichEigenPairs \n");
}
}
void setFreeNonlinearPowerIterations(unsigned int free_power_iterations)
{
Moose::PetscSupport::setSinglePetscOption("-eps_power_update", "0");
Moose::PetscSupport::setSinglePetscOption("-eps_power_snes_max_it", "1");
Moose::PetscSupport::setSinglePetscOption("-eps_max_it",stringify(free_power_iterations));
}
void clearFreeNonlinearPowerIterations(const InputParameters & params)
{
Moose::PetscSupport::setSinglePetscOption("-eps_power_update", "1");
Moose::PetscSupport::setSinglePetscOption("-eps_max_it","1");
Moose::PetscSupport::setSinglePetscOption("-eps_power_snes_max_it", stringify(params.get<unsigned int>("nl_max_its")));
}
void
setNewtonPetscOptions(SolverParams & solver_params, const InputParameters & params)
{
#if !SLEPC_VERSION_LESS_THAN(3, 8, 0) || !PETSC_VERSION_RELEASE
// Whether or not we need to involve an initial inverse power
bool initial_power = params.get<bool>("newton_inverse_power");
Moose::PetscSupport::setSinglePetscOption("-eps_type", "power");
Moose::PetscSupport::setSinglePetscOption("-eps_power_nonlinear", "1");
Moose::PetscSupport::setSinglePetscOption("-eps_power_update", "1");
if (initial_power)
{
Moose::PetscSupport::setSinglePetscOption("-init_eps_power_snes_max_it", "1");
Moose::PetscSupport::setSinglePetscOption("-init_eps_power_ksp_rtol", "1e-2");
Moose::PetscSupport::setSinglePetscOption(
"-init_eps_max_it", stringify(params.get<unsigned int>("free_power_iterations")));
}
Moose::PetscSupport::setSinglePetscOption("-eps_target_magnitude", "");
if (solver_params._eigen_matrix_free)
{
Moose::PetscSupport::setSinglePetscOption("-eps_power_snes_mf_operator", "1");
if (initial_power)
Moose::PetscSupport::setSinglePetscOption("-init_eps_power_snes_mf_operator", "1");
}
if (solver_params._customized_pc_for_eigen)
{
Moose::PetscSupport::setSinglePetscOption("-eps_power_pc_type", "moosepc");
if (initial_power)
Moose::PetscSupport::setSinglePetscOption("-init_eps_power_pc_type", "moosepc");
}
#if PETSC_RELEASE_LESS_THAN(3, 13, 0)
Moose::PetscSupport::setSinglePetscOption("-st_type", "sinvert");
if (initial_power)
Moose::PetscSupport::setSinglePetscOption("-init_st_type", "sinvert");
#endif
#else
mooseError("Newton-based eigenvalue solver requires SLEPc 3.7.3 or higher");
#endif
}
void
setNonlinearPowerOptions(SolverParams & solver_params)
{
#if !SLEPC_VERSION_LESS_THAN(3, 8, 0) || !PETSC_VERSION_RELEASE
Moose::PetscSupport::setSinglePetscOption("-eps_type", "power");
Moose::PetscSupport::setSinglePetscOption("-eps_power_nonlinear", "1");
Moose::PetscSupport::setSinglePetscOption("-eps_target_magnitude", "");
if (solver_params._eigen_matrix_free)
Moose::PetscSupport::setSinglePetscOption("-eps_power_snes_mf_operator", "1");
if (solver_params._customized_pc_for_eigen)
Moose::PetscSupport::setSinglePetscOption("-eps_power_pc_type", "moosepc");
#if PETSC_RELEASE_LESS_THAN(3, 13, 0)
Moose::PetscSupport::setSinglePetscOption("-st_type", "sinvert");
#endif
#else
mooseError("Nonlinear Inverse Power requires SLEPc 3.7.3 or higher");
#endif
}
void
setEigenSolverOptions(SolverParams & solver_params, const InputParameters & params)
{
// Avoid unused variable warnings when you have SLEPc but not PETSc-dev.
libmesh_ignore(params);
switch (solver_params._eigen_solve_type)
{
case Moose::EST_POWER:
Moose::PetscSupport::setSinglePetscOption("-eps_type", "power");
break;
case Moose::EST_ARNOLDI:
Moose::PetscSupport::setSinglePetscOption("-eps_type", "arnoldi");
break;
case Moose::EST_KRYLOVSCHUR:
Moose::PetscSupport::setSinglePetscOption("-eps_type", "krylovschur");
break;
case Moose::EST_JACOBI_DAVIDSON:
Moose::PetscSupport::setSinglePetscOption("-eps_type", "jd");
break;
case Moose::EST_NONLINEAR_POWER:
setNonlinearPowerOptions(solver_params);
break;
case Moose::EST_NEWTON:
setNewtonPetscOptions(solver_params, params);
break;
case Moose::EST_PJFNK:
solver_params._eigen_matrix_free = true;
solver_params._customized_pc_for_eigen = false;
setNewtonPetscOptions(solver_params, params);
break;
case Moose::EST_JFNK:
solver_params._eigen_matrix_free = true;
solver_params._customized_pc_for_eigen = true;
setNewtonPetscOptions(solver_params, params);
break;
default:
mooseError("Unknown eigen solver type \n");
}
}
void
slepcSetOptions(EigenProblem & eigen_problem, const InputParameters & params)
{
Moose::PetscSupport::petscSetOptions(eigen_problem);
setEigenSolverOptions(eigen_problem.solverParams(), params);
setEigenProblemOptions(eigen_problem.solverParams());
setWhichEigenPairsOptions(eigen_problem.solverParams());
setSlepcEigenSolverTolerances(eigen_problem, params);
setSlepcOutputOptions();
Moose::PetscSupport::addPetscOptionsFromCommandline();
}
void
moosePetscSNESFormMatrixTag(SNES /*snes*/, Vec x, Mat mat, void * ctx, TagID tag)
{
EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
NonlinearSystemBase & nl = eigen_problem->getNonlinearSystemBase();
System & sys = nl.system();
PetscVector<Number> X_global(x, sys.comm());
PetscVector<Number> & X_sys = *cast_ptr<PetscVector<Number> *>(sys.solution.get());
// Use the system's update() to get a good local version of the
// parallel solution. This operation does not modify the incoming
// "x" vector, it only localizes information from "x" into
// sys.current_local_solution.
X_global.swap(X_sys);
sys.update();
X_global.swap(X_sys);
PetscMatrix<Number> libmesh_mat(mat, sys.comm());
// Set the dof maps
libmesh_mat.attach_dof_map(sys.get_dof_map());
libmesh_mat.zero();
eigen_problem->computeJacobianTag(*sys.current_local_solution.get(), libmesh_mat, tag);
}
void
moosePetscSNESFormMatricesTags(
SNES /*snes*/, Vec x, std::vector<Mat> & mats, void * ctx, const std::set<TagID> & tags)
{
EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
NonlinearSystemBase & nl = eigen_problem->getNonlinearSystemBase();
System & sys = nl.system();
PetscVector<Number> X_global(x, sys.comm());
PetscVector<Number> & X_sys = *cast_ptr<PetscVector<Number> *>(sys.solution.get());
// Use the system's update() to get a good local version of the
// parallel solution. This operation does not modify the incoming
// "x" vector, it only localizes information from "x" into
// sys.current_local_solution.
X_global.swap(X_sys);
sys.update();
X_global.swap(X_sys);
std::vector<std::unique_ptr<SparseMatrix<Number>>> jacobians;
for (auto & mat : mats)
{
jacobians.emplace_back(libmesh_make_unique<PetscMatrix<Number>>(mat, sys.comm()));
jacobians.back()->attach_dof_map(sys.get_dof_map());
jacobians.back()->zero();
}
eigen_problem->computeMatricesTags(*sys.current_local_solution.get(), jacobians, tags);
}
PetscErrorCode
mooseSlepcEigenFormJacobianA(SNES snes, Vec x, Mat jac, Mat pc, void * ctx)
{
PetscBool jisshell, pisshell;
PetscBool jismffd;
PetscErrorCode ierr;
PetscFunctionBegin;
EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
NonlinearEigenSystem & eigen_nl = eigen_problem->getNonlinearEigenSystem();
// If both jacobian and preconditioning are shell matrices,
// and then assemble them and return
ierr = PetscObjectTypeCompare((PetscObject)jac, MATSHELL, &jisshell);
CHKERRQ(ierr);
ierr = PetscObjectTypeCompare((PetscObject)jac, MATMFFD, &jismffd);
CHKERRQ(ierr);
ierr = PetscObjectTypeCompare((PetscObject)pc, MATSHELL, &pisshell);
CHKERRQ(ierr);
if ((jisshell || jismffd) && pisshell)
{
// Just assemble matrices and return
ierr = MatAssemblyBegin(jac, MAT_FINAL_ASSEMBLY);
CHKERRQ(ierr);
ierr = MatAssemblyBegin(pc, MAT_FINAL_ASSEMBLY);
CHKERRQ(ierr);
ierr = MatAssemblyEnd(jac, MAT_FINAL_ASSEMBLY);
CHKERRQ(ierr);
ierr = MatAssemblyEnd(pc, MAT_FINAL_ASSEMBLY);
CHKERRQ(ierr);
PetscFunctionReturn(0);
}
// Jacobian and precond matrix are the same
if (jac == pc)
{
if (!pisshell)
moosePetscSNESFormMatrixTag(snes, x, pc, ctx, eigen_nl.precondMatrixTag());
PetscFunctionReturn(0);
}
else
{
if (!jisshell && !jismffd && !pisshell) // We need to form both Jacobian and precond matrix
{
std::vector<Mat> mats = {jac, pc};
moosePetscSNESFormMatricesTags(
snes, x, mats, ctx, {eigen_nl.nonEigenMatrixTag(), eigen_nl.precondMatrixTag()});
PetscFunctionReturn(0);
}
if (!pisshell) // We need to form only precond matrix
{
moosePetscSNESFormMatrixTag(snes, x, pc, ctx, eigen_nl.precondMatrixTag());
ierr = MatAssemblyBegin(jac, MAT_FINAL_ASSEMBLY);
CHKERRQ(ierr);
ierr = MatAssemblyEnd(jac, MAT_FINAL_ASSEMBLY);
CHKERRQ(ierr);
PetscFunctionReturn(0);
}
if (!jisshell && !jismffd) // We need to form only Jacobian matrix
{
moosePetscSNESFormMatrixTag(snes, x, jac, ctx, eigen_nl.nonEigenMatrixTag());
ierr = MatAssemblyBegin(pc, MAT_FINAL_ASSEMBLY);
CHKERRQ(ierr);
ierr = MatAssemblyEnd(pc, MAT_FINAL_ASSEMBLY);
CHKERRQ(ierr);
PetscFunctionReturn(0);
}
}
PetscFunctionReturn(0);
}
PetscErrorCode
mooseSlepcEigenFormJacobianB(SNES snes, Vec x, Mat jac, Mat pc, void * ctx)
{
PetscBool jshell, pshell;
PetscBool jismffd;
PetscErrorCode ierr;
PetscFunctionBegin;
EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
NonlinearEigenSystem & eigen_nl = eigen_problem->getNonlinearEigenSystem();
// If both jacobian and preconditioning are shell matrices,
// and then assemble them and return
ierr = PetscObjectTypeCompare((PetscObject)jac, MATSHELL, &jshell);
CHKERRQ(ierr);
ierr = PetscObjectTypeCompare((PetscObject)jac, MATMFFD, &jismffd);
CHKERRQ(ierr);
ierr = PetscObjectTypeCompare((PetscObject)pc, MATSHELL, &pshell);
CHKERRQ(ierr);
if ((jshell || jismffd) && pshell)
{
// Just assemble matrices and return
ierr = MatAssemblyBegin(jac, MAT_FINAL_ASSEMBLY);
CHKERRQ(ierr);
ierr = MatAssemblyBegin(pc, MAT_FINAL_ASSEMBLY);
CHKERRQ(ierr);
ierr = MatAssemblyEnd(jac, MAT_FINAL_ASSEMBLY);
CHKERRQ(ierr);
ierr = MatAssemblyEnd(pc, MAT_FINAL_ASSEMBLY);
CHKERRQ(ierr);
PetscFunctionReturn(0);
}
if (jac != pc && (!jshell && !jshell))
SETERRQ(PetscObjectComm((PetscObject)snes),
PETSC_ERR_ARG_INCOMP,
"Jacobian and precond matrices should be the same for eigen kernels \n");
moosePetscSNESFormMatrixTag(snes, x, pc, ctx, eigen_nl.eigenMatrixTag());
if (eigen_problem->negativeSignEigenKernel())
MatScale(pc, -1.);
PetscFunctionReturn(0);
}
void
moosePetscSNESFormFunction(SNES /*snes*/, Vec x, Vec r, void * ctx, TagID tag)
{
EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
NonlinearSystemBase & nl = eigen_problem->getNonlinearSystemBase();
System & sys = nl.system();
PetscVector<Number> X_global(x, sys.comm()), R(r, sys.comm());
PetscVector<Number> & X_sys = *cast_ptr<PetscVector<Number> *>(sys.solution.get());
// Use the system's update() to get a good local version of the
// parallel solution. This operation does not modify the incoming
// "x" vector, it only localizes information from "x" into
// sys.current_local_solution.
X_global.swap(X_sys);
sys.update();
X_global.swap(X_sys);
R.zero();
eigen_problem->computeResidualTag(*sys.current_local_solution.get(), R, tag);
R.close();
}
PetscErrorCode
mooseSlepcEigenFormFunctionA(SNES snes, Vec x, Vec r, void * ctx)
{
PetscFunctionBegin;
EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
NonlinearEigenSystem & eigen_nl = eigen_problem->getNonlinearEigenSystem();
moosePetscSNESFormFunction(snes, x, r, ctx, eigen_nl.nonEigenVectorTag());
PetscFunctionReturn(0);
}
PetscErrorCode
mooseSlepcEigenFormFunctionB(SNES snes, Vec x, Vec r, void * ctx)
{
PetscFunctionBegin;
EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
NonlinearEigenSystem & eigen_nl = eigen_problem->getNonlinearEigenSystem();
moosePetscSNESFormFunction(snes, x, r, ctx, eigen_nl.eigenVectorTag());
if (eigen_problem->negativeSignEigenKernel())
VecScale(r, -1.);
PetscFunctionReturn(0);
}
PetscErrorCode
mooseSlepcEigenFormFunctionAB(SNES /*snes*/, Vec x, Vec Ax, Vec Bx, void * ctx)
{
PetscFunctionBegin;
EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
NonlinearEigenSystem & nl = eigen_problem->getNonlinearEigenSystem();
System & sys = nl.system();
PetscVector<Number> X_global(x, sys.comm()), AX(Ax, sys.comm()), BX(Bx, sys.comm());
// update local solution
X_global.localize(*sys.current_local_solution.get());
PetscVector<Number> & X_sys = *cast_ptr<PetscVector<Number> *>(sys.solution.get());
// Use the system's update() to get a good local version of the
// parallel solution. This operation does not modify the incoming
// "x" vector, it only localizes information from "x" into
// sys.current_local_solution.
X_global.swap(X_sys);
sys.update();
X_global.swap(X_sys);
AX.zero();
BX.zero();
eigen_problem->computeResidualAB(
*sys.current_local_solution.get(), AX, BX, nl.nonEigenVectorTag(), nl.eigenVectorTag());
AX.close();
BX.close();
if (eigen_problem->negativeSignEigenKernel())
VecScale(Bx, -1.);
PetscFunctionReturn(0);
}
void
attachCallbacksToMat(EigenProblem & eigen_problem, Mat mat, bool eigen)
{
PetscObjectComposeFunction((PetscObject)mat,
"formJacobian",
eigen ? Moose::SlepcSupport::mooseSlepcEigenFormJacobianB
: Moose::SlepcSupport::mooseSlepcEigenFormJacobianA);
PetscObjectComposeFunction((PetscObject)mat,
"formFunction",
eigen ? Moose::SlepcSupport::mooseSlepcEigenFormFunctionB
: Moose::SlepcSupport::mooseSlepcEigenFormFunctionA);
PetscObjectComposeFunction(
(PetscObject)mat, "formFunctionAB", Moose::SlepcSupport::mooseSlepcEigenFormFunctionAB);
PetscContainer container;
PetscContainerCreate(eigen_problem.comm().get(), &container);
PetscContainerSetPointer(container, &eigen_problem);
PetscObjectCompose((PetscObject)mat, "formJacobianCtx", nullptr);
PetscObjectCompose((PetscObject)mat, "formJacobianCtx", (PetscObject)container);
PetscObjectCompose((PetscObject)mat, "formFunctionCtx", nullptr);
PetscObjectCompose((PetscObject)mat, "formFunctionCtx", (PetscObject)container);
PetscContainerDestroy(&container);
}
void
mooseMatMult(EigenProblem & eigen_problem, Vec x, Vec r, TagID tag)
{
NonlinearSystemBase & nl = eigen_problem.getNonlinearSystemBase();
System & sys = nl.system();
PetscVector<Number> X_global(x, sys.comm()), R(r, sys.comm());
// update local solution
X_global.localize(*sys.current_local_solution.get());
R.zero();
eigen_problem.computeResidualTag(*sys.current_local_solution.get(), R, tag);
R.close();
}
PetscErrorCode
mooseMatMult_Eigen(Mat mat, Vec x, Vec r)
{
PetscFunctionBegin;
void * ctx = nullptr;
MatShellGetContext(mat, &ctx);
if (!ctx)
mooseError("No context is set for shell matrix ");
EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
NonlinearEigenSystem & eigen_nl = eigen_problem->getNonlinearEigenSystem();
mooseMatMult(*eigen_problem, x, r, eigen_nl.eigenVectorTag());
if (eigen_problem->negativeSignEigenKernel())
VecScale(r, -1.);
PetscFunctionReturn(0);
}
PetscErrorCode
mooseMatMult_NonEigen(Mat mat, Vec x, Vec r)
{
PetscFunctionBegin;
void * ctx = nullptr;
MatShellGetContext(mat, &ctx);
if (!ctx)
mooseError("No context is set for shell matrix ");
EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
NonlinearEigenSystem & eigen_nl = eigen_problem->getNonlinearEigenSystem();
mooseMatMult(*eigen_problem, x, r, eigen_nl.nonEigenVectorTag());
PetscFunctionReturn(0);
}
void
setOperationsForShellMat(EigenProblem & eigen_problem, Mat mat, bool eigen)
{
MatShellSetContext(mat, &eigen_problem);
MatShellSetOperation(mat,
MATOP_MULT,
eigen ? (void (*)(void))mooseMatMult_Eigen
: (void (*)(void))mooseMatMult_NonEigen);
}
PETSC_EXTERN PetscErrorCode
registerPCToPETSc()
{
PetscErrorCode ierr;
PetscFunctionBegin;
ierr = PCRegister("moosepc", PCCreate_MoosePC);
CHKERRQ(ierr);
PetscFunctionReturn(0);
}
PETSC_EXTERN PetscErrorCode
PCCreate_MoosePC(PC pc)
{
PetscFunctionBegin;
pc->ops->view = PCView_MoosePC;
pc->ops->destroy = PCDestroy_MoosePC;
pc->ops->setup = PCSetUp_MoosePC;
pc->ops->apply = PCApply_MoosePC;
PetscFunctionReturn(0);
}
PetscErrorCode PCDestroy_MoosePC(PC /*pc*/)
{
PetscFunctionBegin;
/* We do not need to do anything right now, but later we may have some data we need to free here
*/
PetscFunctionReturn(0);
}
PetscErrorCode
PCView_MoosePC(PC /*pc*/, PetscViewer viewer)
{
PetscErrorCode ierr;
PetscBool iascii;
PetscFunctionBegin;
ierr = PetscObjectTypeCompare((PetscObject)viewer, PETSCVIEWERASCII, &iascii);
CHKERRQ(ierr);
if (iascii)
{
ierr = PetscViewerASCIIPrintf(viewer, " %s\n", "moosepc");
CHKERRQ(ierr);
}
PetscFunctionReturn(0);
}
PetscErrorCode
PCApply_MoosePC(PC pc, Vec x, Vec y)
{
void * ctx;
Mat Amat, Pmat;
PetscContainer container;
PetscErrorCode ierr;
ierr = PCGetOperators(pc, &Amat, &Pmat);
CHKERRQ(ierr);
ierr = PetscObjectQuery((PetscObject)Pmat, "formFunctionCtx", (PetscObject *)&container);
CHKERRQ(ierr);
if (container)
{
ierr = PetscContainerGetPointer(container, &ctx);
CHKERRQ(ierr);
}
else
{
mooseError(" Can not find a context \n");
}
EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
NonlinearEigenSystem & nl_eigen = eigen_problem->getNonlinearEigenSystem();
auto preconditioner = nl_eigen.preconditioner();
if (!preconditioner)
mooseError("There is no moose preconditioner in nonlinear eigen system \n");
PetscVector<Number> x_vec(x, preconditioner->comm());
PetscVector<Number> y_vec(y, preconditioner->comm());
preconditioner->apply(x_vec, y_vec);
return 0;
}
PetscErrorCode
PCSetUp_MoosePC(PC pc)
{
void * ctx;
PetscErrorCode ierr;
Mat Amat, Pmat;
PetscContainer container;
ierr = PCGetOperators(pc, &Amat, &Pmat);
CHKERRQ(ierr);
ierr = PetscObjectQuery((PetscObject)Pmat, "formFunctionCtx", (PetscObject *)&container);
CHKERRQ(ierr);
if (container)
{
ierr = PetscContainerGetPointer(container, &ctx);
CHKERRQ(ierr);
}
else
{
mooseError(" Can not find a context \n");
}
EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
NonlinearEigenSystem & nl_eigen = eigen_problem->getNonlinearEigenSystem();
Preconditioner<Number> * preconditioner = nl_eigen.preconditioner();
if (!preconditioner)
mooseError("There is no moose preconditioner in nonlinear eigen system \n");
if (!preconditioner->initialized())
preconditioner->init();
preconditioner->setup();
return 0;
}
PetscErrorCode
mooseSlepcStoppingTest(EPS eps,PetscInt its,PetscInt max_it,PetscInt nconv,PetscInt nev,EPSConvergedReason *reason,void *ctx)
{
PetscErrorCode ierr;
EigenProblem * eigen_problem = static_cast<EigenProblem *>(ctx);
ierr = EPSStoppingBasic(eps,its,max_it,nconv,nev,reason,NULL);
LIBMESH_CHKERR(ierr);
if (eigen_problem->doInitialFreePowerIteration() && its==max_it && *reason<=0)
{
*reason = EPS_CONVERGED_USER;
eps->nconv = 1;
}
return 0;
}
PetscErrorCode
epsGetSNES(EPS eps, SNES * snes)
{
PetscErrorCode ierr;
PetscBool same, nonlinear;
ierr = PetscObjectTypeCompare((PetscObject)eps, EPSPOWER, &same);
LIBMESH_CHKERR(ierr);
if (!same)
mooseError("It is not eps power, and there is no snes");
ierr = EPSPowerGetNonlinear(eps, &nonlinear);
LIBMESH_CHKERR(ierr);
if (!nonlinear)
mooseError("It is not a nonlinear eigen solver");
ierr = EPSPowerGetSNES(eps, snes);
LIBMESH_CHKERR(ierr);
return 0;
}