getFieldRMSETimeSeries.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2 
3  DAFoam : Discrete Adjoint with OpenFOAM
4  Version : v4
5 
6  Description:
7  Extract time-series for field RMSE for unsteady simulations
8 
9 \*---------------------------------------------------------------------------*/
10 
11 #include "fvCFD.H"
12 #include "argList.H"
13 #include "Time.H"
14 #include "fvMesh.H"
15 #include "OFstream.H"
16 
17 using namespace Foam;
18 
19 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
20 
21 int main(int argc, char* argv[])
22 {
23 
24  argList::addOption(
25  "varName",
26  "U",
27  "name of the variable to get time-series from");
28 
29  argList::addOption(
30  "varType",
31  "vector",
32  "type of the variable, can be either scalar or vector");
33 
34  argList::addOption(
35  "fieldType",
36  "volume",
37  "type of the field variable, can be either volume or surface");
38 
39  argList::addOption(
40  "patchName",
41  "wing",
42  "if the fieldType=surface, we need to prescribe the name of the patch");
43 
44  argList::addOption(
45  "outputName",
46  "fieldRefStdTimeSeries",
47  "name of the output file (optional)");
48 
49  argList::addOption(
50  "deltaT",
51  "-1",
52  "Use user-prescribed deltaT to extract time series, otherwise, use the deltaT in controlDict");
53 
54 #include "setRootCase.H"
55 #include "createTime.H"
56 #include "createMesh.H"
57 
58  word outputName = "fieldRefStdTimeSeries";
59  if (args.optionFound("outputName"))
60  {
61  outputName = word(args.optionLookup("outputName")());
62  }
63 
64  word varName;
65  if (args.optionFound("varName"))
66  {
67  varName = word(args.optionLookup("varName")());
68  }
69  else
70  {
71  Info << "Error: varName not set! Exit." << endl;
72  return 1;
73  }
74 
75  word varRefName = varName + "Data";
76 
77  word varType;
78  if (args.optionFound("varType"))
79  {
80  varType = word(args.optionLookup("varType")());
81  }
82  else
83  {
84  Info << "Error: varType not set! Exit." << endl;
85  return 1;
86  }
87 
88  word fieldType;
89  if (args.optionFound("fieldType"))
90  {
91  fieldType = word(args.optionLookup("fieldType")());
92  }
93  else
94  {
95  Info << "Error: fieldType not set! Exit." << endl;
96  return 1;
97  }
98 
99  word patchName;
100  if (fieldType == "surface")
101  {
102  if (args.optionFound("patchName"))
103  {
104  patchName = word(args.optionLookup("patchName")());
105  }
106  else
107  {
108  Info << "Error: patchName not set! Exit." << endl;
109  return 1;
110  }
111  }
112 
113  OFstream f(outputName + ".txt");
114 
115  scalar endTime = runTime.endTime().value();
116  scalar deltaT = runTime.deltaT().value();
117 
118  if (args.optionFound("deltaT"))
119  {
120  deltaT = readScalar(args.optionLookup("deltaT")());
121  }
122  Info << "Extracting " << fieldType << " time series for " << varName << endl;
123 
124  label nSteps = round(endTime / deltaT);
125 
126  for (label i = 0; i < nSteps; i++)
127  {
128  word timeName = Foam::name((i + 1) * deltaT);
129 
130  if (varType == "vector")
131  {
132  volVectorField var(
133  IOobject(
134  varName,
135  timeName,
136  mesh,
137  IOobject::MUST_READ,
138  IOobject::NO_WRITE),
139  mesh);
140 
141  volVectorField varRef(
142  IOobject(
143  varRefName,
144  timeName,
145  mesh,
146  IOobject::MUST_READ,
147  IOobject::NO_WRITE),
148  mesh);
149 
150  vector std = vector::zero;
151 
152  if (fieldType == "volume")
153  {
154 
155  forAll(var, cellI)
156  {
157  for (label compI = 0; compI < 3; compI++)
158  {
159  std[compI] += (var[cellI][compI] - varRef[cellI][compI]) * (var[cellI][compI] - varRef[cellI][compI]);
160  }
161  }
162 
163  for (label compI = 0; compI < 3; compI++)
164  {
165  std[compI] = Foam::sqrt(std[compI] / mesh.nCells());
166  }
167  }
168  else if (fieldType == "surface")
169  {
170  label patchI = mesh.boundaryMesh().findPatchID(patchName);
171  if (patchI < 0)
172  {
173  Info << "Error. The prescribed patchName " << patchName << " not found in constant/polyMesh/boundary" << endl;
174  }
175  label nFaces = var.boundaryField()[patchI].size();
176  forAll(var.boundaryField()[patchI], faceI)
177  {
178  vector varBC = var.boundaryField()[patchI][faceI];
179  vector varRefBC = varRef.boundaryField()[patchI][faceI];
180 
181  for (label compI = 0; compI < 3; compI++)
182  {
183  std[compI] += (varBC[compI] - varRefBC[compI]) * (varBC[compI] - varRefBC[compI]);
184  }
185  }
186 
187  for (label compI = 0; compI < 3; compI++)
188  {
189  std[compI] = Foam::sqrt(std[compI] / nFaces);
190  }
191  }
192 
193  f << std[0] << " " << std[1] << " " << std[2] << endl;
194  }
195  else if (varType == "scalar")
196  {
197  volScalarField var(
198  IOobject(
199  varName,
200  timeName,
201  mesh,
202  IOobject::MUST_READ,
203  IOobject::NO_WRITE),
204  mesh);
205 
206  volScalarField varRef(
207  IOobject(
208  varRefName,
209  timeName,
210  mesh,
211  IOobject::MUST_READ,
212  IOobject::NO_WRITE),
213  mesh);
214 
215  scalar std = 0.0;
216 
217  if (fieldType == "volume")
218  {
219  forAll(var, cellI)
220  {
221  std += (var[cellI] - varRef[cellI]) * (var[cellI] - varRef[cellI]);
222  }
223 
224  std = Foam::sqrt(std / mesh.nCells());
225  }
226  else if (fieldType == "surface")
227  {
228  label patchI = mesh.boundaryMesh().findPatchID(patchName);
229  if (patchI < 0)
230  {
231  Info << "Error. The prescribed patchName " << patchName << " not found in constant/polyMesh/boundary" << endl;
232  }
233  label nFaces = var.boundaryField()[patchI].size();
234  forAll(var.boundaryField()[patchI], faceI)
235  {
236  scalar varBC = var.boundaryField()[patchI][faceI];
237  scalar varRefBC = varRef.boundaryField()[patchI][faceI];
238  std += (varBC - varRefBC) * (varBC - varRefBC);
239  }
240 
241  std = Foam::sqrt(std / nFaces);
242  }
243 
244  f << std << endl;
245  }
246  else
247  {
248  Info << "Error: varType not valid. Can be either scalar or vector! Exit." << endl;
249  }
250  }
251 
252  Info << "Done! " << endl;
253 
254  return 0;
255 }
256 
257 // ************************************************************************* //
mesh
fvMesh & mesh
Definition: createRefsHeatTransfer.H:4
Foam
Definition: checkGeometry.C:32
argc
int argc
Definition: setArgs.H:18
forAll
forAll(nuTilda1, cellI)
Definition: nuTilda1EqnIrkPimple.H:19
main
int main(int argc, char *argv[])
Definition: getFieldRMSETimeSeries.C:21
args
Foam::argList & args
Definition: setRootCasePython.H:42
runTime
Time & runTime
Definition: createRefsHeatTransfer.H:1
argv
char ** argv
Definition: setArgs.H:26