MAST
mesh_field_function.cpp
Go to the documentation of this file.
1 /*
2  * MAST: Multidisciplinary-design Adaptation and Sensitivity Toolkit
3  * Copyright (C) 2013-2019 Manav Bhatia
4  *
5  * This library is free software; you can redistribute it and/or
6  * modify it under the terms of the GNU Lesser General Public
7  * License as published by the Free Software Foundation; either
8  * version 2.1 of the License, or (at your option) any later version.
9  *
10  * This library is distributed in the hope that it will be useful,
11  * but WITHOUT ANY WARRANTY; without even the implied warranty of
12  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13  * Lesser General Public License for more details.
14  *
15  * You should have received a copy of the GNU Lesser General Public
16  * License along with this library; if not, write to the Free Software
17  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
18  */
19 
20 // MAST includes
23 #include "base/nonlinear_system.h"
24 
25 // libMesh includes
26 #include "libmesh/dof_map.h"
27 
28 
31  const std::string& nm):
33 _use_qp_sol(false),
34 _qp_sol(),
35 _sys(&sys.system()),
36 _sol(nullptr),
37 _dsol(nullptr),
38 _function(nullptr),
39 _perturbed_function(nullptr)
40 { }
41 
42 
43 
45 MeshFieldFunction(libMesh::System& sys,
46  const std::string& nm):
48 _use_qp_sol(false),
49 _qp_sol(),
50 _sys(&sys),
51 _sol(nullptr),
52 _dsol(nullptr),
53 _function(nullptr),
54 _perturbed_function(nullptr)
55 { }
56 
57 
58 
59 
61 
62  this->clear();
63 }
64 
65 
66 
67 
68 
69 
70 
71 void
72 MAST::MeshFieldFunction::operator() (const libMesh::Point& p,
73  const Real t,
74  RealVectorX& v) const {
75 
76  // if the element has provided a quadrature point solution,
77  // then use it
78  if (_use_qp_sol) {
79  v = _qp_sol;
80  return;
81  }
82 
83  // make sure that the object was initialized
84  libmesh_assert(_function);
85 
86  unsigned int
87  n_vars = _sys->n_vars();
88 
89  DenseRealVector v1;
90  (*_function)(p, t, v1);
91 
92  // make sure that the mesh function was able to find the element
93  // and a solution
94  libmesh_assert_equal_to(v1.size(), n_vars);
95 
96  // now copy this to the output vector
97  v = RealVectorX::Zero(n_vars);
98  for (unsigned int i=0; i<n_vars; i++)
99  v(i) = v1(i);
100 }
101 
102 
103 
104 void
105 MAST::MeshFieldFunction::gradient (const libMesh::Point& p,
106  const Real t,
107  RealMatrixX& v) const {
108 
109  // if the element has provided a quadrature point solution,
110  // then use it
111  if (_use_qp_sol) {
112  v = _qp_sol;
113  return;
114  }
115 
116  // make sure that the object was initialized
117  libmesh_assert(_function);
118 
119  unsigned int
120  n_vars = _sys->n_vars();
121 
122  std::vector<libMesh::Gradient> v1;
123  _function->gradient(p, t, v1);
124 
125  // make sure that the mesh function was able to find the element
126  // and a solution
127  libmesh_assert_equal_to(v1.size(), n_vars);
128 
129  // now copy this to the output vector
130  v = RealMatrixX::Zero(n_vars, 3); // assume 3-dimensional by default
131  for (unsigned int i=0; i<n_vars; i++)
132  for (unsigned int j=0; j<3; j++)
133  v(i, j) = v1[i](j);
134 }
135 
136 
137 
138 void
140  const Real t,
141  RealVectorX& v) const {
142 
143  // if the element has provided a quadrature point solution,
144  // then use it
145  if (_use_qp_sol) {
146  v = _qp_sol;
147  return;
148  }
149 
150  // make sure that the object was initialized
151  libmesh_assert(_perturbed_function);
152 
153  unsigned int
154  n_vars = _sys->n_vars();
155 
156  DenseRealVector v1;
157  (*_perturbed_function)(p, t, v1);
158 
159  // make sure that the mesh function was able to find the element
160  // and a solution
161  libmesh_assert_equal_to(v1.size(), n_vars);
162 
163  // now copy this to the output vector
164  v = RealVectorX::Zero(n_vars);
165  for (unsigned int i=0; i<n_vars; i++)
166  v(i) = v1(i);
167 }
168 
169 
170 
171 
172 void
174  const libMesh::Point& p,
175  const Real t,
176  RealVectorX& v) const {
177 
178 
179  libmesh_error_msg("To be implemented.");
180 }
181 
182 
183 
184 
185 
186 void
188 init(const libMesh::NumericVector<Real>& sol,
189  const libMesh::NumericVector<Real>* dsol) {
190 
191 
192  // first make sure that the object is not already initialized
193  libmesh_assert(!_function);
194 
195  // next, clone this solution and localize to the sendlist
196  _sol = libMesh::NumericVector<Real>::build(_sys->comm()).release();
197 
198  // initialize and then localize the vector with the provided solution
199  /*_sol->init(system.n_dofs(),
200  system.n_local_dofs(),
201  send_list,
202  false,
203  libMesh::GHOSTED);
204  sol.localize(*_sol, send_list);*/
205  _sol->init(sol.size(), true, libMesh::SERIAL);
206  sol.localize(*_sol);
207 
208  // finally, create the mesh interpolation function
209  std::vector<unsigned int> vars;
210  _sys->get_all_variable_numbers(vars);
211 
212  _function = new libMesh::MeshFunction(_sys->get_equation_systems(),
213  *_sol,
214  _sys->get_dof_map(),
215  vars);
216  _function->init();
217 
218  if (dsol) {
219 
220  _dsol = libMesh::NumericVector<Real>::build(_sys->comm()).release();
221 
222  /*_dsol->init(system.n_dofs(),
223  system.n_local_dofs(),
224  send_list,
225  false,
226  libMesh::GHOSTED);
227  dsol->localize(*_dsol, send_list);*/
228  _dsol->init(dsol->size(), true, libMesh::SERIAL);
229  dsol->localize(*_dsol);
230 
231 
232  // finally, create the mesh interpolation function
234  new libMesh::MeshFunction(_sys->get_equation_systems(),
235  *_dsol,
236  _sys->get_dof_map(),
237  vars);
238  _perturbed_function->init();
239 
240  }
241 }
242 
243 
244 
245 
246 void
248 
249  // if a pointer has been attached, then delete it and the
250  // associated vector, and clear the associated system
251  if (_function) {
252  delete _function;
253  _function = nullptr;
254 
255  delete _sol;
256  _sol = nullptr;
257  }
258 
259  if (_perturbed_function) {
260  delete _perturbed_function;
261  _perturbed_function = nullptr;
262 
263  delete _dsol;
264  _dsol = nullptr;
265  }
266 
267 
268  // clear flags for quadrature point solution
269  _use_qp_sol = false;
270 }
271 
272 
273 
274 
275 void
278 
279  _use_qp_sol = true;
280  _qp_sol = sol;
281 }
282 
283 
284 
285 void
288 
289  _use_qp_sol = false;
290  _qp_sol.setZero();
291 }
292 
MeshFieldFunction(MAST::SystemInitialization &sys, const std::string &nm)
constructor
virtual void derivative(const MAST::FunctionBase &f, const libMesh::Point &p, const Real t, RealVectorX &v) const
calculates the value of the function at the specified point, p, and time, t, and returns it in v...
libMesh::NumericVector< Real > * _sol
current solution that is going to be interpolated
libMesh::NumericVector< Real > * _dsol
virtual void gradient(const libMesh::Point &p, const Real t, RealMatrixX &g) const
calculates the gradient of value of the function at the specified point, p, and time, t, and returns it in g.
libMesh::MeshFunction * _function
the MeshFunction object that performs the interpolation
virtual void perturbation(const libMesh::Point &p, const Real t, RealVectorX &v) const
calculates the value of perturbation in the function at the specified point, p, and time...
libMesh::MeshFunction * _perturbed_function
virtual void clear_element_quadrature_point_solution()
clears the quadrature point solution provided by the corresponding set method above.
libMesh::Real Real
virtual void operator()(const libMesh::Point &p, const Real t, RealVectorX &v) const
calculates the value of the function at the specified point, p, and time, t, and returns it in v...
RealVectorX _qp_sol
quadrature point solution of the element
virtual void set_element_quadrature_point_solution(RealVectorX &sol)
When a mesh field function is attached to an assembly routine during system assembly, then the current solution can be provided by the element quadrature point update.
libMesh::DenseVector< Real > DenseRealVector
Matrix< Real, Dynamic, Dynamic > RealMatrixX
This creates the base class for functions that have a saptial and temporal dependence, and provide sensitivity operations with respect to the functions and parameters.
void clear()
clear the solution
Matrix< Real, Dynamic, 1 > RealVectorX
bool _use_qp_sol
flag is set to true when the quadrature point solution is provided by an element
void init(const libMesh::NumericVector< Real > &sol, const libMesh::NumericVector< Real > *dsol=nullptr)
initializes the data structures to perform the interpolation function of sol.
libMesh::System * _sys
current system for which solution is to be interpolated
virtual ~MeshFieldFunction()
destructor