EulerKaapiSolver.cpp

Go to the documentation of this file.
00001 /******************************************************************************
00002 *       SOFA, Simulation Open-Framework Architecture, version 1.0 beta 3      *
00003 *                (c) 2006-2008 MGH, INRIA, USTL, UJF, CNRS                    *
00004 *                                                                             *
00005 * This library is free software; you can redistribute it and/or modify it     *
00006 * under the terms of the GNU Lesser General Public License as published by    *
00007 * the Free Software Foundation; either version 2.1 of the License, or (at     *
00008 * your option) any later version.                                             *
00009 *                                                                             *
00010 * This library is distributed in the hope that it will be useful, but WITHOUT *
00011 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or       *
00012 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License *
00013 * for more details.                                                           *
00014 *                                                                             *
00015 * You should have received a copy of the GNU Lesser General Public License    *
00016 * along with this library; if not, write to the Free Software Foundation,     *
00017 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA.          *
00018 *******************************************************************************
00019 *                               SOFA :: Modules                               *
00020 *                                                                             *
00021 * Authors: The SOFA Team and external contributors (see Authors.txt)          *
00022 *                                                                             *
00023 * Contact information: contact@sofa-framework.org                             *
00024 ******************************************************************************/
00025 #include <sofa/component/odesolver/EulerKaapiSolver.h>
00026 #include <sofa/core/ObjectFactory.h>
00027 #include <math.h>
00028 #include <iostream>
00029 #include <sofa/simulation/common/MechanicalKaapiAction.h>
00030 #include <sofa/simulation/common/MechanicalVPrintAction.h>
00031 
00032 
00033 using
00034   std::cerr;
00035 using
00036   std::endl;
00037   using namespace sofa::simulation::tree;
00038   using namespace sofa::simulation;
00039 
00040 
00041 namespace
00042   sofa
00043 {
00044 
00045   namespace
00046     component
00047   {
00048 
00049     namespace
00050       odesolver
00051     {
00052 
00053       using namespace
00054     sofa::defaulttype;
00055       using namespace
00056     core::componentmodel::behavior;
00057 
00058       int
00059     EulerKaapiSolverClass =
00060     core::RegisterObject ("A simple explicit time integrator with kaapi").
00061     add < EulerKaapiSolver > ().addAlias ("EulerKaapi");
00062 
00063       SOFA_DECL_CLASS (EulerKaapi);
00064 
00065       EulerKaapiSolver::EulerKaapiSolver ():
00066       symplectic (dataField
00067           (&symplectic, true, "symplectic",
00068            "If true, the velocities are updated before the velocities and the method is symplectic (more robust). If false, the positions are updated before the velocities (standard Euler, less robust)."))
00069       {
00070       }
00071 
00072 
00073 
00074    EulerKaapiSolver::VecId EulerKaapiSolver::v_alloc(VecId::Type t)
00075        {
00076                VecId v(t, vectors[t].alloc());
00077                    MechanicalVAllocAction(v).execute( getContext() );
00078                        return v;
00079                        }
00080                      void EulerKaapiSolver::v_clear(VecId v) 
00081                          {
00082                              MechanicalVOpAction(v).execute( getContext() );
00083                              }
00084 
00085                        void EulerKaapiSolver::v_free(VecId v)
00086                            {
00087                                if (vectors[v.type].free(v.index))
00088                                        MechanicalVFreeAction(v).execute( getContext() );
00089                                        }
00090 
00091 
00092 
00093     void EulerKaapiSolver::v_peq(VecId v, VecId a, double f) 
00094     {
00095     std::cout<<"ve peque"<<std::endl;
00096         MechanicalVOpAction(v,v,a,f).execute( getContext() );
00097     }
00098 
00099     void EulerKaapiSolver::projectResponse(VecId dx, double **W)
00100     {
00101         MechanicalApplyConstraintsAction(dx, W).execute( getContext() );
00102     }
00103 
00104 
00105 
00106     void EulerKaapiSolver::accFromF(VecId a, VecId f)
00107     {
00108         MechanicalAccFromFAction(a,f).execute( getContext() );
00109     }
00110 
00111     void EulerKaapiSolver::propagatePositionAndVelocity(double t, VecId x, VecId v)
00112     {
00113         MechanicalPropagatePositionAndVelocityAction(t,x,v).execute( getContext() );
00114     }
00115 
00116     void EulerKaapiSolver::computeForce(VecId result)
00117     {
00118         MechanicalResetForceAction(result).execute( getContext() );
00119         finish();
00120         MechanicalComputeForceAction(result).execute( getContext() );
00121     }
00122 
00123 
00124 void EulerKaapiSolver::computeAcc (double t, VecId a, VecId x, VecId v)
00125       {
00126     MultiVector
00127     f (this, VecId::force ());
00128     std::cout<<"kaapi solver"<<std::endl;
00129     propagatePositionAndVelocity (t, x, v);
00130     computeForce (f);
00131     if (this->f_printLog.getValue () == true)
00132       {
00133         cerr << "OdeSolver::computeAcc, f = " << f << endl;
00134       }
00135 
00136     accFromF (a, f);
00137     projectResponse (a);
00138       }
00139 
00140       void
00141       EulerKaapiSolver::solve (double dt)
00142       {
00143     //objectmodel::BaseContext* group = getContext();
00144     OdeSolver *
00145       group = this;
00146     MultiVector
00147     pos (group, VecId::position ());
00148     MultiVector
00149     vel (group, VecId::velocity ());
00150     MultiVector
00151     acc (group, VecId::dx ());
00152     bool
00153       printLog = 1;
00154     // f_printLog.getValue();
00155 
00156     if (printLog)
00157       {
00158         cerr << "EulerKaapiSolver, dt = " << dt << endl;
00159         cerr << "EulerKaapiSolver, initial x = " << pos << endl;
00160         cerr << "EulerKaapiSolver, initial v = " << vel << endl;
00161       }
00162 
00163     computeAcc (getTime (), acc, pos, vel);
00164 
00165     // update state
00166     if (symplectic.getValue ())
00167       {
00168         vel.peq (acc, dt);
00169         pos.peq (vel, dt);
00170       }
00171     else
00172       {
00173         pos.peq (vel, dt);
00174         vel.peq (acc, dt);
00175       }
00176 
00177     if (printLog)
00178       {
00179         cerr << "EulerKaapiSolver, acceleration = " << acc << endl;
00180         cerr << "EulerKaapiSolver, final x = " << pos << endl;
00181         cerr << "EulerKaapiSolver, final v = " << vel << endl;
00182       }
00183       }
00184 
00185     }               // namespace odesolver
00186 
00187   }             // namespace component
00188 
00189 }               // namespace sofa

Generated on Sun Nov 23 03:42:46 2008 for Sofa by  doxygen 1.5.7.1