MAST
Multidisciplinary-design Adaptation and Sensitivity Toolkit (MAST)
jacobian_1D_and_2D.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-5,
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  res0 = RealVectorX::Zero(ndofs),
89  res = RealVectorX::Zero(ndofs);
90 
92  jac_x = RealMatrixX::Zero(ndofs, ndofs),
93  jac_x_fd = RealMatrixX::Zero(ndofs, ndofs),
94  dummy;
95 
96 
97  // set the solution about which to evaluate the nonlinearity
98  x0 = sol;
99  x = sol;
100 
101  // tell the element about the solution and velocity
102  e->set_solution(x);
103 
104  // get the base residual vector and the Jacobians for numerical comparisons
105  // later.
106  e->internal_residual(true, res0, jac_x);
107 
108 
109  for (unsigned int i=0; i<ndofs; i++) {
110 
111 
112  // first the Jacobian due to x
113  x = x0;
114  // perturb the i^th element of the solution
115  x(i) += delta;
116  e->set_solution(x);
117 
118  // get the new residual
119  res.setZero();
120  e->internal_residual(false, res, dummy);
121 
122  // set the i^th column of the finite-differenced Jacobian
123  jac_x_fd.col(i) = (res-res0)/delta;
124  }
125 
126  // now compare the matrices
127  BOOST_CHECK(MAST::compare_matrix( jac_x_fd, jac_x, tol));
128 }
129 
130 
131 
132 BOOST_FIXTURE_TEST_SUITE (Structural1DJacobianEvaluation, MAST::BuildStructural1DElem)
133 
134 BOOST_AUTO_TEST_CASE (InternalForceJacobianLinear1DIndependentOffset) {
135 
136  this->init(false, false);
137 
138  RealVectorX v;
139  set_deformation(1, 3, libMesh::INVALID_ELEM, v);
140  check_internal_force_jacobian<MAST::BuildStructural1DElem>(*this, v);
141 }
142 
143 
144 BOOST_AUTO_TEST_CASE (InternalForceJacobianNonlinear1DIndependentOffset) {
145 
146  this->init(false, true);
147 
148  RealVectorX v;
149  set_deformation(1, 3, libMesh::INVALID_ELEM, v);
150  check_internal_force_jacobian<MAST::BuildStructural1DElem>(*this, v);
151 }
152 
153 
154 BOOST_AUTO_TEST_CASE (InternalForceJacobianLinear1DWithConstantOffset) {
155 
156  this->init(false, false);
157 
158  RealVectorX v;
159 
160  // set a finite value of the offset
161  (*_hy_off)() = 0.5*(*_thy)();
162  set_deformation(1, 3, libMesh::INVALID_ELEM, v);
163  check_internal_force_jacobian<MAST::BuildStructural1DElem>(*this, v);
164 }
165 
166 
167 BOOST_AUTO_TEST_CASE (InternalForceJacobianNonlinear1DWithConstantOffset) {
168 
169  this->init(false, true);
170 
171  RealVectorX v;
172 
173  // set a finite value of the offset
174  (*_hy_off)() = 0.5*(*_thy)();
175  set_deformation(1, 3, libMesh::INVALID_ELEM, v);
176  check_internal_force_jacobian<MAST::BuildStructural1DElem>(*this, v);
177 }
178 
179 
180 
181 BOOST_AUTO_TEST_CASE (InternalForceJacobianLinear1DIndependentOffsetDependentOffset) {
182 
183  this->init(true, false);
184 
185  RealVectorX v;
186  set_deformation(1, 3, libMesh::INVALID_ELEM, v);
187  check_internal_force_jacobian<MAST::BuildStructural1DElem>(*this, v);
188 }
189 
190 
191 
192 BOOST_AUTO_TEST_CASE (InternalForceJacobianNonlinear1DIndependentOffsetDependentOffset) {
193 
194  this->init(true, true);
195 
196  RealVectorX v;
197  set_deformation(1, 3, libMesh::INVALID_ELEM, v);
198  check_internal_force_jacobian<MAST::BuildStructural1DElem>(*this, v);
199 }
200 
201 
202 
203 
204 BOOST_AUTO_TEST_SUITE_END()
205 
206 
207 
208 BOOST_FIXTURE_TEST_SUITE (Structural2DJacobianEvaluation,
209  MAST::BuildStructural2DElem)
210 
211 BOOST_AUTO_TEST_CASE (InternalForceJacobianLinear2DIndependentOffsetQUAD4) {
212 
213  this->init(false, false, libMesh::QUAD4);
214 
215  RealVectorX v;
216  set_deformation(2, 3, libMesh::QUAD4, v);
217  check_internal_force_jacobian<MAST::BuildStructural2DElem>(*this, v);
218 }
219 
220 
221 BOOST_AUTO_TEST_CASE (InternalForceJacobianNonlinear2DIndependentOffsetQUAD4) {
222 
223  this->init(false, true, libMesh::QUAD4);
224 
225  RealVectorX v;
226  set_deformation(2, 3, libMesh::QUAD4, v);
227  check_internal_force_jacobian<MAST::BuildStructural2DElem>(*this, v);
228 }
229 
230 
231 BOOST_AUTO_TEST_CASE (InternalForceJacobianLinear2DWithConstantOffsetQUAD4) {
232 
233  this->init(false, false, libMesh::QUAD4);
234 
235  // set a finite value of the offset
236  (*_hzoff)() = 0.5*(*_thz)();
237 
238  RealVectorX v;
239  set_deformation(2, 3, libMesh::QUAD4, v);
240  check_internal_force_jacobian<MAST::BuildStructural2DElem>(*this, v);
241 }
242 
243 
244 BOOST_AUTO_TEST_CASE (InternalForceJacobianNonlinear2DWithConstantOffsetQUAD4) {
245 
246  this->init(false, true, libMesh::QUAD4);
247 
248  // set a finite value of the offset
249  (*_hzoff)() = 0.5*(*_thz)();
250 
251  RealVectorX v;
252  set_deformation(2, 3, libMesh::QUAD4, v);
253  check_internal_force_jacobian<MAST::BuildStructural2DElem>(*this, v);
254 }
255 
256 
257 BOOST_AUTO_TEST_CASE (InternalForceJacobianLinear2DDependentOffsetQUAD4) {
258 
259  this->init(true, false, libMesh::QUAD4);
260 
261  RealVectorX v;
262  set_deformation(2, 3, libMesh::QUAD4, v);
263  check_internal_force_jacobian<MAST::BuildStructural2DElem>(*this, v);
264 }
265 
266 
267 BOOST_AUTO_TEST_CASE (InternalForceJacobianNonlinear2DDependentOffsetQUAD4) {
268 
269  this->init(true, true, libMesh::QUAD4);
270 
271  RealVectorX v;
272  set_deformation(2, 3, libMesh::QUAD4, v);
273  check_internal_force_jacobian<MAST::BuildStructural2DElem>(*this, v);
274 }
275 
276 
277 
278 BOOST_AUTO_TEST_CASE (InternalForceJacobianLinear2DIndependentOffsetTRI3) {
279 
280  this->init(false, false, libMesh::TRI3);
281 
282  RealVectorX v;
283  set_deformation(2, 3, libMesh::TRI3, v);
284  check_internal_force_jacobian<MAST::BuildStructural2DElem>(*this, v);
285 }
286 
287 
288 BOOST_AUTO_TEST_CASE (InternalForceJacobianNonlinear2DIndependentOffsetTRI3) {
289 
290  this->init(false, true, libMesh::TRI3);
291 
292  RealVectorX v;
293  set_deformation(2, 3, libMesh::TRI3, v);
294  check_internal_force_jacobian<MAST::BuildStructural2DElem>(*this, v);
295 }
296 
297 
298 BOOST_AUTO_TEST_CASE (InternalForceJacobianLinear2DWithConstantOffsetTRI3) {
299 
300  this->init(false, false, libMesh::TRI3);
301 
302  // set a finite value of the offset
303  (*_hzoff)() = 0.5*(*_thz)();
304 
305  RealVectorX v;
306  set_deformation(2, 3, libMesh::TRI3, v);
307  check_internal_force_jacobian<MAST::BuildStructural2DElem>(*this, v);
308 }
309 
310 
311 BOOST_AUTO_TEST_CASE (InternalForceJacobianNonlinear2DWithConstantOffsetTRI3) {
312 
313  this->init(false, true, libMesh::TRI3);
314 
315  // set a finite value of the offset
316  (*_hzoff)() = 0.5*(*_thz)();
317 
318  RealVectorX v;
319  set_deformation(2, 3, libMesh::TRI3, v);
320  check_internal_force_jacobian<MAST::BuildStructural2DElem>(*this, v);
321 }
322 
323 
324 BOOST_AUTO_TEST_CASE (InternalForceJacobianLinear2DDependentOffsetTRI3) {
325 
326  this->init(true, false, libMesh::TRI3);
327 
328  RealVectorX v;
329  set_deformation(2, 3, libMesh::TRI3, v);
330  check_internal_force_jacobian<MAST::BuildStructural2DElem>(*this, v);
331 }
332 
333 
334 BOOST_AUTO_TEST_CASE (InternalForceJacobianNonlinear2DDependentOffsetTRI3) {
335 
336  this->init(true, true, libMesh::TRI3);
337 
338  RealVectorX v;
339  set_deformation(2, 3, libMesh::TRI3, v);
340  check_internal_force_jacobian<MAST::BuildStructural2DElem>(*this, v);
341 }
342 
343 
344 
345 BOOST_AUTO_TEST_SUITE_END()
346 
347 
348 
void check_internal_force_jacobian(ValType &v, const RealVectorX &sol)
bool compare_matrix(const RealMatrixX &m0, const RealMatrixX &m, const Real tol)
libMesh::Real Real
BOOST_FIXTURE_TEST_SUITE(Structural2DJacobianEvaluation, MAST::BuildStructural2DElem) BOOST_AUTO_TEST_CASE(InternalForceJacobianLinear2DIndependentOffsetQUAD4)
void set_deformation(const unsigned int dim, const unsigned int case_num, const libMesh::ElemType e, RealVectorX &vec)
BOOST_AUTO_TEST_CASE(InternalForceJacobianLinear1DIndependentOffset)
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