22 DAPartDeriv::DAPartDeriv(
30 : modelType_(modelType),
36 daResidual_(daResidual),
37 allOptions_(
daOption.getAllOptions())
56 if (
daOption.getAllOptions().lookupOrDefault<label>(
"debug", 0))
58 Info <<
"Selecting " << modelType <<
" for DAPartDeriv" << endl;
61 dictionaryConstructorTable::iterator cstrIter =
62 dictionaryConstructorTablePtr_->find(modelType);
65 if (cstrIter == dictionaryConstructorTablePtr_->end())
78 <<
"Unknown DAPartDeriv type "
79 << modelType << nl << nl
80 <<
"Valid DAPartDeriv types:" << endl
81 << dictionaryConstructorTablePtr_->sortedToc()
86 return autoPtr<DAPartDeriv>(
99 const Vec jacConColors,
100 const Vec normStatePerturbVec,
133 PetscInt Istart, Iend;
134 VecGetOwnershipRange(jacConColors, &Istart, &Iend);
136 const PetscScalar* colorArray;
137 VecGetArrayRead(jacConColors, &colorArray);
139 const PetscScalar* normStateArray;
140 VecGetArrayRead(normStatePerturbVec, &normStateArray);
142 PetscScalar* wVecArray;
143 VecGetArray(wVec, &wVecArray);
145 PetscScalar deltaVal = 0.0;
148 for (label i = Istart; i < Iend; i++)
150 label relIdx = i - Istart;
151 label colorJ = colorArray[relIdx];
152 if (colorI == colorJ)
154 wVecArray[relIdx] += deltaVal * normStateArray[relIdx];
158 VecRestoreArrayRead(jacConColors, &colorArray);
159 VecRestoreArray(wVec, &wVecArray);
160 VecRestoreArrayRead(normStatePerturbVec, &normStateArray);
167 const Vec coloredColumn,
168 const label transposed,
170 const scalar jacLowerBound)
const
225 PetscInt Istart, Iend;
226 const PetscScalar* resVecArray;
227 const PetscScalar* coloredColumnArray;
229 VecGetArrayRead(resVec, &resVecArray);
230 VecGetArrayRead(coloredColumn, &coloredColumnArray);
233 VecGetOwnershipRange(resVec, &Istart, &Iend);
237 for (PetscInt i = Istart; i < Iend; i++)
239 label relIdx = i - Istart;
240 colI = coloredColumnArray[relIdx];
244 val = resVecArray[relIdx];
248 if(jacLowerBound < 1.0e-16 || fabs(val) > jacLowerBound || colI == rowI)
252 MatSetValue(jacMat, colI, rowI, val, INSERT_VALUES);
256 MatSetValue(jacMat, rowI, colI, val, INSERT_VALUES);
262 VecRestoreArrayRead(resVec, &resVecArray);
263 VecRestoreArrayRead(coloredColumn, &coloredColumnArray);
267 const dictionary options,
288 options.readEntry<word>(
"variable", varName);
289 options.readEntry<wordList>(
"patches", patches);
290 options.readEntry<label>(
"comp", comp);
295 word patchName = patches[idxI];
297 label patchI =
mesh_.boundaryMesh().findPatchID(patchName);
299 if (
mesh_.thisDb().foundObject<volVectorField>(varName))
301 volVectorField& state(
const_cast<volVectorField&
>(
302 mesh_.thisDb().lookupObject<volVectorField>(varName)));
305 if (
mesh_.boundaryMesh()[patchI].size() > 0)
307 if (state.boundaryFieldRef()[patchI].type() ==
"fixedValue")
309 forAll(state.boundaryField()[patchI], faceI)
311 state.boundaryFieldRef()[patchI][faceI][comp] += delta;
314 else if (state.boundaryFieldRef()[patchI].type() ==
"inletOutlet"
315 || state.boundaryFieldRef()[patchI].type() ==
"outletInlet")
318 mixedFvPatchField<vector>& inletOutletPatch =
319 refCast<mixedFvPatchField<vector>>(state.boundaryFieldRef()[patchI]);
320 vector perturbedValue = inletOutletPatch.refValue()[0];
321 perturbedValue[comp] += delta;
322 inletOutletPatch.refValue() = perturbedValue;
326 FatalErrorIn(
"") <<
"boundaryType: " << state.boundaryFieldRef()[patchI].type()
328 <<
"Avaiable options are: fixedValue, inletOutlet, outletInlet"
329 << abort(FatalError);
333 else if (
mesh_.thisDb().foundObject<volScalarField>(varName))
335 volScalarField& state(
const_cast<volScalarField&
>(
336 mesh_.thisDb().lookupObject<volScalarField>(varName)));
339 if (
mesh_.boundaryMesh()[patchI].size() > 0)
341 if (state.boundaryFieldRef()[patchI].type() ==
"fixedValue")
343 forAll(state.boundaryField()[patchI], faceI)
345 state.boundaryFieldRef()[patchI][faceI] += delta;
348 else if (state.boundaryFieldRef()[patchI].type() ==
"inletOutlet"
349 || state.boundaryFieldRef()[patchI].type() ==
"outletInlet")
352 mixedFvPatchField<scalar>& inletOutletPatch =
353 refCast<mixedFvPatchField<scalar>>(state.boundaryFieldRef()[patchI]);
354 scalar perturbedValue = inletOutletPatch.refValue()[0];
355 perturbedValue += delta;
356 inletOutletPatch.refValue() = perturbedValue;
360 FatalErrorIn(
"") <<
"boundaryType: " << state.boundaryFieldRef()[patchI].type()
362 <<
"Avaiable options are: fixedValue, inletOutlet, outletInlet"
363 << abort(FatalError);
369 FatalErrorIn(
"") << varName <<
" is neither volVectorField nor volScalarField!"
370 << abort(FatalError);
376 const dictionary options,
411 options.readEntry<wordList>(
"patches", patches);
413 HashTable<label> axisIndices;
414 axisIndices.set(
"x", 0);
415 axisIndices.set(
"y", 1);
416 axisIndices.set(
"z", 2);
417 word flowAxis = options.getWord(
"flowAxis");
418 word normalAxis = options.getWord(
"normalAxis");
419 label flowAxisIndex = axisIndices[flowAxis];
420 label normalAxisIndex = axisIndices[normalAxis];
425 word patchName = patches[idxI];
427 label patchI =
mesh_.boundaryMesh().findPatchID(patchName);
429 if (
mesh_.thisDb().foundObject<volVectorField>(varName))
431 volVectorField& state(
const_cast<volVectorField&
>(
432 mesh_.thisDb().lookupObject<volVectorField>(varName)));
435 if (
mesh_.boundaryMesh()[patchI].size() > 0)
437 if (state.boundaryFieldRef()[patchI].type() ==
"fixedValue")
439 scalar UmagIn = mag(state.boundaryField()[patchI][0]);
441 state.boundaryField()[patchI][0][normalAxisIndex] / state.boundaryField()[patchI][0][flowAxisIndex];
443 scalar aoa = atan(Uratio) * 180.0 / constant::mathematical::pi;
444 scalar aoaNew = aoa + delta;
446 scalar aoaNewArc = aoaNew * constant::mathematical::pi / 180.0;
448 scalar UxNew = UmagIn / sqrt(1 + tan(aoaNewArc) * tan(aoaNewArc));
449 scalar UyNew = UxNew * tan(aoaNewArc);
451 forAll(state.boundaryField()[patchI], faceI)
453 state.boundaryFieldRef()[patchI][faceI][flowAxisIndex] = UxNew;
454 state.boundaryFieldRef()[patchI][faceI][normalAxisIndex] = UyNew;
457 else if (state.boundaryFieldRef()[patchI].type() ==
"inletOutlet")
460 mixedFvPatchField<vector>& inletOutletPatch =
461 refCast<mixedFvPatchField<vector>>(state.boundaryFieldRef()[patchI]);
462 scalar UmagIn = mag(inletOutletPatch.refValue()[0]);
465 inletOutletPatch.refValue()[0][normalAxisIndex] / inletOutletPatch.refValue()[0][flowAxisIndex];
467 scalar aoa = atan(Uratio) * 180.0 / constant::mathematical::pi;
468 scalar aoaNew = aoa + delta;
470 scalar aoaNewArc = aoaNew * constant::mathematical::pi / 180.0;
472 scalar UxNew = UmagIn / sqrt(1 + tan(aoaNewArc) * tan(aoaNewArc));
473 scalar UyNew = UxNew * tan(aoaNewArc);
475 vector UNew = vector::zero;
476 UNew[flowAxisIndex] = UxNew;
477 UNew[normalAxisIndex] = UyNew;
479 inletOutletPatch.refValue() = UNew;
483 FatalErrorIn(
"") <<
"boundaryType: " << state.boundaryFieldRef()[patchI].type()
485 <<
"Avaiable options are: fixedValue, inletOutlet"
486 << abort(FatalError);
492 FatalErrorIn(
"") <<
"U is not found in volVectorField!"
493 << abort(FatalError);
505 MatConvert(dXvdFFDMat, MATSAME, MAT_INITIAL_MATRIX, &
dXvdFFDMat_);
524 VecCreate(PETSC_COMM_WORLD, normStatePerturbVec);
525 VecSetSizes(*normStatePerturbVec, localSize, PETSC_DETERMINE);
526 VecSetFromOptions(*normStatePerturbVec);
527 VecSet(*normStatePerturbVec, 1.0);
529 dictionary normStateDict =
allOptions_.subDict(
"normalizeStates");
531 wordList normStateNames = normStateDict.toc();
535 word stateName =
stateInfo_[
"volVectorStates"][idxI];
536 if (DAUtility::isInList<word>(stateName, normStateNames))
538 scalar scale = normStateDict.getScalar(stateName);
539 PetscScalar scaleValue = 0.0;
543 for (label comp = 0; comp < 3; comp++)
546 VecSetValue(*normStatePerturbVec, glbIdx, scaleValue, INSERT_VALUES);
554 const word stateName =
stateInfo_[
"volScalarStates"][idxI];
555 if (DAUtility::isInList<word>(stateName, normStateNames))
557 scalar scale = normStateDict.getScalar(stateName);
558 PetscScalar scaleValue = 0.0;
563 VecSetValue(*normStatePerturbVec, glbIdx, scaleValue, INSERT_VALUES);
570 const word stateName =
stateInfo_[
"modelStates"][idxI];
571 if (DAUtility::isInList<word>(stateName, normStateNames))
573 scalar scale = normStateDict.getScalar(stateName);
574 PetscScalar scaleValue = 0.0;
579 VecSetValue(*normStatePerturbVec, glbIdx, scaleValue, INSERT_VALUES);
586 const word stateName =
stateInfo_[
"surfaceScalarStates"][idxI];
587 if (DAUtility::isInList<word>(stateName, normStateNames))
589 scalar scale = normStateDict.getScalar(stateName);
590 PetscScalar scaleValue = 0.0;
596 scale =
mesh_.magSf()[faceI];
604 scale =
mesh_.magSf().boundaryField()[patchIdx][faceIdx];
609 VecSetValue(*normStatePerturbVec, glbIdx, scaleValue, INSERT_VALUES);
614 VecAssemblyBegin(*normStatePerturbVec);
615 VecAssemblyEnd(*normStatePerturbVec);