libMesh
heatsystem.h
Go to the documentation of this file.
1 // The libMesh Finite Element Library.
2 // Copyright (C) 2002-2024 Benjamin S. Kirk, John W. Peterson, Roy H. Stogner
3 
4 // This library is free software; you can redistribute it and/or
5 // modify it under the terms of the GNU Lesser General Public
6 // License as published by the Free Software Foundation; either
7 // version 2.1 of the License, or (at your option) any later version.
8 
9 // This library is distributed in the hope that it will be useful,
10 // but WITHOUT ANY WARRANTY; without even the implied warranty of
11 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 // Lesser General Public License for more details.
13 
14 // You should have received a copy of the GNU Lesser General Public
15 // License along with this library; if not, write to the Free Software
16 // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 
18 
19 
20 #include "libmesh/enum_fe_family.h"
21 #include "libmesh/fem_system.h"
22 #include "libmesh/parameter_pointer.h"
23 #include "libmesh/parameter_vector.h"
24 
25 #include <memory>
26 
27 using namespace libMesh;
28 
29 // FEMSystem, TimeSolver and NewtonSolver will handle most tasks,
30 // but we must specify element residuals
31 class HeatSystem : public FEMSystem
32 {
33 public:
34  // Constructor
36  const std::string & name_in,
37  const unsigned int number_in)
38  : FEMSystem(es, name_in, number_in),
39  _k(1.0),
40  _fe_family("LAGRANGE"),
41  _fe_order(1),
42  _analytic_jacobians(true),
43  dp(1.e-6)
44  { this->init_qois(1); }
45 
46  std::string & fe_family() { return _fe_family; }
47  unsigned int & fe_order() { return _fe_order; }
48  Real & k() { return _k; }
49  bool & analytic_jacobians() { return _analytic_jacobians; }
50 
51  // A function to compute and accumulate residuals
52  void perturb_accumulate_residuals(ParameterVector & parameters);
53 
54  // Sensitivity Calculation
56  {
57  final_sensitivity = 0.0;
58 
59  // Use the trapezoidal rule to compute the sensitivity integral
60  for(unsigned int i = 0; i < R_plus_dp.size()-1; i++)
61  {
62  Number left_contribution = -(R_plus_dp[i] - R_minus_dp[i])/(2.*dp);
63  Number right_contribution = -(R_plus_dp[i+1] - R_minus_dp[i+1])/(2.*dp);
64 
65  final_sensitivity += ( (left_contribution + right_contribution)/2. )*deltat_vector[i];
66  }
67 
68  return final_sensitivity;
69  }
70 
71  void set_tf(Real val)
72  {
73  tf = val;
74  }
75 
77  {
78  if (!parameter_vector.size())
79  for (std::size_t i = 0; i != parameters.size(); ++i)
80  parameter_vector.push_back(std::make_unique<ParameterPointer<Number>>(&parameters[i]));
81 
82  return parameter_vector;
83  }
84 
85  Number & get_QoI_value(unsigned int QoI_index)
86  {
87  return computed_QoI[QoI_index];
88  }
89 
90 protected:
91  // System initialization
92  virtual void init_data ();
93 
94  // Context initialization
95  virtual void init_context (DiffContext & context);
96 
97  // Element residual and jacobian calculations
98  // Time dependent parts
99  virtual bool element_time_derivative (bool request_jacobian,
100  DiffContext & context);
101 
102  // Constraint parts
103  // virtual bool side_constraint (bool request_jacobian,
104  // DiffContext & context);
105 
106  // RHS for adjoint problem
107  virtual void element_qoi_derivative (DiffContext & context,
108  const QoISet & /* qois */);
109 
110  //virtual void element_qoi (DiffContext & context, const QoISet & qois);
111 
112  // Parameters associated with the system
113  std::vector<Number> parameters;
114 
115  // The ParameterVector object that will contain pointers to
116  // the system parameters
118 
119  // The parameters to solve for
121 
122  // The final time parameter
124 
125  // Variables to hold the computed QoIs
126  Number computed_QoI[1];
127 
128  // The FE type to use
129  std::string _fe_family;
130  unsigned int _fe_order;
131 
132  // Index for T variable
133  unsigned int T_var;
134 
135  // Calculate Jacobians analytically or not?
137 
138  // Variables to hold the perturbed residuals
139  std::vector<Number> R_plus_dp;
140  std::vector<Number> R_minus_dp;
141 
142  // A vector to hold the possibly adaptive deltats
143  std::vector<Number> deltat_vector;
144 
145  // Perturbation parameter
147 
148  // The final computed sensitivity
150 };
This is the EquationSystems class.
std::vector< Number > deltat_vector
Definition: heatsystem.h:143
Number & compute_final_sensitivity()
Definition: heatsystem.h:55
Real & k()
Definition: heatsystem.h:48
This class provides all data required for a physics package (e.g.
Definition: diff_context.h:55
Data structure for specifying which Parameters should be independent variables in a parameter sensiti...
Accessor object allowing reading and modification of the independent variables in a parameter sensiti...
Data structure for specifying which Quantities of Interest should be calculated in an adjoint or a pa...
Definition: qoi_set.h:45
The libMesh namespace provides an interface to certain functionality in the library.
void set_tf(Real val)
Definition: heatsystem.h:71
This class provides a specific system class.
Definition: fem_system.h:53
unsigned int T_var
Definition: heatsystem.h:133
std::vector< Number > R_plus_dp
Definition: heatsystem.h:139
ParameterVector & get_parameter_vector()
Definition: heatsystem.h:76
bool _analytic_jacobians
Definition: heatsystem.h:136
Number dp
Definition: heatsystem.h:146
std::vector< Number > parameters
Definition: heatsystem.h:113
void push_back(std::unique_ptr< ParameterAccessor< Number >> new_accessor)
Adds an additional parameter accessor to the end of the vector.
std::vector< Number > R_minus_dp
Definition: heatsystem.h:140
DIE A HORRIBLE DEATH HERE typedef LIBMESH_DEFAULT_SCALAR_TYPE Real
std::string _fe_family
Definition: heatsystem.h:129
Number final_sensitivity
Definition: heatsystem.h:149
unsigned int _fe_order
Definition: heatsystem.h:130
std::string & fe_family()
Definition: heatsystem.h:46
bool & analytic_jacobians()
Definition: heatsystem.h:49
Number & get_QoI_value(unsigned int QoI_index)
Definition: heatsystem.h:85
HeatSystem(EquationSystems &es, const std::string &name_in, const unsigned int number_in)
Definition: heatsystem.h:35
unsigned int & fe_order()
Definition: heatsystem.h:47
ParameterVector parameter_vector
Definition: heatsystem.h:117