Algorithm.cc 19.5 KB
Newer Older
1 2 3 4 5
/* 
    This file is a part of eXlibris C++ Library
    under the GNU General Public License:
    See the LICENSE.md files for terms and 
    conditions.
6
*/
7 8


9 10 11 12 13 14 15 16 17
// DamageBandDyn
#include "Algorithm.h"
#include "Contact.h"
#include "Eval.h"
#include "Export.h"
// SolverBase
#include "xCSRVector.h"
#include "xCSRMatrix.h"
// Xfem
18 19 20
#include "xAlgorithm.h"
#include "xData.h"

21 22 23 24
using Trellis_Util::mPoint;
using AOMD::mEntity;
using AOMD::mVertex;
using namespace xfem;
25

26
void TreatmentOfNatEnv(const xField& displacement_field, xAssembler& assembler, const xIntegrationRule& integration_rule, const xData& data, const xBoundary& groups, double time, double time_step, const xEval<xVector>& eval_velocity, PostProcessing& post_processing, const xEntityFilter filter)
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
  if (data.mesh->dim()>1)
  {
    for (xPhysicalEnv::const_iterator it = data.PhysicalEnv->begin(); it != data.PhysicalEnv->end(); ++it)
    {
      const xEnv& env = *it;

      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> eval_val(val);
        EvalSpaceFunctionCrossTimeFunction<xVector> eval_external_load(eval_val, env.getEvolution());
        eval_external_load.setTime(time);
        xFormLinearWithLoad<xValOperator<xIdentity<xVector> >, xEval<xVector> > linear_form(eval_external_load);
        xClassRegion bc(data.mesh, env.Entity, env.getDimension());
        xFilteredRegion<xClassIter, xEntityFilter> fr_bc(bc.begin(), bc.end(), filter);
        Assemble(linear_form, assembler, integration_rule, displacement_field, fr_bc.begin(), fr_bc.end(), xUpperAdjacency());
        xEvalBinary<xVectorScalarProd> eval_external_load_power(eval_external_load, eval_velocity);
        double external_power_delta = 0.;
        post_processing.exportPowerOnTime("external_load_power", 0, time, time_step, external_power_delta, eval_external_load_power, integration_rule, fr_bc.begin(), fr_bc.end(), xUpperAdjacency());
      }
      if (env.Phys == "PRESSURE")
      {
        assert(env.Type == FIX);
        double val = env.getValue();
        xEvalNormalbis eval_normal;
        xEvalConstant<double> eval_val(val);
        xEvalBinary<xMult<xVector, double, xVector> > eval_pressure(eval_normal, eval_val);
        EvalSpaceFunctionCrossTimeFunction<xVector> eval_external_load(eval_pressure, env.getEvolution());
        eval_external_load.setTime(time);
        xFormLinearWithLoad<xValOperator<xIdentity<xVector> >, xEval<xVector> > linear_form(eval_external_load);
        xClassRegion bc(data.mesh, env.Entity, env.getDimension());
        xFilteredRegion<xClassIter, xEntityFilter> fr_bc(bc.begin(), bc.end(), filter);
        Assemble(linear_form, assembler, integration_rule, displacement_field, fr_bc.begin(), fr_bc.end(), xUpperAdjacency());
        xEvalBinary<xVectorScalarProd> eval_external_load_power(eval_external_load, eval_velocity);
        double external_power_delta = 0.;
        post_processing.exportPowerOnTime("external_load_power", 0, time, time_step, external_power_delta, eval_external_load_power, integration_rule, fr_bc.begin(), fr_bc.end(), xUpperAdjacency());
      }
    }
  }
  // else
  // {
  //   xEvalField<xIdentity<double> > eval_displacement(displacement_field);
  //   for (xPhysicalEnv::const_iterator it = data.PhysicalEnv->begin(); it != data.PhysicalEnv->end(); ++it)
  //   {
  //     const xEnv& env = *it;
  //     if (env.Phys == "TRACTION_X")
  //     {
  //       assert(env.Type == FIX);
  //       double val = env.getValue();
  //       xEvalConstant<double> eval_val(val);
  //       EvalSpaceFunctionCrossTimeFunction<double> eval_external_load(eval_val, env.getEvolution());
  //       eval_external_load.setTime(time);
  //       xFormLinearWithLoad<xValOperator<xIdentity<double> >, xEval<double> > linear_form(eval_external_load);
  //       xClassRegion bc(data.mesh, env.Entity, env.getDimension());
  //       xFilteredRegion<xClassIter, xEntityFilter> fr_bc(bc.begin(), bc.end(), filter);
  //       Assemble(linear_form, assembler, integration_rule, displacement_field, fr_bc.begin(), fr_bc.end(), xUpperAdjacency());
  //       xScale<double> time_step_scale(time_step);
  //       xEvalBinary<xVectorScalarProd> eval_external_load_power(eval_external_load, eval_velocity);
  //       post_processing.exportPowerOnTime("external_load_power", 0, time, eval_external_load_power, integration_rule, bc.begin(), bc.end(), xUpperAdjacency());
  //     }
  //   }
  // }
}
95

