|
| 1 | +/*---------------------------------------------------------------------------*\ |
| 2 | +
|
| 3 | + DAFoam : Discrete Adjoint with OpenFOAM |
| 4 | + Version : v3 |
| 5 | +
|
| 6 | + Description: |
| 7 | + Extract time-series data 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 | +#include "setRootCase.H" |
| 45 | +#include "createTime.H" |
| 46 | +#include "createMesh.H" |
| 47 | + |
| 48 | + word outputName = "VarTimeSeries"; |
| 49 | + if (args.optionFound("outputName")) |
| 50 | + { |
| 51 | + outputName = word(args.optionLookup("outputName")()); |
| 52 | + } |
| 53 | + |
| 54 | + List<scalar> coords; |
| 55 | + if (args.optionFound("coords")) |
| 56 | + { |
| 57 | + coords = scalarList(args.optionLookup("coords")()); |
| 58 | + } |
| 59 | + else |
| 60 | + { |
| 61 | + Info << "Error: coords not set! Exit." << endl; |
| 62 | + return 1; |
| 63 | + } |
| 64 | + point coordPoint = {coords[0], coords[1], coords[2]}; |
| 65 | + label probeCellI = mesh.findCell(coordPoint); |
| 66 | + |
| 67 | + word varName; |
| 68 | + if (args.optionFound("varName")) |
| 69 | + { |
| 70 | + varName = word(args.optionLookup("varName")()); |
| 71 | + } |
| 72 | + else |
| 73 | + { |
| 74 | + Info << "Error: varName not set! Exit." << endl; |
| 75 | + return 1; |
| 76 | + } |
| 77 | + |
| 78 | + word varType; |
| 79 | + if (args.optionFound("varType")) |
| 80 | + { |
| 81 | + varType = word(args.optionLookup("varType")()); |
| 82 | + } |
| 83 | + else |
| 84 | + { |
| 85 | + Info << "Error: varType not set! Exit." << endl; |
| 86 | + return 1; |
| 87 | + } |
| 88 | + |
| 89 | + if (probeCellI < 0) |
| 90 | + { |
| 91 | + Info << "Error: coords " << coords << " are not within a cell! Exit." << endl; |
| 92 | + return 1; |
| 93 | + } |
| 94 | + |
| 95 | + if (varType != "scalar" && varType != "vector") |
| 96 | + { |
| 97 | + Info << "Error: varType = " << varType << " is not supported. The options are either scalar or vector " << endl; |
| 98 | + } |
| 99 | + |
| 100 | + OFstream f(outputName + ".txt"); |
| 101 | + |
| 102 | + scalar endTime = runTime.endTime().value(); |
| 103 | + scalar deltaT = runTime.deltaT().value(); |
| 104 | + label nSteps = round(endTime / deltaT); |
| 105 | + |
| 106 | + for (label i = 0; i < nSteps; i++) |
| 107 | + { |
| 108 | + word timeName = Foam::name(i * deltaT); |
| 109 | + |
| 110 | + if (varType == "vector") |
| 111 | + { |
| 112 | + volVectorField var( |
| 113 | + IOobject( |
| 114 | + varName, |
| 115 | + timeName, |
| 116 | + mesh, |
| 117 | + IOobject::MUST_READ, |
| 118 | + IOobject::NO_WRITE), |
| 119 | + mesh); |
| 120 | + |
| 121 | + f << var[probeCellI][0] << " " << var[probeCellI][1] << " " << var[probeCellI][2] << endl; |
| 122 | + } |
| 123 | + else if (varType == "scalar") |
| 124 | + { |
| 125 | + volScalarField var( |
| 126 | + IOobject( |
| 127 | + varName, |
| 128 | + timeName, |
| 129 | + mesh, |
| 130 | + IOobject::MUST_READ, |
| 131 | + IOobject::NO_WRITE), |
| 132 | + mesh); |
| 133 | + |
| 134 | + f << var[probeCellI] << endl; |
| 135 | + } |
| 136 | + } |
| 137 | + |
| 138 | + Info << "Done! " << endl; |
| 139 | + |
| 140 | + return 0; |
| 141 | +} |
| 142 | + |
| 143 | +// ************************************************************************* // |
0 commit comments