26 "pointProcAddressing",
27 mesh_.facesInstance(),
30 IOobject::READ_IF_PRESENT,
46 adjStateNames.append(
stateInfo_[
"volVectorStates"][idxI]);
47 adjStateType.set(
stateInfo_[
"volVectorStates"][idxI],
"volVectorState");
51 adjStateNames.append(
stateInfo_[
"volScalarStates"][idxI]);
52 adjStateType.set(
stateInfo_[
"volScalarStates"][idxI],
"volScalarState");
56 adjStateNames.append(
stateInfo_[
"modelStates"][idxI]);
57 adjStateType.set(
stateInfo_[
"modelStates"][idxI],
"modelState");
61 adjStateNames.append(
stateInfo_[
"surfaceScalarStates"][idxI]);
62 adjStateType.set(
stateInfo_[
"surfaceScalarStates"][idxI],
"surfaceScalarState");
66 nLocalCells =
mesh.nCells();
67 nLocalFaces =
mesh.nFaces();
68 nLocalPoints =
mesh.nPoints();
69 nLocalXv = nLocalPoints * 3;
70 nLocalInternalFaces =
mesh.nInternalFaces();
71 nLocalBoundaryFaces = nLocalFaces - nLocalInternalFaces;
72 nLocalBoundaryPatches =
mesh.boundaryMesh().size();
78 bFacePatchI.setSize(nLocalBoundaryFaces);
79 bFaceFaceI.setSize(nLocalBoundaryFaces);
81 forAll(mesh_.boundaryMesh(), patchI)
83 forAll(mesh_.boundaryMesh()[patchI], faceI)
85 bFacePatchI[tmpCounter] = patchI;
86 bFaceFaceI[tmpCounter] = faceI;
92 this->calcStateLocalIndexOffset(stateLocalIndexOffset);
95 this->calcAdjStateID(adjStateID);
100 nVolScalarStates =
stateInfo_[
"volScalarStates"].size();
101 nVolVectorStates =
stateInfo_[
"volVectorStates"].size();
102 nSurfaceScalarStates =
stateInfo_[
"surfaceScalarStates"].size();
103 nModelStates =
stateInfo_[
"modelStates"].size();
107 nLocalAdjointBoundaryStates = (nVolVectorStates * 3 + nVolScalarStates + nModelStates) * nLocalBoundaryFaces;
110 label nLocalCellStates = (nVolVectorStates * 3 + nVolScalarStates + nModelStates) * nLocalCells;
111 label nLocalFaceStates = nSurfaceScalarStates * nLocalFaces;
112 nLocalAdjointStates = nLocalCellStates + nLocalFaceStates;
122 nGlobalAdjointStates = globalAdjointStateNumbering.size();
123 nGlobalCells = globalCellNumbering.size();
124 nGlobalFaces = globalFaceNumbering.size();
125 nGlobalXv = globalXvNumbering.size();
130 if (!Pstream::parRun())
133 for (label i = 0; i < nLocalPoints; i++)
135 pointProcAddressing.append(i);
138 nUndecomposedPoints = nLocalPoints;
144 label pointMaxIdx = max(pointProcAddressing);
145 reduce(pointMaxIdx, maxOp<label>());
147 nUndecomposedPoints = pointMaxIdx + 1;
152 isCoupledFace.setSize(nLocalFaces);
153 for (label i = 0; i < nLocalFaces; i++)
155 isCoupledFace[i] = 0;
158 nLocalCoupledBFaces = 0;
159 label faceIdx = nLocalInternalFaces;
160 forAll(mesh_.boundaryMesh(), patchI)
162 forAll(mesh_.boundaryMesh()[patchI], faceI)
164 if (mesh_.boundaryMesh()[patchI].coupled())
167 isCoupledFace[faceIdx] = 1;
168 nLocalCoupledBFaces++;
175 nGlobalCoupledBFaces = globalCoupledBFaceNumbering.size();
178 this->calcLocalIdxLists(adjStateName4LocalAdjIdx, cellIFaceI4LocalAdjIdx);
180 if (daOption_.getOption<label>(
"debug"))
182 this->writeAdjointIndexing();
211 if (adjStateOrdering ==
"state")
222 if (
stateInfo_[
"volVectorStates"][idx] == stateName)
231 if (
stateInfo_[
"volScalarStates"][idx] == stateName)
240 if (
stateInfo_[
"modelStates"][idx] == stateName)
249 if (
stateInfo_[
"surfaceScalarStates"][idx] == stateName && idx == 0)
253 if (
stateInfo_[
"surfaceScalarStates"][idx] == stateName && idx > 0)
261 else if (adjStateOrdering ==
"cell")
272 if (
stateInfo_[
"volVectorStates"][idx] == stateName)
274 offset.set(stateName, counter);
281 if (
stateInfo_[
"volScalarStates"][idx] == stateName)
283 offset.set(stateName, counter);
290 if (
stateInfo_[
"modelStates"][idx] == stateName)
292 offset.set(stateName, counter);
299 if (
stateInfo_[
"surfaceScalarStates"][idx] == stateName)
301 offset.set(stateName, counter);
311 const UList<label>& internalFaceOwner =
mesh_.owner();
316 faceOwner[idxI] = internalFaceOwner[idxI];
323 const UList<label>& pFaceCells =
mesh_.boundaryMesh()[patchIdx].faceCells();
330 List<List<label>> cellOwnedFaces;
335 cellOwnedFaces[ownedCellI].append(idxI);
352 if (
stateInfo_[
"surfaceScalarStates"].size() > 0)
375 if (
stateInfo_[
"surfaceScalarStates"].size() > 0)
377 forAll(cellOwnedFaces, idxI)
379 forAll(cellOwnedFaces[idxI], offsetI)
381 label ownedFace = cellOwnedFaces[idxI][offsetI];
391 FatalErrorIn(
"") <<
"adjStateOrdering invalid" << abort(FatalError);
413 word stateName =
stateInfo_[
"volVectorStates"][idx];
420 word stateName =
stateInfo_[
"volScalarStates"][idx];
427 word stateName =
stateInfo_[
"modelStates"][idx];
434 word stateName =
stateInfo_[
"surfaceScalarStates"][idx];
442 wordList& stateName4LocalAdjIdx,
443 scalarList& cellIFaceI4LocalIdx)
464 word stateName =
stateInfo_[
"volVectorStates"][idx];
467 for (label i = 0; i < 3; i++)
470 cellIFaceI4LocalIdx[localIdx] = cellI + i / 10.0;
472 stateName4LocalAdjIdx[localIdx] = stateName;
479 word stateName =
stateInfo_[
"volScalarStates"][idx];
483 cellIFaceI4LocalIdx[localIdx] = cellI;
485 stateName4LocalAdjIdx[localIdx] = stateName;
491 word stateName =
stateInfo_[
"modelStates"][idx];
495 cellIFaceI4LocalIdx[localIdx] = cellI;
497 stateName4LocalAdjIdx[localIdx] = stateName;
503 word stateName =
stateInfo_[
"surfaceScalarStates"][idx];
507 cellIFaceI4LocalIdx[localIdx] = faceI;
509 stateName4LocalAdjIdx[localIdx] = stateName;
517 const word stateName,
519 const label comp)
const
569 if (adjStateOrdering ==
"state")
585 FatalErrorIn(
"") <<
"comp needs to be set for vector states!"
586 << abort(FatalError);
600 else if (adjStateOrdering ==
"cell")
616 label returnV = -99999999;
617 if (stateType ==
"surfaceScalarState")
620 returnV = idxN * nCellStates
626 else if (stateType ==
"volVectorState")
630 FatalErrorIn(
"") <<
"comp needs to be set for vector states!"
631 << abort(FatalError);
636 returnV = idxJ * nCellStates
645 returnV = idxJ * nCellStates
653 FatalErrorIn(
"") <<
"adjStateOrdering invalid" << abort(FatalError);
657 FatalErrorIn(
"") <<
"stateName not found!" << abort(FatalError);
662 const word stateName,
664 const label comp)
const
703 const label idxPoint,
704 const label idxCoord)
const
734 const label idxPoint,
735 const label idxCoord)
const
756 label localXvIdx = idxPoint * 3 + idxCoord;
810 const label comp)
const
830 label localIdx = cellI * 3 + comp;
836 const label comp)
const
928 FatalErrorIn(
"") <<
"adjStateID4GlobalAdjIdx.size()!=nGlobalAdjointStates"
929 << abort(FatalError);
933 VecCreate(PETSC_COMM_WORLD, &stateIVec);
935 VecSetFromOptions(stateIVec);
936 VecSet(stateIVec, 0);
940 word stateName =
stateInfo_[
"volVectorStates"][idx];
941 PetscScalar valIn =
adjStateID[stateName] + 1;
944 for (label i = 0; i < 3; i++)
947 VecSetValues(stateIVec, 1, &globalIdx, &valIn, INSERT_VALUES);
954 word stateName =
stateInfo_[
"volScalarStates"][idx];
955 PetscScalar valIn =
adjStateID[stateName] + 1;
959 VecSetValues(stateIVec, 1, &globalIdx, &valIn, INSERT_VALUES);
965 word stateName =
stateInfo_[
"modelStates"][idx];
966 PetscScalar valIn =
adjStateID[stateName] + 1;
970 VecSetValues(stateIVec, 1, &globalIdx, &valIn, INSERT_VALUES);
976 word stateName =
stateInfo_[
"surfaceScalarStates"][idx];
977 PetscScalar valIn =
adjStateID[stateName] + 1;
981 VecSetValues(stateIVec, 1, &globalIdx, &valIn, INSERT_VALUES);
985 VecAssemblyBegin(stateIVec);
986 VecAssemblyEnd(stateIVec);
991 VecScatterCreateToAll(stateIVec, &ctx, &vout);
992 VecScatterBegin(ctx, stateIVec, vout, INSERT_VALUES, SCATTER_FORWARD);
993 VecScatterEnd(ctx, stateIVec, vout, INSERT_VALUES, SCATTER_FORWARD);
995 PetscScalar* stateIVecArray;
996 VecGetArray(vout, &stateIVecArray);
1000 adjStateID4GlobalAdjIdx[i] =
static_cast<label
>(stateIVecArray[i]) - 1;
1003 VecRestoreArray(vout, &stateIVecArray);
1004 VecScatterDestroy(&ctx);
1018 PetscInt nCols, Istart, Iend;
1019 const PetscInt* cols;
1020 const PetscScalar*
vals;
1021 scalar maxRatio = 0.0;
1022 label maxRatioRow = -1;
1023 scalar diagV = -1.0;
1024 scalar maxNonDiagV = -1.0;
1025 label maxNonDiagCol = -1;
1026 scalar small = 1e-12;
1027 scalar allNonZeros = 0.0;
1032 MatGetOwnershipRange(matIn, &Istart, &Iend);
1033 for (label i = Istart; i < Iend; i++)
1036 scalar nonDiagSum = 0;
1039 MatGetRow(matIn, i, &nCols, &cols, &
vals);
1040 for (label n = 0; n < nCols; n++)
1050 nonDiagSum = nonDiagSum + fabs(
vals[n]);
1052 if (fabs(
vals[n]) > maxV)
1054 maxV = fabs(
vals[n]);
1060 if (fabs(nonDiagSum / (diag + small)) > maxRatio)
1062 maxRatio = fabs(nonDiagSum / (diag + small));
1064 maxNonDiagCol = maxVIdx;
1069 MatRestoreRow(matIn, i, &nCols, &cols, &
vals);
1072 label rowStateID = -1;
1073 label colStateID = -1;
1074 vector rowCoord(0, 0, 0), colCoord(0, 0, 0);
1078 const word& stateName =
stateInfo_[
"volVectorStates"][idx];
1081 for (label i = 0; i < 3; i++)
1084 if (idxJ == maxRatioRow)
1087 rowCoord =
mesh_.C()[cellI];
1089 if (idxJ == maxNonDiagCol)
1092 colCoord =
mesh_.C()[cellI];
1100 const word& stateName =
stateInfo_[
"volScalarStates"][idx];
1105 if (idxJ == maxRatioRow)
1108 rowCoord =
mesh_.C()[cellI];
1110 if (idxJ == maxNonDiagCol)
1113 colCoord =
mesh_.C()[cellI];
1120 const word& stateName =
stateInfo_[
"modelStates"][idx];
1125 if (idxJ == maxRatioRow)
1128 rowCoord =
mesh_.C()[cellI];
1130 if (idxJ == maxNonDiagCol)
1133 colCoord =
mesh_.C()[cellI];
1140 const word& stateName =
stateInfo_[
"surfaceScalarStates"][idx];
1146 if (idxJ == maxRatioRow)
1151 rowCoord =
mesh_.Cf()[faceI];
1158 rowCoord =
mesh_.Cf().boundaryField()[patchIdx][faceIdx];
1161 if (idxJ == maxNonDiagCol)
1166 colCoord =
mesh_.Cf()[faceI];
1173 colCoord =
mesh_.Cf().boundaryField()[patchIdx][faceIdx];
1180 List<scalar> matCharInfo(13);
1181 matCharInfo[0] = maxRatio;
1182 matCharInfo[1] = diagV;
1183 matCharInfo[2] = maxNonDiagV;
1184 matCharInfo[3] = maxRatioRow;
1185 matCharInfo[4] = rowStateID;
1186 matCharInfo[5] = rowCoord.x();
1187 matCharInfo[6] = rowCoord.y();
1188 matCharInfo[7] = rowCoord.z();
1189 matCharInfo[8] = maxNonDiagCol;
1190 matCharInfo[9] = colStateID;
1191 matCharInfo[10] = colCoord.x();
1192 matCharInfo[11] = colCoord.y();
1193 matCharInfo[12] = colCoord.z();
1196 label myProc = Pstream::myProcNo();
1197 label nProcs = Pstream::nProcs();
1199 List<List<scalar>> gatheredList(nProcs);
1201 gatheredList[myProc] = matCharInfo;
1203 Pstream::gatherList(gatheredList);
1205 Pstream::scatterList(gatheredList);
1207 scalar maxRatioGathered = -1.0;
1209 for (label i = 0; i < nProcs; i++)
1211 if (fabs(gatheredList[i][0]) > maxRatioGathered)
1213 maxRatioGathered = fabs(gatheredList[i][0]);
1219 Info <<
"Jacobian Matrix Characteristics: " << endl;
1220 Info <<
" Mat maxCols: " << maxCols << endl;
1221 Info <<
" Mat allNonZeros: " << allNonZeros << endl;
1222 Info <<
" Max nonDiagSum/Diag: " << gatheredList[procI][0]
1223 <<
" Diag: " << gatheredList[procI][1] <<
" MaxNonDiag: " << gatheredList[procI][2] << endl;
1224 Info <<
" MaxRatioRow: " << gatheredList[procI][3] <<
" RowState: " << gatheredList[procI][4]
1225 <<
" RowCoord: (" << gatheredList[procI][5] <<
" " << gatheredList[procI][6]
1226 <<
" " << gatheredList[procI][7] <<
")" << endl;
1227 Info <<
" MaxNonDiagCol: " << gatheredList[procI][8] <<
" ColState: " << gatheredList[procI][9]
1228 <<
" ColCoord: (" << gatheredList[procI][10] <<
" " << gatheredList[procI][11]
1229 <<
" " << gatheredList[procI][12] <<
")" << endl;
1230 Info <<
" Max nonDiagSum/Diag ProcI: " << procI << endl;
1239 scalar& allNonZeros)
const
1246 PetscInt nCols, Istart, Iend;
1247 const PetscInt* cols;
1248 const PetscScalar*
vals;
1255 MatGetOwnershipRange(matIn, &Istart, &Iend);
1258 for (label i = Istart; i < Iend; i++)
1260 MatGetRow(matIn, i, &nCols, &cols, &
vals);
1263 std::cout <<
"Warning! procI: " << Pstream::myProcNo() <<
" nCols <0 at rowI: " << i << std::endl;
1264 std::cout <<
"Set nCols to zero " << std::endl;
1267 if (nCols > maxCols)
1271 allNonZeros += nCols;
1272 MatRestoreRow(matIn, i, &nCols, &cols, &
vals);
1276 reduce(maxCols, maxOp<label>());
1278 reduce(allNonZeros, sumOp<scalar>());
1289 label myProc = Pstream::myProcNo();
1290 label nProcs = Pstream::nProcs();
1291 std::ostringstream fileNameStream(
"");
1292 fileNameStream <<
"AdjointIndexing"
1293 <<
"_" << myProc <<
"_of_" << nProcs <<
".txt";
1294 word fileName = fileNameStream.str();
1295 OFstream aOut(fileName);
1300 const word& stateName =
stateInfo_[
"volVectorStates"][idx];
1303 xx =
mesh_.C()[cellI].x();
1304 yy =
mesh_.C()[cellI].y();
1305 zz =
mesh_.C()[cellI].z();
1306 for (label i = 0; i < 3; i++)
1309 aOut <<
"Cell: " << cellI <<
" State: " << stateName << i <<
" glbIdx: " << glbIdx
1310 <<
" x: " << xx <<
" y: " << yy <<
" z: " << zz << endl;
1317 const word& stateName =
stateInfo_[
"volScalarStates"][idx];
1320 xx =
mesh_.C()[cellI].x();
1321 yy =
mesh_.C()[cellI].y();
1322 zz =
mesh_.C()[cellI].z();
1324 aOut <<
"Cell: " << cellI <<
" State: " << stateName <<
" glbIdx: " << glbIdx
1325 <<
" x: " << xx <<
" y: " << yy <<
" z: " << zz << endl;
1331 const word& stateName =
stateInfo_[
"modelStates"][idx];
1334 xx =
mesh_.C()[cellI].x();
1335 yy =
mesh_.C()[cellI].y();
1336 zz =
mesh_.C()[cellI].z();
1338 aOut <<
"Cell: " << cellI <<
" State: " << stateName <<
" glbIdx: " << glbIdx
1339 <<
" x: " << xx <<
" y: " << yy <<
" z: " << zz << endl;
1345 const word& stateName =
stateInfo_[
"surfaceScalarStates"][idx];
1351 xx =
mesh_.Cf()[faceI].x();
1352 yy =
mesh_.Cf()[faceI].y();
1353 zz =
mesh_.Cf()[faceI].z();
1360 xx =
mesh_.Cf().boundaryField()[patchIdx][faceIdx].x();
1361 yy =
mesh_.Cf().boundaryField()[patchIdx][faceIdx].y();
1362 zz =
mesh_.Cf().boundaryField()[patchIdx][faceIdx].z();
1364 const polyPatch& pp =
mesh_.boundaryMesh()[patchIdx];
1365 const UList<label>& pFaceCells = pp.faceCells();
1366 cellI = pFaceCells[faceIdx];
1370 aOut <<
"Face: " << faceI <<
" State: " << stateName <<
" glbIdx: " << glbIdx
1371 <<
" x: " << xx <<
" y: " << yy <<
" z: " << zz
1372 <<
" OwnerCellI: " << cellI << endl;
1377 std::ostringstream fileNameStreamPoint(
"");
1378 fileNameStreamPoint <<
"PointIndexing"
1379 <<
"_" << myProc <<
"_of_" << nProcs <<
".txt";
1380 word fileNamePoint = fileNameStreamPoint.str();
1381 OFstream aOutPoint(fileNamePoint);
1382 aOutPoint.precision(9);
1386 xx =
mesh_.points()[idxI].x();
1387 yy =
mesh_.points()[idxI].y();
1388 zz =
mesh_.points()[idxI].z();
1389 for (label i = 0; i < 3; i++)
1392 aOutPoint <<
"Point: " << idxI <<
" Coords: " << i <<
" glbIdx: " << glbIdx
1393 <<
" x: " << xx <<
" y: " << yy <<
" z: " << zz << endl;