Skip to content

Commit

Permalink
Enabled UMag to change during varyingVelocity.
Browse files Browse the repository at this point in the history
  • Loading branch information
friedenhe committed Dec 9, 2023
1 parent b3b52df commit 0c7ad1a
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 86 deletions.
5 changes: 3 additions & 2 deletions dafoam/pyDAFoam.py
Original file line number Diff line number Diff line change
Expand Up @@ -2464,8 +2464,6 @@ def solveAdjointUnsteady(self):
self.calcTotalDerivsField(objFuncName, designVarName, fieldType, dFScaling, True)
else:
raise Error("designVarType not valid!")

Info("Computing partials for old times %f s" % self.solver.getElapsedClockTime())

# we need to calculate dRdW0TPsi for the previous time step
if ddtSchemeOrder == 1:
Expand Down Expand Up @@ -3083,6 +3081,9 @@ def _initSolver(self):

if self.solverInitialized == 1:
raise Error("pyDAFoam: self._initSolver has been called! One shouldn't initialize solvers twice!")

Info("Init solver. ElapsedClockTime %f s" % self.solver.getElapsedClockTime())
Info("Init solver. ElapsedCpuTime %f s" % self.solver.getElapsedCpuTime())

solverName = self.getOption("solverName")
solverArg = solverName + " -python " + self.parallelFlag
Expand Down
2 changes: 1 addition & 1 deletion src/adjoint/Make/files_Compressible
Original file line number Diff line number Diff line change
Expand Up @@ -91,7 +91,7 @@ DASolver/DATurboFoam/DATurboFoam.C

boundaryConditions/multiFreqVector/multiFreqVectorFvPatchField.C
boundaryConditions/nutUSpaldingWallFunctionDF/nutUSpaldingWallFunctionFvPatchScalarFieldDF.C
boundaryConditions/varyingFlowDirection/varyingFlowDirectionFvPatchVectorField.C
boundaryConditions/varyingVelocity/varyingVelocityFvPatchVectorField.C
models/dummyTurbulenceModel/makeDummyTurbulenceModelCompressible.C
models/SpalartAllmarasFv3/makeSpalartAllmarasFv3Compressible.C
models/SpalartAllmarasFv3FieldInversion/makeSpalartAllmarasFv3FieldInversionCompressible.C
Expand Down
2 changes: 1 addition & 1 deletion src/adjoint/Make/files_Incompressible
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,7 @@ DASolver/DAPimpleDyMFoam/DAPimpleDyMFoam.C

boundaryConditions/multiFreqVector/multiFreqVectorFvPatchField.C
boundaryConditions/nutUSpaldingWallFunctionDF/nutUSpaldingWallFunctionFvPatchScalarFieldDF.C
boundaryConditions/varyingFlowDirection/varyingFlowDirectionFvPatchVectorField.C
boundaryConditions/varyingVelocity/varyingVelocityFvPatchVectorField.C
models/dummyTurbulenceModel/makeDummyTurbulenceModelIncompressible.C
models/SpalartAllmarasFv3/makeSpalartAllmarasFv3Incompressible.C
models/SpalartAllmarasFv3FieldInversion/makeSpalartAllmarasFv3FieldInversionIncompressible.C
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,34 +27,36 @@
\*---------------------------------------------------------------------------*/

#include "varyingFlowDirectionFvPatchVectorField.H"
#include "varyingVelocityFvPatchVectorField.H"
#include "volFields.H"
#include "addToRunTimeSelectionTable.H"

// * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //

