Skip to content

Commit

Permalink
Added snapCenterToCell for heat source.
Browse files Browse the repository at this point in the history
  • Loading branch information
friedenhe committed Jun 28, 2024
1 parent 0170ac1 commit 5da16ff
Show file tree
Hide file tree
Showing 6 changed files with 71 additions and 10 deletions.
23 changes: 23 additions & 0 deletions src/adjoint/DAFvSource/DAFvSource.C
Original file line number Diff line number Diff line change
Expand Up @@ -199,6 +199,29 @@ void DAFvSource::syncDAOptionToActuatorDVs()
}
}
}

void DAFvSource::findGlobalSnappedCenter(
label snappedCenterCellI,
vector& center)
{
scalar centerX = 0.0;
scalar centerY = 0.0;
scalar centerZ = 0.0;

if (snappedCenterCellI >= 0)
{
centerX = mesh_.C()[snappedCenterCellI][0];
centerY = mesh_.C()[snappedCenterCellI][1];
centerZ = mesh_.C()[snappedCenterCellI][2];
}
reduce(centerX, sumOp<scalar>());
reduce(centerY, sumOp<scalar>());
reduce(centerZ, sumOp<scalar>());

center[0] = centerX;
center[1] = centerY;
center[2] = centerZ;
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

} // End namespace Foam
Expand Down
4 changes: 4 additions & 0 deletions src/adjoint/DAFvSource/DAFvSource.H
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,10 @@ public:

/// virtual function for regIOobject
bool writeData(Ostream& os) const;

void findGlobalSnappedCenter(
label snappedCenterCellI,
vector& center);
};

// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
Expand Down
39 changes: 33 additions & 6 deletions src/adjoint/DAFvSource/DAFvSourceHeatSource.C
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,12 @@ DAFvSourceHeatSource::DAFvSourceHeatSource(
{
printInterval_ = daOption.getOption<label>("printInterval");

// now we need to initialize actuatorDiskDVs_ by synchronizing the values
// defined in fvSource from DAOption to actuatorDiskDVs_
// NOTE: we need to call this function whenever we change the actuator
// design variables during optimization
this->syncDAOptionToActuatorDVs();

const dictionary& allOptions = daOption_.getAllOptions();

dictionary fvSourceSubDict = allOptions.subDict("fvSource");
Expand Down Expand Up @@ -101,6 +107,28 @@ DAFvSourceHeatSource::DAFvSourceHeatSource(
// eps is a smoothing parameter, it should be the local mesh cell size in meters
// near the cylinder region
cylinderEps_.set(sourceName, sourceSubDict.getScalar("eps"));

snapCenter2Cell_.set(sourceName, sourceSubDict.lookupOrDefault<label>("snapCenter2Cell", 0));
if (snapCenter2Cell_[sourceName])
{
point centerPoint = {actuatorDiskDVs_[sourceName][0], actuatorDiskDVs_[sourceName][1], actuatorDiskDVs_[sourceName][2]};
snappedCenterCellI_.set(sourceName, mesh_.findCell(centerPoint));
label foundCellI = 0;
if (snappedCenterCellI_[sourceName] >= 0)
{
foundCellI = 1;
}
reduce(foundCellI, sumOp<label>());
if (foundCellI != 1)
{
FatalErrorIn(" ") << "There should be only one cell found globally while "
<< foundCellI << " was returned."
<< " Please adjust center such that it is located completely"
<< " within a cell in the mesh domain. The center should not "
<< " be outside of the mesh domain or on a mesh face "
<< abort(FatalError);
}
}
}
else
{
Expand All @@ -109,12 +137,6 @@ DAFvSourceHeatSource::DAFvSourceHeatSource(
<< abort(FatalError);
}
}

// now we need to initialize actuatorDiskDVs_ by synchronizing the values
// defined in fvSource from DAOption to actuatorDiskDVs_
// NOTE: we need to call this function whenever we change the actuator
// design variables during optimization
this->syncDAOptionToActuatorDVs();
}
// * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //

Expand Down Expand Up @@ -210,6 +232,11 @@ void DAFvSourceHeatSource::calcFvSource(volScalarField& fvSource)
{
vector cylinderCenter =
{actuatorDiskDVs_[sourceName][0], actuatorDiskDVs_[sourceName][1], actuatorDiskDVs_[sourceName][2]};

if (snapCenter2Cell_[sourceName])
{
this->findGlobalSnappedCenter(snappedCenterCellI_[sourceName], cylinderCenter);
}
vector cylinderDir =
{actuatorDiskDVs_[sourceName][3], actuatorDiskDVs_[sourceName][4], actuatorDiskDVs_[sourceName][5]};
scalar radius = actuatorDiskDVs_[sourceName][6];
Expand Down
6 changes: 6 additions & 0 deletions src/adjoint/DAFvSource/DAFvSourceHeatSource.H
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,12 @@ protected:
/// print interval for primal and adjoint
label printInterval_;

/// whether to snap the center to a cell in the mesh if yes the center will move with the mesh
HashTable<label> snapCenter2Cell_;

/// the cell index for the center if snapCenter2Cell_ = 1
HashTable<label> snappedCenterCellI_;

public:
TypeName("heatSource");
// Constructors
Expand Down
6 changes: 3 additions & 3 deletions tests/refs/DAFoam_Test_DAHeatTransferFoamRef.txt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
Dictionary Key: HF_INNER
@value 179999.9999999994 1e-08 1e-10
@value 180150.558627527 1e-08 1e-10
Dictionary Key: HF_OUTER
@value -220000.0000000005 1e-08 1e-10
@value -220150.5586275289 1e-08 1e-10
Dictionary Key: IR1KS
@value 19.89626068314585 1e-08 1e-10
Dictionary Key: R1
@value 0.994617581067802 1e-08 1e-10
Dictionary Key: R1KS
@value 6.252992336157548 1e-08 1e-10
Dictionary Key: TNormSum
@value 6400011.35124065 1e-08 1e-10
@value 6400011.351333449 1e-08 1e-10
Dictionary Key: fail
@value 0 1e-08 1e-10
3 changes: 2 additions & 1 deletion tests/runTests_DAHeatTransferFoam.py
Original file line number Diff line number Diff line change
Expand Up @@ -102,12 +102,13 @@
"source2": {
"type": "heatSource",
"source": "cylinderSmooth",
"center": [0.2, 0.055, 0.025],
"center": [0.201, 0.056, 0.026],
"axis": [1.0, 0.0, 0.0],
"length": 0.4,
"radius": 0.005,
"power": 1000.0,
"eps": 0.001,
"snapCenter2Cell": True,
},
},
}
Expand Down

0 comments on commit 5da16ff

Please sign in to comment.