MAST
level_set_reinitialization_transient_assembly.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
26 #include "base/nonlinear_system.h"
28 #include "numerics/utility.h"
29 #include "mesh/geom_elem.h"
30 
31 // libMesh includes
32 #include "libmesh/nonlinear_solver.h"
33 #include "libmesh/numeric_vector.h"
34 #include "libmesh/sparse_matrix.h"
35 #include "libmesh/dof_map.h"
36 
37 
38 
42 _ref_sol (nullptr) {
43 
44 }
45 
46 
47 
48 
51 
52 }
53 
54 
55 
56 void
58 set_reference_solution(const libMesh::NumericVector<Real>& sol) {
59 
60  libmesh_assert(!_ref_sol);
61  _ref_sol = &sol;
62 }
63 
64 
65 
66 void
68 residual_and_jacobian (const libMesh::NumericVector<Real>& X,
69  libMesh::NumericVector<Real>* R,
70  libMesh::SparseMatrix<Real>* J,
71  libMesh::NonlinearImplicitSystem& S) {
72 
73  libmesh_assert(_system);
74  libmesh_assert(_discipline);
75  libmesh_assert(_elem_ops);
76 
78  &solver = dynamic_cast<MAST::TransientSolverBase&>(*_elem_ops);
80  &transient_sys = _system->system();
81 
83  &level_set_elem_ops = dynamic_cast<MAST::LevelSetTransientAssemblyElemOperations&>
84  (solver.get_elem_operation_object());
85 
86  // make sure that the system for which this object was created,
87  // and the system passed through the function call are the same
88  libmesh_assert_equal_to(&S, &transient_sys);
89 
90  if (R) R->zero();
91  if (J) J->zero();
92 
93  // iterate over each element, initialize it and get the relevant
94  // analysis quantities
95  RealVectorX vec, ref_sol;
96  RealMatrixX mat;
97 
98  std::vector<libMesh::dof_id_type> dof_indices;
99  const libMesh::DofMap& dof_map = transient_sys.get_dof_map();
100 
101 
102 
103  // stores the localized solution, velocity, acceleration, etc. vectors.
104  // These pointers will have to be deleted
105  std::vector<libMesh::NumericVector<Real>*>
106  local_qtys;
107 
108  std::unique_ptr<libMesh::NumericVector<Real> > localized_solution;
109  localized_solution.reset(build_localized_vector(transient_sys,
110  *_ref_sol).release());
111 
112  // if a solution function is attached, initialize it
113  if (_sol_function)
114  _sol_function->init( X);
115 
116  // ask the solver to localize the relevant solutions
117  solver.build_local_quantities(X, local_qtys);
118 
119  libMesh::MeshBase::const_element_iterator el =
120  transient_sys.get_mesh().active_local_elements_begin();
121  const libMesh::MeshBase::const_element_iterator end_el =
122  transient_sys.get_mesh().active_local_elements_end();
123 
124  for ( ; el != end_el; ++el) {
125 
126  const libMesh::Elem* elem = *el;
127 
128  dof_map.dof_indices (elem, dof_indices);
129 
130  MAST::GeomElem geom_elem;
131  solver.set_elem_data(elem->dim(), *elem, geom_elem);
132  geom_elem.init(*elem, *_system);
133  solver.init(geom_elem);
134 
135  // get the solution
136  unsigned int ndofs = (unsigned int)dof_indices.size();
137  vec.setZero(ndofs);
138  ref_sol.setZero(ndofs);
139  mat.setZero(ndofs, ndofs);
140 
141  for (unsigned int i=0; i<dof_indices.size(); i++)
142  ref_sol(i) = (*localized_solution)(dof_indices[i]);
143 
144  solver.set_element_data(dof_indices, local_qtys);
145  level_set_elem_ops.set_elem_reference_solution(ref_sol);
146 
147  // if (_sol_function)
148  // physics_elem->attach_active_solution_function(*_sol_function);
149 
150  // perform the element level calculations
151  solver.elem_calculations(J!=nullptr?true:false, vec, mat);
152  solver.clear_elem();
153 
154  // copy to the libMesh matrix for further processing
155  DenseRealVector v;
156  DenseRealMatrix m;
157  if (R)
158  MAST::copy(v, vec);
159  if (J)
160  MAST::copy(m, mat);
161 
162  // constrain the quantities to account for hanging dofs,
163  // Dirichlet constraints, etc.
164  if (R && J)
165  dof_map.constrain_element_matrix_and_vector(m, v, dof_indices);
166  else if (R)
167  dof_map.constrain_element_vector(v, dof_indices);
168  else
169  dof_map.constrain_element_matrix(m, dof_indices);
170 
171  // add to the global matrices
172  if (R) R->add_vector(v, dof_indices);
173  if (J) J->add_matrix(m, dof_indices);
174  dof_indices.clear();
175  }
176 
177  // delete pointers to the local solutions
178  for (unsigned int i=0; i<local_qtys.size(); i++)
179  delete local_qtys[i];
180 
181  // if a solution function is attached, clear it
182  if (_sol_function)
183  _sol_function->clear();
184 
185  if (R) R->close();
186  if (J && close_matrix) J->close();
187 }
MAST::AssemblyElemOperations * _elem_ops
provides assembly elem operations for use by this class
MAST::NonlinearSystem & system()
libMesh::DenseMatrix< Real > DenseRealMatrix
This class implements a system for solution of nonlinear systems.
virtual MAST::TransientAssemblyElemOperations & get_elem_operation_object()
bool close_matrix
flag to control the closing fo the Jacobian after assembly
MAST::SystemInitialization * _system
System for which this assembly is performed.
virtual void elem_calculations(bool if_jac, RealVectorX &vec, RealMatrixX &mat)=0
performs the element calculations over elem, and returns the element vector and matrix quantities in ...
MAST::PhysicsDisciplineBase * _discipline
PhysicsDisciplineBase object for which this class is assembling.
virtual void set_elem_data(unsigned int dim, const libMesh::Elem &ref_elem, MAST::GeomElem &elem) const =0
some analyses may want to set additional element data before initialization of the GeomElem...
virtual void residual_and_jacobian(const libMesh::NumericVector< Real > &X, libMesh::NumericVector< Real > *R, libMesh::SparseMatrix< Real > *J, libMesh::NonlinearImplicitSystem &S)
function that assembles the matrices and vectors quantities for nonlinear solution ...
void set_reference_solution(const libMesh::NumericVector< Real > &sol)
For reinitialization to , the solution before initialization is used to calculate the source and velo...
libMesh::DenseVector< Real > DenseRealVector
Matrix< Real, Dynamic, Dynamic > RealMatrixX
void copy(DenseRealMatrix &m1, const RealMatrixX &m2)
Definition: utility.h:167
virtual void build_local_quantities(const libMesh::NumericVector< Real > &current_sol, std::vector< libMesh::NumericVector< Real > * > &qtys)
localizes the relevant solutions for system assembly.
void clear()
clear the solution
Matrix< Real, Dynamic, 1 > RealVectorX
virtual void init(const MAST::GeomElem &elem)=0
initializes the object for calculation of element quantities for the specified elem.
std::unique_ptr< libMesh::NumericVector< Real > > build_localized_vector(const libMesh::System &sys, const libMesh::NumericVector< Real > &global) const
localizes the parallel vector so that the local copy stores all values necessary for calculation of t...
This class acts as a wrapper around libMesh::Elem for the purpose of providing a uniform interface fo...
Definition: geom_elem.h:59
void init(const libMesh::NumericVector< Real > &sol, const libMesh::NumericVector< Real > *dsol=nullptr)
initializes the data structures to perform the interpolation function of sol.
virtual void clear_elem()
clears the element initialization
void set_elem_reference_solution(const RealVectorX &sol)
set element reference solution for reinitialization
virtual void init(const libMesh::Elem &elem, const MAST::SystemInitialization &sys_init)
initialize the object for the specified reference elem.
Definition: geom_elem.cpp:128
MAST::MeshFieldFunction * _sol_function
system solution that will be initialized before each solution