96 97 98
void TreatmentOfEssEnv(const xField& displacement_field, const xField& velocity_field, const xField& acceleration_field, const xData& data, const double& time)
{
  const int dim = data.mesh->dim();
99

100 101 102 103
  for (xPhysicalEnv::const_iterator it = data.PhysicalEnv->begin(); it != data.PhysicalEnv->end(); ++it)
  {
    const xEnv& env = *it;
    int geom = env.Geom;
104

105 106 107 108 109
    if (geom==(dim-1))
    {
      std::string phys = env.Phys;
      int type = env.Type;
      int entity = env.Entity;
110

111 112 113
      if (phys == "DISPLACEMENT_X" || phys == "DISPLACEMENT_Y" || phys == "DISPLACEMENT_Z")
      {
        assert(type==FIX);
114

115 116 117
        double val = env.getValue();
        // const xPieceWiseLinearDoubleToDouble& time_function = env.getEvolution();
        // val *= time_function(time);
118

119
        xClassRegion bc(data.mesh, entity, env.getDimension());
120

121 122 123
        // std::string kin("KINEMATIC_");
        std::string kin("DISPLACEMENT_");
        kin+=phys[phys.size()-1];
124

125 126 127 128 129 130
        DirichletBoundaryCondition(displacement_field, kin, bc.begin(), bc.end(), val);
        //DynamixDirichletBoundaryCondition(displacement_field, kin, bc.begin(), bc.end(), mass, val);
      }
      if (phys == "VELOCITY_X" || phys == "VELOCITY_Y" || phys == "VELOCITY_Z")
      {
        assert(type==FIX);
131

132 133 134
        double val = env.getValue();
        // const xPieceWiseLinearDoubleToDouble& time_function = env.getEvolution();
        // val *= time_function(time);
135

136
        xClassRegion bc(data.mesh, entity, env.getDimension());
137

138 139
        std::string kin("DISPLACEMENT_");
        kin+=phys[phys.size()-1];
140

141 142 143 144 145
        DirichletBoundaryCondition(velocity_field, kin, bc.begin(), bc.end(), val);
      }
      if (phys == "ACCELERATION_X" || phys == "ACCELERATION_Y" || phys == "ACCELERATION_Z" )
      {
        assert(type==FIX);
146

147 148 149
        double val = env.getValue();
        // const xPieceWiseLinearDoubleToDouble& time_function = env.getEvolution();
        // val *= time_function(time);
150

151
        xClassRegion bc(data.mesh, entity, env.getDimension());
152

153 154
        std::string kin("DISPLACEMENT_");
        kin+=phys[phys.size()-1];
155

156 157 158 159
        DirichletBoundaryCondition(acceleration_field, kin, bc.begin(), bc.end(), val);
      }
    }
  }
160 161
}

