getProbeTimeSeries.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 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  "coords",
26  "'(0 0 1)'",
27  "probe point coordinates");
28 
29  argList::addOption(
30  "varName",
31  "U",
32  "name of the variable to get time-series from");
33 
34  argList::addOption(
35  "varType",
36  "vector",
37  "type of the variable, can be either scalar or vector");
38 
39  argList::addOption(
40  "outputName",
41  "VarTimeSeries",
42  "name of the output file (optional)");
43 
44  argList::addOption(
45  "deltaT",
46  "-1",
47  "Use user-prescribed deltaT to extract time series, otherwise, use the deltaT in controlDict");
48 
49 #include "setRootCase.H"
50 #include "createTime.H"
51 #include "createMesh.H"
52 
53  word outputName = "VarTimeSeries";
54  if (args.optionFound("outputName"))
55  {
56  outputName = word(args.optionLookup("outputName")());
57  }
58 
59  List<scalar> coords;
60  if (args.optionFound("coords"))
61  {
62  coords = scalarList(args.optionLookup("coords")());
63  }
64  else
65  {
66  Info << "Error: coords not set! Exit." << endl;
67  return 1;
68  }
69  point coordPoint = {coords[0], coords[1], coords[2]};
70  label probeCellI = mesh.findCell(coordPoint);
71 
72  word varName;
73  if (args.optionFound("varName"))
74  {
75  varName = word(args.optionLookup("varName")());
76  }
77  else
78  {
79  Info << "Error: varName not set! Exit." << endl;
80  return 1;
81  }
82 
83  word varType;
84  if (args.optionFound("varType"))
85  {
86  varType = word(args.optionLookup("varType")());
87  }
88  else
89  {
90  Info << "Error: varType not set! Exit." << endl;
91  return 1;
92  }
93 
94  if (probeCellI < 0)
95  {
96  Info << "Error: coords " << coords << " are not within a cell! Exit." << endl;
97  return 1;
98  }
99 
100  if (varType != "scalar" && varType != "vector")
101  {
102  Info << "Error: varType = " << varType << " is not supported. The options are either scalar or vector " << endl;
103  }
104 
105  OFstream f(outputName + ".txt");
106 
107  scalar endTime = runTime.endTime().value();
108  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 
116  label nSteps = round(endTime / deltaT);
117 
118  for (label i = 0; i < nSteps; i++)
119  {
120  word timeName = Foam::name(i * deltaT);
121 
122  if (varType == "vector")
123  {
124  volVectorField var(
125  IOobject(
126  varName,
127  timeName,
128  mesh,
129  IOobject::MUST_READ,
130  IOobject::NO_WRITE),
131  mesh);
132 
133  f << var[probeCellI][0] << " " << var[probeCellI][1] << " " << var[probeCellI][2] << endl;
134  }
135  else if (varType == "scalar")
136  {
137  volScalarField var(
138  IOobject(
139  varName,
140  timeName,
141  mesh,
142  IOobject::MUST_READ,
143  IOobject::NO_WRITE),
144  mesh);
145 
146  f << var[probeCellI] << endl;
147  }
148  }
149 
150  Info << "Done! " << endl;
151 
152  return 0;
153 }
154 
155 // ************************************************************************* //
main
int main(int argc, char *argv[])
Definition: getProbeTimeSeries.C:21
mesh
fvMesh & mesh
Definition: createRefsHeatTransfer.H:4
Foam
Definition: checkGeometry.C:32
argc
int argc
Definition: setArgs.H:18
args
Foam::argList & args
Definition: setRootCasePython.H:42
runTime
Time & runTime
Definition: createRefsHeatTransfer.H:1
argv
char ** argv
Definition: setArgs.H:26