setProbeData.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2 
3  DAFoam : Discrete Adjoint with OpenFOAM
4  Version : v4
5 
6 Description
7  Set reference data into a field by prescribing a probe point coordinate
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  "fieldName",
26  "UData",
27  "field variables name to set");
28 
29  argList::addOption(
30  "fieldType",
31  "vector",
32  "field variables type");
33 
34  argList::addOption(
35  "time",
36  "-1",
37  "prescribe a specific time to set data");
38 
39  argList::addOption(
40  "probeCoord",
41  "(0 0 0)",
42  "prescribe the probe point coordinate");
43 
44  argList::addOption(
45  "value",
46  "(1 0 0)",
47  "prescribe the value to add to the probe point coordinate. If it is a scalar, set the value for first element and set zeros for the other two elements");
48 
49  argList::addOption(
50  "mode",
51  "findCell",
52  "The mode can be either findCell or findNearestCell");
53 
54 #include "setRootCase.H"
55 #include "createTime.H"
56 #include "createMesh.H"
57 
58  word fieldName;
59  if (args.optionFound("fieldName"))
60  {
61  fieldName = word(args.optionLookup("fieldName")());
62  }
63  else
64  {
65  Info << "fieldName not set! Exit." << endl;
66  return 1;
67  }
68 
69  word fieldType;
70  if (args.optionFound("fieldType"))
71  {
72  fieldType = word(args.optionLookup("fieldType")());
73  }
74  else
75  {
76  Info << "fieldType not set! Exit." << endl;
77  return 1;
78  }
79 
80  word mode = "findCell";
81  if (args.optionFound("mode"))
82  {
83  mode = word(args.optionLookup("mode")());
84  }
85  else
86  {
87  Info << "mode not set! Use the default findCell mode." << endl;
88  }
89 
90  vector probeCoord = {0.0, 0.0, 0.0};
91  if (args.readIfPresent("probeCoord", probeCoord))
92  {
93  Info << "The probeCoord is: " << probeCoord << endl;
94  }
95  else
96  {
97  Info << "probeCoord not set!" << endl;
98  }
99 
100  vector value = {0.0, 0.0, 0.0};
101  if (args.readIfPresent("value", value))
102  {
103  Info << "The value is: " << value << endl;
104  }
105  else
106  {
107  Info << "value not set!" << endl;
108  }
109 
110  scalar time = -1.0;
111  if (args.optionFound("time"))
112  {
113  time = readScalar(args.optionLookup("time")());
114  if (time == 9999)
115  {
116  Info << "Extract latestTime" << endl;
117  }
118  else
119  {
120  Info << "Extract time = " << time << endl;
121  }
122  }
123  else
124  {
125  Info << "time not set! Extract all time instances." << endl;
126  }
127 
128  Info << "Set field " << fieldName << " value at " << probeCoord << " with " << value << endl;
129 
130  scalar endTime = runTime.endTime().value();
131  scalar deltaT = runTime.deltaT().value();
132  label nInstances = -1;
133  if (time == -1.0)
134  {
135  nInstances = round(endTime / deltaT);
136  }
137  else
138  {
139  nInstances = 1;
140  }
141 
142  for (label n = 1; n < nInstances + 1; n++)
143  {
144 
145  scalar t = -1.0;
146  if (time == -1.0)
147  {
148  // read all times
149  t = n * deltaT;
150  }
151  else if (time == 9999)
152  {
153  // read from the latestTime (it is not necessarily the endTime)
154  instantList timeDirs = runTime.findTimes(runTime.path(), runTime.constant());
155  t = timeDirs.last().value();
156  }
157  else
158  {
159  // read from the specified time
160  t = time;
161  }
162 
163  runTime.setTime(t, n);
164  Info << "Working on time = " << t << endl;
165 
166  if (fieldType == "scalar")
167  {
168  volScalarField fieldRead(
169  IOobject(
170  fieldName,
171  mesh.time().timeName(),
172  mesh,
173  IOobject::MUST_READ,
174  IOobject::NO_WRITE),
175  mesh);
176 
177  if (mode == "findCell")
178  {
179  point coordPoint = {probeCoord[0], probeCoord[1], probeCoord[2]};
180  label probeCellI = mesh.findCell(coordPoint);
181  if (probeCellI < 0)
182  {
183  Info << "Cell not found for coord: " << probeCoord << endl;
184  Info << "Exit!" << endl;
185  return 0;
186  }
187  fieldRead[probeCellI] = value[0];
188  }
189  else if (mode == "findNearestCell")
190  {
191  point coordPoint = {probeCoord[0], probeCoord[1], probeCoord[2]};
192  label probeCellI = mesh.findNearestCell(coordPoint);
193  fieldRead[probeCellI] = value[0];
194  }
195  else
196  {
197  FatalErrorIn("mode not valid! Options are findCell or findNearestCell")
198  << exit(FatalError);
199  }
200  fieldRead.write();
201  }
202  else if (fieldType == "vector")
203  {
204  volVectorField fieldRead(
205  IOobject(
206  fieldName,
207  mesh.time().timeName(),
208  mesh,
209  IOobject::MUST_READ,
210  IOobject::NO_WRITE),
211  mesh);
212 
213  if (mode == "findCell")
214  {
215  point coordPoint = {probeCoord[0], probeCoord[1], probeCoord[2]};
216  label probeCellI = mesh.findCell(coordPoint);
217  fieldRead[probeCellI] = value;
218  }
219  else if (mode == "findNearestCell")
220  {
221  point coordPoint = {probeCoord[0], probeCoord[1], probeCoord[2]};
222  label probeCellI = mesh.findNearestCell(coordPoint);
223  fieldRead[probeCellI] = value;
224  }
225  else
226  {
227  FatalErrorIn("mode not valid! Options are findCell or findNearestCell")
228  << exit(FatalError);
229  }
230  fieldRead.write();
231  }
232  else
233  {
234  Info << "fieldName and type not supported!" << endl;
235  }
236  }
237 
238  Info << "Finished!" << endl;
239 
240  return 0;
241 }
242 
243 // ************************************************************************* //
main
int main(int argc, char *argv[])
Definition: setProbeData.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
dafoam_plot3dtransform.mode
mode
Definition: dafoam_plot3dtransform.py:21