compiling but segmentation fault

Submitted by JoshuaP on Wed, 12/03/2014 - 11:08

hey,

in my script I needed the variable f_total_[0],f_total_[1], f_total_[2] which is protected: in fix_mesh_surface_stress.

My script is running fine as long as Im not trying to use this variable.
To get this variable I put in the header of fix_mesh_surface_stress
friend class myclass;

In the header of myclass I have
class FixMeshSurfaceStress *mymesh;

up to here no segmentation fault does occur, but if I want to get the variable now with

Fext_[0] = mymesh->f_total_[0];
Fext_[1] = mymesh->f_total_[1];
Fext_[2] = mymesh->f_total_[2];

it can compile but when i want to use it in liggghts a segmentation fault occurs.

Thanks

richti83's picture

richti83 | Wed, 12/03/2014 - 15:27

I think you can not access f_total_ because its a vector, there is a reason why it is protected.

A better way to access the total forces and torques of one mesh is to call compute_vector(int i) of FixMeshSurfaceStress :

FixMeshSurfaceStress *fixMeshSurfaceStress = dynamic_cast(mesh);
if (fixMeshSurfaceStress == NULL) error->all(FLERR, "Internal Error - mesh is no fixMeshSurfaceStress!");
// get all outputs
for(int i=0; i<6; i++)
{
// Force and Torque from fixMeshSurfaceStress
double tmp = fixMeshSurfaceStress->compute_vector(i);
// und an die FMU uebergeben
if (i<3)
//do something with tmp (points to forceX/Y/Z)
else
//do something with tmp (points to torqueX/Y/Z)
}

I have this in mesh_mover to react on forces, in your class you need to get mesh first (in mesh_mover it is allready present)
something like this might work:

int ifix = modify->find_fix("YOUR FIX MESH ID"); //mesh id is defined in inputskript
if(ifix == -1)
error->all(FLERR,"mesh id not found");
FixMesh *fix_mesh_ = static_cast(modify->fix[ifix]);
mesh = fix_mesh_->mesh();

(untested)
Don't forget to include fix_mesh.h and fix_surface_mesh.h in your cpp file and to define

class FixMesh *fix_mesh_;
class AbstractMesh *mesh_;

in the header of your class.

What is your intend to code this ?

Best,
Christian.

I'm not an associate of DCS GmbH and not a core developer of LIGGGHTS®
ResearchGate | Contact

JoshuaP | Wed, 12/03/2014 - 15:32

Hi Christian,

Im trying to do the same as you did. I try to change mesh_mover, to react on forces.
Many thanks for your answer, I will try to implement that.
Regards

JoshuaP | Wed, 12/03/2014 - 16:33

I also tried it before with compute_vector. But I didnt used
FixMeshSurfaceStress *fixMeshSurfaceStress = dynamic_cast(mesh);
I just did
FixMeshSurfaceStress *fixMeshSurfaceStress

You said that the "mesh" is already defined in mesh_mover. How can I call it? Or do I have to take it from FixMesh or FixMoveMesh?

Regards

richti83's picture

richti83 | Wed, 12/03/2014 - 17:52

Here is my implementation to transfer forces from mesh_mover to a rigid body. To be honest I don't apply the forces to a mesh because I have the fix6dof which is (c) by DCS GmbH. But I apply the forces to a multisphere to fake mesh/mesh interaction.


void MeshMoverRigid::final_integrate(double dTAbs,double dTSetup,double dt)
{
FixMesh *mesh = fix_move_mesh_->fixMesh(); //fix_move_mesh_ is internal known by the caller class ..., get reference to mesh here ..
if (mesh == NULL) error->all(FLERR, "Internal Error mesh is NULL!");
FixMeshSurfaceStress *fixMeshSurfaceStress = dynamic_cast(mesh);
if (fixMeshSurfaceStress == NULL) error->all(FLERR, "Internal Error mesh is no fixMeshSurfaceStress!");
// get all outputs
double fx=fixMeshSurfaceStress->compute_vector(0);
double fy=fixMeshSurfaceStress->compute_vector(1);
double fz=fixMeshSurfaceStress->compute_vector(2);
double Mx=fixMeshSurfaceStress->compute_vector(3);
double My=fixMeshSurfaceStress->compute_vector(4);
double Mz=fixMeshSurfaceStress->compute_vector(5);
double F_ext[3];
double M_ext[3];
vectorConstruct3D(F_ext,fx,fy,fz);
vectorConstruct3D(M_ext,Mx,My,Mz);
ms_->add_ext_forces(F_ext,internal_body_index_); //apply forces to clump
ms_->add_ext_torque(M_ext,internal_body_index_); //apply torque to clump
}

Here is a video for all who are interested in the results: https://www.youtube.com/watch?v=6lfoAgqTIxE&feature=youtu.be

I'm not an associate of DCS GmbH and not a core developer of LIGGGHTS®
ResearchGate | Contact

JoshuaP | Thu, 12/04/2014 - 11:51

Hi Christian,

many thanks again for your help. It looks like it runs well, but I need to validate it now. Here is my implementation:

FixMesh *mesh = fix_move_mesh_->fixMesh();
FixMeshSurfaceStress* mymesh = dynamic_cast(mesh);

Fext_[0] = mymesh->compute_vector(0);
Fext_[1] = mymesh->compute_vector(1);
Fext_[2] = mymesh->compute_vector(2);

Fan_[0] = input->variable->compute_equal(myvar1_);
Fan_[1] = input->variable->compute_equal(myvar2_);
Fan_[2] = input->variable->compute_equal(myvar3_);

vel_[0] += ((Fan_[0]/mass+Fext_[0]/mass)*dt;
vel_[1] += ((Fan_[1]/mass+Fext_[1]/mass)*dt;
vel_[2] += ((Fan_[2]/mass+Fext_[2]/mass-gravity)*dt;

I add this in Constructor and initial_integrate.
I noticed that you used the final_integrate, is there a difference?

best regards
Joshua