ColoringIncompressible.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2 
3  DAFoam : Discrete Adjoint with OpenFOAM
4  Version : v3
5 
6 \*---------------------------------------------------------------------------*/
7 
9 
10 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
11 
12 namespace Foam
13 {
14 
15 // Constructors
16 ColoringIncompressible::ColoringIncompressible(
17  char* argsAll,
18  PyObject* pyOptions)
19  : argsAll_(argsAll),
20  pyOptions_(pyOptions),
21  argsPtr_(nullptr),
22  runTimePtr_(nullptr),
23  meshPtr_(nullptr)
24 {
25 }
26 
28 {
29 }
30 
32 {
33  /*
34  Description:
35  Run the coloring solver and save the computed colors to disks
36  */
37 
38 #include "setArgs.H"
39 #include "setRootCasePython.H"
40 #include "createTimePython.H"
41 #include "createMeshPython.H"
42 #include "createFields.H"
43 #include "createAdjoint.H"
44 
45  word solverName = daOption.getOption<word>("solverName");
46  autoPtr<DAStateInfo> daStateInfo(DAStateInfo::New(solverName, mesh, daOption, daModel));
47 
48  // dRdW
49  {
50  autoPtr<DAJacCon> daJacCon(DAJacCon::New("dRdW", mesh, daOption, daModel, daIndex));
51 
52  if (!daJacCon->coloringExists())
53  {
54  dictionary options;
55  const HashTable<List<List<word>>>& stateResConInfo = daStateInfo->getStateResConInfo();
56  options.set("stateResConInfo", stateResConInfo);
57 
58  // need to first setup preallocation vectors for the dRdWCon matrix
59  // because directly initializing the dRdWCon matrix will use too much memory
60  daJacCon->setupJacConPreallocation(options);
61 
62  // now we can initilaize dRdWCon
63  daJacCon->initializeJacCon(options);
64 
65  // setup dRdWCon
66  daJacCon->setupJacCon(options);
67  Info << "dRdWCon Created. " << mesh.time().elapsedClockTime() << " s" << endl;
68 
69  // compute the coloring
70  Info << "Calculating dRdW Coloring... " << mesh.time().elapsedClockTime() << " s" << endl;
71  daJacCon->calcJacConColoring();
72  Info << "Calculating dRdW Coloring... Completed! " << mesh.time().elapsedClockTime() << " s" << endl;
73 
74  // clean up
75  daJacCon->clear();
76  }
77  }
78 
79  // dFdW
80  const dictionary& allOptions = daOption.getAllOptions();
81  dictionary objFuncDict = allOptions.subDict("objFunc");
82  // create a dummy DAResidual just for initializing DAObjFunc
83  autoPtr<DAResidual> daResidual(DAResidual::New("dummy", mesh, daOption, daModel, daIndex));
84  forAll(objFuncDict.toc(), idxI)
85  {
86  word objFuncName = objFuncDict.toc()[idxI];
87  dictionary objFuncSubDict = objFuncDict.subDict(objFuncName);
88  forAll(objFuncSubDict.toc(), idxJ)
89  {
90  word objFuncPart = objFuncSubDict.toc()[idxJ];
91  dictionary objFuncSubDictPart = objFuncSubDict.subDict(objFuncPart);
92 
93  autoPtr<DAJacCon> daJacCon(DAJacCon::New("dFdW", mesh, daOption, daModel, daIndex));
94 
95  word postFix = "_" + objFuncName + "_" + objFuncPart;
96 
97  if (!daJacCon->coloringExists(postFix))
98  {
99  autoPtr<DAObjFunc> daObjFunc(
101  mesh,
102  daOption,
103  daModel,
104  daIndex,
105  daResidual,
106  objFuncName,
107  objFuncPart,
108  objFuncSubDictPart));
109 
110  dictionary options;
111  const List<List<word>>& objFuncConInfo = daObjFunc->getObjFuncConInfo();
112  const labelList& objFuncFaceSources = daObjFunc->getObjFuncFaceSources();
113  const labelList& objFuncCellSources = daObjFunc->getObjFuncCellSources();
114  options.set("objFuncConInfo", objFuncConInfo);
115  options.set("objFuncFaceSources", objFuncFaceSources);
116  options.set("objFuncCellSources", objFuncCellSources);
117 
118  // now we can initilaize dFdWCon
119  daJacCon->initializeJacCon(options);
120 
121  // setup dFdWCon
122  daJacCon->setupJacCon(options);
123  Info << "dFdWCon Created. " << mesh.time().elapsedClockTime() << " s" << endl;
124 
125  // compute the coloring
126  Info << "Calculating dFdW " << objFuncName << "-"
127  << objFuncPart << " Coloring... "
128  << mesh.time().elapsedClockTime() << " s" << endl;
129 
130  daJacCon->calcJacConColoring(postFix);
131 
132  Info << "Calculating dFdW " << objFuncName << "-"
133  << objFuncPart << " Coloring... Completed"
134  << mesh.time().elapsedClockTime() << " s" << endl;
135 
136  // clean up
137  daJacCon->clear();
138  }
139  }
140  }
141 
142  return;
143 }
144 
145 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
146 
147 } // End namespace Foam
148 
149 // ************************************************************************* //
allOptions
const dictionary & allOptions
Definition: createRefsRhoSimpleC.H:15
forAll
forAll(pseudoP.boundaryField(), patchI)
Definition: solvePseudoPEqn.H:10
Foam::DAStateInfo::New
static autoPtr< DAStateInfo > New(const word modelType, const fvMesh &mesh, const DAOption &daOption, const DAModel &daModel)
Definition: DAStateInfo.C:43
setRootCasePython.H
daOption
DAOption daOption(mesh, pyOptions_)
createFields.H
createMeshPython.H
mesh
fvMesh & mesh
Definition: createRefsHeatTransfer.H:4
Foam::ColoringIncompressible::~ColoringIncompressible
virtual ~ColoringIncompressible()
Destructor.
Definition: ColoringIncompressible.C:27
solverName
word solverName
Definition: createAdjointCompressible.H:30
ColoringIncompressible.H
Foam::DAResidual::New
static autoPtr< DAResidual > New(const word modelType, const fvMesh &mesh, const DAOption &daOption, const DAModel &daModel, const DAIndex &daIndex)
Definition: DAResidual.C:38
createTimePython.H
Foam
Definition: multiFreqScalarFvPatchField.C:144
Foam::ColoringIncompressible::run
void run()
run
Definition: ColoringIncompressible.C:31
Foam::DAObjFunc::New
static autoPtr< DAObjFunc > New(const fvMesh &mesh, const DAOption &daOption, const DAModel &daModel, const DAIndex &daIndex, const DAResidual &daResidual, const word objFuncName, const word objFuncPart, const dictionary &objFuncDict)
Definition: DAObjFunc.C:62
createAdjoint.H
daModel
DAModel daModel(mesh, daOption)
daIndex
DAIndex daIndex(mesh, daOption, daModel)
setArgs.H
Foam::DAJacCon::New
static autoPtr< DAJacCon > New(const word modelType, const fvMesh &mesh, const DAOption &daOption, const DAModel &daModel, const DAIndex &daIndex)
Definition: DAJacCon.C:47