Foam::varyingFlowDirectionFvPatchVectorField::varyingFlowDirectionFvPatchVectorField(
Foam::varyingVelocityFvPatchVectorField::varyingVelocityFvPatchVectorField(
const fvPatch& p,
const DimensionedField<vector, volMesh>& iF)
: fixedValueFvPatchVectorField(p, iF),
UMag_(0.0),
flowDir_("x"),
normalDir_("y"),
U0_(0.0),
URate_(0.0),
flowComponent_(0),
normalComponent_(1),
alpha0_(0),
rate_(0)
alphaRate_(0)
{
}

Foam::varyingFlowDirectionFvPatchVectorField::varyingFlowDirectionFvPatchVectorField(
Foam::varyingVelocityFvPatchVectorField::varyingVelocityFvPatchVectorField(
const fvPatch& p,
const DimensionedField<vector, volMesh>& iF,
const dictionary& dict)
: fixedValueFvPatchVectorField(p, iF),
UMag_(dict.lookupOrDefault<scalar>("UMag", 0.0)),
flowDir_(dict.lookupOrDefault<word>("flowDir", "x")),
normalDir_(dict.lookupOrDefault<word>("normalDir", "y")),
U0_(dict.lookupOrDefault<scalar>("U0", 0.0)),
URate_(dict.lookupOrDefault<scalar>("URate", 0.0)),
flowComponent_(dict.lookupOrDefault<label>("flowComponent", 0)),
normalComponent_(dict.lookupOrDefault<label>("normalComponent", 1)),
alpha0_(dict.lookupOrDefault<scalar>("alpha0", 0.0)),
rate_(dict.lookupOrDefault<scalar>("rate", 0.0))
alphaRate_(dict.lookupOrDefault<scalar>("alphaRate", 0.0))
{

if (dict.found("value"))
Expand All @@ -68,81 +70,79 @@ Foam::varyingFlowDirectionFvPatchVectorField::varyingFlowDirectionFvPatchVectorF
}
}

Foam::varyingFlowDirectionFvPatchVectorField::varyingFlowDirectionFvPatchVectorField(
const varyingFlowDirectionFvPatchVectorField& ptf,
Foam::varyingVelocityFvPatchVectorField::varyingVelocityFvPatchVectorField(
const varyingVelocityFvPatchVectorField& ptf,
const fvPatch& p,
const DimensionedField<vector, volMesh>& iF,
const fvPatchFieldMapper& mapper)
: fixedValueFvPatchVectorField(p, iF),
UMag_(ptf.UMag_),
flowDir_(ptf.flowDir_),
normalDir_(ptf.normalDir_),
U0_(ptf.U0_),
URate_(ptf.URate_),
flowComponent_(ptf.flowComponent_),
normalComponent_(ptf.normalComponent_),
alpha0_(ptf.alpha0_),
rate_(ptf.rate_)
alphaRate_(ptf.alphaRate_)
{
this->evaluate();
}

Foam::varyingFlowDirectionFvPatchVectorField::varyingFlowDirectionFvPatchVectorField(
const varyingFlowDirectionFvPatchVectorField& wbppsf)
Foam::varyingVelocityFvPatchVectorField::varyingVelocityFvPatchVectorField(
const varyingVelocityFvPatchVectorField& wbppsf)
: fixedValueFvPatchVectorField(wbppsf),
UMag_(wbppsf.UMag_),
flowDir_(wbppsf.flowDir_),
normalDir_(wbppsf.normalDir_),
U0_(wbppsf.U0_),
URate_(wbppsf.URate_),
flowComponent_(wbppsf.flowComponent_),
normalComponent_(wbppsf.normalComponent_),
alpha0_(wbppsf.alpha0_),
rate_(wbppsf.rate_)
alphaRate_(wbppsf.alphaRate_)
{
this->evaluate();
}

Foam::varyingFlowDirectionFvPatchVectorField::varyingFlowDirectionFvPatchVectorField(
const varyingFlowDirectionFvPatchVectorField& wbppsf,
Foam::varyingVelocityFvPatchVectorField::varyingVelocityFvPatchVectorField(
const varyingVelocityFvPatchVectorField& wbppsf,
const DimensionedField<vector, volMesh>& iF)
: fixedValueFvPatchVectorField(wbppsf, iF),
UMag_(wbppsf.UMag_),
flowDir_(wbppsf.flowDir_),
normalDir_(wbppsf.normalDir_),
U0_(wbppsf.U0_),
URate_(wbppsf.URate_),
flowComponent_(wbppsf.flowComponent_),
normalComponent_(wbppsf.normalComponent_),
alpha0_(wbppsf.alpha0_),
rate_(wbppsf.rate_)
alphaRate_(wbppsf.alphaRate_)
{
this->evaluate();
}

// * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
void Foam::varyingFlowDirectionFvPatchVectorField::updateCoeffs()
void Foam::varyingVelocityFvPatchVectorField::updateCoeffs()
{
// calculate patch values
const scalar t = this->db().time().timeOutputValue();
scalar alpha = alpha0_ + t * rate_;

HashTable<label> compTable;
compTable.set("x", 0);
compTable.set("y", 1);
compTable.set("z", 2);
scalar alpha = alpha0_ + t * alphaRate_;
scalar U = U0_ + t * URate_;

vectorField& thisPatchRef = *this;
vectorField thisPatch = thisPatchRef;
forAll(thisPatch, idxI)
forAll(thisPatch, idxI)
{
label compFlow = compTable[flowDir_];
label compNormal = compTable[normalDir_];
thisPatch[idxI][compFlow] = UMag_ * cos(alpha);
thisPatch[idxI][compNormal] = UMag_ * sin(alpha);
thisPatch[idxI][flowComponent_] = U * cos(alpha);
thisPatch[idxI][normalComponent_] = U * sin(alpha);
}

fvPatchVectorField::operator==(thisPatch);

fixedValueFvPatchVectorField::updateCoeffs();
}

void Foam::varyingFlowDirectionFvPatchVectorField::write(Ostream& os) const
void Foam::varyingVelocityFvPatchVectorField::write(Ostream& os) const
{
fixedValueFvPatchVectorField::write(os);
os.writeEntry("UMag", UMag_);
os.writeEntry("flowDir", flowDir_);
os.writeEntry("normalDir", normalDir_);
os.writeEntry("U0", U0_);
os.writeEntry("URate", URate_);
os.writeEntry("flowComponent", flowComponent_);
os.writeEntry("normalComponent", normalComponent_);
os.writeEntry("alpha0", alpha0_);
os.writeEntry("rate", rate_);
os.writeEntry("alphaRate", alphaRate_);
//writeEntry("value", os);
}

Expand All @@ -152,7 +152,7 @@ namespace Foam
{
makePatchTypeField(
fvPatchVectorField,
varyingFlowDirectionFvPatchVectorField);
varyingVelocityFvPatchVectorField);
}

// ************************************************************************* //
Original file line number Diff line number Diff line change
Expand Up @@ -27,39 +27,42 @@
Description:
This boundary condition provides a varying flow direction at the far field
This boundary condition provides a varying velocity for the patch
U_flowDir = UMag * cos(alpha)
U_normalDir = UMag * sin(alpha)
alpha = alpha0 + rate * t
U[flowComponent] = U * cos(alpha)
U[normalComponent] = U * sin(alpha)
alpha = alpha0 + alphaRate * t
U = U0 + URate * t
where
U_flowDir : velocity component parallel to the flow. Options are: x, y, or z
U_normalDir : velocity component normal to the flow. Options are: x, y, or z
UMag : velocity magnitude [m/s]
flowComponent : velocity component parallel to the flow. Options are: 0, 1, or 2, which corresponds to x, y, and z
normalComponent : velocity component normal to the flow. Options are: 0, 1, or 2, which corresponds to x, y, and z
U0 : initial velocity magnitude [m/s]
URate : rate of change for the velocity magnitude
alpha0 : initial flow angle [rad]
rate : rate of change for the flow angle [rad/s]
alphaRate : rate of change for the flow angle [rad/s]
Example of the boundary condition specification:
myPatch
{
type varyingFlowDirection;
UMag 10;
flowDir x;
normalDir y;
type varyingVelocity;
U0 10;
URate -0.2;
flowComponent 0;
normalComponent 1;
alpha0 0;
rate 0.1;
alphaRate 0.1;
}
This will change the flow direction with a rate of 0.1 rad/s starting from 0 rad
U[0] = cos(0+0.1*t)
U[1] = sin(0+0.1*t)
U[0] = (10-0.2*t)*cos(0+0.1*t)
U[1] = (10-0.2*t)*sin(0+0.1*t)
\*---------------------------------------------------------------------------*/

#ifndef varyingFlowDirectionFvPatchVectorFields_H
#define varyingFlowDirectionFvPatchVectorFields_H
#ifndef varyingVelocityFvPatchVectorFields_H
#define varyingVelocityFvPatchVectorFields_H

#include "fixedValueFvPatchFields.H"

Expand All @@ -69,67 +72,68 @@ namespace Foam
{

/*---------------------------------------------------------------------------*\
Class varyingFlowDirectionFvPatchVectorField Declaration
Class varyingVelocityFvPatchVectorField Declaration
\*---------------------------------------------------------------------------*/

class varyingFlowDirectionFvPatchVectorField
class varyingVelocityFvPatchVectorField
: public fixedValueFvPatchVectorField
{
// Private data

scalar UMag_;
word flowDir_;
word normalDir_;
scalar U0_;
scalar URate_;
label flowComponent_;
label normalComponent_;
scalar alpha0_;
scalar rate_;
scalar alphaRate_;

public:
//- Runtime type information
TypeName("varyingFlowDirection");
TypeName("varyingVelocity");

// Constructors

//- Construct from patch and internal field
varyingFlowDirectionFvPatchVectorField(
varyingVelocityFvPatchVectorField(
const fvPatch&,
const DimensionedField<vector, volMesh>&);

//- Construct from patch, internal field and dictionary
varyingFlowDirectionFvPatchVectorField(
varyingVelocityFvPatchVectorField(
const fvPatch&,
const DimensionedField<vector, volMesh>&,
const dictionary&);

//- Construct by mapping given varyingFlowDirectionFvPatchVectorField onto
//- Construct by mapping given varyingVelocityFvPatchVectorField onto
// a new patch
varyingFlowDirectionFvPatchVectorField(
const varyingFlowDirectionFvPatchVectorField&,
varyingVelocityFvPatchVectorField(
const varyingVelocityFvPatchVectorField&,
const fvPatch&,
const DimensionedField<vector, volMesh>&,
const fvPatchFieldMapper&);

//- Construct as copy
varyingFlowDirectionFvPatchVectorField(
const varyingFlowDirectionFvPatchVectorField&);
varyingVelocityFvPatchVectorField(
const varyingVelocityFvPatchVectorField&);

//- Construct and return a clone
virtual tmp<fvPatchVectorField> clone() const
{
return tmp<fvPatchVectorField>(
new varyingFlowDirectionFvPatchVectorField(*this));
new varyingVelocityFvPatchVectorField(*this));
}

//- Construct as copy setting internal field reference
varyingFlowDirectionFvPatchVectorField(
const varyingFlowDirectionFvPatchVectorField&,
varyingVelocityFvPatchVectorField(
const varyingVelocityFvPatchVectorField&,
const DimensionedField<vector, volMesh>&);

//- Construct and return a clone setting internal field reference
virtual tmp<fvPatchVectorField> clone(
const DimensionedField<vector, volMesh>& iF) const
{
return tmp<fvPatchVectorField>(
new varyingFlowDirectionFvPatchVectorField(*this, iF));
new varyingVelocityFvPatchVectorField(*this, iF));
}

// Member functions
Expand Down

0 comments on commit 0c7ad1a

Please sign in to comment.