Mechanics.h 13 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347
/*
    Copyright © 2004 Nicolas MOES and Nicolas CHEVAUGEON All Right Reserved
    CrackComput source code is subject to non-permissive licence,
    see the LICENSE files for conditions.
*/
#ifndef _MECHANICS_
#define _MECHANICS_

#include "xIntegrationRule.h"
#include "xPhysSurf.h"
#include "xField.h"
#include "xMaterial.h"
#include "xMaterialSensitivity.h"
#include "xTensorOperations.h"
#include "xForm.h"
#include "xOperators.h"
#include "lCrack.h"
#include "xcCrack.h"
#include "xEnv.h"
#include <set>

namespace xfem{
  class xMesh;
  class xAssembler;
}

class xcCrack;
using namespace xfem;
using Trellis_Util::mPoint;
using AOMD::mEntity;

using  xtool::xIdentity;
using  xtool::xMult;
using  xtool::xAdd;
using  xtensor::xVector;
using  xtensor::xTensor4Isotropic;
using  xtensor::xSymmetrize;
using  xtensor::xNormalizeVector;


template < class VALTYPE >
class xEvalFieldAtPoint {
 public :
  xEvalFieldAtPoint (const xEval< VALTYPE>& e, const xMesh &m) : mesh(m), eval(e) {
  }
  VALTYPE   operator()(mPoint &p){
    VALTYPE val;
    std::set<mEntity *> sete;
    mesh.locateElementOctree( p, sete);
    if (sete.size()){
      xGeomElem e( *(sete.begin()));
      e.setUVWForXYZ(p); 
      eval(&e,&e, val);
    }
    else {
      std::cout << "warning can't find point " << p << "in the mesh" << std::endl; 
    }
    return val;
  }
 private:
  const xMesh &mesh;
  const xEval< VALTYPE>& eval;
};


class Mechanics_c {
public :
  enum exportablefield {DISPLACEMENT, SKINDISPLACEMENT, STRESS, STRAIN, ENERGY, TEMPERATURE, EXACT_STRESS, ESHELBY};
  enum solvername {SUPERLU, TAUCS, PARALLELKSP, PETSC, MUMPS};
  
  Mechanics_c ( xMesh &mesh, const xPhysicalEnv &phys, bool thermocoupling  =false);
  void addCrack(xcCrack *);
  
  ~Mechanics_c ();
  string  getFormulationName(void)  const  { return "FORMULATION_MECHANICS"; }


  void formulationMechanics ();
  void formulationThermics  ();
  /// apply Dirichlet BC on boundary of the mesh
  /*!  BC are applied on each region defined by xEnv  env = *data.PhysicalEnv->begin() to   *data.PhysicalEnv->end()
      the flag overridevaluebyzero can be set to true so that the value associated to env (env.getValue)
      is replaced by zero. This override is there to permit to apply the value only on parts of the approximation space
      and zero on other part. For example, if using order 2 Hierachics shape function, the displacement should be set to env.getValue only to ddl associated to first order shape functions and zero to the second order ones.
   */
  void applyDirichletToMechanics   (const xField& listFunctionSpaceMeca, bool overridevaluebyzero = false );
  void applyDirichletToThermics   (const xField& listFunctionSpaceThermo);
  template <typename ASSEMBLER>
  void applyNeumannToMechanics   (const xField& listFunctionSpaceMecha, 
				ASSEMBLER& assembler, xIntegrationRule& integrator);
  template <typename ASSEMBLER>
  void applyNeumannToThermics   (const xField& listFunctionSpaceThermo, 
			       ASSEMBLER& assembler, xIntegrationRule& integrator);
   template <typename ASSEMBLER>
  void applyThermicsLoadToMechanics   (const xField& listFunctionSpaceMecha, 
				      ASSEMBLER& assembler, xIntegrationRule& integrator);
  //  void compute_sifs(double rho_geo =0.2,
  //				 int  nb_layers_cylinder = 2, int nb_layers_core = 1,
  //				 int  nb_modes=6);
  void compute_sifs();
  
  
  //const xField &getDisplacementField(){return disp_l;}; 
  xField &getDisplacementField(){return disp_l;};
  xField &getTemperatureField() {return temperature;};
  const xEvalField <xIdentity <double>  > &getTemperatureEval(){return temperatureeval;}; 
  void setExactStress(const xEval<xTensor2> &a){exact_stress=&a;};
  const xEval<xTensor2> &getExactStress(){return *(exact_stress);};
  double computeEnergyErrorNorm() const;
  void  setDegree(int d){degree_classic=d;};
  void setSolver(const std::string &solverstring){
    if (solverstring == "taucs")        solver = TAUCS;
    else if (solverstring == "petsc")   solver = PETSC;
    else if (solverstring == "ksp")     solver = PARALLELKSP;
    else if (solverstring == "superlu") solver =SUPERLU;
    else if (solverstring == "mumps")   solver = MUMPS;
    else{ 
      std::cout << "solver " << solverstring << "not known" << std::endl; throw;
    }
    
  }
  void  exportMap(exportablefield f, std::string filename) const;
  void setFilters();
  const xEval<xTensor4Isotropic > &getHooke() const {return *phooke;};
  xEval<xTensor4Isotropic > &getHooke() {return *phooke;};
  double computeEnergy() const;
  double computeExternalForcesWork() const;

