Skip to content

Commit

Permalink
Fixed bugs in snapCenter.
Browse files Browse the repository at this point in the history
  • Loading branch information
friedenhe committed Jun 30, 2024
1 parent 465a738 commit ac5ff75
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 26 deletions.
7 changes: 6 additions & 1 deletion src/adjoint/DAFvSource/DAFvSourceHeatSource.C
Original file line number Diff line number Diff line change
Expand Up @@ -121,6 +121,7 @@ DAFvSourceHeatSource::DAFvSourceHeatSource(
if (snappedCenterCellI_[sourceName] >= 0)
{
foundCellI = 1;
//Pout << "snap source " << sourceName << " to center " << mesh_.C()[snappedCenterCellI_[sourceName]] << endl;
}
reduce(foundCellI, sumOp<label>());
if (foundCellI != 1)
Expand All @@ -132,6 +133,10 @@ DAFvSourceHeatSource::DAFvSourceHeatSource(
<< " be outside of the mesh domain or on a mesh face "
<< abort(FatalError);
}

vector snappedCenter = vector::zero;
this->findGlobalSnappedCenter(snappedCenterCellI_[sourceName], snappedCenter);
Info << "heat source " << sourceName << " snap to center " << snappedCenter << endl;
}
}
else
Expand Down Expand Up @@ -236,7 +241,7 @@ void DAFvSourceHeatSource::calcFvSource(volScalarField& fvSource)
{
vector cylinderCenter =
{actuatorDiskDVs_[sourceName][0], actuatorDiskDVs_[sourceName][1], actuatorDiskDVs_[sourceName][2]};

if (snapCenter2Cell_[sourceName])
{
this->findGlobalSnappedCenter(snappedCenterCellI_[sourceName], cylinderCenter);
Expand Down
40 changes: 22 additions & 18 deletions src/adjoint/DAObjFunc/DAObjFuncLocation.C
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,9 @@ DAObjFuncLocation::DAObjFuncLocation(
<< " be outside of the mesh domain or on a mesh face "
<< abort(FatalError);
}
vector snappedCenter = vector::zero;
this->findGlobalSnappedCenter(snappedCenterCellI_, snappedCenter);
Info << "snap to center " << snappedCenter << endl;
}

if (mode_ == "maxRadius")
Expand Down Expand Up @@ -199,19 +202,19 @@ void DAObjFuncLocation::calcObjFunc(
// calculate Location
scalar objValTmp = 0.0;

vector center = center_;
if (snapCenter2Cell_)
{
this->findGlobalSnappedCenter(snappedCenterCellI_, center);
}

forAll(objFuncFaceSources, idxI)
{
const label& objFuncFaceI = objFuncFaceSources[idxI];
label bFaceI = objFuncFaceI - daIndex_.nLocalInternalFaces;
const label patchI = daIndex_.bFacePatchI[bFaceI];
const label faceI = daIndex_.bFaceFaceI[bFaceI];

vector center = center_;
if (snapCenter2Cell_)
{
this->findGlobalSnappedCenter(snappedCenterCellI_, center);
}

vector faceC = mesh_.Cf().boundaryField()[patchI][faceI] - center;

tensor faceCTensor(tensor::zero);
Expand Down Expand Up @@ -247,19 +250,19 @@ void DAObjFuncLocation::calcObjFunc(
// calculate Location
scalar objValTmp = 0.0;

vector center = center_;
if (snapCenter2Cell_)
{
this->findGlobalSnappedCenter(snappedCenterCellI_, center);
}

forAll(objFuncFaceSources, idxI)
{
const label& objFuncFaceI = objFuncFaceSources[idxI];
label bFaceI = objFuncFaceI - daIndex_.nLocalInternalFaces;
const label patchI = daIndex_.bFacePatchI[bFaceI];
const label faceI = daIndex_.bFaceFaceI[bFaceI];

vector center = center_;
if (snapCenter2Cell_)
{
this->findGlobalSnappedCenter(snappedCenterCellI_, center);
}

vector faceC = mesh_.Cf().boundaryField()[patchI][faceI] - center;

tensor faceCTensor(tensor::zero);
Expand Down Expand Up @@ -292,14 +295,15 @@ void DAObjFuncLocation::calcObjFunc(
else if (mode_ == "maxRadius")
{
scalar radius = 0.0;
if (maxRPatchI_ >= 0 && maxRFaceI_ >= 0)

vector center = center_;
if (snapCenter2Cell_)
{
this->findGlobalSnappedCenter(snappedCenterCellI_, center);
}

vector center = center_;
if (snapCenter2Cell_)
{
this->findGlobalSnappedCenter(snappedCenterCellI_, center);
}
if (maxRPatchI_ >= 0 && maxRFaceI_ >= 0)
{

vector faceC = mesh_.Cf().boundaryField()[maxRPatchI_][maxRFaceI_] - center;

Expand Down
30 changes: 23 additions & 7 deletions src/adjoint/DAObjFunc/DAObjFuncMass.C
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ DAObjFuncMass::DAObjFuncMass(

objFuncDict_.readEntry<scalar>("scale", scale_);

rho_ = objFuncDict_.lookupOrDefault<scalar>("rho", -1.0);
}

/// calculate the value of objective function
Expand Down Expand Up @@ -82,15 +83,30 @@ void DAObjFuncMass::calcObjFunc(
objFuncValue = 0.0;

const objectRegistry& db = mesh_.thisDb();
const volScalarField& rho = db.lookupObject<volScalarField>("solid:rho");

// calculate mass
forAll(objFuncCellSources, idxI)
if (rho_ < 0.0)
{
const label& cellI = objFuncCellSources[idxI];
scalar volume = mesh_.V()[cellI];
objFuncCellValues[idxI] = scale_ * volume * rho[cellI];
objFuncValue += objFuncCellValues[idxI];
const volScalarField& rho = db.lookupObject<volScalarField>("solid:rho");

// calculate mass
forAll(objFuncCellSources, idxI)
{
const label& cellI = objFuncCellSources[idxI];
scalar volume = mesh_.V()[cellI];
objFuncCellValues[idxI] = scale_ * volume * rho[cellI];
objFuncValue += objFuncCellValues[idxI];
}
}
else
{
// calculate mass
forAll(objFuncCellSources, idxI)
{
const label& cellI = objFuncCellSources[idxI];
scalar volume = mesh_.V()[cellI];
objFuncCellValues[idxI] = scale_ * volume * rho_;
objFuncValue += objFuncCellValues[idxI];
}
}

// need to reduce the sum of force across all processors
Expand Down
3 changes: 3 additions & 0 deletions src/adjoint/DAObjFunc/DAObjFuncMass.H
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ public:
TypeName("mass");
// Constructors

/// user-defined density
scalar rho_ = -1.0;

//- Construct from components
DAObjFuncMass(
const fvMesh& mesh,
Expand Down

0 comments on commit ac5ff75

Please sign in to comment.