modelwrapper.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_MODELWRAPPER_H
00023 #define MECHSYS_MODELWRAPPER_H
00024 
00025 // MechSys - models
00026 #include "models/equilibmodel.h"
00027 #include "models/coupled/coupledmodel.h"
00028 #include "models/linearelastic.h"
00029 #include "models/camclay.h"
00030 #include "models/subcam.h"
00031 #include "models/subtij.h"
00032 #include "models/subcamx.h"
00033 #include "models/barcelonax.h"
00034 #include "models/subbar.h"
00035 
00036 #ifdef MODELWRAPPER_WITH_VTK
00037   #include "vtkwrap/vtkwin.h"
00038   #include "vtkwrap/structgrid.h"
00039   #include "vtkwrap/sgridisosurf.h"
00040   #include "vtkwrap/colors.h"
00041   #include "vtkwrap/plane.h"
00042   #include "vtkwrap/hedgehog.h"
00043   #include "vtkwrap/sphere.h"
00044   #include "tensors/tensors.h"
00045   #include "tensors/functions.h"
00046   #include "numerical/meshgrid.h"
00047 #endif // MODELWRAPPER_WITH_VTK
00048 
00049 class ModelWrapper
00050 {
00051 public:
00052     // Constructor
00053     ModelWrapper(String const & Name, Array<REAL> const & Prms, Array<REAL> const & IniData);
00054 
00055     // Destructor
00056     ~ModelWrapper();
00057 
00058     // Methods
00059     void Actualize(Tensor2 const & DSig, REAL const & DPp, Tensor2 & DEps, REAL & DnSr)
00060     { (_cmodel==NULL ? _emodel->Actualize(DSig,DEps) : _cmodel->Actualize(DSig,DPp,DEps,DnSr)); }
00061 
00062     // Access methods
00063     //
00064     String          Name() const { return (_cmodel==NULL ? _emodel->Name() : _cmodel->Name()); }
00065     Tensor2 const & Sig () const { return (_cmodel==NULL ? _emodel->Sig () : _cmodel->Sig ()); }
00066     Tensor2 const & Eps () const { return (_cmodel==NULL ? _emodel->Eps () : _cmodel->Eps ()); }
00067     REAL            Pp  () const { return (_cmodel==NULL ?      0.0        : _cmodel->Pp  ()); }
00068 
00069     void InternalStateValues (Array<REAL>   & IntStateVals ) const
00070     { (_cmodel==NULL ? _emodel->InternalStateValues(IntStateVals ) : _cmodel->InternalStateValues(IntStateVals));  }
00071 
00072     void InternalStateNames  (Array<String> & IntStateNames) const
00073     { (_cmodel==NULL ? _emodel->InternalStateNames (IntStateNames) : _cmodel->InternalStateNames (IntStateNames)); }
00074 
00075 #ifdef MODELWRAPPER_WITH_VTK
00076     // VTK
00077     void AddInipsqActorsTo  (VTKWin & win, int nPts, bool ShowHH, REAL Opac, REAL & p_Max);
00078     void AddIniStateActorsTo(VTKWin & win, int nPts, bool ShowHH, REAL Opac, REAL & p_Max);
00079 #endif // MODELWRAPPER_WITH_VTK
00080 
00081 private:
00082     // Data
00083     CoupledModel * _cmodel;
00084     EquilibModel * _emodel;
00085     Array<REAL>    _prms;
00086 
00087 }; // class ModelWrapper
00088 
00089 #define ALLOCMODELFUNC(NAME,PRMS,INIDATA, PTR) (PTR = new ModelWrapper(NAME,PRMS,INIDATA))
00090 #define USE_COUPLEDMODEL
00091 #include "labtestsim/lts.h"
00092 #include "labtestsim/inputdata.h"
00093 
00094 
00096 
00097 
00098 inline ModelWrapper::ModelWrapper(String const & Name, Array<REAL> const & Prms, Array<REAL> const & IniData) // {{{
00099     : _cmodel (NULL),
00100       _emodel (NULL),
00101       _prms   (Prms)
00102 {
00103     // Discover if Name is a coupled model or not
00104     bool is_coupled = false;
00105     if (EquilibModelFactory.find(Name)==EquilibModelFactory.end())
00106     {
00107         if (CoupledModelFactory.find(Name)==CoupledModelFactory.end())
00108             throw new Fatal(_("ModelWrapper::ModelWrapper: Could not find %s model neither inside EquilibModelFactory nor CoupledModelFactory"),(Name.GetSTL().c_str()));
00109         is_coupled = true;
00110     }
00111 
00112     // Allocate the model
00113     if (is_coupled) _cmodel = AllocCoupledModel(Name,Prms,IniData);
00114     else
00115     {
00116         // Set inidata array
00117         Array<REAL> inidata;
00118         inidata.push_back(IniData[0]); // X stress component
00119         inidata.push_back(IniData[1]); // Y stress component
00120         inidata.push_back(IniData[2]); // Z stress component
00121         inidata.push_back(IniData[3]); // vini initial specific volume
00122         inidata.push_back(IniData[4]); // OCR Over consolidation rate
00123         _emodel = AllocEquilibModel(Name,Prms,inidata);
00124     }
00125 } // }}}
00126 
00127 inline ModelWrapper::~ModelWrapper() // {{{
00128 {
00129     if (_cmodel!=NULL) delete _cmodel;
00130     if (_emodel!=NULL) delete _emodel;
00131 } // }}}
00132 
00133 #ifdef MODELWRAPPER_WITH_VTK
00134 
00135 inline void ModelWrapper::AddInipsqActorsTo(VTKWin & win, int nPts, bool ShowHH, REAL Opac, REAL & p_Max) // {{{
00136 {
00137     using Tensors::Stress_p_q_S_sin3th;
00138     using Tensors::Tensor2;
00139     using Tensors::Norm;
00140     using Tensors::Hid2Sig;
00141     using Tensors::I;
00142 
00143     // Initial state
00144     Tensor2 sig_ini =  this->Sig();
00145     REAL    suc_ini = -this->Pp();
00146     Array<REAL> isv;   this->InternalStateValues(isv);
00147 
00148     if (this->Name()=="BarcelonaX")
00149     { // {{{
00150 
00151         // Initial data
00152         REAL k  = _prms[6];
00153         REAL z0 = isv[0];
00154         REAL z1 = isv[1];
00155         REAL M  = dynamic_cast<BarcelonaX*>(_cmodel)->GetM(/*Theta*/Util::ToRad(30.0));
00156         BarcelonaX::IntVars z; z=z0,z1;
00157         Tensor2 sig;
00158         Tensor2 dfdsig;
00159         REAL    dfdsuc;
00160 
00161         std::cout << "BarcelonaX:  z0=" << z0 << ", z1=" << z1 << std::endl;
00162 
00163         // MeshGrid
00164         REAL cp  = 1.3;
00165         REAL cs  = 1.3;
00166         REAL cq  = 1.2;
00167         REAL p0m = dynamic_cast<BarcelonaX*>(_cmodel)->Calc_p0(z1,z0);                 // p0 max
00168         REAL qm  = M*sqrt((z0/2.0+k*z1)*(p0m-z0/2.0)); // q max
00169         REAL pm  = p0m;                                // p max
00170         REAL p_min=-cp*k*z1;      p_Max=cp*pm;
00171         REAL s_min=0.0;      REAL s_max=cs*z1;
00172         REAL q_min=-cq*qm;   REAL q_max=cq*qm;
00173         MeshGrid mg(p_min ,p_Max ,nPts,
00174                     s_min ,s_max ,nPts,
00175                     q_min ,q_max ,nPts);
00176         int size = mg.Length();
00177 
00178         // Constants
00179         REAL        sq2 = sqrt(2.0);
00180         REAL        sq3 = sqrt(3.0);
00181         REAL F_ZERO_TOL = 1.0e-2;
00182         REAL          L = sqrt(z0*z0+z1*z1);
00183 
00184         // Calculate Yield Function Values and Gradients
00185         double * F = new double [size];
00186         StructGrid::VectorTuple * T = new StructGrid::VectorTuple [size];
00187         for (int i=0; i<size; ++i)
00188         { // {{{
00189             // Grid values (position)
00190             REAL   p = mg.X()[i];
00191             REAL suc = mg.Y()[i];
00192             REAL   q = mg.Z()[i];
00193             REAL  th = 0.0;
00194             REAL s1,s2,s3; // principal values of stress
00195             Hid2Sig(p,q,th, s1,s2,s3);
00196             sig=s1,s2,s3,0,0,0;
00197 
00198             // Yield function value
00199             F[i] = dynamic_cast<BarcelonaX*>(_cmodel)->YieldFunc(sig,z,suc);
00200 
00201             // Derivatives
00202             dynamic_cast<BarcelonaX*>(_cmodel)->Calc_dFdSig_dFdSuc(sig,z,suc, dfdsig,dfdsuc); // V=dfdsig, S=dfdsuc
00203             REAL     trV = (dfdsig(0)+dfdsig(1)+dfdsig(2));   // = tr(V) = tr(dfdsig)
00204             Tensor2 devV = dfdsig - (trV/3.0)*I; 
00205             REAL    dfdp = trV;
00206             REAL    dfdq = 2.0*sq3*Norm(devV)/(3.0*sq2);
00207             if (q<0.0) dfdq=-dfdq;
00208 
00209             // Normal vectors
00210             REAL Grad[3];
00211             Grad[0] = dfdp;
00212             Grad[1] = 0.0; //dfdsuc;
00213             Grad[2] = dfdq;
00214 
00215             // Normalize
00216             T[i].vx=Grad[0]; T[i].vy=Grad[1]; T[i].vz=Grad[2];
00217             if (fabs(F[i])/L<=F_ZERO_TOL)
00218             {
00219                 REAL norm=1.0;
00220                 if (true) norm = sqrt(Grad[0]*Grad[0]+Grad[1]*Grad[1]+Grad[2]*Grad[2]);
00221                 if (norm/z0>1.0e-5)
00222                 { T[i].vx=Grad[0]/norm; T[i].vy=Grad[1]/norm; T[i].vz=Grad[2]/norm; }
00223                 else
00224                 { T[i].vx=0.0; T[i].vy=0.0; T[i].vz=0.0; }
00225             }
00226             else
00227             { T[i].vx=0.0; T[i].vy=0.0; T[i].vz=0.0; }
00228         } // }}}
00229 
00230         // Structured grid
00231         StructGrid sg   (mg.X(),nPts, mg.Y(),nPts, mg.Z(),nPts, F, 0); // p,suc,q, F,T
00232         StructGrid sg_hh(mg.X(),nPts, mg.Y(),nPts, mg.Z(),nPts, 0, T); // p,suc,q, F,T
00233         //sg.WriteFile("tmp.sgrid.vtk");
00234 
00235         // LookupTable of colors
00236         vtkLookupTable * lt = vtkLookupTable::New();
00237         lt->SetNumberOfColors(10);
00238         lt->Build();
00239         for (int i=0; i<10; ++i) lt->SetTableValue(i,CLR["salmon"].C);
00240 
00241         // Generate the IsoSurface
00242         SGridIsoSurf iso(sg.GetGrid(), /*F*/0.0, lt);
00243         iso.GetActor()->GetProperty()->SetOpacity(Opac);
00244         win.AddActor(iso.GetActor());
00245 
00246         /*
00247         // Vertical plane
00248         Plane pl(p_min,z1,q_min, 0,1,0, "peacock", 0.3);
00249         pl.GetObject()->SetPoint1(p_min,z1,q_max);
00250         pl.GetObject()->SetPoint2(p_Max,z1,q_min);
00251         //pl.GetObject()->SetPoint2(p_Max/2.0,z1,q_min);
00252         //win.AddActor(pl.GetActor());
00253         */
00254 
00255         // Normals
00256         if (ShowHH)
00257         {
00258             REAL normals_sf = 0.2*L;
00259             HedgeHog * hh = new HedgeHog (sg_hh.GetGrid(), normals_sf);
00260             hh->GetActor()->GetProperty()->SetColor(CLR["light_blue"].C);
00261             win.AddActor(hh->GetActor());
00262             delete hh;
00263         }
00264 
00265         // Stress/suction points
00266         REAL p_ini,q_ini,sin3th_ini;
00267         Tensor2 S_ini;
00268         Stress_p_q_S_sin3th(sig_ini,p_ini,q_ini,S_ini,sin3th_ini);
00269         Sphere sp_ini(p_ini,suc_ini,q_ini, 0.03*p_Max, "yellow");
00270         win.AddActor(sp_ini.GetActor());
00271 
00272         std::cout << "BarcelonaX:  p_ini=" << p_ini << ", q_ini=" << q_ini << ", suc_ini=" << suc_ini << std::endl;
00273 
00274         // Clean up
00275         lt->Delete();
00276         delete [] F;
00277 
00278     } // BarcelonaX // }}}
00279 
00280     else if (this->Name()=="SubBar")
00281     { // {{{
00282 
00283         REAL k  = _prms[6];
00284         REAL z0 = isv[0];
00285         REAL z1 = isv[1];
00286         REAL z2 = isv[2];
00287         REAL z3 = isv[3];
00288         REAL M  = dynamic_cast<SubBar*>(_cmodel)->GetM(/*Theta*/Util::ToRad(30.0));
00289         SubBar::IntVars z; z=z0,z1,z2,z3;
00290         Tensor2 sig;
00291         Tensor2 dfdsig;
00292         REAL    dfdsuc;
00293 
00294         std::cout << "VTKSubBar:  z0=" << z0 << ", z1=" << z1 << ", z2=" << z2 << ", z3=" << z3 << std::endl;
00295 
00296         // MeshGrid
00297         SubBar::IntVars z_tmp; z_tmp=z2,z3,0,0;
00298         REAL cp  = 1.1;
00299         REAL cs  = 1.3;
00300         REAL cq  = 1.1;
00301         REAL p0m = dynamic_cast<SubBar*>(_cmodel)->Calc_p0(z_tmp,z3);             // p0 max
00302         REAL qm  = M*sqrt((z2/2.0+k*z3)*(p0m-z2/2.0)); // q max
00303         REAL pm  = p0m;                                // p max
00304         REAL p_min=-cp*k*z3;      p_Max=cp*pm;
00305         REAL s_min=0.0;      REAL s_max=cs*z3;
00306         REAL q_min=-cq*qm;   REAL q_max=cq*qm;
00307         MeshGrid mg(p_min ,p_Max ,nPts,
00308                     s_min ,s_max ,nPts,
00309                     q_min ,q_max ,nPts);
00310         int size = mg.Length();
00311 
00312         // Constants
00313         REAL        sq2 = sqrt(2.0);
00314         REAL        sq3 = sqrt(3.0);
00315         REAL F_ZERO_TOL = 1.0e-2;
00316         REAL          L = sqrt(z0*z0+z1*z1);
00317 
00318         // Calculate Yield Function Values and Gradients
00319         double * F_sl = new double [size]; // Subloading
00320         double * F_no = new double [size]; // Normal
00321         StructGrid::VectorTuple * T = new StructGrid::VectorTuple [size];
00322         for (int i=0; i<size; ++i)
00323         { // {{{
00324             // Grid values (position)
00325             REAL   p = mg.X()[i];
00326             REAL suc = mg.Y()[i];
00327             REAL   q = mg.Z()[i];
00328             REAL  th = 0.0;
00329             REAL s1,s2,s3; // principal values of stress
00330             Hid2Sig(p,q,th, s1,s2,s3);
00331             sig=s1,s2,s3,0,0,0;
00332 
00333             // Yield function value
00334             F_sl[i] = dynamic_cast<SubBar*>(_cmodel)->YieldFunc       (sig,z,suc);
00335             F_no[i] = dynamic_cast<SubBar*>(_cmodel)->YieldFunc_Normal(sig,z,suc);
00336 
00337             // Derivatives
00338             dynamic_cast<SubBar*>(_cmodel)->Calc_dFdSig_dFdSuc(sig,z,suc, dfdsig,dfdsuc); // V=dfdsig, S=dfdsuc
00339             REAL     trV = (dfdsig(0)+dfdsig(1)+dfdsig(2));   // = tr(V) = tr(dfdsig)
00340             Tensor2 devV = dfdsig - (trV/3.0)*I; 
00341             REAL    dfdp = trV;
00342             REAL    dfdq = 2.0*sq3*Norm(devV)/(3.0*sq2);
00343             if (q<0.0) dfdq = -dfdq;
00344 
00345             // Normal vectors
00346             REAL Grad[3];
00347             Grad[0] = dfdp;
00348             Grad[1] = dfdsuc;
00349             Grad[2] = dfdq;
00350 
00351             // Normalize
00352             T[i].vx=Grad[0]; T[i].vy=Grad[1]; T[i].vz=Grad[2];
00353             if (fabs(F_sl[i])/L<=F_ZERO_TOL)
00354             {
00355                 REAL norm=1.0;
00356                 if (true) norm = sqrt(Grad[0]*Grad[0]+Grad[1]*Grad[1]+Grad[2]*Grad[2]);
00357                 if (norm/z0>1.0e-5)
00358                 { T[i].vx=Grad[0]/norm; T[i].vy=Grad[1]/norm; T[i].vz=Grad[2]/norm; }
00359                 else
00360                 { T[i].vx=0.0; T[i].vy=0.0; T[i].vz=0.0; }
00361             }
00362             else
00363             { T[i].vx=0.0; T[i].vy=0.0; T[i].vz=0.0; }
00364         } // }}}
00365 
00366         // Structured grid
00367         StructGrid sg_sl(mg.X(),nPts, mg.Y(),nPts, mg.Z(),nPts, F_sl, T); // p,suc,q, F,T
00368         StructGrid sg_no(mg.X(),nPts, mg.Y(),nPts, mg.Z(),nPts, F_no, T); // p,suc,q, F,T
00369 
00370         // LookupTable of colors
00371         vtkLookupTable * lt_sl = vtkLookupTable::New();
00372         vtkLookupTable * lt_no = vtkLookupTable::New();
00373         lt_sl->SetNumberOfColors(10);
00374         lt_sl->Build();
00375         lt_no->SetNumberOfColors(10);
00376         lt_no->Build();
00377         for (int i=0; i<10; ++i) lt_sl->SetTableValue(i,CLR["forest_green"].C);
00378         for (int i=0; i<10; ++i) lt_no->SetTableValue(i,CLR["peacock"].C);
00379 
00380         // Generate the IsoSurface
00381         SGridIsoSurf iso_sl(sg_sl.GetGrid(), /*F*/0.0, lt_sl);
00382         SGridIsoSurf iso_no(sg_no.GetGrid(), /*F*/0.0, lt_no);
00383         iso_sl.GetActor()->GetProperty()->SetOpacity(Opac*0.7);
00384         iso_no.GetActor()->GetProperty()->SetOpacity(Opac);
00385         win.AddActor(iso_sl.GetActor());
00386         win.AddActor(iso_no.GetActor());
00387 
00388         /*
00389         // Vertical plane
00390         Plane pl(p_min,z3,q_min, 0,1,0, "wheat", 0.5);
00391         pl.GetObject()->SetPoint1(p_min,z3,q_max);
00392         pl.GetObject()->SetPoint2(p_Max,z3,q_min);
00393         //pl.GetObject()->SetPoint2(p_Max/2.0,z1,q_min);
00394         win.AddActor(pl.GetActor());
00395         */
00396 
00397         // Stress/suction points
00398         REAL p_ini,q_ini,sin3th_ini;
00399         Tensor2 S_ini;
00400         Stress_p_q_S_sin3th(sig_ini,p_ini,q_ini,S_ini,sin3th_ini);
00401         Sphere sp_ini(p_ini,suc_ini,q_ini, 0.03*p_Max, "yellow");
00402         win.AddActor(sp_ini.GetActor());
00403 
00404         std::cout << "VTKSubBar:  p_ini=" << p_ini << ", q_ini=" << q_ini << ", suc_ini=" << suc_ini << std::endl;
00405 
00406         /*
00407         // Normals
00408         REAL normals_sf = 0.3*L;
00409         HedgeHog * hh_sl = new HedgeHog (sg_sl.GetGrid(), normals_sf);
00410         win.AddActor(hh_sl->GetActor());
00411         delete hh_sl;
00412         */
00413 
00414         // Clean up
00415         delete [] F_sl;
00416         delete [] F_no;
00417         lt_sl -> Delete();
00418         lt_no -> Delete();
00419 
00420     } // SubBar // }}}
00421 
00422 } // }}}
00423 
00424 inline void ModelWrapper::AddIniStateActorsTo(VTKWin & win, int nPts, bool ShowHH, REAL Opac, REAL & p_Max) // {{{
00425 {
00426 } // }}}
00427 
00428 #endif // MODELWRAPPER_WITH_VTK
00429 
00430 #endif // MECHSYS_MODELWRAPPER_H
00431 
00432 // vim:fdm=marker

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