162
void TreatmentOfInitCond(const xData& data_, xField& displacement_field_, xField& velocity_field_, const xIntegrationRule& integration_rule_, std::string sub_, xEntityFilter filter_)
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
  std::map<int, std::vector<const xEnv*> > env_map;
  for (xPhysicalEnv::const_iterator it=data_.PhysicalEnv->begin(); it!=data_.PhysicalEnv->end(); ++it)
  {
    const xEnv& env = *it;
    int geom = env.Geom;

    if (geom==IC_LINE || geom==IC_SURFACE || geom==IC_VOLUME)
    {
      int e = env.Entity;
      std::map<int, std::vector<const xEnv*> >::iterator found = env_map.find(e);
      if (found==env_map.end())
      {
        std::vector<const xEnv*> vec;
        vec.push_back(&env);
        env_map.insert(std::pair<int, std::vector<const xEnv*> >(e, vec));
      }
      else
      {
        env_map[e].push_back(&env);
      }
    }
  }

  xAssemblerBasic<> assembler_disp;
  xAssemblerBasic<> assembler_velo;
  xDoubleManager* disp_double_manager = displacement_field_.getDoubleManager();
  xDoubleManager* velo_double_manager = velocity_field_.getDoubleManager();
  lalg::xCSRVector disp_b(disp_double_manager->size(sub_));
  lalg::xCSRVector disp_sol(disp_double_manager->size(sub_));
  lalg::xCSRMatrix disp_A(disp_double_manager->size(sub_));
  lalg::xCSRVector velo_b(velo_double_manager->size(sub_));
  lalg::xCSRVector velo_sol(velo_double_manager->size(sub_));
  lalg::xCSRMatrix velo_A(velo_double_manager->size(sub_));
  assembler_disp.setTarget(disp_A, disp_b);
  assembler_velo.setTarget(velo_A, velo_b);

  if (data_.mesh->dim()>1)
  {
    xFormBilinearWithoutLaw<xValOperator<xIdentity<xVector> >, xValOperator<xIdentity<xVector> > > l2;

    std::map<int, std::vector<const xEnv*> >::iterator it = env_map.begin();
    std::map<int, std::vector<const xEnv*> >::iterator end = env_map.end();
    for (; it!=end; ++it)
    {
      std::vector<const xEnv*> vec = it->second;
      xClassRegion bc_unfiltered(data_.mesh, it->first, vec[0]->getDimension());
      xFilteredRegion<xClassIter, xEntityFilter> bc(bc_unfiltered.begin(), bc_unfiltered.end(), filter_);

      xVector initial_displacement(0.,0.,0.);
      xVector initial_velocity(0.,0.,0.);

      std::vector<const xEnv*>::iterator ite = vec.begin();
      std::vector<const xEnv*>::iterator ende = vec.end();
      for (; ite!=ende; ++ite)
      {
        const xEnv* env = *ite;

        std::string phys = env->Phys;
        double value = env->Val_fix;
        std::cout << "Initial condition on: " << env->Entity << " for phys: " << phys << " with value: " << value << std::endl;
        if (phys == "DISPLACEMENT_X") initial_displacement(0) = value;
        if (phys == "DISPLACEMENT_Y") initial_displacement(1) = value;
        if (phys == "DISPLACEMENT_Z") initial_displacement(2) = value;
        if (phys == "VELOCITY_X") initial_velocity(0) = value;
        if (phys == "VELOCITY_Y") initial_velocity(1) = value;
        if (phys == "VELOCITY_Z") initial_velocity(2) = value;
      }

      if (vec[0]->getDimension()==data_.mesh->dim())
      {
        xEvalConstant<xVector> eval_initial_displacement(initial_displacement);
        xFormLinearWithLoad<xValOperator<xIdentity<xVector> >, xEval<xVector> > l2_rhs_disp(eval_initial_displacement);
        Assemble(l2, assembler_disp, integration_rule_, displacement_field_, displacement_field_, bc.begin(), bc.end());
        Assemble(l2_rhs_disp, assembler_disp, integration_rule_, displacement_field_, bc.begin(), bc.end());

        xEvalConstant<xVector> eval_initial_velocity(initial_velocity);
        xFormLinearWithLoad<xValOperator<xIdentity<xVector> >, xEval<xVector> > l2_rhs_velo(eval_initial_velocity);
        Assemble(l2, assembler_velo, integration_rule_, velocity_field_, velocity_field_, bc.begin(), bc.end());
        Assemble(l2_rhs_velo, assembler_velo, integration_rule_, velocity_field_, bc.begin(), bc.end());
      }
      else
      {
        xIntegrationRuleBasic integration_rule(2);

        xEvalConstant<xVector> eval_initial_displacement(initial_displacement);
        xFormLinearWithLoad<xValOperator<xIdentity<xVector> >, xEval<xVector> > l2_rhs_disp(eval_initial_displacement);
        Assemble(l2, assembler_disp, integration_rule, displacement_field_, displacement_field_, bc.begin(), bc.end());
        Assemble(l2_rhs_disp, assembler_disp, integration_rule, displacement_field_, bc.begin(), bc.end());

        xEvalConstant<xVector> eval_initial_velocity(initial_velocity);
        xFormLinearWithLoad<xValOperator<xIdentity<xVector> >, xEval<xVector> > l2_rhs_velo(eval_initial_velocity);
        Assemble(l2, assembler_velo, integration_rule, velocity_field_, velocity_field_, bc.begin(), bc.end());
        Assemble(l2_rhs_velo, assembler_velo, integration_rule, velocity_field_, bc.begin(), bc.end());
      }
    }
  }
  else
  {
    xFormBilinearWithoutLaw<xValOperator<xIdentity<double> >, xValOperator<xIdentity<double> > > l2;

    std::map<int, std::vector<const xEnv*> >::iterator it = env_map.begin();
    std::map<int, std::vector<const xEnv*> >::iterator end = env_map.end();
    for (; it!=end; ++it)
    {
      std::vector<const xEnv*> vec = it->second;
      xClassRegion bc_unfiltered(data_.mesh, it->first, vec[0]->getDimension());
      xFilteredRegion<xClassIter, xEntityFilter> bc(bc_unfiltered.begin(), bc_unfiltered.end(), filter_);

      double initial_displacement = 0.;
      double initial_velocity = 0.;

      std::vector<const xEnv*>::iterator ite = vec.begin();
      std::vector<const xEnv*>::iterator ende = vec.end();
      for (; ite!=ende; ++ite)
      {
        const xEnv* env = *ite;
        std::string phys = env->Phys;
        double value = env->Val_fix;
        if (phys == "DISPLACEMENT_X") initial_displacement = value;
        if (phys == "VELOCITY_X") initial_velocity = value;
      }

      xEvalConstant<double> eval_initial_displacement(initial_displacement);
      xFormLinearWithLoad<xValOperator<xIdentity<double> >, xEval<double> > l2_rhs_disp(eval_initial_displacement);
      Assemble(l2, assembler_disp, integration_rule_, displacement_field_, displacement_field_, bc.begin(), bc.end());
      Assemble(l2_rhs_disp, assembler_disp, integration_rule_, displacement_field_, bc.begin(), bc.end());

      xEvalConstant<double> eval_initial_velocity(initial_velocity);
      xFormLinearWithLoad<xValOperator<xIdentity<double> >, xEval<double> > l2_rhs_velo(eval_initial_velocity);
      Assemble(l2, assembler_velo, integration_rule_, velocity_field_, velocity_field_, bc.begin(), bc.end());
      Assemble(l2_rhs_velo, assembler_velo, integration_rule_, velocity_field_, bc.begin(), bc.end());
    }
  }

  lalg::xLinearSystemSolverSuperLU<> solver;

  solver.connectMatrix(disp_A);
  solver.solve(disp_b, disp_sol);
  Visit(xWriteSolutionVisitor<lalg::xCSRVector>(disp_sol.begin()), disp_double_manager->begin(sub_), disp_double_manager->end(sub_));

  solver.connectMatrix(velo_A);
  solver.solve(velo_b, velo_sol);
  Visit(xWriteSolutionVisitor<lalg::xCSRVector>(velo_sol.begin()), velo_double_manager->begin(sub_), velo_double_manager->end(sub_));
}
308