  //void   setHooke( xEval<xTensor4Isotropic > &_hooke) {phooke = &_hooke;};
private:
  xDoubleManager dof_manager;
  xMesh & mesh;
  xPhysicalEnv phys;
  xField disp_l;
  xField temperature;
  xEntityFilter filter_integrate_tip;
  const xEval<xTensor2 > *exact_stress;
  int degree_classic;
  xEvalField<xIdentity <double > > temperatureeval;
  const bool thermocoupling;
  std::list<xcCrack * > crackList;
  int dim;
  solvername solver;
  xEval<xTensor4Isotropic > * phooke;
  xEval<double > *palpha;
  MPI_Comm comm;
  int mpi_rank;
  int mpi_size;
};




class xEvalStressThermics :public xEval< xTensor2 >  {
 public:
  xEvalStressThermics(const xField & disp, const xField  &temp): eval_temperature(temp), eval_strain(disp) {
    phooke = new xUniformMaterialSensitivity<xTensor4Isotropic >("strain");
    pthermofac = new xUniformMaterialSensitivity<double>  ("temperature_gradient_of_volume_forces");
  }
  ~xEvalStressThermics(){
    delete phooke;
    delete pthermofac;
  }
  void operator()(xGeomElem*  appro, xGeomElem* integ, result_type& result) const{
   
    xEvalBinary< xMult<xTensor4Isotropic, xTensor2, xTensor2> > stressmeca(*phooke, eval_strain);

    xMaterial *mat = xMaterialManagerSingleton::instance().getMaterial( integ);
    double T0 = mat->getProperties()->scalar("T0");  
    xEvalConstant<double > eval_T0(-T0);
    xEvalBinary<xAdd<double, double, double >  > TmT0(eval_temperature, eval_T0);

    xTensor2 Ident(0.);
    for (int i =0; i<3 ; ++i) Ident(i,i) = -1.;
    xEvalConstant<xTensor2 > Id(Ident);
    
    xEvalBinary< xMult<double, double, double > >     stressthermoscal(*pthermofac,  TmT0);
    xEvalBinary< xMult<xTensor2, double, xTensor2 > > stressthermo(Id, stressthermoscal );
    xEvalBinary< xAdd <xTensor2, xTensor2, xTensor2 > > stress(stressthermo, stressmeca );
    
    stress(appro, integ, result);

    
  }
  const xEvalField<xIdentity<double > >  eval_temperature;
  const xEvalGradField<xSymmetrize>       eval_strain;
  xEval<xTensor4Isotropic > *phooke;
  xEval<double  > *pthermofac;
  
};

