MAST
nonlinear_implicit_assembly_elem_operations.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
22 #include "base/elem_base.h"
23 
24 
25 
28 
29 }
30 
31 
34 
35 }
36 
37 
38 
39 namespace MAST {
40 
41  bool
42  is_numerical_zero(const Real v, const Real eps) {
43 
44  return fabs(v) <= eps;
45  }
46 
47 
48  bool
49  compare(const Real v1, const Real v2, const Real tol) {
50 
51  const Real
52  eps = 1.0e-7;
53 
54  bool rval = false;
55 
56  // check to see if the values are both small enough
57  // to be zero
58  if (MAST::is_numerical_zero(v1, eps) &&
59  MAST::is_numerical_zero(v2, eps))
60  rval = true;
61  // check to see if the absolute difference is small enough
62  else if (MAST::is_numerical_zero(v1-v2, eps))
63  rval = true;
64  // check to see if the relative difference is small enough
65  else if (fabs(v1) > 0)
66  rval = fabs((v1-v2)/v1) <= tol;
67 
68  return rval;
69  }
70 
71  bool
72  compare_matrix(const RealMatrixX& m0, const RealMatrixX& m, const Real tol) {
73 
74  unsigned int
75  m0_rows = (unsigned int) m0.rows(),
76  m0_cols = (unsigned int) m0.cols();
77  libmesh_assert_equal_to(m0_rows, m.rows());
78  libmesh_assert_equal_to(m0_cols, m.cols());
79 
80 
81  bool pass = true;
82  for (unsigned int i=0; i<m0_rows; i++) {
83  for (unsigned int j=0; j<m0_cols; j++)
84  if (!MAST::compare(m0(i,j), m(i,j), tol)) {
85  libMesh::out << "Failed comparison at (i,j) = ("
86  << i << ", " << j << ") : "
87  << "expected: " << m0(i,j) << " , "
88  << "computed: " << m(i,j) << " : "
89  << "diff: " << m0(i,j) - m(i,j) << " , "
90  << "tol: " << tol << std::endl;
91  pass = false;
92  }
93  }
94 
95  return pass;
96  }
97 }
98 
99 
100 
101 void
105  dsol,
106  res0,
107  dres;
108 
110  jac0,
111  jac,
112  dummy;
113 
114  unsigned int ndofs = (unsigned int)sol.size();
115  res0.setZero(ndofs);
116  dres.setZero(ndofs);
117  jac0.setZero(ndofs, ndofs);
118  jac.setZero(ndofs, ndofs);
119 
120  this->set_elem_solution(sol);
121  this->elem_calculations(true, res0, jac0);
122  Real delta = 1.0e-8;
123 
124  for (unsigned int i=0; i<sol.size(); i++) {
125  dsol = sol;
126  dsol(i) += delta;
127 
128  this->set_elem_solution(dsol);
129  this->elem_calculations(false, dres, dummy);
130  jac.col(i) = (dres-res0)/delta;
131  }
132 
133  // write the numerical and analytical jacobians
134  libMesh::out
135  << "Analytical Jacobian: " << std::endl
136  << jac0
137  << std::endl << std::endl
138  << "Numerical Jacobian: " << std::endl
139  << jac
140  << std::endl << std::endl;
141 
142  MAST::compare_matrix(jac, jac0, 1.0e-5);
143  // set the original solution vector for the element
144  this->set_elem_solution(sol);
145 }
146 
147 
void check_element_numerical_jacobian(RealVectorX &sol)
a helper function to evaluate the numerical Jacobian and compare it with the analytical Jacobian...
bool compare_matrix(const RealMatrixX &m0, const RealMatrixX &m, const Real tol)
virtual void set_elem_solution(const RealVectorX &sol)
sets the element solution
libMesh::Real Real
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 ...
Matrix< Real, Dynamic, Dynamic > RealMatrixX
Matrix< Real, Dynamic, 1 > RealVectorX
bool is_numerical_zero(const Real v, const Real eps)
bool compare(const Real v1, const Real v2, const Real tol)