309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
void TreatmentOfRigidMotion(const xData& data_, xField& displacement_field_, xField& velocity_field_, xField& acceleration_field_, xEntityFilter filter_)
{
  xValueCreator<xValueDouble> value_creator;
  for (xPhysicalEnv::const_iterator it=data_.PhysicalEnv->begin(); it!=data_.PhysicalEnv->end(); ++it)
  {
    const xEnv& env = *it;
    int geom = env.Geom;
    if (geom==RIGID_LINE || geom==RIGID_SURFACE)
    {
      xClassRegion bc_unfiltered(data_.mesh, env.Entity, env.getDimension());
      xFilteredRegion<xClassIter, xEntityFilter> bc(bc_unfiltered.begin(), bc_unfiltered.end(), filter_);
      std::cout << "Rigid motion on: " << env.Entity << " of phys: " << env.Phys << std::endl;
      DeclareInterpolationRigid(displacement_field_, value_creator, env.Phys, bc.begin(), bc.end());
      DeclareInterpolationRigid(velocity_field_, value_creator, env.Phys, bc.begin(), bc.end());
      DeclareInterpolationRigid(acceleration_field_, value_creator, env.Phys, bc.begin(), bc.end());
    }
  }
}
327

328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350
void TreatmentOfAdditionalMass(const xData& data_, xField& displacement_field_, xField& acceleration_field_, xAssembler& assembler_mass_, xEntityFilter filter_)
{
  for (xPhysicalEnv::const_iterator it=data_.PhysicalEnv->begin(); it!=data_.PhysicalEnv->end(); ++it)
  {
    const xEnv& env = *it;
    int geom = env.Geom;
    if (geom==RIGID_LINE || geom==RIGID_SURFACE)
    {
      xClassRegion bc_unfiltered(data_.mesh, env.Entity, env.getDimension());
      xFilteredRegion<xClassIter, xEntityFilter> bc(bc_unfiltered.begin(), bc_unfiltered.end(), filter_);
      double density = env.Val_fix;
      if (density > 0.)
      {
        std::cout << "Additional mass on:" << env.Entity << " with value: " << density << std::endl;
        xEvalConstant<double> eval_density(density);
        typedef xValOperator<xIdentity<xVector> > ValOp_t;
        xFormBilinearWithLaw<ValOp_t, const xEval<double>, ValOp_t> mass_form(eval_density);
        xIntegrationRuleBasic integration_rule(2);
        Assemble(mass_form, assembler_mass_, integration_rule, displacement_field_, acceleration_field_, bc.begin(), bc.end());
      }
    }
  }
}
351

