DataTransferKit - Multiphysics Solution Transfer Services  2.0
DTK_NewtonSolver_impl.hpp
Go to the documentation of this file.
1 //---------------------------------------------------------------------------//
2 /*
3  Copyright (c) 2012, Stuart R. Slattery
4  All rights reserved.
5 
6  Redistribution and use in source and binary forms, with or without
7  modification, are permitted provided that the following conditions are
8  met:
9 
10  *: Redistributions of source code must retain the above copyright
11  notice, this list of conditions and the following disclaimer.
12 
13  *: Redistributions in binary form must reproduce the above copyright
14  notice, this list of conditions and the following disclaimer in the
15  documentation and/or other materials provided with the distribution.
16 
17  *: Neither the name of the University of Wisconsin - Madison nor the
18  names of its contributors may be used to endorse or promote products
19  derived from this software without specific prior written permission.
20 
21  THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
22  "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
23  LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
24  A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
25  HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
26  SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
27  LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
28  DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
29  THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
30  (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
31  OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32 */
33 //---------------------------------------------------------------------------//
39 //---------------------------------------------------------------------------//
40 
41 #ifndef DTK_NEWTONSOLVER_IMPL_HPP
42 #define DTK_NEWTONSOLVER_IMPL_HPP
43 
44 #include <cmath>
45 #include <limits>
46 
47 #include "DTK_DBC.hpp"
48 
49 #include <Teuchos_ScalarTraits.hpp>
50 
51 #include <Intrepid_RealSpaceTools.hpp>
52 
53 namespace DataTransferKit
54 {
55 //---------------------------------------------------------------------------//
59 template <typename NonlinearProblem>
61  NonlinearProblem &problem,
62  const double tolerance,
63  const int max_iters )
64 {
65  DTK_REQUIRE( 2 == u.rank() );
66 
67  // Allocate nonlinear residual, Jacobian, Newton update, and work arrays.
68  int d0 = u.dimension( 0 );
69  int d1 = u.dimension( 1 );
70  MDArray F( d0, d1 );
71  MDArray J( d0, d1, d1 );
72  MDArray J_inv( d0, d1, d1 );
73  MDArray update( d0, d1 );
74  MDArray u_old = u;
75  MDArray conv_check( d0 );
76 
77  // Compute the initial state.
78  NPT::updateState( problem, u );
79 
80  // Computen the initial nonlinear residual and scale by -1 to get -F(u).
81  NPT::evaluateResidual( problem, u, F );
82  Intrepid::RealSpaceTools<Scalar>::scale(
83  F, -Teuchos::ScalarTraits<Scalar>::one() );
84 
85  // Compute the initial Jacobian.
86  NPT::evaluateJacobian( problem, u, J );
87 
88  // Check for degeneracy of the Jacobian. If it is degenerate then the
89  // problem is ill conditioned and return very large numbers in the state
90  // vector that correspond to no solution.
91  MDArray det( 1 );
92  Intrepid::RealSpaceTools<Scalar>::det( det, J );
93  if ( std::abs( det( 0 ) ) < tolerance )
94  {
95  for ( int m = 0; m < d0; ++m )
96  {
97  for ( int n = 0; n < d1; ++n )
98  {
99  u( m, n ) = std::numeric_limits<Scalar>::max();
100  }
101  }
102  return;
103  }
104 
105  // Nonlinear solve.
106  for ( int k = 0; k < max_iters; ++k )
107  {
108  // Solve the linear model, delta_u = J^-1 * -F(u).
109  Intrepid::RealSpaceTools<Scalar>::inverse( J_inv, J );
110  Intrepid::RealSpaceTools<Scalar>::matvec( update, J_inv, F );
111 
112  // Update the solution, u += delta_u.
113  Intrepid::RealSpaceTools<Scalar>::add( u, update );
114 
115  // Check for convergence.
116  Intrepid::RealSpaceTools<Scalar>::subtract( u_old, u );
117  Intrepid::RealSpaceTools<Scalar>::vectorNorm( conv_check, u_old,
118  Intrepid::NORM_TWO );
119  if ( tolerance > conv_check( 0 ) )
120  {
121  break;
122  }
123 
124  // Reset for the next iteration.
125  u_old = u;
126 
127  // Update any state-dependent data from the last iteration using the
128  // new solution vector.
129  NPT::updateState( problem, u );
130 
131  // Compute the nonlinear residual and scale by -1 to get -F(u).
132  NPT::evaluateResidual( problem, u, F );
133  Intrepid::RealSpaceTools<Scalar>::scale(
134  F, -Teuchos::ScalarTraits<Scalar>::one() );
135 
136  // Compute the Jacobian.
137  NPT::evaluateJacobian( problem, u, J );
138  }
139 
140  // Check for convergence.
141  DTK_ENSURE( tolerance > conv_check( 0 ) );
142 }
143 
144 //---------------------------------------------------------------------------//
145 
146 } // end namespace DataTransferKit
147 
148 //---------------------------------------------------------------------------//
149 
150 #endif // end DTK_NEWTONSOLVER_IMPL_HPP
151 
152 //---------------------------------------------------------------------------//
153 // end DTK_NewtonSolver_impl.hpp
154 //---------------------------------------------------------------------------//
static void solve(MDArray &u, NonlinearProblem &problem, const double tolerance, const int max_iters)
Get the center of the reference cell of the given topology.
Assertions and Design-by-Contract for error handling.
DTK_BasicEntitySet.cpp.