MAST
Multidisciplinary-design Adaptation and Sensitivity Toolkit (MAST)
jacobian_dot_sensitivity.cpp
Go to the documentation of this file.
1 /*
2  * MAST: Multidisciplinary-design Adaptation and Sensitivity Toolkit
3  * Copyright (C) 2013-2020 Manav Bhatia and MAST authors
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 
21 // BOOST includes
22 #include <boost/test/unit_test.hpp>
23 
24 
25 // MAST includes
26 #include "tests/structural/build_structural_elem_1D.h"
27 #include "tests/structural/build_structural_elem_2D.h"
28 #include "tests/base/test_comparisons.h"
30 #include "elasticity/structural_discipline.h"
34 #include "base/parameter.h"
38 #include "base/nonlinear_system.h"
39 
40 
41 // libMesh includes
42 #include "libmesh/dof_map.h"
43 
44 
45 extern
46 void
47 set_deformation(const unsigned int dim,
48  const unsigned int case_num,
49  const libMesh::ElemType e,
50  RealVectorX& vec);
51 
52 
53 
54 template <typename ValType>
56  const RealVectorX& sol) {
57 
58  const Real
59  delta = 1.e-8,
60  tol = 1.e-2;
61 
62  // get reference to the element in this mesh
63  const libMesh::Elem& elem = **(v._mesh->local_elements_begin());
64 
65  // now create the structural element
66  std::unique_ptr<MAST::StructuralElementBase>
67  e(MAST::build_structural_element(*v._structural_sys,
68  elem,
69  *v._p_card).release());
70 
71 
72  // number of dofs in this element
73  const libMesh::DofMap& dofmap = v._sys->get_dof_map();
74  std::vector<unsigned int> dof_ids;
75  dofmap.dof_indices(&elem, dof_ids);
76 
77  const unsigned int ndofs = (unsigned int)dof_ids.size();
78 
79 
80  // make sure that the number of dofs are appropriately set
81  libmesh_assert_equal_to(sol.size(), ndofs);
82 
83 
84  // now get the residual and Jacobian evaluations
86  x0 = RealVectorX::Zero(ndofs),
87  x = RealVectorX::Zero(ndofs),
88  x_sens = RealVectorX::Zero(ndofs),
89  res0 = RealVectorX::Zero(ndofs),
90  res = RealVectorX::Zero(ndofs);
91 
93  jac_sens = RealMatrixX::Zero(ndofs, ndofs),
94  jac0 = RealMatrixX::Zero(ndofs, ndofs),
95  jac_sens_fd = RealMatrixX::Zero(ndofs, ndofs),
96  dummy;
97 
98 
99  // set the solution about which to evaluate the nonlinearity
100  x0 = sol;
101  x = sol;
102 
103  // tell the element about the solution and velocity
104  e->set_solution(x);
105 
106  // first the baseline Jacobian
107  e->internal_residual(true, res, jac0);
108 
109  // now check the sensitivity result using finite differencing
110  for (unsigned int i=0; i<ndofs; i++) {
111 
112  // first the Jacobian at baseline solution
113  x = x0;
114  x_sens = RealVectorX::Zero(ndofs);
115  x_sens(i) = 2.; // this is the sensitivity of x(i) wrt a param p.
116 
117  // tell the element about this sensitivity
118  e->set_solution(x);
119  e->set_solution(x_sens, true);
120 
121  // next, the Jacobian-derivative times sensitivity
122  jac_sens.setZero();
123  e->internal_residual_jac_dot_state_sensitivity(jac_sens);
124 
125  // perturb the i^th element of the solution using the sensitivity
126  x(i) += x_sens(i) * delta; // x0 + dx/dp * dp
127  e->set_solution(x);
128 
129  // get the new residual
130  jac_sens_fd.setZero();
131  e->internal_residual(true, res, jac_sens_fd);
132 
133  // set the i^th column of the finite-differenced Jacobian
134  jac_sens_fd -= jac0;
135  jac_sens_fd /= delta;
136 
137  // now compare the matrices
138  BOOST_TEST_MESSAGE(" ** dJac/dXi . dXi/dp for i: " << i << " **");
139  BOOST_CHECK(MAST::compare_matrix( jac_sens_fd, jac_sens, tol));
140  }
141 
142 }
143 
144 
145 
146 BOOST_FIXTURE_TEST_SUITE (Structural1DJacobianDerivativeTimesStateSensEvaluation,
148 
149 
150 BOOST_AUTO_TEST_CASE (InternalForceJacobianNonlinear1DIndependentOffset) {
151 
152  this->init(false, true);
153 
154  RealVectorX v;
155  set_deformation(1, 3, libMesh::INVALID_ELEM, v);
156  check_internal_force_jacobian_sensitivity<MAST::BuildStructural1DElem>(*this, v);
157 }
158 
159 
160 
161 BOOST_AUTO_TEST_CASE (InternalForceJacobianNonlinear1DWithConstantOffset) {
162 
163  this->init(false, true);
164 
165  RealVectorX v;
166 
167  // set a finite value of the offset
168  (*_hy_off)() = 0.5*(*_thy)();
169  set_deformation(1, 3, libMesh::INVALID_ELEM, v);
170  check_internal_force_jacobian_sensitivity<MAST::BuildStructural1DElem>(*this, v);
171 }
172 
173 
174 
175 
176 BOOST_AUTO_TEST_CASE (InternalForceJacobianNonlinear1DIndependentOffsetDependentOffset) {
177 
178  this->init(true, true);
179 
180  RealVectorX v;
181  set_deformation(1, 3, libMesh::INVALID_ELEM, v);
182  check_internal_force_jacobian_sensitivity<MAST::BuildStructural1DElem>(*this, v);
183 }
184 
185 
186 
187 
188 BOOST_AUTO_TEST_SUITE_END()
189 
190 
191 
192 BOOST_FIXTURE_TEST_SUITE (Structural2DJacobianDerivativeTimesStateSensEvaluation,
193  MAST::BuildStructural2DElem)
194 
195 
196 BOOST_AUTO_TEST_CASE (InternalForceJacobianNonlinear2DIndependentOffsetQUAD4) {
197 
198  this->init(false, true, libMesh::QUAD4);
199 
200  RealVectorX v;
201  set_deformation(2, 3, libMesh::QUAD4, v);
202  check_internal_force_jacobian_sensitivity<MAST::BuildStructural2DElem>(*this, v);
203 }
204 
205 
206 
207 BOOST_AUTO_TEST_CASE (InternalForceJacobianNonlinear2DWithConstantOffsetQUAD4) {
208 
209  this->init(false, true, libMesh::QUAD4);
210 
211  // set a finite value of the offset
212  (*_hzoff)() = 0.5*(*_thz)();
213 
214  RealVectorX v;
215  set_deformation(2, 3, libMesh::QUAD4, v);
216  check_internal_force_jacobian_sensitivity<MAST::BuildStructural2DElem>(*this, v);
217 }
218 
219 
220 
221 BOOST_AUTO_TEST_CASE (InternalForceJacobianNonlinear2DDependentOffsetQUAD4) {
222 
223  this->init(true, true, libMesh::QUAD4);
224 
225  RealVectorX v;
226  set_deformation(2, 3, libMesh::QUAD4, v);
227  check_internal_force_jacobian_sensitivity<MAST::BuildStructural2DElem>(*this, v);
228 }
229 
230 
231 
232 BOOST_AUTO_TEST_CASE (InternalForceJacobianNonlinear2DIndependentOffsetTRI3) {
233 
234  this->init(false, true, libMesh::TRI3);
235 
236  RealVectorX v;
237  set_deformation(2, 3, libMesh::TRI3, v);
238  check_internal_force_jacobian_sensitivity<MAST::BuildStructural2DElem>(*this, v);
239 }
240 
241 
242 
243 BOOST_AUTO_TEST_CASE (InternalForceJacobianNonlinear2DWithConstantOffsetTRI3) {
244 
245  this->init(false, true, libMesh::TRI3);
246 
247  // set a finite value of the offset
248  (*_hzoff)() = 0.5*(*_thz)();
249 
250  RealVectorX v;
251  set_deformation(2, 3, libMesh::TRI3, v);
252  check_internal_force_jacobian_sensitivity<MAST::BuildStructural2DElem>(*this, v);
253 }
254 
255 
256 
257 BOOST_AUTO_TEST_CASE (InternalForceJacobianNonlinear2DDependentOffsetTRI3) {
258 
259  this->init(true, true, libMesh::TRI3);
260 
261  RealVectorX v;
262  set_deformation(2, 3, libMesh::TRI3, v);
263  check_internal_force_jacobian_sensitivity<MAST::BuildStructural2DElem>(*this, v);
264 }
265 
266 
267 
268 BOOST_AUTO_TEST_SUITE_END()
269 
270 
271 
void set_deformation(const unsigned int dim, const unsigned int case_num, const libMesh::ElemType e, RealVectorX &vec)
bool compare_matrix(const RealMatrixX &m0, const RealMatrixX &m, const Real tol)
libMesh::Real Real
void check_internal_force_jacobian_sensitivity(ValType &v, const RealVectorX &sol)
Matrix< Real, Dynamic, Dynamic > RealMatrixX
Matrix< Real, Dynamic, 1 > RealVectorX
std::unique_ptr< MAST::StructuralElementBase > build_structural_element(MAST::SystemInitialization &sys, const MAST::GeomElem &elem, const MAST::ElementPropertyCardBase &p)
builds the structural element for the specified element type
BOOST_AUTO_TEST_CASE(InternalForceJacobianNonlinear1DWithConstantOffset)
BOOST_FIXTURE_TEST_SUITE(Structural1DJacobianDerivativeTimesStateSensEvaluation, MAST::BuildStructural1DElem) BOOST_AUTO_TEST_CASE(InternalForceJacobianNonlinear1DIndependentOffset)