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