KratosMultiphysics
KRATOS Multiphysics (Kratos) is a framework for building parallel, multi-disciplinary simulation software, aiming at modularity, extensibility, and high performance. Kratos is written in C++, and counts with an extensive Python interface.
local_temperature_average_response_function.h
Go to the documentation of this file.
1 // KRATOS ___ ___ _ ___ __ ___ ___ ___ ___
2 // / __/ _ \| \| \ \ / /__| \_ _| __| __|
3 // | (_| (_) | .` |\ V /___| |) | || _|| _|
4 // \___\___/|_|\_| \_/ |___/___|_| |_| APPLICATION
5 //
6 // License: BSD License
7 // Kratos default license: kratos/license.txt
8 //
9 // Main authors: Jordi Cotela
10 //
11 
12 #ifndef KRATOS_LOCAL_TEMPERATURE_AVERAGE_RESPONSE_FUNCTION_H_INCLUDED
13 #define KRATOS_LOCAL_TEMPERATURE_AVERAGE_RESPONSE_FUNCTION_H_INCLUDED
14 
15 #include "includes/kratos_flags.h"
16 #include "includes/model_part.h"
19 
20 namespace Kratos {
23 
26 
28 {
29 public:
32 
34 
38 
41  {
42  KRATOS_TRY;
43 
44  mTargetModelPartName = Settings["model_part_name"].GetString();
45 
46  auto& r_target_model_part = GetTargetModelPart(rModelPart, mTargetModelPartName);
47  auto& r_nodes = r_target_model_part.Nodes();
48  mNumNodes = r_nodes.size();
49 
50  VariableUtils variable_utils;
51  variable_utils.SetFlag(STRUCTURE,true,r_nodes);
52 
53  // Note: this should not be parallel, the operation is not threadsafe if the variable is uninitialized
54  for (auto& r_node : r_nodes)
55  {
56  r_node.SetValue(NUMBER_OF_NEIGHBOUR_ELEMENTS,0);
57  }
58 
59  mNumNodes = rModelPart.GetCommunicator().GetDataCommunicator().SumAll(mNumNodes);
60 
61  auto& r_elements = rModelPart.Elements();
62  const int num_elements = r_elements.size();
63 
64  #pragma omp parallel for
65  for (int i = 0; i < num_elements; i++)
66  {
67  auto i_elem = r_elements.begin() + i;
68  auto& r_geom = i_elem->GetGeometry();
69  for (unsigned int i = 0; i < r_geom.PointsNumber(); i++)
70  {
71  auto& r_node = r_geom[i];
72  if (r_node.Is(STRUCTURE))
73  {
74  r_node.SetLock();
75  r_node.GetValue(NUMBER_OF_NEIGHBOUR_ELEMENTS) += 1;
76  r_node.UnSetLock();
77  }
78  }
79  }
80 
81  rModelPart.GetCommunicator().AssembleNonHistoricalData(NUMBER_OF_NEIGHBOUR_ELEMENTS);
82 
83  KRATOS_CATCH("");
84  }
85 
88  {
89  }
90 
94 
98 
99  void Initialize() override
100  {
101  KRATOS_TRY;
102 
103  KRATOS_CATCH("");
104  }
105 
106  void CalculateGradient(const Element& rAdjointElement,
107  const Matrix& rResidualGradient,
108  Vector& rResponseGradient,
109  const ProcessInfo& rProcessInfo) override
110  {
111  ComputePointTemperatureSensitivityContribution(rResidualGradient, rAdjointElement.GetGeometry().Points(),rResponseGradient);
112  }
113 
114  void CalculateGradient(const Condition& rAdjointCondition,
115  const Matrix& rResidualGradient,
116  Vector& rResponseGradient,
117  const ProcessInfo& rProcessInfo) override
118  {
119  noalias(rResponseGradient) = ZeroVector(rResidualGradient.size1());
120  }
121 
122  void CalculateFirstDerivativesGradient(const Element& rAdjointElement,
123  const Matrix& rResidualGradient,
124  Vector& rResponseGradient,
125  const ProcessInfo& rProcessInfo) override
126  {
127  ComputePointTemperatureSensitivityContribution(rResidualGradient, rAdjointElement.GetGeometry().Points(),rResponseGradient);
128  }
129 
130  void CalculateSecondDerivativesGradient(const Element& rAdjointElement,
131  const Matrix& rResidualGradient,
132  Vector& rResponseGradient,
133  const ProcessInfo& rProcessInfo) override
134  {
135  ComputePointTemperatureSensitivityContribution(rResidualGradient, rAdjointElement.GetGeometry().Points(),rResponseGradient);
136  }
137 
138  void CalculatePartialSensitivity(Element& rAdjointElement,
139  const Variable<array_1d<double, 3>>& rVariable,
140  const Matrix& rSensitivityMatrix,
141  Vector& rSensitivityGradient,
142  const ProcessInfo& rProcessInfo) override
143  {
144  if (rSensitivityGradient.size() != rSensitivityMatrix.size1())
145  rSensitivityGradient.resize(rSensitivityMatrix.size1(), false);
146  noalias(rSensitivityGradient) = ZeroVector(rSensitivityMatrix.size1());
147  }
148 
149  void CalculatePartialSensitivity(Condition& rAdjointElement,
150  const Variable<array_1d<double, 3>>& rVariable,
151  const Matrix& rSensitivityMatrix,
152  Vector& rSensitivityGradient,
153  const ProcessInfo& rProcessInfo) override
154  {
155  if (rSensitivityGradient.size() != rSensitivityMatrix.size1())
156  rSensitivityGradient.resize(rSensitivityMatrix.size1(), false);
157  noalias(rSensitivityGradient) = ZeroVector(rSensitivityMatrix.size1());
158  }
159 
160  double CalculateValue(ModelPart& rModelPart) override
161  {
162  KRATOS_TRY;
163  const ModelPart& r_target_model_part =
164  GetTargetModelPart(rModelPart, mTargetModelPartName);
165 
166  const double domain_aggregated_temperature =
167  VariableUtils().SumHistoricalVariable<double>(TEMPERATURE, r_target_model_part);
168 
169  const Communicator& r_communicator = r_target_model_part.GetCommunicator();
170  const int number_of_nodes = r_communicator.LocalMesh().NumberOfNodes();
171  const int total_nodes = r_communicator.GetDataCommunicator().SumAll(number_of_nodes);
172 
173  return domain_aggregated_temperature / static_cast<double>(total_nodes);
174 
175  KRATOS_CATCH("");
176  }
177 
179 
180 protected:
183 
187 
191 
193 
194 private:
197 
198  int mNumNodes = 0;
199  std::string mTargetModelPartName;
200 
204 
205  void ComputePointTemperatureSensitivityContribution(
206  const Matrix& rDerivativesOfResidual,
207  const Element::NodesArrayType& rNodes,
208  Vector& rLocalSensitivityContribution) const
209  {
210  if (rLocalSensitivityContribution.size() != rDerivativesOfResidual.size1())
211  rLocalSensitivityContribution.resize(rDerivativesOfResidual.size1(), false);
212 
213  noalias(rLocalSensitivityContribution) = ZeroVector(rLocalSensitivityContribution.size());
214 
215  const unsigned int num_nodes = rNodes.size();
216 
217  for (unsigned int i = 0; i < num_nodes; i++)
218  {
219  if (rNodes[i].Is(STRUCTURE))
220  {
221  double factor = 1.0 / (rNodes[i].GetValue(NUMBER_OF_NEIGHBOUR_ELEMENTS)*mNumNodes);
222  rLocalSensitivityContribution[i] = factor;
223  }
224  }
225  }
226 
227  ModelPart& GetTargetModelPart(ModelPart& rModelPart, const std::string& rTargetModelPartName)
228  {
229  KRATOS_TRY;
230  if (rModelPart.Name() == rTargetModelPartName)
231  {
232  return rModelPart;
233  }
234  else if (rModelPart.HasSubModelPart(rTargetModelPartName))
235  {
236  return rModelPart.GetSubModelPart(rTargetModelPartName);
237  }
238  else
239  {
240  KRATOS_ERROR << "Unknown ModelPart " << rTargetModelPartName << "." << std::endl;
241  }
242  KRATOS_CATCH("")
243  return rModelPart;
244  }
245 
249 
251 };
252 
254 
256 
257 }
258 
259 #endif // KRATOS_LOCAL_TEMPERATURE_AVERAGE_RESPONSE_FUNCTION_H_INCLUDED
A base class for adjoint response functions.
Definition: adjoint_response_function.h:39
The Commmunicator class manages communication for distributed ModelPart instances.
Definition: communicator.h:67
virtual bool AssembleNonHistoricalData(Variable< int > const &ThisVariable)
Definition: communicator.cpp:527
virtual const DataCommunicator & GetDataCommunicator() const
Definition: communicator.cpp:340
MeshType & LocalMesh()
Returns the reference to the mesh storing all local entities.
Definition: communicator.cpp:245
Base class for all Conditions.
Definition: condition.h:59
Base class for all Elements.
Definition: element.h:60
GeometryType & GetGeometry()
Returns the reference of the geometry.
Definition: geometrical_object.h:158
const PointsArrayType & Points() const
Definition: geometry.h:1768
void resize(std::size_t NewSize1, std::size_t NewSize2, bool preserve=0)
Definition: amatrix_interface.h:224
Definition: local_temperature_average_response_function.h:28
void CalculateSecondDerivativesGradient(const Element &rAdjointElement, const Matrix &rResidualGradient, Vector &rResponseGradient, const ProcessInfo &rProcessInfo) override
Calculate the local gradient w.r.t. second derivatives of primal solution.
Definition: local_temperature_average_response_function.h:130
void CalculatePartialSensitivity(Condition &rAdjointElement, const Variable< array_1d< double, 3 >> &rVariable, const Matrix &rSensitivityMatrix, Vector &rSensitivityGradient, const ProcessInfo &rProcessInfo) override
Calculate the partial sensitivity w.r.t. design variable.
Definition: local_temperature_average_response_function.h:149
void CalculateGradient(const Element &rAdjointElement, const Matrix &rResidualGradient, Vector &rResponseGradient, const ProcessInfo &rProcessInfo) override
Calculate the local gradient w.r.t. primal solution.
Definition: local_temperature_average_response_function.h:106
void CalculatePartialSensitivity(Element &rAdjointElement, const Variable< array_1d< double, 3 >> &rVariable, const Matrix &rSensitivityMatrix, Vector &rSensitivityGradient, const ProcessInfo &rProcessInfo) override
Calculate the partial sensitivity w.r.t. design variable.
Definition: local_temperature_average_response_function.h:138
~LocalTemperatureAverageResponseFunction() override
Destructor.
Definition: local_temperature_average_response_function.h:87
KRATOS_CLASS_POINTER_DEFINITION(LocalTemperatureAverageResponseFunction)
LocalTemperatureAverageResponseFunction(Parameters Settings, ModelPart &rModelPart)
Constructor.
Definition: local_temperature_average_response_function.h:40
void CalculateFirstDerivativesGradient(const Element &rAdjointElement, const Matrix &rResidualGradient, Vector &rResponseGradient, const ProcessInfo &rProcessInfo) override
Calculate the local gradient w.r.t. first derivatives of primal solution.
Definition: local_temperature_average_response_function.h:122
double CalculateValue(ModelPart &rModelPart) override
Calculate the scalar valued response function.
Definition: local_temperature_average_response_function.h:160
void CalculateGradient(const Condition &rAdjointCondition, const Matrix &rResidualGradient, Vector &rResponseGradient, const ProcessInfo &rProcessInfo) override
Calculate the local gradient w.r.t. primal solution.
Definition: local_temperature_average_response_function.h:114
void Initialize() override
Definition: local_temperature_average_response_function.h:99
SizeType NumberOfNodes() const
Definition: mesh.h:259
This class aims to manage meshes for multi-physics simulations.
Definition: model_part.h:77
Communicator & GetCommunicator()
Definition: model_part.h:1821
ElementsContainerType & Elements(IndexType ThisIndex=0)
Definition: model_part.h:1189
This class provides to Kratos a data structure for I/O based on the standard of JSON.
Definition: kratos_parameters.h:59
std::string GetString() const
This method returns the string contained in the current Parameter.
Definition: kratos_parameters.cpp:684
PointerVector is a container like stl vector but using a vector to store pointers to its data.
Definition: pointer_vector.h:72
size_type size() const
Definition: pointer_vector.h:255
ProcessInfo holds the current value of different solution parameters.
Definition: process_info.h:59
Variable class contains all information needed to store and retrive data from a data container.
Definition: variable.h:63
This class implements a set of auxiliar, already parallelized, methods to perform some common tasks r...
Definition: variable_utils.h:63
void SetFlag(const Flags &rFlag, const bool FlagValue, TContainerType &rContainer)
Sets a flag according to a given status over a given container.
Definition: variable_utils.h:900
TDataType SumHistoricalVariable(const TVarType &rVariable, const ModelPart &rModelPart, const unsigned int BuffStep=0)
This method accumulates and return a variable value For a nodal historical variable,...
Definition: variable_utils.h:1253
#define KRATOS_CATCH(MoreInfo)
Definition: define.h:110
#define KRATOS_TRY
Definition: define.h:109
#define KRATOS_ERROR
Definition: exception.h:161
Kratos::ModelPart ModelPart
Definition: kratos_wrapper.h:31
REF: G. R. Cowper, GAUSSIAN QUADRATURE FORMULAS FOR TRIANGLES.
Definition: mesh_condition.cpp:21
KratosZeroVector< double > ZeroVector
Definition: amatrix_interface.h:561
T & noalias(T &TheMatrix)
Definition: amatrix_interface.h:484
integer i
Definition: TensorModule.f:17
float factor
for node in (self.combined_model_part).Nodes: pold = node.GetSolutionStepValue(PRESSURE,...
Definition: ulf_PGLASS.py:254