DAInputField.C
Go to the documentation of this file.
1 /*---------------------------------------------------------------------------*\
2 
3  DAFoam : Discrete Adjoint with OpenFOAM
4  Version : v4
5 
6 \*---------------------------------------------------------------------------*/
7 
8 #include "DAInputField.H"
9 
10 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
11 
12 namespace Foam
13 {
14 
15 defineTypeNameAndDebug(DAInputField, 0);
16 addToRunTimeSelectionTable(DAInput, DAInputField, dictionary);
17 // * * * * * * * * * * * * * * * * Constructors * * * * * * * * * * * * * * //
18 
20  const word inputName,
21  const word inputType,
22  fvMesh& mesh,
23  const DAOption& daOption,
24  const DAModel& daModel,
25  const DAIndex& daIndex)
26  : DAInput(
27  inputName,
28  inputType,
29  mesh,
30  daOption,
31  daModel,
32  daIndex)
33 {
34  fieldName_ = daOption_.getAllOptions().subDict("inputInfo").subDict(inputName).getWord("fieldName");
35  fieldType_ = daOption_.getAllOptions().subDict("inputInfo").subDict(inputName).getWord("fieldType");
36 
37  if (daOption_.getAllOptions().subDict("inputInfo").subDict(inputName).found("indices"))
38  {
39  daOption_.getAllOptions().subDict("inputInfo").subDict(inputName).readEntry("indices", indices_);
40  }
41  else
42  {
43  indices_.setSize(3);
44  for (label i = 0; i < 3; i++)
45  {
46  indices_[i] = i;
47  }
48  }
49 }
50 
51 void DAInputField::run(const scalarList& input)
52 {
53  /*
54  Description:
55  Assign the input array to OF's field variables. Note that we need different treatment for distributed and
56  non-distributed field inputs
57  */
58 
59  if (fieldType_ == "scalar")
60  {
61  volScalarField& field = const_cast<volScalarField&>(mesh_.thisDb().lookupObject<volScalarField>(fieldName_));
62  if (this->distributed())
63  {
64  forAll(field, cellI)
65  {
66  field[cellI] = input[cellI];
67  }
68  }
69  else
70  {
71  for (label globalCellI = 0; globalCellI < daIndex_.nGlobalCells; globalCellI++)
72  {
73  if (daIndex_.globalCellNumbering.isLocal(globalCellI))
74  {
75  label localCellI = daIndex_.globalCellNumbering.toLocal(globalCellI);
76  field[localCellI] = input[globalCellI];
77  }
78  }
79  }
80  field.correctBoundaryConditions();
81  }
82  else if (fieldType_ == "vector")
83  {
84  volVectorField& field = const_cast<volVectorField&>(mesh_.thisDb().lookupObject<volVectorField>(fieldName_));
85  if (this->distributed())
86  {
87  label counterI = 0;
88  forAll(field, cellI)
89  {
90  forAll(indices_, idxI)
91  {
92  label comp = indices_[idxI];
93  field[cellI][comp] = input[counterI];
94  counterI++;
95  }
96  }
97  }
98  else
99  {
100  for (label globalCellI = 0; globalCellI < daIndex_.nGlobalCells; globalCellI++)
101  {
102  if (daIndex_.globalCellNumbering.isLocal(globalCellI))
103  {
104  label localCellI = daIndex_.globalCellNumbering.toLocal(globalCellI);
105  forAll(indices_, idxI)
106  {
107  label comp = indices_[idxI];
108  label inputIdx = globalCellI * 3 + comp;
109  field[localCellI][comp] = input[inputIdx];
110  }
111  }
112  }
113  }
114  field.correctBoundaryConditions();
115  }
116  else
117  {
118  FatalErrorIn("DAInputField::run") << exit(FatalError);
119  }
120 }
121 
122 // * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * //
123 
124 } // End namespace Foam
125 
126 // ************************************************************************* //
Foam::DAInput::daIndex_
const DAIndex & daIndex_
DAIndex object.
Definition: DAInput.H:57
Foam::DAInputField::fieldType_
word fieldType_
field type, can be either scalar or vector
Definition: DAInputField.H:37
Foam::DAOption
Definition: DAOption.H:29
Foam::addToRunTimeSelectionTable
addToRunTimeSelectionTable(DAFunction, DAFunctionForce, dictionary)
DAInputField.H
Foam::DAInputField::indices_
labelList indices_
if it is vector fields, we can also provide components/indices
Definition: DAInputField.H:40
Foam::DAInputField::run
virtual void run(const scalarList &input)
Definition: DAInputField.C:51
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::DAInputField::distributed
virtual label distributed()
Definition: DAInputField.H:94
Foam::DAIndex::globalCellNumbering
globalIndex globalCellNumbering
Definition: DAIndex.H:168
Foam::DAIndex
Definition: DAIndex.H:32
Foam::DAInput::daOption_
const DAOption & daOption_
DAOption object.
Definition: DAInput.H:51
Foam::DAInputField::fieldName_
word fieldName_
name of the field
Definition: DAInputField.H:34
Foam::DAIndex::nGlobalCells
label nGlobalCells
global cell size
Definition: DAIndex.H:129
Foam::DAModel
Definition: DAModel.H:57
Foam::defineTypeNameAndDebug
defineTypeNameAndDebug(DAFunction, 0)
Foam
Definition: checkGeometry.C:32
Foam::DAInput
Definition: DAInput.H:30
forAll
forAll(nuTilda1, cellI)
Definition: nuTilda1EqnIrkPimple.H:19
Foam::DAInputField::DAInputField
DAInputField(const word inputName, const word inputType, fvMesh &mesh, const DAOption &daOption, const DAModel &daModel, const DAIndex &daIndex)
Definition: DAInputField.C:19
Foam::DAInput::mesh_
fvMesh & mesh_
fvMesh
Definition: DAInput.H:48