|
| 1 | +/*---------------------------------------------------------------------------*\ |
| 2 | +
|
| 3 | + DAFoam : Discrete Adjoint with OpenFOAM |
| 4 | + Version : v4 |
| 5 | +
|
| 6 | +Description |
| 7 | + Deform the mesh for field inversion |
| 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 | + Info << "Deform the mesh for field inversion...." << endl; |
| 25 | + |
| 26 | + argList::addOption( |
| 27 | + "origin", |
| 28 | + "(0 0 0)", |
| 29 | + "prescribe the origin to deform the mesh"); |
| 30 | + |
| 31 | + argList::addOption( |
| 32 | + "axis", |
| 33 | + "(0 0 0)", |
| 34 | + "prescribe the axis to deform the mesh"); |
| 35 | + |
| 36 | + argList::addOption( |
| 37 | + "omega", |
| 38 | + "0", |
| 39 | + "prescribe the angular velocity to deform the mesh"); |
| 40 | + |
| 41 | + argList::addOption( |
| 42 | + "time", |
| 43 | + "-1", |
| 44 | + "prescribe a specific time to deform the mesh"); |
| 45 | + |
| 46 | +#include "setRootCase.H" |
| 47 | +#include "createTime.H" |
| 48 | +#include "createMesh.H" |
| 49 | + |
| 50 | + |
| 51 | + vector origin={0.0, 0.0, 0.0}; |
| 52 | + if (args.readIfPresent("origin", origin)) |
| 53 | + { |
| 54 | + Info<< "The origin to rotate the mesh: " << origin << endl; |
| 55 | + } |
| 56 | + else |
| 57 | + { |
| 58 | + Info << "origin not set!" << endl; |
| 59 | + } |
| 60 | + |
| 61 | + vector axis={0, 0, 0}; |
| 62 | + if (args.readIfPresent("axis", axis)) |
| 63 | + { |
| 64 | + Info<< "The axis to rotate the mesh: " << axis << endl; |
| 65 | + } |
| 66 | + else |
| 67 | + { |
| 68 | + Info << "axis center not set!" << endl; |
| 69 | + } |
| 70 | + |
| 71 | + scalar omega = 0.0; |
| 72 | + if (args.readIfPresent("omega", omega)) |
| 73 | + { |
| 74 | + Info<< "The angular velocity to rotate the mesh: " << omega << endl; |
| 75 | + } |
| 76 | + else |
| 77 | + { |
| 78 | + Info << "omega not set! Don't rotate mesh." << endl; |
| 79 | + } |
| 80 | + |
| 81 | + scalar time = -1.0; |
| 82 | + if (args.optionFound("time")) |
| 83 | + { |
| 84 | + time = readScalar(args.optionLookup("time")()); |
| 85 | + if (time == 9999) |
| 86 | + { |
| 87 | + Info << "Deform latestTime" << endl; |
| 88 | + } |
| 89 | + else |
| 90 | + { |
| 91 | + Info << "Deform time = " << time << endl; |
| 92 | + } |
| 93 | + } |
| 94 | + else |
| 95 | + { |
| 96 | + Info << "Time not set! Deform all time instances." << endl; |
| 97 | + } |
| 98 | + |
| 99 | + Info << "Deforming the mesh..." << endl; |
| 100 | + |
| 101 | + while (runTime.run()) |
| 102 | + { |
| 103 | + ++runTime; |
| 104 | + |
| 105 | + Info<< "Time = " << runTime.timeName() << nl << endl; |
| 106 | + |
| 107 | + pointField ourNewPoints(mesh.points()); |
| 108 | + scalar theta = omega * runTime.deltaT().value(); |
| 109 | + scalar cosTheta = std::cos(theta); |
| 110 | + scalar sinTheta = std::sin(theta); |
| 111 | + |
| 112 | + forAll(ourNewPoints, pointI) |
| 113 | + { |
| 114 | + scalar xTemp = ourNewPoints[pointI][0] - origin[0]; |
| 115 | + scalar yTemp = ourNewPoints[pointI][1] - origin[1]; |
| 116 | + |
| 117 | + ourNewPoints[pointI][0] = cosTheta * xTemp - sinTheta * yTemp + origin[0]; |
| 118 | + ourNewPoints[pointI][1] = sinTheta * xTemp + cosTheta * yTemp + origin[1]; |
| 119 | + } |
| 120 | + |
| 121 | + mesh.movePoints(ourNewPoints); |
| 122 | + |
| 123 | + pointIOField writePoints |
| 124 | + ( |
| 125 | + IOobject |
| 126 | + ( |
| 127 | + "points", |
| 128 | + runTime.timeName(), |
| 129 | + "polyMesh", |
| 130 | + mesh, |
| 131 | + IOobject::NO_READ, |
| 132 | + IOobject::AUTO_WRITE |
| 133 | + ), |
| 134 | + mesh.points() |
| 135 | + ); |
| 136 | + |
| 137 | + runTime.write(); |
| 138 | + } |
| 139 | + |
| 140 | + Info << "Finished!" << endl; |
| 141 | + |
| 142 | + return 0; |
| 143 | +} |
| 144 | + |
| 145 | +// ************************************************************************* // |
0 commit comments