Skip to content

Commit 17e39b7

Browse files
authored
Added a new post util to extract time series std error. (#594)
1 parent ee24e98 commit 17e39b7

File tree

7 files changed

+288
-4
lines changed

7 files changed

+288
-4
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
getFieldRefStdTimeSeries.C
2+
3+
EXE = $(DAFOAM_ROOT_PATH)/OpenFOAM/sharedBins/getFieldRefStdTimeSeries
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,257 @@
1+
/*---------------------------------------------------------------------------*\
2+
3+
DAFoam : Discrete Adjoint with OpenFOAM
4+
Version : v3
5+
6+
Description:
7+
Extract time-series data at a given probe point 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+
// ************************************************************************* //
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
getProbeTimeSeries.C
2+
3+
EXE = $(DAFOAM_ROOT_PATH)/OpenFOAM/sharedBins/getProbeTimeSeries
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
EXE_INC = \
2+
-std=c++11 \
3+
-Wno-old-style-cast \
4+
-Wno-conversion-null \
5+
-Wno-deprecated-copy \
6+
-I$(LIB_SRC)/finiteVolume/lnInclude \
7+
-I$(LIB_SRC)/meshTools/lnInclude
8+
9+
EXE_LIBS = \
10+
-lfiniteVolume \
11+
-lmeshTools
12+

src/utilities/postProcessing/getTimeSeries/getTimeSeries.C src/utilities/postProcessing/getProbeTimeSeries/getProbeTimeSeries.C

+13-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44
Version : v3
55
66
Description:
7-
Extract time-series data for unsteady simulations
7+
Extract time-series data at a given probe point for unsteady simulations
88
99
\*---------------------------------------------------------------------------*/
1010

@@ -41,6 +41,11 @@ int main(int argc, char* argv[])
4141
"VarTimeSeries",
4242
"name of the output file (optional)");
4343

44+
argList::addOption(
45+
"deltaT",
46+
"-1",
47+
"Use user-prescribed deltaT to extract time series, otherwise, use the deltaT in controlDict");
48+
4449
#include "setRootCase.H"
4550
#include "createTime.H"
4651
#include "createMesh.H"
@@ -101,6 +106,13 @@ int main(int argc, char* argv[])
101106

102107
scalar endTime = runTime.endTime().value();
103108
scalar deltaT = runTime.deltaT().value();
109+
110+
if (args.optionFound("deltaT"))
111+
{
112+
deltaT = readScalar(args.optionLookup("deltaT")());
113+
}
114+
Info << "Extracting " << varName << " time series" << endl;
115+
104116
label nSteps = round(endTime / deltaT);
105117

106118
for (label i = 0; i < nSteps; i++)

src/utilities/postProcessing/getTimeSeries/Make/files

-3
This file was deleted.

0 commit comments

Comments
 (0)