template <typename ASSEMBLER>
void Mechanics_c :: applyNeumannToThermics  (const xField& listFunctionSpace, 
					 ASSEMBLER& assembler, xIntegrationRule& integration_rule){
  
for (auto env : phys){
    if (env.Phys == "SURFACIC_HEAT_FLUX") {
      assert(env.Type == FIX);
      xEvalConstant<double>  flux(env.getValue());
      xFormLinearWithLoad<xValOperator<xIdentity<double> >, 
                           xEvalConstant<double> > lin(flux); 
      xClassRegion bc(&mesh, env.Entity, env.getDimension());
      if (bc.begin() == bc.end()) std::cout << "WARNING : region with id " <<  env.Entity << "is empty" << std::endl;
      Assemble(lin, assembler, integration_rule,
	       listFunctionSpace, bc.begin(), bc.end(), xUpperAdjacency()); 
    }
  } // End loop over the environemnt info
  return;
}

template <typename ASSEMBLER>
void Mechanics_c :: applyNeumannToMechanics   (const xField& listFunctionSpace, 
					     ASSEMBLER& assembler, xIntegrationRule& integration_rule) {
  MPI_Comm comm = mesh.getPartitionManager().getComm();
  for (auto env : phys){
      if (env.Phys == "TRACTION_X" || env.Phys == "TRACTION_Y"  || env.Phys == "TRACTION_Z" ) {
      assert(env.Type == FIX);
      xVector val;
      if (env.Phys == "TRACTION_X") val(0) =  env.getValue();
      if (env.Phys == "TRACTION_Y") val(1) =  env.getValue();
      if (env.Phys == "TRACTION_Z") val(2) =  env.getValue();
      xEvalConstant<xVector>  flux(val);
      xFormLinearWithLoad<xValOperator<xIdentity<xVector> >, xEvalConstant<xVector> > lin(flux); 
      xClassRegion bc(&mesh, env.Entity, env.getDimension());
      int sizeloc = bc.size();
      int sizeglob;
      MPI_Reduce(&sizeloc, &sizeglob, 1, MPI_INT, MPI_MAX, 0, comm );

      if (sizeglob == 0) std::cout << "WARNING : region with id " <<  env.Entity << "is empty" << std::endl;
      std::cout << "assembling TRACTION at boundary: "<< env.Entity << std::endl;
      Assemble(lin, assembler, integration_rule, listFunctionSpace, bc.begin(), bc.end(), 
	       xUpperAdjacency()); 
    }


    if (env.Phys == "STRESS") {
      if (!exact_stress){
    std::cout << "exact stress field unknow, can't apply boundary conditions"<<__FILE__ <<__LINE__ << std::endl;
	throw;
      }
      assert(env.Type == FIX);
      //double fact =  env.getValue();
      xEvalNormal eval_normal;
      
      xEvalBinary<xMult<xTensor2, xVector, xVector> > flux(*exact_stress, eval_normal);
      xFormLinearWithLoad<xValOperator<xIdentity<xVector> >, 
                           xEvalBinary<xMult<xTensor2, xVector, xVector> > > lin(flux); 
      xClassRegion bc(&mesh, env.Entity, env.getDimension());
      if (bc.begin() == bc.end()) std::cout << "WARNING : region with id " <<  env.Entity << "is empty" << std::endl;
      std::cout << "assembling exact stress at boundary: "<< env.Entity << std::endl;
      Assemble(lin, assembler, integration_rule, listFunctionSpace, bc.begin(), bc.end(), 
	       xUpperAdjacency()); 

    }
    }
  return;
}

