newtonraphson.h

00001 /*************************************************************************************
00002  * MechSys - A C++ library to simulate (Continuum) Mechanical Systems                *
00003  * Copyright (C) 2005 Dorival de Moraes Pedroso <dorival.pedroso at gmail.com>       *
00004  * Copyright (C) 2005 Raul Dario Durand Farfan  <raul.durand at gmail.com>           *
00005  *                                                                                   *
00006  * This file is part of MechSys.                                                     *
00007  *                                                                                   *
00008  * MechSys is free software; you can redistribute it and/or modify it under the      *
00009  * terms of the GNU General Public License as published by the Free Software         *
00010  * Foundation; either version 2 of the License, or (at your option) any later        *
00011  * version.                                                                          *
00012  *                                                                                   *
00013  * MechSys is distributed in the hope that it will be useful, but WITHOUT ANY        *
00014  * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A   *
00015  * PARTICULAR PURPOSE. See the GNU General Public License for more details.          *
00016  *                                                                                   *
00017  * You should have received a copy of the GNU General Public License along with      *
00018  * MechSys; if not, write to the Free Software Foundation, Inc., 51 Franklin Street, *
00019  * Fifth Floor, Boston, MA 02110-1301, USA                                           *
00020  *************************************************************************************/
00021 
00022 #ifndef MECHSYS_FEM_NEWTON_H
00023 #define MECHSYS_FEM_NEWTON_H
00024 
00025 #ifdef HAVE_CONFIG_H
00026   #include "config.h"
00027 #else
00028   #ifndef REAL
00029     #define REAL double
00030   #endif
00031 #endif
00032 
00033 // MechSys
00034 #include "fem/solver/solver.h"
00035 
00036 namespace FEM
00037 {
00038 
00039 class NewtonRaphson : public Solver
00040 {
00041 public:
00042     NewtonRaphson(Array<String> const & SolverCtes, FEM::InputData const & ID, FEM::Data & data, FEM::Output & output)
00043         : Solver              (ID, data, output),
00044           _itol               (1.0e-10) ,
00045           _max_it             (20)      ,
00046           _use_modfied_newton (false)   ,
00047           _nSS                (1)
00048     {
00049         for (size_t i=0; i<SolverCtes.size(); ++i)
00050         {
00051             LineParser LP(SolverCtes[i]);
00052             LP.ReplaceAllChars('=',' ');
00053             String key;               LP>>key;
00054                  if (key=="ITOL")     LP>>_itol;
00055             else if (key=="maxIt")    LP>>_max_it;
00056             else if (key=="modified") LP>>_use_modfied_newton;
00057             else if (key=="nSS")      LP>>_nSS;
00058             else throw new Fatal(_("AutoME::AutoME: < %s > is invalid "), key.c_str());
00059         }
00060         if (_nSS<1) throw new Fatal(_("NewtonRaphson::NewtonRaphson: The number of substeps (nSS=%d) must be greater than or equal to 1"), _nSS);
00061     }
00062     void _do_solve_for_an_increment(int                  const   increment,  // In
00063                                     LinAlg::Vector<REAL> const & DF_ext   ,  // In
00064                                     LinAlg::Vector<REAL> const & DU_ext   ,  // In
00065                                     LinAlg::Matrix<REAL>       & K        ,  // (no needed outside)
00066                                     LinAlg::Vector<REAL>       & dF_int   ,  // (no needed outside)
00067                                     LinAlg::Vector<REAL>       & Rinternal,  // (no needed outside)
00068                                     IntSolverData              & ISD      ); // Out
00069 private:
00070     REAL _itol;
00071     int  _max_it;
00072     bool _use_modfied_newton;
00073     int  _nSS;
00074     LinAlg::Vector<REAL> _dU;
00075 }; // class NewtonRaphson
00076 
00077 
00079 
00080 
00081 inline void NewtonRaphson::_do_solve_for_an_increment(int                  const   increment, // In // {{{
00082                                                       LinAlg::Vector<REAL> const & DF_ext   , // In
00083                                                       LinAlg::Vector<REAL> const & DU_ext   , // In
00084                                                       LinAlg::Matrix<REAL>       & K        , // (no needed outside)
00085                                                       LinAlg::Vector<REAL>       & dF_int   , // (no needed outside)
00086                                                       LinAlg::Vector<REAL>       & Rinternal, // (no needed outside)
00087                                                       IntSolverData              & ISD      ) // Out
00088 
00089 {
00090     // Allocate and fill dF_ext and dU_ext
00091     int n_tot_dof = DF_ext.Size();
00092     LinAlg::Vector<REAL> dF_ext; dF_ext.Resize(n_tot_dof);
00093     LinAlg::Vector<REAL> dU_ext; dU_ext.Resize(n_tot_dof);
00094     LinAlg::CopyScal(1.0/_nSS,DF_ext, dF_ext); // dF_ext <- DF_ext/_nss
00095     LinAlg::CopyScal(1.0/_nSS,DU_ext, dU_ext); // dU_ext <- DU_ext/_nss
00096 
00097     // Start
00098     for (int i=0; i<_nSS; ++i)
00099     {
00100         // Check for the first increment
00101         if (increment==0)
00102         {
00103             // Allocate a vector of increment of displacements
00104             _dU.Resize(dF_ext.Size());
00105         }
00106 
00107         // Calculate the first residual (=dF_ext)
00108         LinAlg::Copy(dF_ext, Rinternal); // R <- dF_ext
00109         REAL resid = 1.0+_itol;          // resid <- norm_R/den
00110 
00111         // Calculate the first (external) disp. increment
00112         LinAlg::Copy(dU_ext, _dU);  // _dU <- dU_ext
00113 
00114         // Check for Modified-Newton-Raphson scheme
00115         if (_use_modfied_newton)
00116             _assemb_K(K);
00117 
00118         // Loop over iterations
00119         bool converged=false;
00120         for (int k=0; k<_max_it; ++k)
00121         {
00122             // Check for convergence
00123             if (resid<=_itol) { converged=true; break;}
00124             
00125             // Calculate dU_ext using a Forward Euler step
00126             if (_use_modfied_newton)
00127                 _inv_K_times_X(K, true, Rinternal, _dU); // In=R,U; Out=_dU  =>  _dU=inv[K(U)]*R
00128             else
00129             {
00130                 _assemb_K(K);
00131                 _inv_K_times_X(K, false, Rinternal, _dU); // In=R,U; Out=_dU  =>  _dU=inv[K(U)]*R
00132             }
00133             
00134             // Update Nodes and Elements state
00135             _update_nodes_and_elements_state(_dU, dF_int);
00136 
00137             // Calculate residual (internal)
00138             LinAlg::Axpy(-1.0,dF_int, Rinternal);  //  R <- R - dF_int (for the first iteration: R<-dF_ext-dF_int)
00139             _dU.SetValues(0.0);                    // _dU <- 0.0 TODO: check this: after the first iteration there will be no more increment of disp.
00140 
00141             // Calc resid
00142             REAL denom = 0.0;                      // Normalizer
00143             for (int i=0; i<Rinternal.Size(); i++) denom += pow((dF_ext(i)+dF_int(i))/2.0, 2.0);
00144             resid = LinAlg::Norm(Rinternal)/sqrt(denom);
00145             ISD.BE_resid(resid); 
00146 
00147         }
00148         if (!converged)
00149             throw new Fatal(_("NewtonRaphson::_do_solve did not converge for %d iterations"),_max_it);
00150     }
00151 
00152 } // }}}
00153 
00154 
00156 
00157 
00158 // Allocate a new NewtonRaphson solver
00159 Solver * NewtonRaphsonMaker(Array<String> const & SolverCtes, FEM::InputData const & ID, FEM::Data & data, FEM::Output & output) // {{{
00160 {
00161     return new NewtonRaphson(SolverCtes, ID, data, output);
00162 } // }}}
00163 
00164 // Register an NewtonRaphson solver into SolverFactory array map
00165 int NewtonRaphsonRegister() // {{{
00166 {
00167     SolverFactory["NewtonRaphson"] = NewtonRaphsonMaker;
00168     return 0;
00169 } // }}}
00170 
00171 // Execute the autoregistration
00172 int __NewtonRaphson_dummy_int = NewtonRaphsonRegister();
00173 
00174 }; // namespace FEM
00175 
00176 #endif // MECHSYS_FEM_NEWTON_H
00177 
00178 // vim:fdm=marker

Generated on Wed Jan 24 15:56:25 2007 for MechSys by  doxygen 1.4.7