FreeCAD as pre-post processor for MBDyn

About the development of the FEM module/workbench.

Moderator: bernd

josegegas
Posts: 241
Joined: Sat Feb 11, 2017 12:54 am
Location: New Zealand

Re: FreeCAD as pre-post procesor for MBDyn

Post by josegegas »

mfasano wrote: Mon Sep 09, 2019 2:43 pm One issue with using FreeCAD for multibody dynamics is there is no routine to calculate moment of inertia for arbitrary shapes(part design); that would need to be written.
Yes, I found this to be a problem. By now I am using my own code to calculate inertia moments, only for basic shapes (spheres, cones, cylinders and boxes). I found FreeCAD gives me the inertia matrix of any body, by creating a "simple", non -parametric copy of the body and then doing:

.Shape.Solids[0].MatrixOfInertia

This gives me three inertia moments (Ixx, Iyy and Izz) for the MBDyn simulation, but I don't really understand how these values are obtained. I mean, to calculate the inertia moments one needs the body's mass, but FreeCAD ignores the mass. What are the units of the values FreeCAD yields? How can I calculate these values including the object's mass? I am lost with this, and that is why I use my own code to calculate inertia moments. This works only for simple objects. If someone can guide me how to interpret the values FreeCAD gives me would be awesome! ;)
josegegas
Posts: 241
Joined: Sat Feb 11, 2017 12:54 am
Location: New Zealand

Re: FreeCAD as pre-post procesor for MBDyn

Post by josegegas »

saso wrote: Mon Sep 09, 2019 12:32 pm MBDyn license is GPL so it would have to be installed separately, ok, but it seems like default installation packages are only for linux?
I've never used MBDyn on Windows. Not sure if there are installation packages... I don't care much about this :P MBDyn is such a nice software and if we could integrate it with FreeCAD, this would add a lot of value for engineering and design. And if only works for Linux, will be an excuse to push people into FOSS :lol:
josegegas
Posts: 241
Joined: Sat Feb 11, 2017 12:54 am
Location: New Zealand

Re: FreeCAD as pre-post procesor for MBDyn

Post by josegegas »

vocx wrote: Mon Sep 09, 2019 12:25 am
josegegas wrote: Sun Sep 08, 2019 5:21 am I've been using MBDyn to do simulations of engines and other mechanisms. It is a free and open source multi body dynamics software. It is very powerful but it lacks pre and post processing graphic interface. I've been using FreeCAD as pre and post processing tool for MBDyn and all works very well. Both programs can be integrated nicely through python scripts. I think they could also be integrated in a new workbench.
I don't know MBDyn, but it sounds like it would be in the same ballpark as Modelica. Saso has a keen interest in something like this, but so far no real development has taken place; see Modelica.

It'd be awesome if you could develop something that is general, that is, able to use different solvers, one of which could be MBDyn and another Modelica. That's basically how the FEM Workbench works.
saso wrote: Sat Dec 01, 2018 1:17 pm ping
Not sure if something general will be as easy as just for MBDyn. What I like about the idea of MBDyn is the ease with which it can be integrated with FreeCAD. MBDyn takes an "input file", which is a text file with all the definitions (simulation parameters, bodies, gravity, joints, etc), and returns two text files, one has the bodies positions, orientations, velocity and acceleration. Another has the joint's reaction forces. All we have to do to integrate it with FreeCAD is to write a Python program to automatically generate the MBDyn input file, execute MBDyn and then read the output files to produce the animations and the plots....
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: FreeCAD as pre-post procesor for MBDyn

Post by vocx »

josegegas wrote: Tue Sep 10, 2019 3:14 am .Shape.Solids[0].MatrixOfInertia

This gives me three inertia moments (Ixx, Iyy and Izz) for the MBDyn simulation, but I don't really understand how these values are obtained. I mean, to calculate the inertia moments one needs the body's mass, but FreeCAD ignores the mass. What are the units of the values FreeCAD yields? How can I calculate these values including the object's mass? I am lost with this, and that is why I use my own code to calculate inertia moments. This works only for simple objects. If someone can guide me how to interpret the values FreeCAD gives me would be awesome! ;)
First of all, you are replying to a single comment from mfasano, but you wrote four replies? Why?! You could have answered in a single reply; then you don't fragment the conversation into many posts, and it's easier to follow.

Now, with that said, let's go to the code.

FreeCAD uses the Part Module (or Workbench) to expose the underlying "topological shapes" which are provided by the OpenCASCADE Technology (OCCT) libraries; in this case you want to get the properties of a Solid topological shape.

https://github.com/FreeCAD/FreeCAD/blob ... p.cpp#L150