template <class ASSEMBLER>
void Mechanics_c::applyThermicsLoadToMechanics   (const xField& listFunctionSpace, 
				     ASSEMBLER& assembler, xIntegrationRule& integrator){


  if (!thermocoupling) return;

  xEvalGradField<xIdentity<xVector > > gradT (temperature);

  xUniformMaterialSensitivity< double> thermic_volume_forces_factor("temperature_gradient_of_volume_forces" );
 xUniformMaterialSensitivity< double> thermic_surface_forces_factor("temperature_jump_of_surface_tension" );
  //xEvalDependantMaterialSensitivity<double  > thermic_volume_forces_factor ("temperature_gradient_of_volume_forces", getTemperatureEval());

  xEvalBinary<xMult<xVector, double, xVector > > thermic_forces(gradT, thermic_volume_forces_factor  ); 
  
  xFormLinearWithLoad<xValOperator<xIdentity< xVector> >,  xEvalBinary<xMult<xVector, double, xVector > >   > lin( thermic_forces); 
  xRegion all(&mesh);
  Assemble(lin, assembler, integrator, listFunctionSpace, all.begin(), all.end()); 
  
 
     
  mEntity * e= *all.begin();
  xGeomElem geo(e);
  //xMaterial *mat = xMaterialManagerSingleton::instance().getMaterial( &geo);
  xUniformMaterialParameter<double> T0_eval("T0");
  xEvalNormal eval_normal;
  
  xEvalField<xIdentity<double  > >     T (temperature);
  xEvalBinary<xAdd<double, double, double >  > TmT0(xAdd<double, double, double >(-1.), T, T0_eval);
  xEvalBinary<xMult<double, double, double >  >    fac(thermic_surface_forces_factor, TmT0 );
    

  xEvalBinary<xMult< xVector, double ,  xVector> > flux(eval_normal, fac);


  xFormLinearWithLoad< xValOperator<xIdentity<xVector> >, xEvalBinary<xMult <xVector, double, xVector> > > linflux(flux); 
 
  
  //  xExportGmshAscii pexport; 
  xIntegrationRulePartition   integrator_exp(1);  
  
  std::list<xcCrack * >::iterator it = crackList.begin();
  while (it!=crackList.end()){
    // std::cout << "apply load on crack " << std::endl;
   
    xcrack::lCrack * fissure = (*it)->getlCrack();
    
    
    const xLevelSet &lsn = *fissure->getFieldn();

    xEvalGradLevelSet<xNormalizeVector >  normal_crack0(lsn);
    xEvalBinary<xMult< xVector, double ,  xVector> > flux0(normal_crack0, fac);
    xFormLinearWithLoad< xValOperator<xIdentity<xVector> >, xEvalBinary<xMult <xVector, double, xVector> > > linflux0(flux0); 

    xEvalUnary<std::negate<xVector > > normal_crack1(normal_crack0);
    xEvalBinary<xMult< xVector, double ,  xVector> > flux1(normal_crack1, fac);
    xFormLinearWithLoad< xValOperator<xIdentity<xVector> >, xEvalBinary<xMult <xVector, double, xVector> > > linflux1(flux1); 
    
    xMesh * m = fissure->getMeshCrackSurface();
    
    int normalOriented_tag= AOMD_Util::Instance()->lookupMeshDataId("normal_oriented");
    AOMD::mMesh::iter itm = m->begin(2);
    while (itm!=m->end(2)){
      (*itm)->attachInt(normalOriented_tag, -1);
      ++itm;
    }
    
    Assemble(linflux0, assembler, integrator,listFunctionSpace, m->begin(2), m->end(2), xUpperCreator());
    itm = m->begin(2);
    while (itm!=m->end(2)){
      (*itm)->attachInt(normalOriented_tag, 1);
      ++itm;
    }
    Assemble(linflux1, assembler, integrator, listFunctionSpace, m->begin(2), m->end(2), xUpperCreator());
    ++it;
  }
  
  for (auto env : phys) {
      if (env.Phys == "TRACTION_X" || env.Phys == "TRACTION_Y"  || env.Phys == "TRACTION_Z" || env.Phys == "STRESS" ) {
    xClassRegion bc(&mesh, env.Entity, env.getDimension());
	if (bc.begin() == bc.end()) std::cout << "WARNING : region with id " <<  env.Entity << "is empty" << std::endl;
	Assemble(linflux, assembler, integrator, listFunctionSpace, bc.begin(), bc.end(), 
		 xUpperAdjacency()); 
      }
    }
  return;
  
}
#endif