-
Notifications
You must be signed in to change notification settings - Fork 90
/
Copy pathAlbany_ModelEvaluator.cpp
1631 lines (1432 loc) · 60 KB
/
Albany_ModelEvaluator.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
//*****************************************************************//
// Albany 3.0: Copyright 2016 Sandia Corporation //
// This Software is released under the BSD license detailed //
// in the file "license.txt" in the top-level Albany directory //
//*****************************************************************//
#include "Albany_ModelEvaluator.hpp"
#include "Albany_ObserverImpl.hpp"
#include "Albany_ThyraUtils.hpp"
#include "Albany_Application.hpp"
#include "Albany_TpetraThyraUtils.hpp"
#include "Albany_Hessian.hpp"
#include "Albany_Utils.hpp"
#include "Albany_StringUtils.hpp"
#include "Teuchos_ScalarTraits.hpp"
#include "Teuchos_TestForException.hpp"
// uncomment the following to write stuff out to matrix market to debug
//#define WRITE_TO_MATRIX_MARKET
#ifdef WRITE_TO_MATRIX_MARKET
static int mm_counter_sol = 0;
static int mm_counter_res = 0;
static int mm_counter_jac = 0;
#endif // WRITE_TO_MATRIX_MARKET
// IK, 4/24/15: adding option to write the mass matrix to matrix market file,
// which is needed
// for some applications. Uncomment the following line to turn on.
//#define WRITE_MASS_MATRIX_TO_MM_FILE
namespace {
void sanitize_nans(const Thyra_Derivative& v)
{
if (!v.isEmpty() && Teuchos::nonnull(v.getMultiVector())) {
v.getMultiVector()->assign(0.0);
}
}
} // namespace
namespace Albany
{
ModelEvaluator::
ModelEvaluator (const Teuchos::RCP<Albany::Application>& app_,
const Teuchos::RCP<Teuchos::ParameterList>& appParams_,
const bool adjoint_model_)
: app(app_)
, appParams(appParams_)
, supplies_prec(app_->suppliesPreconditioner())
, supports_xdot(false)
, supports_xdotdot(false)
, adjoint_model(adjoint_model_)
{
Teuchos::RCP<Teuchos::FancyOStream> out =
Teuchos::VerboseObjectBase::getDefaultOStream();
// Parameters (e.g., for sensitivities, SG expansions, ...)
Teuchos::ParameterList& problemParams = appParams->sublist("Problem");
const Teuchos::ParameterList& parameterParams = problemParams.sublist("Parameters");
const std::string soln_method = problemParams.get("Solution Method", "Steady");
if (soln_method == "Transient") {
use_tempus = true;
}
getParameterSizes(parameterParams, total_num_param_vecs, num_param_vecs, num_dist_param_vecs);
*out << "Total number of parameters = " << total_num_param_vecs << std::endl;
*out << "Number of non-distributed parameters = " << num_param_vecs << std::endl;
int num_response_vecs = app->getNumResponses();
param_names.resize(num_param_vecs);
param_lower_bds.resize(num_param_vecs);
param_upper_bds.resize(num_param_vecs);
for (int l = 0; l < num_param_vecs; ++l) {
const Teuchos::ParameterList& pList = parameterParams.sublist(util::strint("Parameter", l));
const std::string& parameterType = pList.isParameter("Type") ?
pList.get<std::string>("Type") : std::string("Scalar");
if(parameterType == "Scalar") {
param_names[l] =
Teuchos::rcp(new Teuchos::Array<std::string>(1));
(*param_names[l])[0] =
pList.get<std::string>("Name");
*out << "Number of parameters in parameter vector " << l << " = 1" << std::endl;
}
if(parameterType == "Vector") {
const int numParameters = pList.get<int>("Dimension");
TEUCHOS_TEST_FOR_EXCEPTION(
numParameters == 0,
Teuchos::Exceptions::InvalidParameter,
std::endl
<< "Error! In Albany::ModelEvaluator constructor: "
<< "Parameter vector "
<< l
<< " has zero parameters!"
<< std::endl);
param_names[l] =
Teuchos::rcp(new Teuchos::Array<std::string>(numParameters));
for (int k = 0; k < numParameters; ++k) {
(*param_names[l])[k] =
pList.sublist(util::strint("Scalar", k)).get<std::string>("Name");
}
*out << "Number of parameters in parameter vector " << l << " = "
<< numParameters << std::endl;
}
}
*out << "Number of response vectors = " << num_response_vecs << std::endl;
// Setup sacado and thyra storage for parameters
sacado_param_vec.resize(num_param_vecs);
param_vecs.resize(num_param_vecs);
param_vss.resize(num_param_vecs);
thyra_response_vec.resize(num_response_vecs);
Teuchos::RCP<const Teuchos::Comm<int>> commT = app->getComm();
for (int l = 0; l < param_vecs.size(); ++l) {
try {
// Initialize Sacado parameter vector
// The following call will throw, and it is often due to an incorrect
// input line in the "Parameters" PL
// in the input file. Give the user a hint about what might be happening
app->getParamLib()->fillVector<PHAL::AlbanyTraits::Residual>(
*(param_names[l]), sacado_param_vec[l]);
} catch (const std::logic_error& le) {
*out << "Error: exception thrown from ParamLib fillVector in file "
<< __FILE__ << " line " << __LINE__ << std::endl;
*out << "This is probably due to something incorrect in the "
"\"Parameters\" list in the input file, one of the lines:"
<< std::endl;
for (int k = 0; k < param_names[l]->size(); ++k)
*out << " " << (*param_names[l])[k] << std::endl;
throw le; // rethrow to shut things down
}
// Create vector space for parameter vector
param_vss[l] = createLocallyReplicatedVectorSpace(sacado_param_vec[l].size(), commT);
// Create Thyra vector for parameters
param_vecs[l] = Thyra::createMember(param_vss[l]);
const Teuchos::ParameterList& pList = parameterParams.sublist(util::strint("Parameter",l));
int numParameters = param_vss[l]->dim();
// Loading lower and upper bounds (if any)
// IKT: I believe these parameters are only relevant for optimization
const std::string& parameterType = pList.isParameter("Type") ?
pList.get<std::string>("Type") : std::string("Scalar");
if(parameterType == "Scalar") {
// Loading lower bounds (if any)
if (pList.isParameter("Lower Bound")) {
param_lower_bds[l] = Thyra::createMember(param_vss[l]);
ST lb = pList.get<ST>("Lower Bound");
TEUCHOS_TEST_FOR_EXCEPTION (1!=numParameters, Teuchos::Exceptions::InvalidParameter,
"Error! numParameters!=1.\n");
auto param_lower_bd_nonConstView = getNonconstLocalData(param_lower_bds[l]);
param_lower_bd_nonConstView[0] = lb;
}
// Loading upper bounds (if any)
if (pList.isParameter("Upper Bound")) {
param_upper_bds[l] = Thyra::createMember(param_vss[l]);
ST ub = pList.get<ST>("Upper Bound");
TEUCHOS_TEST_FOR_EXCEPTION (1!=numParameters, Teuchos::Exceptions::InvalidParameter,
"Error! numParameters!=1.\n");
auto param_upper_bd_nonConstView = getNonconstLocalData(param_upper_bds[l]);
param_upper_bd_nonConstView[0] = ub;
}
// Loading nominal values (if any)
auto param_vec_nonConstView = getNonconstLocalData(param_vecs[l]);
if (pList.isParameter("Nominal Value")) {
ST nvals = pList.get<ST>("Nominal Value");
TEUCHOS_TEST_FOR_EXCEPTION (1!=numParameters, Teuchos::Exceptions::InvalidParameter,
"Error! numParameters!=1.\n");
sacado_param_vec[l][0].baseValue = param_vec_nonConstView[0] = nvals;
} else {
param_vec_nonConstView[0] = sacado_param_vec[l][0].baseValue;
}
}
if(parameterType == "Vector") {
param_lower_bds[l] = Thyra::createMember(param_vss[l]);
param_upper_bds[l] = Thyra::createMember(param_vss[l]);
auto param_lower_bd_nonConstView = getNonconstLocalData(param_lower_bds[l]);
auto param_upper_bd_nonConstView = getNonconstLocalData(param_upper_bds[l]);
auto param_vec_nonConstView = getNonconstLocalData(param_vecs[l]);
for (int k = 0; k < numParameters; ++k) {
std::string sublistName = util::strint("Scalar",k);
//IKT: I believe the following parameters are only for optimization
if (pList.sublist(sublistName).isParameter("Lower Bound")) {
ST lb = pList.sublist(sublistName).get<ST>("Lower Bound");
param_lower_bd_nonConstView[k] = lb;
}
if (pList.sublist(sublistName).isParameter("Upper Bound")) {
ST ub = pList.sublist(sublistName).get<ST>("Upper Bound");
param_upper_bd_nonConstView[k] = ub;
}
if (pList.sublist(sublistName).isParameter("Nominal Value")) {
ST nvals = pList.sublist(sublistName).get<ST>("Nominal Value");
sacado_param_vec[l][k].baseValue = param_vec_nonConstView[k] = nvals;
} else {
param_vec_nonConstView[k] = sacado_param_vec[l][k].baseValue;
}
}
}
}
// Setup distributed parameters
distParamLib = app->getDistributedParameterLibrary();
dist_param_names.resize(num_dist_param_vecs);
*out << "Number of distributed parameters vectors = " << num_dist_param_vecs
<< std::endl;
std::string p_name;
std::string emptyString("");
for (int i = num_param_vecs; i < total_num_param_vecs; i++) {
const std::string& p_sublist_name = util::strint("Parameter", i);
Teuchos::ParameterList param_list = parameterParams.sublist(p_sublist_name);
p_name = param_list.get<std::string>("Name");
TEUCHOS_TEST_FOR_EXCEPTION(
!distParamLib->has(p_name),
Teuchos::Exceptions::InvalidParameter,
std::endl
<< "Error! In Albany::ModelEvaluator constructor: "
<< "Invalid distributed parameter name \""
<< p_name
<< "\""
<< std::endl);
dist_param_names[i-num_param_vecs] = p_name;
Teuchos::RCP<const DistributedParameter> distParam = setDistParamVec(p_name, param_list);
}
for (int l = 0; l < app->getNumResponses(); ++l) {
// Create Thyra vector for responses
thyra_response_vec[l] = Thyra::createMember(app->getResponse(l)->responseVectorSpace());
}
// Determine the number of solution vectors (x, xdot, xdotdot)
int num_sol_vectors = app->getAdaptSolMgr()->getInitialSolution()->domain()->dim();
if (num_sol_vectors > 1) { // have x dot
supports_xdot = true;
if (num_sol_vectors > 2) // have both x dot and x dotdot
supports_xdotdot = true;
}
// Setup nominal values, lower and upper bounds, and final point
nominalValues = this->createInArgsImpl();
lowerBounds = this->createInArgsImpl();
upperBounds = this->createInArgsImpl();
// All the ME vectors are unallocated here
allocateVectors();
// TODO: Check if correct nominal values for parameters
for (int l = 0; l < num_param_vecs; ++l) {
nominalValues.set_p(l, param_vecs[l]);
if(Teuchos::nonnull(param_lower_bds[l])) {
lowerBounds.set_p(l, param_lower_bds[l]);
}
if(Teuchos::nonnull(param_upper_bds[l])) {
upperBounds.set_p(l, param_upper_bds[l]);
}
}
for (int l = 0; l < num_dist_param_vecs; ++l) {
nominalValues.set_p(l+num_param_vecs, distParamLib->get(dist_param_names[l])->vector());
lowerBounds.set_p(l+num_param_vecs, distParamLib->get(dist_param_names[l])->lower_bounds_vector());
upperBounds.set_p(l+num_param_vecs, distParamLib->get(dist_param_names[l])->upper_bounds_vector());
}
overwriteNominalValuesWithFinalPoint = appParams->get("Overwrite Nominal Values With Final Point",false);
timer = Teuchos::TimeMonitor::getNewTimer("Albany: Total Fill Time");
}
void ModelEvaluator::setNominalValue(int j, Teuchos::RCP<Thyra_Vector> p)
{
TEUCHOS_TEST_FOR_EXCEPTION(
j >= num_param_vecs + num_dist_param_vecs || j < 0,
Teuchos::Exceptions::InvalidParameter,
std::endl
<< "Error! Albany::ModelEvaluator::setNominalValue(): "
<< "Invalid parameter index j = "
<< j
<< std::endl);
nominalValues.set_p(j, p);
}
Teuchos::RCP<const DistributedParameter> ModelEvaluator::setDistParamVec(const std::string p_name,
const Teuchos::ParameterList param_list)
{
Teuchos::RCP<const DistributedParameter> distParam = distParamLib->get(p_name);
// set parameters bounds - IKT: I think this is only relevant for the optimization
if (param_list.isParameter("Lower Bound")) {
TEUCHOS_TEST_FOR_EXCEPTION(
distParam->lower_bounds_vector() == Teuchos::null,
Teuchos::Exceptions::InvalidParameter,
"\nError! In Albany::ModelEvaluator constructor: "
<< "distParam->lower_bounds_vector() == Teuchos::null\n");
distParam->lower_bounds_vector()->assign(
param_list.get<ST>("Lower Bound"));
}
if (param_list.isParameter("Upper Bound")) {
TEUCHOS_TEST_FOR_EXCEPTION(
distParam->upper_bounds_vector() == Teuchos::null,
Teuchos::Exceptions::InvalidParameter,
"\nError! In Albany::ModelEvaluator constructor: "
"distParam->upper_bounds_vector() == Teuchos::null\n");
distParam->upper_bounds_vector()->assign(
param_list.get<ST>("Upper Bound"));
}
if (param_list.isParameter("Parameter Analytic Expression")) {
TEUCHOS_TEST_FOR_EXCEPTION(
distParam->vector() == Teuchos::null,
Teuchos::Exceptions::InvalidParameter,
"\nError! In Albany::ModelEvaluator constructor: "
<< "distParam->vector() == Teuchos::null.\n");
if (app->getComm()->getSize() > 1) {
TEUCHOS_TEST_FOR_EXCEPTION(true, Teuchos::Exceptions::InvalidParameter,
"\nError! In Albany::ModelEvaluator constructor: "
<< "'Parameter Analytic Expression' option for initializing distributed"
<< " parameters only works in serial.\n");
}
//IKT, 7/2021: the following has been added to enable verification of
//sensitivities for distributed parameters using MMS problems. Other
//analytical expressions for distributed parameters may be added besides
//the currently-available 'Quadratic' one, if desired.
const std::string param_expr = param_list.get<std::string>("Parameter Analytic Expression");
Teuchos::RCP<Albany::AbstractDiscretization> disc = app->getDisc();
const Teuchos::ArrayRCP<double>& ov_coords = disc->getCoordinates();
const int num_dims = app->getSpatialDimension();
const int num_nodes = disc->getVectorSpace()->dim();
/*const int num_dofs = ov_coords.size();
std::cout << "IKT num_dims, num_dofs, num_nodes = " << num_dims << ", " << num_dofs
<< ", " << num_nodes << "\n";
for (int i=0; i<num_dofs; i++) {
std::cout << "IKT i, ov_coords = " << i << ", " << ov_coords[i] << "\n";
}*/
Teuchos::ArrayRCP<double> coeffs(2);
if (param_list.isParameter("Parameter Analytic Expression Coefficients")) {
Teuchos::Array<double> coeffs_array = param_list.get<Teuchos::Array<double>>("Parameter Analytic Expression Coefficients");
if (coeffs_array.size() != 2) {
TEUCHOS_TEST_FOR_EXCEPTION(true, Teuchos::Exceptions::InvalidParameter,
"\nError! In Albany::ModelEvaluator constructor: "
<< "'Parameter Analytic Expression Coefficients' array must have size 2."
<< " You have provided an array of size " << coeffs_array.size() << ".\n");
}
coeffs[0] = coeffs_array[0];
coeffs[1] = coeffs_array[1];
}
else {
coeffs[0] = 1.0;
coeffs[1] = 0.0;
}
Teuchos::ArrayRCP<ST> distParamVec_ArrayRCP =
getNonconstLocalData(distParam->vector());
if (param_expr == "Linear")
{
if (num_dims == 1) {
for (int i=0; i < num_nodes; i++) {
const double x = ov_coords[i];
distParamVec_ArrayRCP[i] = x + coeffs[0];
}
}
else if (num_dims == 2) {
for (int i=0; i < num_nodes; i++) {
const double x = ov_coords[2*i];
const double y = ov_coords[2*i+1];
distParamVec_ArrayRCP[i] = (x + coeffs[0]) + (y + coeffs[1]);
}
}
else {
TEUCHOS_TEST_FOR_EXCEPTION(true, Teuchos::Exceptions::InvalidParameter,
"\nError! In Albany::ModelEvaluator constructor: "
<< "Linear Parameter Analytic Expression not valid for >2D.\n");
}
}
else if (param_expr == "Quadratic")
{
if (num_dims == 1) {
for (int i=0; i < num_nodes; i++) {
const double x = ov_coords[i];
distParamVec_ArrayRCP[i] = x*(coeffs[0]-x) + coeffs[1];
}
}
else if (num_dims == 2) {
for (int i=0; i < num_nodes; i++) {
const double x = ov_coords[2*i];
const double y = ov_coords[2*i+1];
distParamVec_ArrayRCP[i] = x*(coeffs[0]-x)*y*(coeffs[0]-y) + coeffs[1];
}
}
else {
TEUCHOS_TEST_FOR_EXCEPTION(true, Teuchos::Exceptions::InvalidParameter,
"\nError! In Albany::ModelEvaluator constructor: "
<< "Quadratic Parameter Analytic Expression not valid for >2D.\n");
}
}
else {
TEUCHOS_TEST_FOR_EXCEPTION(true, Teuchos::Exceptions::InvalidParameter,
"\nError! In Albany::ModelEvaluator constructor: "
<< "Invalid value for 'Parameter Analytic Expression' = "
<< param_expr << ". Valid expressions are: 'Linear', 'Quadratic'.\n");
}
}
if (param_list.isParameter("Initial Uniform Value")) {
TEUCHOS_TEST_FOR_EXCEPTION(
distParam->vector() == Teuchos::null,
Teuchos::Exceptions::InvalidParameter,
"\nError! In Albany::ModelEvaluator constructor: "
<< "distParam->vector() == Teuchos::null.\n");
distParam->vector()->assign(
param_list.get<ST>("Initial Uniform Value"));
}
return distParam;
}
void ModelEvaluator::allocateVectors()
{
const Teuchos::RCP<const Thyra_MultiVector> xMV = app->getAdaptSolMgr()->getCurrentSolution();
// Create non-const versions of x_init [and x_dot_init [and x_dotdot_init]]
const Teuchos::RCP<const Thyra_Vector> x_init = xMV->col(0);
const Teuchos::RCP<Thyra_Vector> x_init_nonconst = x_init->clone_v();
nominalValues.set_x(x_init_nonconst);
// Have xdot
if (xMV->domain()->dim() > 1) {
const Teuchos::RCP<const Thyra_Vector> x_dot_init = xMV->col(1);
const Teuchos::RCP<Thyra_Vector> x_dot_init_nonconst = x_dot_init->clone_v();
nominalValues.set_x_dot(x_dot_init_nonconst);
}
// Have xdotdot
if (xMV->domain()->dim() > 2) {
// Set xdotdot in parent class to pass to time integrator
// GAH set x_dotdot for transient simulations. Note that xDotDot is a member
// of Piro::TransientDecorator<ST>
const Teuchos::RCP<const Thyra_Vector> x_dotdot_init = xMV->col(2);
const Teuchos::RCP<Thyra_Vector> x_dotdot_init_nonconst = x_dotdot_init->clone_v();
// IKT, 3/30/17: set x_dotdot in nominalValues for Tempus, now that
// it is available in Thyra::ModelEvaluator
this->xDotDot = x_dotdot_init_nonconst;
nominalValues.set_x_dot_dot(x_dotdot_init_nonconst);
} else {
this->xDotDot = Teuchos::null;
}
}
// Overridden from Thyra::ModelEvaluator<ST>
Teuchos::RCP<const Thyra_VectorSpace>
ModelEvaluator::get_x_space() const
{
return app->getVectorSpace();
}
Teuchos::RCP<const Thyra_VectorSpace>
ModelEvaluator::get_f_space() const
{
return app->getVectorSpace();
}
Teuchos::RCP<const Thyra_VectorSpace>
ModelEvaluator::get_p_space(int l) const
{
TEUCHOS_TEST_FOR_EXCEPTION(
l >= num_param_vecs + num_dist_param_vecs || l < 0,
Teuchos::Exceptions::InvalidParameter,
std::endl
<< "Error! Albany::ModelEvaluator::get_p_space(): "
<< "Invalid parameter index l = "
<< l
<< std::endl);
Teuchos::RCP<const Thyra_VectorSpace> vs;
if (l < num_param_vecs) {
vs = param_vss[l];
} else {
vs = distParamLib->get(dist_param_names[l-num_param_vecs])->vector_space();
}
return vs;
}
Teuchos::RCP<const Thyra_VectorSpace>
ModelEvaluator::get_g_space(int l) const
{
TEUCHOS_TEST_FOR_EXCEPTION(
l >= app->getNumResponses() || l < 0,
Teuchos::Exceptions::InvalidParameter,
std::endl
<< "Error! Albany::ModelEvaluator::get_g_space(): "
<< "Invalid response index l = "
<< l
<< std::endl);
return app->getResponse(l)->responseVectorSpace();
}
Teuchos::RCP<const Teuchos::Array<std::string>>
ModelEvaluator::get_p_names(int l) const
{
TEUCHOS_TEST_FOR_EXCEPTION(
l >= num_param_vecs + num_dist_param_vecs || l < 0,
Teuchos::Exceptions::InvalidParameter,
std::endl
<< "Error! Albany::ModelEvaluator::get_p_names(): "
<< "Invalid parameter index l = "
<< l
<< std::endl);
if (l < num_param_vecs) return param_names[l];
return Teuchos::rcp(
new Teuchos::Array<std::string>(1, dist_param_names[l - num_param_vecs]));
}
Teuchos::RCP<Thyra_LinearOp>
ModelEvaluator::create_W_op() const
{
return app->getDisc()->createJacobianOp();
}
Teuchos::RCP<Thyra_Preconditioner>
ModelEvaluator::create_W_prec() const
{
Teuchos::RCP<Thyra::DefaultPreconditioner<ST>> W_prec = Teuchos::rcp(new Thyra::DefaultPreconditioner<ST>);
Teuchos::RCP<Thyra_LinearOp> precOp = app->getPreconditioner();
W_prec->initializeRight(precOp);
return W_prec;
}
Teuchos::RCP<Thyra_LinearOp>
ModelEvaluator::create_hess_g_pp( int j, int l1, int l2 ) const
{
TEUCHOS_TEST_FOR_EXCEPTION(
l1 != l2,
Teuchos::Exceptions::InvalidParameter,
std::endl
<< "Error! Albany::ModelEvaluator::create_hess_g_pp(): "
<< "Parameter index l1 is not equal to l2"
<< "l1 = " << l1
<< "l2 = " << l2
<< std::endl);
auto pl = app->getProblemPL()->sublist("Hessian").sublist(util::strint("Response",j)).sublist(util::strint("Parameter",l1));
bool HessVecProdBasedOp = pl.get("Reconstruct H_pp using Hessian-vector products",true);
if (l1 < num_param_vecs) {
TEUCHOS_TEST_FOR_EXCEPTION(!HessVecProdBasedOp, std::logic_error,
std::endl
<< "Error! Albany::ModelEvaluator::create_hess_g_pp(): "
<< "Hessian pp operator for response " << j << " and non-distributed parameter " << l1 << " can only be reconstructed via Hessian-vector products"
<< std::endl);
return Albany::createDenseHessianLinearOp(param_vss[l1]);
} else {
// distributed parameters
TEUCHOS_TEST_FOR_EXCEPTION(
l1 >= num_param_vecs + num_dist_param_vecs || l1 < num_param_vecs,
Teuchos::Exceptions::InvalidParameter,
std::endl
<< "Error! Albany::ModelEvaluator::create_hess_g_pp(): "
<< "Invalid parameter index l1 = "
<< l1
<< std::endl);
if(HessVecProdBasedOp) {
const auto p = distParamLib->get(dist_param_names[l1-num_param_vecs]);
return Albany::createSparseHessianLinearOp(p);
} else {
Teuchos::RCP<Thyra_LinearOp> linOp = app->getResponse(j)->get_Hess_pp_operator(dist_param_names[l1 - num_param_vecs]);
TEUCHOS_TEST_FOR_EXCEPTION(Teuchos::is_null(linOp), std::logic_error,
std::endl
<< "Error! Albany::ModelEvaluator::create_hess_g_pp(): "
<< "Hessian pp operator not defined for response " << j << " and parameter " << dist_param_names[l1 - num_param_vecs]
<< std::endl);
return linOp;
}
}
}
Teuchos::RCP<Thyra_LinearOp>
ModelEvaluator::create_DfDp_op_impl(int j) const
{
TEUCHOS_TEST_FOR_EXCEPTION(
j >= num_param_vecs + num_dist_param_vecs || j < num_param_vecs,
Teuchos::Exceptions::InvalidParameter,
std::endl
<< "Error! Albany::ModelEvaluator::create_DfDp_op_impl(): "
<< "Invalid parameter index j = "
<< j
<< std::endl);
return Teuchos::rcp( new DistributedParameterDerivativeOp(app, dist_param_names[j - num_param_vecs]) );
}
Teuchos::RCP<const Thyra_LOWS_Factory>
ModelEvaluator::get_W_factory() const
{
return Teuchos::null;
}
Thyra_ModelEvaluator::InArgs<ST>
ModelEvaluator::createInArgs() const
{
return this->createInArgsImpl();
}
void
ModelEvaluator::reportFinalPoint(
const Thyra_ModelEvaluator::InArgs<ST>& finalPoint,
const bool wasSolved)
{
// Set nominal values to the final point, if the model was solved
if (overwriteNominalValuesWithFinalPoint && wasSolved) {
nominalValues = finalPoint;
}
Application::SolutionStatus status = wasSolved ? Application::SolutionStatus::Converged : Application::SolutionStatus::NotConverged;
app->setSolutionStatus(status);
}
Teuchos::RCP<Thyra_LinearOp>
ModelEvaluator::create_DgDx_op_impl(int j) const
{
TEUCHOS_TEST_FOR_EXCEPTION(
j >= app->getNumResponses() || j < 0,
Teuchos::Exceptions::InvalidParameter,
std::endl
<< "Error! Albany::ModelEvaluator::create_DgDx_op_impl(): "
<< "Invalid response index j = "
<< j
<< std::endl);
return app->getResponse(j)->createGradientOp();
}
// AGS: x_dotdot time integrators not implemented in Thyra ME yet
Teuchos::RCP<Thyra_LinearOp>
ModelEvaluator::create_DgDx_dotdot_op_impl(int j) const
{
TEUCHOS_TEST_FOR_EXCEPTION(
j >= app->getNumResponses() || j < 0,
Teuchos::Exceptions::InvalidParameter,
std::endl
<< "Error! Albany::ModelEvaluator::create_DgDx_dotdot_op(): "
<< "Invalid response index j = "
<< j
<< std::endl);
return app->getResponse(j)->createGradientOp();
}
Teuchos::RCP<Thyra_LinearOp>
ModelEvaluator::create_DgDx_dot_op_impl(int j) const
{
TEUCHOS_TEST_FOR_EXCEPTION(
j >= app->getNumResponses() || j < 0,
Teuchos::Exceptions::InvalidParameter,
std::endl
<< "Error! Albany::ModelEvaluator::create_DgDx_dot_op_impl(): "
<< "Invalid response index j = "
<< j
<< std::endl);
return app->getResponse(j)->createGradientOp();
}
Thyra_OutArgs ModelEvaluator::createOutArgsImpl() const
{
Thyra_ModelEvaluator::OutArgsSetup<ST> result;
result.setModelEvalDescription(this->description());
const int n_g = app->getNumResponses();
result.set_Np_Ng(num_param_vecs + num_dist_param_vecs, n_g);
result.setSupports(Thyra_ModelEvaluator::OUT_ARG_f, true);
if (supplies_prec)
result.setSupports(Thyra_ModelEvaluator::OUT_ARG_W_prec, true);
result.setSupports(Thyra_ModelEvaluator::OUT_ARG_W_op, true);
result.set_W_properties(Thyra_ModelEvaluator::DerivativeProperties(
Thyra_ModelEvaluator::DERIV_LINEARITY_UNKNOWN,
Thyra_ModelEvaluator::DERIV_RANK_FULL,
true));
for (int l = 0; l < num_param_vecs; ++l) {
result.setSupports(
Thyra_ModelEvaluator::OUT_ARG_DfDp,
l,
Thyra_ModelEvaluator::DERIV_MV_JACOBIAN_FORM);
}
for (int i = 0; i < num_dist_param_vecs; i++)
result.setSupports(
Thyra_ModelEvaluator::OUT_ARG_DfDp,
i + num_param_vecs,
Thyra_ModelEvaluator::DERIV_LINEAR_OP);
for (int i = 0; i < n_g; ++i) {
Thyra_ModelEvaluator::DerivativeSupport dgdx_support;
//Check that responses are scalar; throw an error if they are not,
//as distributed responses are not supported yet.
if (!app->getResponse(i)->isScalarResponse()) {
TEUCHOS_TEST_FOR_EXCEPTION(
true,
std::logic_error,
std::endl
<< "Error! Albany::ModelEvaluator::createOutArgsImpl(): "
<< "The response associated to the index i = "
<< i
<< " is not a scalar response. Only scalar responses are currently supported."
<< std::endl);
}
if (app->getResponse(i)->isScalarResponse()) {
dgdx_support = Thyra_ModelEvaluator::DERIV_MV_GRADIENT_FORM;
} else {
//IKT 6/30/2021: note that this case will not get hit ever because
//distributed responses are not supported in Albany yet
dgdx_support = Thyra_ModelEvaluator::DERIV_LINEAR_OP;
}
result.setSupports(
Thyra_ModelEvaluator::OUT_ARG_DgDx, i, dgdx_support);
if (supports_xdot) {
result.setSupports(
Thyra_ModelEvaluator::OUT_ARG_DgDx_dot, i, dgdx_support);
}
// AGS: x_dotdot time integrators not implemented in Thyra ME yet
// result.setSupports(
// Thyra_ModelEvaluator::OUT_ARG_DgDx_dotdot, i, dgdx_support);
for (int l1 = 0; l1 < num_param_vecs; l1++) {
result.setSupports(
Thyra_ModelEvaluator::OUT_ARG_DgDp,
i,
l1,
Thyra_ModelEvaluator::DERIV_MV_JACOBIAN_FORM);
}
if (app->getResponse(i)->isScalarResponse()) {
for (int j1 = 0; j1 < num_dist_param_vecs; j1++) {
result.setSupports(
Thyra_ModelEvaluator::OUT_ARG_DgDp,
i,
j1 + num_param_vecs,
Thyra_ModelEvaluator::DERIV_MV_GRADIENT_FORM);
}
}
}
// Set Hessian-related supports:
const Teuchos::ParameterList& hessParams = appParams->sublist("Problem").sublist("Hessian");
// Default value:
const bool dADHessVec = hessParams.isParameter("Use AD for Hessian-vector products (default)") ?
hessParams.get<bool>("Use AD for Hessian-vector products (default)") : true;
// Default value for residual:
bool dADHessVec_f;
if(hessParams.isSublist("Residual")) {
dADHessVec_f = hessParams.sublist("Residual").isParameter("Use AD for Hessian-vector products (default)") ?
hessParams.sublist("Residual").get<bool>("Use AD for Hessian-vector products (default)") : dADHessVec;
}
else {
dADHessVec_f = dADHessVec;
}
const int num_params = num_param_vecs + num_dist_param_vecs;
bool aDHessVec_f[num_params + 1][num_params + 1];
bool aDHessVec_g[num_params + 1][num_params + 1];
aDHessVec_f[0][0] = dADHessVec_f;
for (int j1 = 0; j1 < num_params; j1++) {
aDHessVec_f[0][j1+1] = dADHessVec_f;
aDHessVec_f[j1+1][0] = dADHessVec_f;
for (int j2 = 0; j2 < num_params; j2++) {
aDHessVec_f[j1+1][j2+1] = dADHessVec_f;
}
}
if(hessParams.isSublist("Residual")) {
std::string toDisable = hessParams.sublist("Residual").isParameter("Disable AD for Hessian-vector product contributions of") ?
hessParams.sublist("Residual").get<std::string>("Disable AD for Hessian-vector product contributions of") : "";
std::string toEnable = hessParams.sublist("Residual").isParameter("Enable AD for Hessian-vector product contributions of") ?
hessParams.sublist("Residual").get<std::string>("Enable AD for Hessian-vector product contributions of") : "";
std::vector<std::string> toDisableVec, toEnableVec;
util::splitStringOnDelim(toDisable,' ',toDisableVec);
util::splitStringOnDelim(toEnable,' ',toEnableVec);
for (auto toDisableEntry : toDisableVec)
for (auto toEnableEntry : toEnableVec)
TEUCHOS_TEST_FOR_EXCEPTION(
toDisableEntry.compare(toEnableEntry)==0,
Teuchos::Exceptions::InvalidParameter,
std::endl
<< "Error! Albany::ModelEvaluator::createOutArgsImpl(): "
<< "Hessian contribution of the residual = "
<< toDisableEntry
<< " is set both in the AD enabled and disabled list."
<< std::endl);
int i1, i2;
for (auto toDisableEntry : toDisableVec) {
Albany::getHessianBlockIDs(i1, i2, toDisableEntry);
TEUCHOS_TEST_FOR_EXCEPTION(
i1 > num_params || i2 > num_params || i1 < 0 || i2 < 0,
Teuchos::Exceptions::InvalidParameter,
std::endl
<< "Error! Albany::ModelEvaluator::createOutArgsImpl(): "
<< "Hessian contribution of the residual = "
<< toDisableEntry
<< " has an ID out of the range: [0, "
<< num_params
<< "]"
<< std::endl);
aDHessVec_f[i1][i2] = false;
}
for (auto toEnableEntry : toEnableVec) {
Albany::getHessianBlockIDs(i1, i2, toEnableEntry);
TEUCHOS_TEST_FOR_EXCEPTION(
i1 > num_params || i2 > num_params || i1 < 0 || i2 < 0,
Teuchos::Exceptions::InvalidParameter,
std::endl
<< "Error! Albany::ModelEvaluator::createOutArgsImpl(): "
<< "Hessian contribution of the residual = "
<< toEnableEntry
<< " has an ID out of the range: [0, "
<< num_params
<< "]"
<< std::endl);
aDHessVec_f[i1][i2] = true;
}
if (num_params > 1) {
bool tmp = aDHessVec_f[0][1];
for (int j1 = 2; j1 < num_params + 1; j1++) {
TEUCHOS_TEST_FOR_EXCEPTION(
aDHessVec_f[0][j1] != tmp,
Teuchos::Exceptions::InvalidParameter,
std::endl
<< "Error! Albany::ModelEvaluator::createOutArgsImpl(): "
<< "AD support for xp Hessian contributions of the residual are not consistent; "
<< "AD is enable for some blocks but not for others."
<< std::endl);
}
tmp = aDHessVec_f[1][0];
for (int j1 = 2; j1 < num_params + 1; j1++) {
TEUCHOS_TEST_FOR_EXCEPTION(
aDHessVec_f[j1][0] != tmp,
Teuchos::Exceptions::InvalidParameter,
std::endl
<< "Error! Albany::ModelEvaluator::createOutArgsImpl(): "
<< "AD support for px Hessian contributions of the residual are not consistent; "
<< "AD is enable for some blocks but not for others."
<< std::endl);
}
tmp = aDHessVec_f[1][1];
for (int j1 = 2; j1 < num_params + 1; j1++) {
for (int j2 = 2; j2 < num_params + 1; j2++) {
TEUCHOS_TEST_FOR_EXCEPTION(
aDHessVec_f[j1][j2] != tmp,
Teuchos::Exceptions::InvalidParameter,
std::endl
<< "Error! Albany::ModelEvaluator::createOutArgsImpl(): "
<< "AD support for pp Hessian contributions of the residual are not consistent; "
<< "AD is enable for some blocks but not for others."
<< std::endl);
}
}
}
}
result.setSupports(
Thyra_ModelEvaluator::OUT_ARG_hess_vec_prod_f_xx,
aDHessVec_f[0][0]);
for (int j1 = 0; j1 < num_params; j1++) {
result.setSupports(
Thyra_ModelEvaluator::OUT_ARG_hess_vec_prod_f_xp,
j1,
aDHessVec_f[0][j1+1]);
result.setSupports(
Thyra_ModelEvaluator::OUT_ARG_hess_vec_prod_f_px,
j1,
aDHessVec_f[j1+1][0]);
for (int j2 = 0; j2 < num_params; j2++) {
result.setSupports(
Thyra_ModelEvaluator::OUT_ARG_hess_vec_prod_f_pp,
j1,
j2,
aDHessVec_f[j1+1][j2+1]);
}
}
for (int i = 0; i < n_g; ++i) {
// Default value for response:
bool dADHessVec_g, supportHpp;
if(hessParams.isSublist(util::strint("Response", i))) {
dADHessVec_g = hessParams.sublist(util::strint("Response", i)).isParameter("Use AD for Hessian-vector products (default)") ?
hessParams.sublist(util::strint("Response", i)).get<bool>("Use AD for Hessian-vector products (default)") : dADHessVec;
supportHpp = hessParams.sublist(util::strint("Response", i)).isParameter("Reconstruct H_pp") ?
hessParams.sublist(util::strint("Response", i)).get<bool>("Reconstruct H_pp") : true;
}
else {
dADHessVec_g = dADHessVec;
supportHpp = true;
}
auto& analysisParams = appParams->sublist("Piro").sublist("Analysis");
if(analysisParams.isSublist("ROL")) {
bool reconstructHppROL = false;
if(analysisParams.sublist("ROL").isSublist("Matrix Based Dot Product"))
reconstructHppROL = reconstructHppROL ||
(analysisParams.sublist("ROL").sublist("Matrix Based Dot Product").get<std::string>("Matrix Type") == "Hessian Of Response");
if(analysisParams.sublist("ROL").isSublist("Custom Secant"))
reconstructHppROL = reconstructHppROL ||
(analysisParams.sublist("ROL").sublist("Custom Secant").get<std::string>("Initialization Type") == "Hessian Of Response");
TEUCHOS_TEST_FOR_EXCEPTION(
(supportHpp == false) && (reconstructHppROL == true),
Teuchos::Exceptions::InvalidParameter,
std::endl
<< "Error! Albany::ModelEvaluator::createOutArgsImpl(): "
<< "The construction of H_pp is requested but not supported"
<< ". Please set the Option in the Hessian and ROL sublists consistently."
<< std::endl);
}
aDHessVec_g[0][0] = dADHessVec_g;
for (int j1 = 0; j1 < num_params; j1++) {
aDHessVec_g[0][j1+1] = dADHessVec_g;
aDHessVec_g[j1+1][0] = dADHessVec_g;
for (int j2 = 0; j2 < num_params; j2++) {
aDHessVec_g[j1+1][j2+1] = dADHessVec_g;
}
}
if(hessParams.isSublist(util::strint("Response", i))) {
std::string toDisable = hessParams.sublist(util::strint("Response", i)).isParameter("Disable AD for Hessian-vector product contributions of") ?
hessParams.sublist(util::strint("Response", i)).get<std::string>("Disable AD for Hessian-vector product contributions of") : "";
std::string toEnable = hessParams.sublist(util::strint("Response", i)).isParameter("Enable AD for Hessian-vector product contributions of") ?
hessParams.sublist(util::strint("Response", i)).get<std::string>("Enable AD for Hessian-vector product contributions of") : "";
std::vector<std::string> toDisableVec, toEnableVec;
util::splitStringOnDelim(toDisable,' ',toDisableVec);
util::splitStringOnDelim(toEnable,' ',toEnableVec);
for (auto toDisableEntry : toDisableVec)
for (auto toEnableEntry : toEnableVec)
TEUCHOS_TEST_FOR_EXCEPTION(
toDisableEntry.compare(toEnableEntry)==0,
Teuchos::Exceptions::InvalidParameter,
std::endl
<< "Error! Albany::ModelEvaluator::createOutArgsImpl(): "
<< "Hessian contribution of the response " << i << " = "
<< toDisableEntry
<< " is set both in the AD enabled and disabled list."
<< std::endl);
int i1, i2;
for (auto toDisableEntry : toDisableVec) {
Albany::getHessianBlockIDs(i1, i2, toDisableEntry);
TEUCHOS_TEST_FOR_EXCEPTION(
i1 > num_params || i2 > num_params || i1 < 0 || i2 < 0,