The code of src/Mod/Part/App/TopoShapeSolidPyImp.cpp implements the Solid.MatrixOfInertia() method.

Code: Select all

Py::Object TopoShapeSolidPy::getMatrixOfInertia(void) const
{
    GProp_GProps props;
    BRepGProp::VolumeProperties(getTopoShapePtr()->getShape(), props);
    gp_Mat m = props.MatrixOfInertia();
    Base::Matrix4D mat;
    for (int i=0; i<3; i++) {
        for (int j=0; j<3; j++) {
            mat[i][j] = m(i+1,j+1);
        }
    }
    return Py::Matrix(mat);
}
As you can see, the matrix of inertia is itself calculated from an instance props that calls its own MatrixOfInertia() method. This props is an instance of a class GProp_GProps. We see at the top of the file the necessary import that provides this class, # include <GProp_GProps.hxx>

It seems this header file is part of the OCCT sources. We can find its documentation online. https://www.opencascade.com/doc/occt-7. ... props.html

Code: Select all

gp_Mat GProp_GProps::MatrixOfInertia() const

returns the matrix of inertia. It is a symmetrical matrix. The coefficients of the matrix are the quadratic moments of inertia.

| Ixx Ixy Ixz | matrix = | Ixy Iyy Iyz | | Ixz Iyz Izz |

The moments of inertia are denoted by Ixx, Iyy, Izz. The products of inertia are denoted by Ixy, Ixz, Iyz. The matrix of inertia is returned in the central coordinate system (G, Gx, Gy, Gz) where G is the centre of mass of the system and Gx, Gy, Gz the directions parallel to the X(1,0,0) Y(0,1,0) Z(0,0,1) directions of the absolute cartesian coordinate system. It is possible to compute the matrix of inertia at another location point using the Huyghens theorem (you can use the method of package GProp : HOperator).
The same TopoShapeSolidPyImp.cpp file defines other methods from the GProp class. You'd need to read more about these functions, and maybe there is the key to set the mass or density of your solid shape.

In particular the following seems relevant

Code: Select all

Standard_Real GProp_GProps::Mass() const

Returns the mass of the current system. If no density is attached to the components of the current system the returned value corresponds to :

* the total length of the edges of the current system if this framework retains only linear properties, as is the case for example, when using only the LinearProperties function to combine properties of lines from shapes, or
* the total area of the faces of the current system if this framework retains only surface properties, as is the case for example, when using only the SurfaceProperties function to combine properties of surfaces from shapes, or
* the total volume of the solids of the current system if this framework retains only volume properties, as is the case for example, when using only the VolumeProperties function to combine properties of volumes from solids. Warning A length, an area, or a volume is computed in the current data unit system. The mass of a single object is obtained by multiplying its length, its area or its volume by the given density. You must be consistent with respect to the units used.
I don't know the internals well enough, but it seems to me that there should be a way to define the density of a topological shape. Maybe it exists in OCCT but isn't exposed in FreeCAD, or maybe it is already there. For this I suggest asking Werner Meyer, one of the main FreeCAD developers, who probably knows more than anybody about OCCT with FreeCAD (except for maybe Jürgen Riegel, the original developer).

Since this is getting technical, maybe we should move the entire thread to the FEM subforum where it will get attention from other guys that know about physical simulations. Maybe one of them knows how to deal with this. In the case of finite element simulations (static and dynamic analysis), the density is added to objects when choosing a material, like general steel, aluminium, structural concrete, etc.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: FreeCAD as pre-post procesor for MBDyn

Post by vocx »

josegegas wrote: Tue Sep 10, 2019 4:04 am ... MBDyn takes an "input file", which is a text file with all the definitions (simulation parameters, bodies, gravity, joints, etc), and returns two text files, ...
This is basically how the FEM Workbench works today. Most finite element solvers take a single input file with the mesh of the bodies, and the properties for the analysis, process all that for a while, and spit the result into a text or binary file. So if it's like that, it should be even possible to integrate MBDyn as a solver into the current FEM Workbench. Of course, you'd need to write a few functions to define your specific boundary conditions (simulation parameters, gravity, joints, etc.), and that write the input file, and parse the output. It sounds possible.

Now, the FEM workbench doesn't actually include the solver. It uses external solvers like CalculiX, z88, Elmer, etc. So MBDyn could be used in the same way. Talk to Bernd in the FEM forum for advice on this.

I know MBDyn isn't a finite element solver, but if it works with input files, it could be treated in basically the same way, and made available in the same FEM Workbench. Otherwise, you could copy the FEM Workbench, and use the same principles in a separate interface, the "Dynamics Workbench" or something like that.
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
josegegas
Posts: 241
Joined: Sat Feb 11, 2017 12:54 am
Location: New Zealand