352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
// void TreatmentOfContact(const xField& displacement_field, const xField& velocity_field, const xData& data, const xBoundary& groups, double time_step, const xEntityFilter filter)
// {
//   // HARDCODED
//   // Plane plane1(mPoint(0.,0.,0.),xVector(0.,1.,0.));
//   // Plane plane2(mPoint(0.005,0.,0.),xVector(1.,0.,0.));
//   // const double radius=0.002;
//   // Sphere sphere(mPoint(0.05-radius,-radius,0.),radius);
//   // std::vector<PointToOrientedSurface> vec;
//   // vec.push_back(plane1);
//   // vec.push_back(plane2);
//   // vec.push_back(sphere);
//   // InterVector point_to_oriented_surface(vec);
//   FreePunch point_to_oriented_surface(0.0002);
//   // HARDCODED

//   for (xPhysicalEnv::const_iterator it = data.PhysicalEnv->begin(); it != data.PhysicalEnv->end(); ++it)
//   {
//     const xEnv& env = *it;
//     int geom = env.Geom;
//     if (geom==CONTACT_LINE)
//     {
//       assert(env.Type==FIX);
//       double penalty_coefficient = env.getValue();
//       EvalContact eval_contact(point_to_oriented_surface, penalty_coefficient);
//       // EvalContact eval_contact(plane1, penalty_coefficient);
//       xFormLinearWithLoad<xValOperator<xIdentity<xVector> >, xEval<xVector> > lin(eval_contact);
//       xClassRegion bc(data.mesh, env.Entity, env.getDimension());
//       xFilteredRegion<xClassIter, xEntityFilter> fr_bc(bc.begin(), bc.end(), filter);
//       xFilteredRegion<xClassIter, xEntityFilter>::FilterIter it=fr_bc.begin();
//       xFilteredRegion<xClassIter, xEntityFilter>::FilterIter end=fr_bc.end();
//       xDoubleManager* disp_double_manager = displacement_field.getDoubleManager();
//       xDoubleManager* velo_double_manager = velocity_field.getDoubleManager();
//       std::hash_map<mVertex*,xVector> displacement_map;
//       for(; it != end; ++it)
//       {
//         mEntity* e = *it;
//         for (int i=0; i<1; ++i)
//         {
//           mVertex* n = static_cast<mVertex*>(e->get(0,i));
//           xFiniteElement FEM;
//           FEM.setKeys(n, displacement_field.begin(), displacement_field.end());
//           xGeomElem geom_elem(e);
//           geom_elem.setUVWForXYZ(n->point());
//           xVector vec;
//           eval_contact(&geom_elem, &geom_elem, vec);
//           std::vector<xValue<double>*> disp_vals;
//           std::vector<xValue<double>*> velo_vals;
//           disp_double_manager->getValPtr(FEM.beginKey(), FEM.endKey(), disp_vals);
//           velo_double_manager->getValPtr(FEM.beginKey(), FEM.endKey(), velo_vals);
//           std::vector<xValue<double>*>::const_iterator it_disp=disp_vals.begin();
//           std::vector<xValue<double>*>::const_iterator it_velo=velo_vals.begin();
//           int j=0;
//           for (; it_disp!=disp_vals.end(); ++it_disp, ++it_velo, ++j)
//           {
//             if (vec(j)<0.)
//             {
//               // (*it_disp)->setVal((*it_disp)->getVal()-vec(j));
//               disp_map.insert(std::make_pair(*it_disp,(*it_disp)->getVal()-vec(j)));
//               velo_map.insert(std::make_pair(*it_velo,0.));
//               // (*it_velo)->setVal((*it_velo)->getVal()-vec(j)/time_step);
//               // (*it_velo)->setVal(0.);
//             }
//           }
//           // displacement_map[n]=vec;
//         }
//       }
//       // for (std::hash_map<mVertex*,xVector>::iterator it=displacement_map.begin(); it!=displacement_map.end(); ++it)
//       // {
//       //   mVertex* n = it->first;
//       //   xVector disp = it->second;
//       //   mPoint pt = n->point();
//       //   mPoint pt_trans(disp(0), disp(1), disp(2));
//       //   n->move(pt-pt_trans);
//       // }
//       // xIntegrationRuleNodal integration_rule;
//       // Assemble(lin, assembler, integration_rule, displacement_field, fr_bc.begin(), fr_bc.end(), xUpperAdjacency());
//     }
//   }
// }
431 432