-
Notifications
You must be signed in to change notification settings - Fork 1.1k
/
Copy pathSystemBase.C
1697 lines (1449 loc) · 48.1 KB
/
SystemBase.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://mooseframework.inl.gov
//*
//* 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 "MooseApp.h"
#include "SystemBase.h"
#include "Factory.h"
#include "SubProblem.h"
#include "MooseVariableFE.h"
#include "MooseVariableFV.h"
#include "MooseVariableScalar.h"
#include "MooseVariableConstMonomial.h"
#include "Conversion.h"
#include "Parser.h"
#include "AllLocalDofIndicesThread.h"
#include "MooseTypes.h"
#include "InitialCondition.h"
#include "ScalarInitialCondition.h"
#include "Assembly.h"
#include "MooseMesh.h"
#include "MooseUtils.h"
#include "FVBoundaryCondition.h"
#include "FEProblemBase.h"
#include "TimeIntegrator.h"
#include "libmesh/dof_map.h"
#include "libmesh/string_to_enum.h"
#include "libmesh/fe_interface.h"
using namespace libMesh;
/// Free function used for a libMesh callback
void
extraSendList(std::vector<dof_id_type> & send_list, void * context)
{
SystemBase * sys = static_cast<SystemBase *>(context);
sys->augmentSendList(send_list);
}
/// Free function used for a libMesh callback
void
extraSparsity(SparsityPattern::Graph & sparsity,
std::vector<dof_id_type> & n_nz,
std::vector<dof_id_type> & n_oz,
void * context)
{
SystemBase * sys = static_cast<SystemBase *>(context);
sys->augmentSparsity(sparsity, n_nz, n_oz);
}
SystemBase::SystemBase(SubProblem & subproblem,
FEProblemBase & fe_problem,
const std::string & name,
Moose::VarKindType var_kind)
: libMesh::ParallelObject(subproblem),
ConsoleStreamInterface(subproblem.getMooseApp()),
_subproblem(subproblem),
_fe_problem(fe_problem),
_app(subproblem.getMooseApp()),
_factory(_app.getFactory()),
_mesh(subproblem.mesh()),
_name(name),
_vars(libMesh::n_threads()),
_var_map(),
_max_var_number(0),
_u_dot(nullptr),
_u_dotdot(nullptr),
_u_dot_old(nullptr),
_u_dotdot_old(nullptr),
_saved_old(nullptr),
_saved_older(nullptr),
_saved_dot_old(nullptr),
_saved_dotdot_old(nullptr),
_var_kind(var_kind),
_max_var_n_dofs_per_elem(0),
_max_var_n_dofs_per_node(0),
_automatic_scaling(false),
_verbose(false),
_solution_states_initialized(false)
{
}
MooseVariableFieldBase &
SystemBase::getVariable(THREAD_ID tid, const std::string & var_name) const
{
MooseVariableFieldBase * var =
dynamic_cast<MooseVariableFieldBase *>(_vars[tid].getVariable(var_name));
if (!var)
mooseError("Variable '", var_name, "' does not exist in this system");
return *var;
}
MooseVariableFieldBase &
SystemBase::getVariable(THREAD_ID tid, unsigned int var_number) const
{
if (var_number < _numbered_vars[tid].size())
if (_numbered_vars[tid][var_number])
return *_numbered_vars[tid][var_number];
mooseError("Variable #", Moose::stringify(var_number), " does not exist in this system");
}
template <typename T>
MooseVariableFE<T> &
SystemBase::getFieldVariable(THREAD_ID tid, const std::string & var_name)
{
return *_vars[tid].getFieldVariable<T>(var_name);
}
template <typename T>
MooseVariableField<T> &
SystemBase::getActualFieldVariable(THREAD_ID tid, const std::string & var_name)
{
return *_vars[tid].getActualFieldVariable<T>(var_name);
}
template <typename T>
MooseVariableFV<T> &
SystemBase::getFVVariable(THREAD_ID tid, const std::string & var_name)
{
return *_vars[tid].getFVVariable<T>(var_name);
}
template <typename T>
MooseVariableFE<T> &
SystemBase::getFieldVariable(THREAD_ID tid, unsigned int var_number)
{
return *_vars[tid].getFieldVariable<T>(var_number);
}
template <typename T>
MooseVariableField<T> &
SystemBase::getActualFieldVariable(THREAD_ID tid, unsigned int var_number)
{
return *_vars[tid].getActualFieldVariable<T>(var_number);
}
MooseVariableScalar &
SystemBase::getScalarVariable(THREAD_ID tid, const std::string & var_name) const
{
MooseVariableScalar * var = dynamic_cast<MooseVariableScalar *>(_vars[tid].getVariable(var_name));
if (!var)
mooseError("Scalar variable '" + var_name + "' does not exist in this system");
return *var;
}
MooseVariableScalar &
SystemBase::getScalarVariable(THREAD_ID tid, unsigned int var_number) const
{
MooseVariableScalar * var =
dynamic_cast<MooseVariableScalar *>(_vars[tid].getVariable(var_number));
if (!var)
mooseError("variable #" + Moose::stringify(var_number) + " does not exist in this system");
return *var;
}
const std::set<SubdomainID> *
SystemBase::getVariableBlocks(unsigned int var_number)
{
mooseAssert(_var_map.find(var_number) != _var_map.end(), "Variable does not exist.");
if (_var_map[var_number].empty())
return nullptr;
else
return &_var_map[var_number];
}
void
SystemBase::addVariableToZeroOnResidual(std::string var_name)
{
_vars_to_be_zeroed_on_residual.push_back(var_name);
}
void
SystemBase::addVariableToZeroOnJacobian(std::string var_name)
{
_vars_to_be_zeroed_on_jacobian.push_back(var_name);
}
void
SystemBase::setVariableGlobalDoFs(const std::string & var_name)
{
AllLocalDofIndicesThread aldit(_subproblem, {var_name});
ConstElemRange & elem_range = *_mesh.getActiveLocalElementRange();
Threads::parallel_reduce(elem_range, aldit);
// Gather the dof indices across procs to get all the dof indices for var_name
aldit.dofIndicesSetUnion();
const auto & all_dof_indices = aldit.getDofIndices();
_var_all_dof_indices.assign(all_dof_indices.begin(), all_dof_indices.end());
}
void
SystemBase::zeroVariables(std::vector<std::string> & vars_to_be_zeroed)
{
if (vars_to_be_zeroed.size() > 0)
{
NumericVector<Number> & solution = this->solution();
auto problem = dynamic_cast<FEProblemBase *>(&_subproblem);
if (!problem)
mooseError("System needs to be registered in FEProblemBase for using zeroVariables.");
AllLocalDofIndicesThread aldit(*problem, vars_to_be_zeroed, true);
ConstElemRange & elem_range = *_mesh.getActiveLocalElementRange();
Threads::parallel_reduce(elem_range, aldit);
const auto & dof_indices_to_zero = aldit.getDofIndices();
solution.close();
for (const auto & dof : dof_indices_to_zero)
solution.set(dof, 0);
solution.close();
// Call update to update the current_local_solution for this system
system().update();
}
}
void
SystemBase::zeroVariablesForResidual()
{
zeroVariables(_vars_to_be_zeroed_on_residual);
}
void
SystemBase::zeroVariablesForJacobian()
{
zeroVariables(_vars_to_be_zeroed_on_jacobian);
}
Order
SystemBase::getMinQuadratureOrder()
{
Order order = CONSTANT;
const std::vector<MooseVariableFieldBase *> & vars = _vars[0].fieldVariables();
for (const auto & var : vars)
{
FEType fe_type = var->feType();
if (fe_type.default_quadrature_order() > order)
order = fe_type.default_quadrature_order();
}
return order;
}
void
SystemBase::prepare(THREAD_ID tid)
{
if (_subproblem.hasActiveElementalMooseVariables(tid))
{
const std::set<MooseVariableFieldBase *> & active_elemental_moose_variables =
_subproblem.getActiveElementalMooseVariables(tid);
const std::vector<MooseVariableFieldBase *> & vars = _vars[tid].fieldVariables();
for (const auto & var : vars)
var->clearDofIndices();
for (const auto & var : active_elemental_moose_variables)
if (&(var->sys()) == this)
var->prepare();
}
else
{
const std::vector<MooseVariableFieldBase *> & vars = _vars[tid].fieldVariables();
for (const auto & var : vars)
var->prepare();
}
}
void
SystemBase::prepareFace(THREAD_ID tid, bool resize_data)
{
// We only need to do something if the element prepare was restricted
if (_subproblem.hasActiveElementalMooseVariables(tid))
{
const std::set<MooseVariableFieldBase *> & active_elemental_moose_variables =
_subproblem.getActiveElementalMooseVariables(tid);
std::vector<MooseVariableFieldBase *> newly_prepared_vars;
const std::vector<MooseVariableFieldBase *> & vars = _vars[tid].fieldVariables();
for (const auto & var : vars)
{
mooseAssert(&var->sys() == this,
"I will cry if we store variables in our warehouse that don't belong to us");
// If it wasn't in the active list, we need to prepare it. This has the potential to duplicate
// prepare if we have these conditions:
//
// 1. We have a displaced problem
// 2. We are using AD
// 3. We are not using global AD indexing
//
// But I think I would rather risk duplicate prepare than introduce an additional member set
// variable for tracking prepared variables. Set insertion is slow and some simulations have a
// ton of variables
if (!active_elemental_moose_variables.count(var))
{
var->prepare();
newly_prepared_vars.push_back(var);
}
}
// Make sure to resize the residual and jacobian datastructures for all the new variables
if (resize_data)
for (const auto var_ptr : newly_prepared_vars)
{
_subproblem.assembly(tid, number()).prepareVariable(var_ptr);
if (_subproblem.checkNonlocalCouplingRequirement())
_subproblem.assembly(tid, number()).prepareVariableNonlocal(var_ptr);
}
}
}
void
SystemBase::prepareNeighbor(THREAD_ID tid)
{
const std::vector<MooseVariableFieldBase *> & vars = _vars[tid].fieldVariables();
for (const auto & var : vars)
var->prepareNeighbor();
}
void
SystemBase::prepareLowerD(THREAD_ID tid)
{
const std::vector<MooseVariableFieldBase *> & vars = _vars[tid].fieldVariables();
for (const auto & var : vars)
var->prepareLowerD();
}
void
SystemBase::reinitElem(const Elem * /*elem*/, THREAD_ID tid)
{
if (_subproblem.hasActiveElementalMooseVariables(tid))
{
const std::set<MooseVariableFieldBase *> & active_elemental_moose_variables =
_subproblem.getActiveElementalMooseVariables(tid);
for (const auto & var : active_elemental_moose_variables)
if (&(var->sys()) == this)
var->computeElemValues();
}
else
{
const std::vector<MooseVariableFieldBase *> & vars = _vars[tid].fieldVariables();
for (const auto & var : vars)
var->computeElemValues();
}
}
void
SystemBase::reinitElemFace(const Elem * /*elem*/, unsigned int /*side*/, THREAD_ID tid)
{
const std::vector<MooseVariableFieldBase *> & vars = _vars[tid].fieldVariables();
for (const auto & var : vars)
var->computeElemValuesFace();
}
void
SystemBase::reinitNeighborFace(const Elem * /*elem*/, unsigned int /*side*/, THREAD_ID tid)
{
const std::vector<MooseVariableFieldBase *> & vars = _vars[tid].fieldVariables();
for (const auto & var : vars)
var->computeNeighborValuesFace();
}
void
SystemBase::reinitNeighbor(const Elem * /*elem*/, THREAD_ID tid)
{
const std::vector<MooseVariableFieldBase *> & vars = _vars[tid].fieldVariables();
for (const auto & var : vars)
var->computeNeighborValues();
}
void
SystemBase::reinitLowerD(THREAD_ID tid)
{
const std::vector<MooseVariableFieldBase *> & vars = _vars[tid].fieldVariables();
for (const auto & var : vars)
var->computeLowerDValues();
}
void
SystemBase::reinitNode(const Node * /*node*/, THREAD_ID tid)
{
const std::vector<MooseVariableFieldBase *> & vars = _vars[tid].fieldVariables();
for (const auto & var : vars)
{
var->reinitNode();
if (var->isNodalDefined())
var->computeNodalValues();
}
}
void
SystemBase::reinitNodeFace(const Node * /*node*/, BoundaryID /*bnd_id*/, THREAD_ID tid)
{
const std::vector<MooseVariableFieldBase *> & vars = _vars[tid].fieldVariables();
for (const auto & var : vars)
{
var->reinitNode();
if (var->isNodalDefined())
var->computeNodalValues();
}
}
void
SystemBase::reinitNodes(const std::vector<dof_id_type> & nodes, THREAD_ID tid)
{
const std::vector<MooseVariableFieldBase *> & vars = _vars[tid].fieldVariables();
for (const auto & var : vars)
{
var->reinitNodes(nodes);
var->computeNodalValues();
}
}
void
SystemBase::reinitNodesNeighbor(const std::vector<dof_id_type> & nodes, THREAD_ID tid)
{
const std::vector<MooseVariableFieldBase *> & vars = _vars[tid].fieldVariables();
for (const auto & var : vars)
{
var->reinitNodesNeighbor(nodes);
var->computeNodalNeighborValues();
}
}
void
SystemBase::reinitScalars(THREAD_ID tid, bool reinit_for_derivative_reordering /*=false*/)
{
const std::vector<MooseVariableScalar *> & vars = _vars[tid].scalars();
for (const auto & var : vars)
var->reinit(reinit_for_derivative_reordering);
}
void
SystemBase::augmentSendList(std::vector<dof_id_type> & send_list)
{
std::set<dof_id_type> & ghosted_elems = _subproblem.ghostedElems();
DofMap & dof_map = dofMap();
std::vector<dof_id_type> dof_indices;
System & sys = system();
unsigned int sys_num = sys.number();
unsigned int n_vars = sys.n_vars();
for (const auto & elem_id : ghosted_elems)
{
Elem * elem = _mesh.elemPtr(elem_id);
if (elem->active())
{
dof_map.dof_indices(elem, dof_indices);
// Only need to ghost it if it's actually not on this processor
for (const auto & dof : dof_indices)
if (dof < dof_map.first_dof() || dof >= dof_map.end_dof())
send_list.push_back(dof);
// Now add the DoFs from all of the nodes. This is necessary because of block
// restricted variables. A variable might not live _on_ this element but it
// might live on nodes connected to this element.
for (unsigned int n = 0; n < elem->n_nodes(); n++)
{
Node * node = elem->node_ptr(n);
// Have to get each variable's dofs
for (unsigned int v = 0; v < n_vars; v++)
{
const Variable & var = sys.variable(v);
unsigned int var_num = var.number();
unsigned int n_comp = var.n_components();
// See if this variable has any dofs at this node
if (node->n_dofs(sys_num, var_num) > 0)
{
// Loop over components of the variable
for (unsigned int c = 0; c < n_comp; c++)
send_list.push_back(node->dof_number(sys_num, var_num, c));
}
}
}
}
}
}
/**
* Save the old and older solutions.
*/
void
SystemBase::saveOldSolutions()
{
const auto states =
_solution_states[static_cast<unsigned short>(Moose::SolutionIterationType::Time)].size();
if (states > 1)
{
_saved_solution_states.resize(states);
for (unsigned int i = 1; i <= states - 1; ++i)
if (!_saved_solution_states[i])
_saved_solution_states[i] =
&addVector("save_solution_state_" + std::to_string(i), false, PARALLEL);
for (unsigned int i = 1; i <= states - 1; ++i)
*(_saved_solution_states[i]) = solutionState(i);
}
if (!_saved_dot_old && solutionUDotOld())
_saved_dot_old = &addVector("save_solution_dot_old", false, PARALLEL);
if (!_saved_dotdot_old && solutionUDotDotOld())
_saved_dotdot_old = &addVector("save_solution_dotdot_old", false, PARALLEL);
if (solutionUDotOld())
*_saved_dot_old = *solutionUDotOld();
if (solutionUDotDotOld())
*_saved_dotdot_old = *solutionUDotDotOld();
}
/**
* Restore the old and older solutions when the saved solutions present.
*/
void
SystemBase::restoreOldSolutions()
{
const auto states =
_solution_states[static_cast<unsigned short>(Moose::SolutionIterationType::Time)].size();
if (states > 1)
for (unsigned int i = 1; i <= states - 1; ++i)
if (_saved_solution_states[i])
{
solutionState(i) = *(_saved_solution_states[i]);
removeVector("save_solution_state_" + std::to_string(i));
_saved_solution_states[i] = nullptr;
}
if (_saved_dot_old && solutionUDotOld())
{
*solutionUDotOld() = *_saved_dot_old;
removeVector("save_solution_dot_old");
_saved_dot_old = nullptr;
}
if (_saved_dotdot_old && solutionUDotDotOld())
{
*solutionUDotDotOld() = *_saved_dotdot_old;
removeVector("save_solution_dotdot_old");
_saved_dotdot_old = nullptr;
}
}
SparseMatrix<Number> &
SystemBase::addMatrix(TagID tag)
{
if (!_subproblem.matrixTagExists(tag))
mooseError("Cannot add tagged matrix with TagID ",
tag,
" in system '",
name(),
"' because the tag does not exist in the problem");
if (hasMatrix(tag))
return getMatrix(tag);
const auto matrix_name = _subproblem.matrixTagName(tag);
SparseMatrix<Number> & mat = system().add_matrix(matrix_name);
associateMatrixToTag(mat, tag);
return mat;
}
void
SystemBase::removeMatrix(TagID tag_id)
{
if (!_subproblem.matrixTagExists(tag_id))
mooseError("Cannot remove the matrix with TagID ",
tag_id,
"\nin system '",
name(),
"', because that tag does not exist in the problem");
if (hasMatrix(tag_id))
{
const auto matrix_name = _subproblem.matrixTagName(tag_id);
system().remove_matrix(matrix_name);
_tagged_matrices[tag_id] = nullptr;
}
}
NumericVector<Number> &
SystemBase::addVector(const std::string & vector_name, const bool project, const ParallelType type)
{
if (hasVector(vector_name))
return getVector(vector_name);
NumericVector<Number> & vec = system().add_vector(vector_name, project, type);
return vec;
}
NumericVector<Number> &
SystemBase::addVector(TagID tag, const bool project, const ParallelType type)
{
if (!_subproblem.vectorTagExists(tag))
mooseError("Cannot add tagged vector with TagID ",
tag,
" in system '",
name(),
"' because the tag does not exist in the problem");
if (hasVector(tag))
{
auto & vec = getVector(tag);
if (type != ParallelType::AUTOMATIC && vec.type() != type)
mooseError("Cannot add tagged vector '",
_subproblem.vectorTagName(tag),
"', in system '",
name(),
"' because a vector with the same name was found with a different parallel type");
return vec;
}
const auto vector_name = _subproblem.vectorTagName(tag);
NumericVector<Number> & vec = system().add_vector(vector_name, project, type);
associateVectorToTag(vec, tag);
return vec;
}
void
SystemBase::closeTaggedVector(const TagID tag)
{
if (!_subproblem.vectorTagExists(tag))
mooseError("Cannot close vector with TagID ",
tag,
" in system '",
name(),
"' because that tag does not exist in the problem");
else if (!hasVector(tag))
mooseError("Cannot close vector tag with name '",
_subproblem.vectorTagName(tag),
"' in system '",
name(),
"' because there is no vector associated with that tag");
getVector(tag).close();
}
void
SystemBase::closeTaggedVectors(const std::set<TagID> & tags)
{
for (const auto tag : tags)
closeTaggedVector(tag);
}
void
SystemBase::zeroTaggedVector(const TagID tag)
{
if (!_subproblem.vectorTagExists(tag))
mooseError("Cannot zero vector with TagID ",
tag,
" in system '",
name(),
"' because that tag does not exist in the problem");
else if (!hasVector(tag))
mooseError("Cannot zero vector tag with name '",
_subproblem.vectorTagName(tag),
"' in system '",
name(),
"' because there is no vector associated with that tag");
if (!_subproblem.vectorTagNotZeroed(tag))
getVector(tag).zero();
}
void
SystemBase::zeroTaggedVectors(const std::set<TagID> & tags)
{
for (const auto tag : tags)
zeroTaggedVector(tag);
}
void
SystemBase::removeVector(TagID tag_id)
{
if (!_subproblem.vectorTagExists(tag_id))
mooseError("Cannot remove the vector with TagID ",
tag_id,
"\nin system '",
name(),
"', because that tag does not exist in the problem");
if (hasVector(tag_id))
{
auto vector_name = _subproblem.vectorTagName(tag_id);
system().remove_vector(vector_name);
_tagged_vectors[tag_id] = nullptr;
}
}
void
SystemBase::addVariable(const std::string & var_type,
const std::string & name,
InputParameters & parameters)
{
_numbered_vars.resize(libMesh::n_threads());
auto components = parameters.get<unsigned int>("components");
// Convert the std::vector parameter provided by the user into a std::set for use by libMesh's
// System::add_variable method
std::set<SubdomainID> blocks;
const std::vector<SubdomainName> * block_param =
¶meters.get<std::vector<SubdomainName>>("block");
if (block_param->empty() && _fe_problem.isParamValid("default_block"))
block_param = &_fe_problem.getDefaultBlocks();
for (const auto & subdomain_name : *block_param)
{
SubdomainID blk_id = _mesh.getSubdomainID(subdomain_name);
blocks.insert(blk_id);
}
const auto fe_type =
FEType(Utility::string_to_enum<Order>(parameters.get<MooseEnum>("order")),
Utility::string_to_enum<FEFamily>(parameters.get<MooseEnum>("family")));
const auto fe_field_type = FEInterface::field_type(fe_type);
unsigned int var_num;
if (var_type == "ArrayMooseVariable")
{
if (fe_field_type == TYPE_VECTOR)
mooseError("Vector family type cannot be used in an array variable");
// Build up the variable names
std::vector<std::string> var_names;
for (unsigned int i = 0; i < components; i++)
var_names.push_back(SubProblem::arrayVariableComponent(name, i));
// The number returned by libMesh is the _last_ variable number... we want to hold onto the
// _first_
var_num = system().add_variables(var_names, fe_type, &blocks) - (components - 1);
// Set as array variable
if (parameters.isParamSetByUser("array") && !parameters.get<bool>("array"))
mooseError("Variable '",
name,
"' is an array variable ('components' > 1) but 'array' is set to false.");
parameters.set<bool>("array") = true;
}
else
var_num = system().add_variable(name, fe_type, &blocks);
parameters.set<unsigned int>("_var_num") = var_num;
parameters.set<SystemBase *>("_system_base") = this;
for (THREAD_ID tid = 0; tid < libMesh::n_threads(); tid++)
{
parameters.set<THREAD_ID>("tid") = tid;
std::shared_ptr<MooseVariableBase> var =
_factory.create<MooseVariableBase>(var_type, name, parameters, tid);
_vars[tid].add(name, var);
if (auto fe_var = dynamic_cast<MooseVariableFieldBase *>(var.get()))
{
auto required_size = var_num + components;
if (required_size > _numbered_vars[tid].size())
_numbered_vars[tid].resize(required_size);
for (MooseIndex(components) component = 0; component < components; ++component)
_numbered_vars[tid][var_num + component] = fe_var;
if (auto * const functor = dynamic_cast<Moose::FunctorBase<ADReal> *>(fe_var))
_subproblem.addFunctor(name, *functor, tid);
else if (auto * const functor = dynamic_cast<Moose::FunctorBase<ADRealVectorValue> *>(fe_var))
_subproblem.addFunctor(name, *functor, tid);
else if (auto * const functor = dynamic_cast<Moose::FunctorBase<RealEigenVector> *>(fe_var))
_subproblem.addFunctor(name, *functor, tid);
else
mooseError("This should be a functor");
}
if (var->blockRestricted())
for (const SubdomainID & id : var->blockIDs())
for (MooseIndex(components) component = 0; component < components; ++component)
_var_map[var_num + component].insert(id);
else
for (MooseIndex(components) component = 0; component < components; ++component)
_var_map[var_num + component] = std::set<SubdomainID>();
}
// getMaxVariableNumber is an API method used in Rattlesnake
if (var_num > _max_var_number)
_max_var_number = var_num;
_du_dot_du.resize(var_num + 1);
}
bool
SystemBase::hasVariable(const std::string & var_name) const
{
auto & names = getVariableNames();
if (system().has_variable(var_name))
return system().variable_type(var_name).family != SCALAR;
if (std::find(names.begin(), names.end(), var_name) != names.end())
// array variable
return true;
else
return false;
}
bool
SystemBase::isArrayVariable(const std::string & var_name) const
{
auto & names = getVariableNames();
if (!system().has_variable(var_name) &&
std::find(names.begin(), names.end(), var_name) != names.end())
// array variable
return true;
else
return false;
}
bool
SystemBase::hasScalarVariable(const std::string & var_name) const
{
if (system().has_variable(var_name))
return system().variable_type(var_name).family == SCALAR;
else
return false;
}
bool
SystemBase::isScalarVariable(unsigned int var_num) const
{
return (system().variable(var_num).type().family == SCALAR);
}
unsigned int
SystemBase::nVariables() const
{
unsigned int n = nFieldVariables();
n += _vars[0].scalars().size();
return n;
}
unsigned int
SystemBase::nFieldVariables() const
{
unsigned int n = 0;
for (auto & var : _vars[0].fieldVariables())
n += var->count();
return n;
}
unsigned int
SystemBase::nFVVariables() const
{
unsigned int n = 0;
for (auto & var : _vars[0].fieldVariables())
if (var->isFV())
n += var->count();
return n;
}
/**
* Check if the named vector exists in the system.
*/
bool
SystemBase::hasVector(const std::string & name) const
{
return system().have_vector(name);
}
/**
* Get a raw NumericVector with the given name.
*/
NumericVector<Number> &
SystemBase::getVector(const std::string & name)
{
return system().get_vector(name);
}
const NumericVector<Number> &
SystemBase::getVector(const std::string & name) const
{
return system().get_vector(name);
}
NumericVector<Number> &
SystemBase::getVector(TagID tag)
{
if (!hasVector(tag))
{
if (!_subproblem.vectorTagExists(tag))
mooseError("Cannot retreive vector with tag ", tag, " because that tag does not exist");
else
mooseError("Cannot retreive vector with tag ",
tag,
" in system '",
name(),
"'\nbecause a vector has not been associated with that tag.");
}
return *_tagged_vectors[tag];
}
const NumericVector<Number> &
SystemBase::getVector(TagID tag) const
{
if (!hasVector(tag))
{
if (!_subproblem.vectorTagExists(tag))
mooseError("Cannot retreive vector with tag ", tag, " because that tag does not exist");
else
mooseError("Cannot retreive vector with tag ",
tag,
" in system '",
name(),
"'\nbecause a vector has not been associated with that tag.");
}
return *_tagged_vectors[tag];
}
void
SystemBase::associateVectorToTag(NumericVector<Number> & vec, TagID tag)
{
if (!_subproblem.vectorTagExists(tag))
mooseError("Cannot associate vector to tag ", tag, " because that tag does not exist");
if (_tagged_vectors.size() < tag + 1)
_tagged_vectors.resize(tag + 1);
_tagged_vectors[tag] = &vec;
}
void
SystemBase::disassociateVectorFromTag(NumericVector<Number> & vec, TagID tag)
{
if (!_subproblem.vectorTagExists(tag))
mooseError("Cannot disassociate vector from tag ", tag, " because that tag does not exist");
if (hasVector(tag) && &getVector(tag) != &vec)
mooseError("You can not disassociate a vector from a tag which it was not associated to");
disassociateVectorFromTag(tag);
}
void
SystemBase::disassociateVectorFromTag(TagID tag)
{
if (!_subproblem.vectorTagExists(tag))
mooseError("Cannot disassociate vector from tag ", tag, " because that tag does not exist");
if (_tagged_vectors.size() < tag + 1)
_tagged_vectors.resize(tag + 1);
_tagged_vectors[tag] = nullptr;
}
void
SystemBase::disassociateDefaultVectorTags()
{
const auto tags = defaultVectorTags();
for (const auto tag : tags)
if (_subproblem.vectorTagExists(tag))
disassociateVectorFromTag(tag);
}
SparseMatrix<Number> &
SystemBase::getMatrix(TagID tag)
{
if (!hasMatrix(tag))
{
if (!_subproblem.matrixTagExists(tag))
mooseError("Cannot retreive matrix with tag ", tag, " because that tag does not exist");
else
mooseError("Cannot retreive matrix with tag ",
tag,
" in system '",
name(),
"'\nbecause a matrix has not been associated with that tag.");
}
return *_tagged_matrices[tag];
}
const SparseMatrix<Number> &
SystemBase::getMatrix(TagID tag) const