Re: FreeCAD as pre-post procesor for MBDyn

Post by josegegas »

I see. So to include the object's mass or density I'll have to use the OCCT classes. The easiest solution I could think about (to avoid this) is to multiply (or whatever operation is needed) the given Ixx Iyy and Izz by the mass. I could do this if I knew what units these values have. Yes moving the conversation would be great... Perhaps someone already knows the answer...
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: FreeCAD as pre-post procesor for MBDyn

Post by vocx »

josegegas wrote: Tue Sep 10, 2019 5:33 am I see. So to include the object's mass or density I'll have to use the OCCT classes. The easiest solution I could think about (to avoid this) is to multiply (or whatever operation is needed) the given Ixx Iyy and Izz by the mass. I could do this if I knew what units these values have. Yes moving the conversation would be great... Perhaps someone already knows the answer...
OCCT seems to imply that the quantities of length, area, and volume are unitless, but should be consistent, like this description from LS-Dyna https://www.dynasupport.com/howtos/gene ... tent-units

FreeCAD's base length unit is the millimeter, so maybe as mass the requirement is either grams (time in seconds) or kilograms (time in milliseconds).

FreeCAD's preferences indicate "Standard" units as "mm/kg/s/degree".
Always add the important information to your posts if you need help. Also see Tutorials and Video tutorials.
To support the documentation effort, and code development, your donation is appreciated: liberapay.com/FreeCAD.
User avatar
mfasano
Posts: 122
Joined: Wed Apr 11, 2018 12:31 pm

Re: FreeCAD as pre-post procesor for MBDyn

Post by mfasano »

josegegas wrote: Tue Sep 10, 2019 2:53 am
mfasano wrote: Mon Sep 09, 2019 2:43 pm I have just started a work bench as a pre and post processor for MBDyn.
Sounds great that you had the same idea! Would you mind sharing some details? My approach has been to create customized objects in FreeCAD that contain the properties of each object required by MBDyn. Thus, I create a "Gravity" object in FreeCAD that has the gravitational acceleration and the direction vector. There is also a "Rigid Body" object that has the mass, center of mass, inertia tensor, etc. A "Node" object that has the initial conditions, etc... Then my Python script reads the objects and writes the txt file (.mbd) to be read by MBDyn. I then run the simulation, and read the output files to animate the objects in FreeCAD. What is your approach?
I just started a couple weeks ago so I am not very far. I am a beginner at python and am just learning to set up a workbench.

As for storing design variable, gravity vector, density, etc. I thought of using Scripted objects or store in a spread sheet. I was going to use constraints in assembly 4 which are just alignment of local coordinate systems(LCS) of 2 parts. Then you would define a joint between the to parts at said LCSs. I woudl have buttons for each jont type and you would chose an AS4 constraint and then chose on which axis the relative motion would be. the joint would be a scripted object. Then an MBDyn input file would be written. I had hoped to use the animation workbench as an API to do the animation of the assembly.
User avatar
mfasano
Posts: 122
Joined: Wed Apr 11, 2018 12:31 pm

Re: FreeCAD as pre-post procesor for MBDyn

Post by mfasano »

vocx wrote: Tue Sep 10, 2019 6:56 am
josegegas wrote: Tue Sep 10, 2019 5:33 am I see. So to include the object's mass or density I'll have to use the OCCT classes. The easiest solution I could think about (to avoid this) is to multiply (or whatever operation is needed) the given Ixx Iyy and Izz by the mass. I could do this if I knew what units these values have. Yes moving the conversation would be great... Perhaps someone already knows the answer...
OCCT seems to imply that the quantities of length, area, and volume are unitless, but should be consistent, like this description from LS-Dyna https://www.dynasupport.com/howtos/gene ... tent-units

FreeCAD's base length unit is the millimeter, so maybe as mass the requirement is either grams (time in seconds) or kilograms (time in milliseconds).

FreeCAD's preferences indicate "Standard" units as "mm/kg/s/degree".
In this thread https://forum.freecadweb.org/viewtopic.php?t=6836 it says the Solid.MatrixOfInertia() method gives units of l^5; this you multipy by the uniform density to get moment of inertia.
User avatar
mfasano
Posts: 122
Joined: Wed Apr 11, 2018 12:31 pm

Re: FreeCAD as pre-post procesor for MBDyn

Post by mfasano »

We are not the first to consider using FreeCAD as a pre and post processor for MBDyn. The progrmmers of MBDyn thought of making it a Google Summer of Code project. I guess that never happened. Maybe if we make a good start of it, we could make extending it a Summer of Code project.
Post Reply