Loading...
Searching...
No Matches
HyperreductionConfiguration.C
1#include "HyperreductionConfiguration.H"
2#include "GeometricField.H"
3#include "fvPatchField.H"
4#include "volFieldsFwd.H"
5
6void HyperreductionConfiguration::setTracerGradOnMagicNeighborhoods(Foam::PtrList<Foam::volVectorField>& defT, Foam::label nModes)
7{
8 m_tracerGradOnMagicNeighborhoods.resize(nModes);
9 for (Foam::label k = 0; k < nModes; k++)
10 {
11 m_tracerGradOnMagicNeighborhoods.set(k, new Foam::volVectorField(defT[k]));
12 }
13}
14
15void HyperreductionConfiguration::setTracerGradOnMagicPoints(Foam::PtrList<Foam::volVectorField>& defT, Foam::label nModes)
16{
17 m_tracerGradOnMagicPoints.resize(nModes);
18 for (Foam::label k = 0; k < nModes; k++)
19 {
20 m_tracerGradOnMagicPoints.set(k, new Foam::volVectorField(defT[k]));
21 }
22}
23
24void HyperreductionConfiguration::setDeformationTensorOnMagicNeighborhoods(Foam::PtrList<Foam::volTensorField>& defT, Foam::label nModes)
25{
26 m_deformationTensorOnMagicNeighborhoods.resize(nModes);
27 for (Foam::label k = 0; k < nModes; k++)
28 {
29 m_deformationTensorOnMagicNeighborhoods.set(k, new Foam::volTensorField(defT[k]));
30 }
31}
32
33void HyperreductionConfiguration::setDeformationTensorOnMagicPoints(Foam::PtrList<Foam::volTensorField>& defT, Foam::label nModes)
34{
35 m_deformationTensorOnMagicPoints.resize(nModes);
36 for (Foam::label k = 0; k < nModes; k++)
37 {
38 m_deformationTensorOnMagicPoints.set(k, new Foam::volTensorField(defT[k]));
39 }
40}
41
42void HyperreductionConfiguration::setMeanVectorDEIM(const Foam::volVectorField& mean)
43{
44 m_meanVectorDEIM = new Foam::volVectorField(mean);
45}
46
47void HyperreductionConfiguration::setMeanScalarDEIM(const Foam::volScalarField& mean)
48{
49 m_meanScalarDEIM = new Foam::volScalarField(mean);
50}
51
52void HyperreductionConfiguration::setMeanVectorDEIMMagic(const Foam::volVectorField& mean)
53{
54 m_meanVectorDEIMMagic = new Foam::volVectorField(mean);
55}
56
57void HyperreductionConfiguration::setMeanScalarDEIMMagic(const Foam::volScalarField& mean)
58{
59 m_meanScalarDEIMMagic = new Foam::volScalarField(mean);
60}
61
62void HyperreductionConfiguration::setDeltaWeight(const Eigen::VectorXd& dw)
63{
64 m_deltaWeight = dw;
65}
66
67void HyperreductionConfiguration::set_magicDelta(const Foam::volScalarField& mD)
68{
69 m_magicDelta = new Foam::GeometricField<Foam::scalar, Foam::fvPatchField, Foam::volMesh>(mD);
70}
71
72void HyperreductionConfiguration::initializeHyperreduction(std::unique_ptr<PODConfiguration>& PODConfig)
73{
74 if (m_HRMethod == "GappyDEIM")
75 {
76 m_HRMethod = "DEIM";
77 }
78
79 if (m_interpFieldCentered)
80 {
81 m_folderDEIM = "./ITHACAoutput/Hyperreduction/" + m_HRMethod + "_centered/";
82 }
83 else
84 {
85 m_folderDEIM = "./ITHACAoutput/Hyperreduction/" + m_HRMethod + "/";
86 }
87
88 std::string nbModesUInFolderDEIM = "";
89 std::string ECPAlgoInFolderDEIM = "";
90
91 bool isReducedField = ITHACAutilities::containsSubstring(m_HRInterpolatedField, "reduced");
92 if (isReducedField)
93 {
94 initializeReducedField(m_HRInterpolatedField, PODConfig);
95 nbModesUInFolderDEIM = std::to_string(PODConfig->nModes()["U"]) + "modesU/";
96 }
97 m_nMagicPoints = PODConfig->nModes()[m_HRInterpolatedField];
98 m_HRSnapshotsField = m_HRInterpolatedField;
99
100 if (m_HRMethod == "ECP")
101 {
102 if (!(m_ECPAlgo == "Global" || m_ECPAlgo == "EachMode"))
103 {
104 Foam::Info << "Error: ECPAlgo must be Global or EachMode" << Foam::endl;
105 abort();
106 }
107
108 if (m_HRInterpolatedField == "fullStressFunction")
109 {
110 m_HRSnapshotsField = "projFullStressFunction_" + std::to_string(PODConfig->nModes()["U"]) + "modes";
111 }
112 else if (ITHACAutilities::containsSubstring(m_HRInterpolatedField, "reducedFullStressFunction"))
113 {
114 m_HRSnapshotsField = "projReducedFullStressFunction_" + std::to_string(PODConfig->nModes()["U"]) + "modes";
115 }
116 else if (m_HRInterpolatedField == "nut")
117 {
118 m_HRSnapshotsField = "projSmagFromNut_" + std::to_string(PODConfig->nModes()["U"]) + "modes";
119 }
120 else if (ITHACAutilities::containsSubstring(m_HRInterpolatedField, "reducedNut"))
121 {
122 m_HRSnapshotsField = "projSmagFromReducedNut_" + std::to_string(PODConfig->nModes()["U"]) + "modes";
123 }
124 else
125 {
126 Foam::Info << "Error: ECP method is coded only for fullStressFunction and nut" << Foam::endl;
127 abort();
128 }
129
130 ECPAlgoInFolderDEIM = m_ECPAlgo + "/";
131 nbModesUInFolderDEIM = std::to_string(PODConfig->nModes()["U"]) + "modesU/";
132
133 int nECPFields = 1;
134 if (m_ECPAlgo == "EachMode")
135 {
136 nECPFields = PODConfig->nModes()["U"];
137 }
138
139 for (int c = 0; c < nECPFields; c++)
140 {
141 for (int k = 0; k <= (c + 1) * (m_ECPAlgo == "EachMode") * (ITHACAutilities::containsSubstring(m_HRInterpolatedField, "nut")); k++)
142 {
143 Foam::word fieldNameModec = m_HRSnapshotsField;
144 if (m_ECPAlgo == "EachMode")
145 {
146 fieldNameModec += "_" + std::to_string(c + 1);
147 if (ITHACAutilities::containsSubstring(m_HRInterpolatedField, "nut"))
148 {
149 fieldNameModec += "_" + std::to_string(k);
150 }
151 }
152 PODConfig->appendField(fieldNameModec, "scalar");
153 PODConfig->insert_nModes(fieldNameModec, PODConfig->nModes()[m_HRInterpolatedField]);
154 PODConfig->insert_hilbertSpacePOD(PODConfig->field_name().last(), "L2");
155 PODConfig->set_varyingEnergy(PODConfig->field_name().last(), 0);
156 PODConfig->set_resolvedVaryingEnergy(PODConfig->field_name().last(), 0);
157 }
158 }
159 }
160
161 m_folderDEIM += m_HRInterpolatedField.substr(0, m_HRInterpolatedField.find("_")) + "/" + nbModesUInFolderDEIM;
162 m_folderDEIM += std::to_string(m_nMagicPoints) + "magicPoints/" + ECPAlgoInFolderDEIM;
163}
164
165void HyperreductionConfiguration::initializeReducedField(const Foam::word origField,
166 std::unique_ptr<PODConfiguration>& PODConfig)
167{
168 Foam::word nameToReplace = origField.substr(7);
169 nameToReplace[0] = tolower(nameToReplace[0]);
170 Foam::word modifiedFieldName = createReducedFieldName(origField, PODConfig);
171 m_HRInterpolatedField = modifiedFieldName;
172 addReducedFieldToPOD(nameToReplace, modifiedFieldName, PODConfig);
173}
174
175void HyperreductionConfiguration::addReducedFieldToPOD(const Foam::word& nameToReplace,
176 const Foam::word& modifiedFieldName, std::unique_ptr<PODConfiguration>& PODConfig)
177{
178 const auto& field_type = PODConfig->field_type();
179 const auto& field_name = PODConfig->field_name();
180
181 PODConfig->appendField(modifiedFieldName , field_type[find(field_name.begin(), field_name.end(), nameToReplace) - field_name.begin()]);
182 PODConfig->insert_nModes(modifiedFieldName, PODConfig->nModes()[nameToReplace]);
183 PODConfig->insert_hilbertSpacePOD(modifiedFieldName, PODConfig->hilbertSpacePOD()[nameToReplace]);
184 PODConfig->set_varyingEnergy(modifiedFieldName, 0);
185 PODConfig->set_resolvedVaryingEnergy(modifiedFieldName, 0);
186}
187
188Foam::word HyperreductionConfiguration::createReducedFieldName(const Foam::word& fieldName,
189 std::unique_ptr<PODConfiguration>& PODConfig)
190{
191 return fieldName + "_" + std::to_string(PODConfig->nModes()["U"]) + "modes";
192}
bool containsSubstring(std::string contain, std::string contained)
Returns true if contained is a substring of contain, false otherwise.