3#include "chrono/physics/ChSystemSMC.h"
4#include "chrono/physics/ChSystem.h"
5#include "chrono/timestepper/ChTimestepper.h"
6#include "chrono/timestepper/ChTimestepperHHT.h"
7#include "chrono/core/ChFrame.h"
8#include "chrono/physics/ChLinkTSDA.h"
9#include "chrono/geometry/ChTriangleMeshConnected.h"
23 void step(
double proteus_dt,
int n_substeps);
27 void addMesh(std::shared_ptr<ChMesh> mesh);
68 shared_ptr<ChTriangleMeshConnected>
trimesh;
70 std::shared_ptr<ChBody>
body;
73 ChVector3d
hxyz(
double* x,
double t);
74 double hx(
double* x,
double t);
75 double hy(
double* x,
double t);
76 double hz(
double* x,
double t);
79 void prestep(
double* force,
double* torque);
97 std::vector<double> y, std::vector<double>
z,
98 std::vector<double> ang, std::vector<double> ang2,
99 std::vector<double> ang3,
double t_max);
135 if (tstype ==
"HHT") {
136 system->SetTimestepperType(ChTimestepper::Type::HHT);
137 auto mystepper = std::dynamic_pointer_cast<ChTimestepperHHT>(
system->GetTimestepper());
138 mystepper->SetAlpha(-0.2);
139 mystepper->SetMaxItersSuccess(10);
140 mystepper->SetAbsTolerances(1e-6);
143 mystepper->SetVerbose(verbose);
145 else if (tstype ==
"Euler") {
146 system->SetTimestepperType(ChTimestepper::Type::EULER_IMPLICIT_LINEARIZED);
148 else if (tstype ==
"Trapezoidal") {
149 system->SetTimestepperType(ChTimestepper::Type::TRAPEZOIDAL);
155 double dt2 = proteus_dt/(double)n_substeps;
156 for (
int i = 0; i < n_substeps; ++i) {
157 system->DoStepDynamics(dt2);
169 body = chrono_types::make_shared<ChBody>();
180 free_x = ChVector3d(1., 1., 1.);
181 free_r = ChVector3d(1., 1., 1.);
210 ChVector3d xNew = ChFrame<double>(
pos,
rotq).TransformPointLocalToParent(local);
218 ChVector3d xx = ChVector3d(x[0], x[1], x[2]);
219 ChVector3d local = ChFrame<double>(
pos_last,
rotq_last).TransformPointParentToLocal(xx);
220 ChVector3d xNew = ChFrame<double>(
pos,
rotq).TransformPointLocalToParent(local);
226 ChCollisionModel::SetDefaultSuggestedEnvelope(envelope);
227 ChCollisionModel::SetDefaultSuggestedMargin(margin);
233 ChVector3d local = ChFrame<double>(
pos_last,
rotq_last).TransformPointParentToLocal(ChVector3d(x[0],x[1],x[2]));
234 ChVector3d xNew = ChFrame<double>(
pos,
rotq).TransformPointLocalToParent(local);
235 return xNew.x() - x[0];
241 ChVector3d local = ChFrame<double>(
pos_last,
rotq_last).TransformPointParentToLocal(ChVector3d(x[0],x[1],x[2]));
242 ChVector3d xNew = ChFrame<double>(
pos,
rotq).TransformPointLocalToParent(local);
243 return xNew.y() - x[1];
249 ChVector3d local = ChFrame<double>(
pos_last,
rotq_last).TransformPointParentToLocal(ChVector3d(x[0],x[1],x[2]));
250 ChVector3d xNew = ChFrame<double>(
pos,
rotq).TransformPointLocalToParent(local);
251 return xNew.z() - x[2];
260 auto trimesh_coords =
trimesh->GetCoordsVertices();
261 for (
int i = 0; i < trimesh_coords.size(); i++) {
262 trimesh_pos0.push_back(ChVector3d(trimesh_coords[i].x(),
263 trimesh_coords[i].y(),
264 trimesh_coords[i].
z()));
265 trimesh_pos.push_back(ChVector3d(trimesh_coords[i].x(),
266 trimesh_coords[i].y(),
267 trimesh_coords[i].
z()));
291 double forceG[3]={0.,0.,0.};
296 forceG[1]+force[1]*
free_x.y(),
297 forceG[2]+force[2]*
free_x.z()),
305 double spring_length =
spring->GetLength();
307 spring->SetDisabled(
true);
310 spring->SetDisabled(
false);
337 std::vector<double> x,
338 std::vector<double> y,
339 std::vector<double>
z,
340 std::vector<double> ang,
341 std::vector<double> ang2,
342 std::vector<double> ang3,
344 auto fixed_body = chrono_types::make_shared<ChBody>();
345 fixed_body->SetPos(
body->GetPos());
346 fixed_body->SetFixed(
true);
348 lock_motion = chrono_types::make_shared<ChLinkLockLock>();
350 lock_motion->Initialize(
body, fixed_body, fixed_body->GetFrameCOMToAbs());
353 auto forced_motion = chrono_types::make_shared<ChFunctionInterp>();
354 for (
int i = 0; i < x.size(); i++) {
355 forced_motion->AddPoint(t[i], x[i]);
357 std::shared_ptr<ChFunction> forced_ptr = forced_motion;
361 auto forced_motion = chrono_types::make_shared<ChFunctionInterp>();
362 for (
int i = 0; i < y.size(); i++) {
363 forced_motion->AddPoint(t[i], y[i]);
365 std::shared_ptr<ChFunction> forced_ptr = forced_motion;
369 auto forced_motion = chrono_types::make_shared<ChFunctionInterp>();
370 for (
int i = 0; i <
z.size(); i++) {
371 forced_motion->AddPoint(t[i],
z[i]);
373 std::shared_ptr<ChFunction> forced_ptr = forced_motion;
376 if (ang.size() > 0) {
377 auto forced_motion = chrono_types::make_shared<ChFunctionInterp>();
378 for (
int i = 0; i < ang.size(); i++) {
379 forced_motion->AddPoint(t[i], ang[i]);
381 std::shared_ptr<ChFunction> forced_ptr = forced_motion;
384 if (ang2.size() > 0) {
385 auto forced_motion = chrono_types::make_shared<ChFunctionInterp>();
386 for (
int i = 0; i < ang2.size(); i++) {
387 forced_motion->AddPoint(t[i], ang2[i]);
389 std::shared_ptr<ChFunction> forced_ptr = forced_motion;
392 if (ang3.size() > 0) {
393 auto forced_motion = chrono_types::make_shared<ChFunctionInterp>();
394 for (
int i = 0; i < ang3.size(); i++) {
395 forced_motion->AddPoint(t[i], ang3[i]);
397 std::shared_ptr<ChFunction> forced_ptr = forced_motion;
403 auto fixed_body = chrono_types::make_shared<ChBody>();
404 fixed_body->SetPos(
body->GetPos());
405 fixed_body->SetFixed(
true);
407 auto lock = chrono_types::make_shared<ChLinkLockLock>();
408 lock->Initialize(
body, fixed_body, fixed_body->GetFrameCOMToAbs());
410 auto forced_motion = chrono_types::make_shared<ChFunctionPoly>();
411 forced_motion->SetCoefficients(std::vector<double>{coeff1});
412 std::shared_ptr<ChFunction> forced_ptr = forced_motion;
413 lock->SetMotionX(forced_ptr);
418 auto fixed_body = chrono_types::make_shared<ChBody>();
419 fixed_body->SetPos(
body->GetPos());
420 fixed_body->SetFixed(
true);
422 auto lock = chrono_types::make_shared<ChLinkLockLock>();
423 lock->Initialize(
body, fixed_body, fixed_body->GetFrameCOMToAbs());
425 auto forced_motion = chrono_types::make_shared<ChFunctionSine>();
426 forced_motion->SetAmplitude(a);
427 forced_motion->SetFrequency(
f);
428 std::shared_ptr<ChFunction> forced_ptr = forced_motion;
429 lock->SetMotionX(forced_ptr);
433 free_x = ChVector3d(free_x_in[0], free_x_in[1], free_x_in[2]);
434 free_r = ChVector3d(free_r_in[0], free_r_in[1], free_r_in[2]);
444 spring = chrono_types::make_shared<ChLinkTSDA>();
445 std::shared_ptr<ChBody> anchor_body = chrono_types::make_shared<ChBody>();
446 anchor_body->SetPos(ChVector3d(anchor[0], anchor[1], anchor[2]));
447 anchor_body->SetFixed(
true);
452 ChVector3d(fairlead[0], fairlead[1], fairlead[2]),
453 ChVector3d(0.,0.,0.));
454 spring->SetSpringCoefficient(stiffness);
455 spring->SetDampingCoefficient(damping);
461 auto mybod2 = chrono_types::make_shared<ChBody>();
462 mybod2->SetName(
"PRIS1");
463 mybod2->SetPos(ChVector3d(pris1[0], pris1[1], pris1[2]));
464 mybod2->SetMass(0.00001);
465 mybod2->SetFixed(
true);
467 auto mylink1 = chrono_types::make_shared<ChLinkLockPrismatic>();
468 ChQuaternion<double>
Q;
469 Q.SetFromAngleAxis(CH_PI/2., VECT_Y);
470 auto mycoordsys1 = ChFrame<double>(mybod2->GetPos(),
Q);
471 mylink1->Initialize(mybod2,
body, mycoordsys1);
482 auto fairlead = chrono_types::make_shared<ChBody>();
483 fairlead->SetName(
"PRIS3");
484 fairlead->SetPos(
body->GetPos());
485 fairlead->SetMass(0.00001);
487 auto mybod2 = chrono_types::make_shared<ChBody>();
488 mybod2->SetName(
"PRIS1");
489 mybod2->SetPos(ChVector3d(pris1[0], pris1[1], pris1[2]));
490 mybod2->SetMass(0.00001);
494 auto mybod3 = chrono_types::make_shared<ChBody>();
495 mybod3->SetName(
"PRIS2");
496 mybod3->SetPos(ChVector3d(pris2[0], pris2[1], pris2[2]));
497 mybod3->SetFixed(
true);
500 auto mylink1 = chrono_types::make_shared<ChLinkLockPrismatic>();
502 ChQuaternion<double> QX;
503 QX.SetFromAngleAxis(CH_PI/2., VECT_Y);
504 auto mycoordsys1 = ChFrame<double>(mybod2->GetPos(), QX);
505 mylink1->Initialize(fairlead, mybod2, mycoordsys1);
509 auto mylink2 = chrono_types::make_shared<ChLinkLockPrismatic>();
511 ChQuaternion<double> QY;
512 QY.SetFromAngleAxis(CH_PI/2., VECT_X);
513 auto mycoordsys2 = ChFrame<double>(mybod3->GetPos(), QY);
514 mylink2->Initialize(mybod2, mybod3,mycoordsys2);
516 auto mylink3 = chrono_types::make_shared<ChLinkLockSpherical>();
520 mylink3->Initialize(fairlead,
body,
false, fairlead->GetFrameCOMToAbs(),
body->GetFrameCOMToAbs());
524 spring = chrono_types::make_shared<ChLinkTSDA>();
525 spring->Initialize(fairlead,
528 ChVector3d(0.,0.,0.),
529 ChVector3d(0.,0.,0.));
530 spring->SetSpringCoefficient(stiffness);
531 spring->SetDampingCoefficient(damping);
532 spring->SetName(
"SPRING1");
542 auto xxs =
trimesh->GetCoordsVertices();
543 auto nns =
trimesh->GetCoordsNormals();
545 double min_dist=1e10;
547 for (
int i = 0; i < xxs.size(); i++) {
548 dist_vec =
pos-xxs[i];
549 dist = dist_vec.Length();
550 if (dist < min_dist) {
553 if (dist_vec.Dot(nns[i]) > 0) {
557 min_dist = -min_dist;
560 dist_n[0] = min_dist;
563 dist_n[1] = dist_vec[0];
564 dist_n[1] = dist_vec[1];
565 dist_n[2] = dist_vec[2];
571 auto xxs =
trimesh->GetCoordsVertices();
572 auto nns =
trimesh->GetCoordsNormals();
573 double min_dist = 1e10;
574 ChVector3d p(x[0], x[1], x[2]);
575 ChVector3d d_vector(0.0);
578 int node_closest = -1;
579 for (
int i = 0; i < xxs.size(); i++) {
580 double dist = (p - xxs[i]).Length();
581 if (dist < min_dist) {
586 if (node_closest != -1) {
588 vel[0] = ddlast.x()/dt;
589 vel[1] = ddlast.y()/dt;
590 vel[2] = ddlast.z()/dt;
610 std::shared_ptr<ChBody> body2,
611 std::shared_ptr<ChSystem> system,
612 ChFrame<double> frame,
618 double limit_Rz=0.) {
619 auto mylink = chrono_types::make_shared<ChLinkLock>();
620 system->AddLink(mylink);
621 auto chlimit_X = mylink->LimitX();
622 chlimit_X.SetActive(
true);
623 chlimit_X.SetMax(limit_X);
624 auto chlimit_Y = mylink->LimitY();
625 chlimit_Y.SetActive(
true);
626 chlimit_Y.SetMax(limit_Y);
627 auto chlimit_Z = mylink->LimitZ();
628 chlimit_Z.SetActive(
true);
629 chlimit_Z.SetMax(limit_Z);
630 auto chlimit_Rx = mylink->LimitRx();
631 chlimit_Rx.SetMax(limit_Rx);
632 chlimit_Rx.SetActive(
true);
633 auto chlimit_Ry = mylink->LimitRy();
634 chlimit_Ry.SetActive(
true);
635 chlimit_Ry.SetMax(limit_Ry);
636 auto chlimit_Rz = mylink->LimitRz();
637 chlimit_Rz.SetActive(
true);
638 chlimit_Rz.SetMax(limit_Rz);
639 mylink->Initialize(body1, body2, frame);
648 std::shared_ptr<ChPhysicsItem> sp(item,
no_op_delete());
651static std::map<ChPhysicsItem*, std::shared_ptr<ChPhysicsItem>> spans;
654 std::shared_ptr<ChPhysicsItem> sp = spans.at(item);
cppRigidBody * newRigidBody(cppSystem *system)
std::shared_ptr< ChPhysicsItem > getPhysicsItemSharedPtr(ChPhysicsItem *item)
std::shared_ptr< ChPhysicsItem > getPhysicsItemSharedPtr2(ChPhysicsItem *item)
void ChLinkLockBodies(std::shared_ptr< ChBody > body1, std::shared_ptr< ChBody > body2, std::shared_ptr< ChSystem > system, ChFrame< double > frame, double limit_X=0., double limit_Y=0., double limit_Z=0., double limit_Rx=0., double limit_Ry=0., double limit_Rz=0.)
ChQuaternion< double > rotq0
ChVector3d hxyz(double *x, double t)
ChMatrix33< double > rotm_last
double hx(double *x, double t)
std::shared_ptr< ChBody > body
ChQuaternion< double > rotq
void prestep(double *force, double *torque)
unsigned int accumulator_torque_idx
ChQuaternion rotq0_trimesh
void addSpring(double stiffness, double damping, double *fairlead, double *anchor, double rest_length)
std::shared_ptr< ChLinkLockLock > lock_motion
void setConstraints(double *free_x, double *free_y)
void setName(std::string name)
void getTriangleMeshVel(double *x, double dt, double *vel)
void setPrescribedMotionCustom(std::vector< double > t, std::vector< double > x, std::vector< double > y, std::vector< double > z, std::vector< double > ang, std::vector< double > ang2, std::vector< double > ang3, double t_max)
void setPrescribedMotionPoly(double coeff1)
void addPrismaticLinksWithSpring(double *pris1, double *pris2, double stiffness, double damping, double rest_length)
void updateTriangleMeshVisualisationPos()
ChQuaternion< double > rotq_last
ChMatrix33< double > rotm
shared_ptr< ChTriangleMeshConnected > trimesh
std::vector< ChVector3d > trimesh_pos_last
unsigned int accumulator_force_idx
double mooring_restlength
void addPrismaticLinkX(double *pris1)
double hy(double *x, double t)
cppRigidBody(cppSystem *system)
std::vector< ChVector3d > trimesh_pos
void setPrescribedMotionSine(double a, double f)
std::shared_ptr< ChLinkTSDA > spring
double hz(double *x, double t)
void getTriangleMeshSDF(ChVector3d pos_node, double *dist_n)
std::vector< ChVector3d > trimesh_pos0
std::shared_ptr< ChSystem > system
void setTimestepperType(std::string tstype, bool verbose)
std::shared_ptr< ChSystemSMC > systemSMC
void addMesh(std::shared_ptr< ChMesh > mesh)
void step(double proteus_dt, int n_substeps)
void setCollisionEnvelopeMargin(double envelope, double margin)
void setDirectory(std::string dir)