DAFvSource.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2 
3  DAFoam : Discrete Adjoint with OpenFOAM
4  Version : v4
5 
6 \*---------------------------------------------------------------------------*/
7 
8 #include "DAFvSource.H"
9 
10 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
11 
12 namespace Foam
13 {
14 
15 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
16 
17 defineTypeNameAndDebug(DAFvSource, 0);
18 defineRunTimeSelectionTable(DAFvSource, dictionary);
19 
20 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
21 
22 DAFvSource::DAFvSource(
23  const word modelType,
24  const fvMesh& mesh,
25  const DAOption& daOption,
26  const DAModel& daModel,
27  const DAIndex& daIndex)
28  : regIOobject(
29  IOobject(
30  "DAFvSource", // always use DAFvSource for the db name
31  mesh.time().timeName(),
32  mesh, // register to mesh
33  IOobject::NO_READ,
34  IOobject::NO_WRITE,
35  true // always register object
36  )),
37  modelType_(modelType),
38  mesh_(mesh),
39  daOption_(daOption),
40  daModel_(daModel),
41  daIndex_(daIndex)
42 {
43 }
44 
45 // * * * * * * * * * * * * * * * * * Selectors * * * * * * * * * * * * * * * //
46 
47 autoPtr<DAFvSource> DAFvSource::New(
48  const word modelType,
49  const fvMesh& mesh,
50  const DAOption& daOption,
51  const DAModel& daModel,
52  const DAIndex& daIndex)
53 {
54  // standard setup for runtime selectable classes
55 
56  if (daOption.getAllOptions().lookupOrDefault<label>("debug", 0))
57  {
58  Info << "Selecting " << modelType << " for DAFvSource" << endl;
59  }
60 
61  dictionaryConstructorTable::iterator cstrIter =
62  dictionaryConstructorTablePtr_->find(modelType);
63 
64  // if the solver name is not found in any child class, print an error
65  if (cstrIter == dictionaryConstructorTablePtr_->end())
66  {
67  FatalErrorIn(
68  "DAFvSource::New"
69  "("
70  " const word,"
71  " const fvMesh&,"
72  " const DAOption&,"
73  " const DAModel&,"
74  " const DAIndex&"
75  ")")
76  << "Unknown DAFvSource type "
77  << modelType << nl << nl
78  << "Valid DAFvSource types:" << endl
79  << dictionaryConstructorTablePtr_->sortedToc()
80  << exit(FatalError);
81  }
82 
83  // child class found
84  return autoPtr<DAFvSource>(
85  cstrIter()(modelType, mesh, daOption, daModel, daIndex));
86 }
87 
88 // * * * * * * * * * * * * * * * Member Functions * * * * * * * * * * * * * //
89 
90 void DAFvSource::calcFvSource(volVectorField& fvSource)
91 {
92  /*
93  Description:
94  Calculate the fvSource term
95  NOTE: this need to be implemented in the child class, if not,
96  print an error!
97  */
98  FatalErrorIn("") << "calcFvSource not implemented " << endl
99  << " in the child class for " << modelType_
100  << abort(FatalError);
101 }
102 
103 void DAFvSource::calcFvSource(volScalarField& fvSource)
104 {
105  /*
106  Description:
107  Calculate the fvSource term
108  NOTE: this need to be implemented in the child class, if not,
109  print an error!
110  */
111  FatalErrorIn("") << "calcFvSource not implemented " << endl
112  << " in the child class for " << modelType_
113  << abort(FatalError);
114 }
115 
116 bool DAFvSource::writeData(Ostream& os) const
117 {
118  /*
119  Description:
120  This is a virtual function for regIOobject
121  */
122  // do nothing
123  return true;
124 }
125 
127 {
128  /*
129  Description:
130  Initialize the values for all types of fvSource in DAGlobalVar, including
131  actuatorDiskPars, heatSourcePars, etc
132  NOTE: this need to be implemented in the child class, if not,
133  print an error!
134  */
135  FatalErrorIn("") << "initFvSourcePars not implemented " << endl
136  << " in the child class for " << modelType_
137  << abort(FatalError);
138 }
139 
141  label snappedCenterCellI,
142  vector& center)
143 {
144  scalar centerX = 0.0;
145  scalar centerY = 0.0;
146  scalar centerZ = 0.0;
147 
148  if (snappedCenterCellI >= 0)
149  {
150  centerX = mesh_.C()[snappedCenterCellI][0];
151  centerY = mesh_.C()[snappedCenterCellI][1];
152  centerZ = mesh_.C()[snappedCenterCellI][2];
153  }
154  reduce(centerX, sumOp<scalar>());
155  reduce(centerY, sumOp<scalar>());
156  reduce(centerZ, sumOp<scalar>());
157 
158  center[0] = centerX;
159  center[1] = centerY;
160  center[2] = centerZ;
161 }
162 
164 {
165  // calculate fvSource based on the latest parameters defined in DAGlobalVar
166  if (mesh_.thisDb().foundObject<volVectorField>("fvSource"))
167  {
168  volVectorField& fvSource = const_cast<volVectorField&>(
169  mesh_.thisDb().lookupObject<volVectorField>("fvSource"));
170 
171  this->calcFvSource(fvSource);
172  }
173  else if (mesh_.thisDb().foundObject<volScalarField>("fvSource"))
174  {
175  volScalarField& fvSource = const_cast<volScalarField&>(
176  mesh_.thisDb().lookupObject<volScalarField>("fvSource"));
177 
178  this->calcFvSource(fvSource);
179  }
180  else
181  {
182  FatalErrorIn("") << "fvSource not found! "
183  << abort(FatalError);
184  }
185 }
186 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
187 
188 } // End namespace Foam
189 
190 // ************************************************************************* //
Foam::DAFvSource::writeData
bool writeData(Ostream &os) const
virtual function for regIOobject
Definition: DAFvSource.C:116
Foam::DAOption
Definition: DAOption.H:29
Foam::DAFvSource::calcFvSource
virtual void calcFvSource(volVectorField &fvSource)
compute the FvSource term
Definition: DAFvSource.C:90
DAFvSource.H
fvSource
volScalarField & fvSource
Definition: createRefsHeatTransfer.H:7
Foam::DAOption::getAllOptions
const dictionary & getAllOptions() const
return a reference of allOptions_ dictionary
Definition: DAOption.H:56
mesh
fvMesh & mesh
Definition: createRefsHeatTransfer.H:4
Foam::DAFvSource::initFvSourcePars
virtual void initFvSourcePars()
Initialize the values for all types of fvSource in DAGlobalVar, including actuatorDiskPars,...
Definition: DAFvSource.C:126
Foam::DAIndex
Definition: DAIndex.H:32
Foam::defineRunTimeSelectionTable
defineRunTimeSelectionTable(DAFunction, dictionary)
Foam::DAFvSource::updateFvSource
void updateFvSource()
calculate fvSource based on the latest parameters defined in DAGlobalVar
Definition: DAFvSource.C:163
Foam::DAFvSource::New
static autoPtr< DAFvSource > New(const word modelType, const fvMesh &mesh, const DAOption &daOption, const DAModel &daModel, const DAIndex &daIndex)
Definition: DAFvSource.C:47
Foam::DAModel
Definition: DAModel.H:57
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(DAFunction, 0)
Foam
Definition: checkGeometry.C:32
Foam::DAFvSource::modelType_
const word & modelType_
model name
Definition: DAFvSource.H:47
Foam::DAFvSource::mesh_
const fvMesh & mesh_
fvMesh
Definition: DAFvSource.H:50
Foam::DAFvSource::findGlobalSnappedCenter
void findGlobalSnappedCenter(label snappedCenterCellI, vector &center)
Definition: DAFvSource.C:140