FreeCAD as pre-post processor for MBDyn

About the development of the FEM module/workbench.

Moderator: bernd

User avatar
mfasano
Posts: 122
Joined: Wed Apr 11, 2018 12:31 pm

Re: FreeCAD as pre-post processor for MBDyn

Post by mfasano »

A summary of the state of the MBDyn work bench.

The main developers are myself mfasano and T-Garnier. We have been consulting with MBDyn contributers louisgag and Dr. Zanonni. luzpaz has helped with documentation in the repository and some spelling errors in the code.

The github repository is below. There are descriptions of how to use the commands and a couple tutorials in the repository’s wiki. There is a to do list in the readme.
https://github.com/mfasano727/MBDynFCwb

It was started as an extension of the code developed by a google summer of code student and Dr. Zanonni. He has been developing python code to generate the MBDyn input file. I thought about inheriting his classes for the FreeCAD workbench but found it expedient not to. Now there is little of the GSOC program left in workbench.
I thought using the Assembly 4 would be a good base to work from. It makes Assembling parts simple, and the use of the local coordinate systems(LCS) makes positions and orientations needed for MBDyn built in to the model. I thought using the Assembly 4 constraints(alignments of LCSs) to simplify defining MBDyn joints. It worked for the revolute joints but it didn’t work for the other joints. I’m thinking of having the user choosing the LCSs for the revolute joints separately instead of the constraint; like it is for the other joints.

All the MBDyn nodes, forces and elements are stored in scripted objects. For now, only have rigid bodies are available. When you create a rigid body you select from a list of linked parts from the assembly. You give a density, but may be able to select a material at some point. The volume, center of mass and moment of inertia properties for mass, CoM and MoI are used; this may be an issue for some parts. A structural node is created at the center of mass when you create a body. You can create structural nodes separately; you can input the parameters manually or chose a part and an LCS. References can also be created but are not utilized.

So far you can create several joints. They are revolute pin, revolute hinge, axial rotation, clamp, inline, and prismatic. I hope to have the total and total pin joints very soon. The total joint should allow you to simulate almost any mechanism with rigid bodies.

So far there is only the ramp drive caller. I hope to have the template drive caller soon. The template drive caller is integral to the total joints. There are no forces except gravity are implemented yet, nor are there plugin variables.
For post processing it will animate model. You can store different simulations with the same model. For now you can use different initial values. Later you will be able to change drive callers and forces. You had arrows indicating forces and torques on your animations. We would like to do that.

I will be working on separating the code that creates the scripted objects from the UI commands. This will allow us to create unit tests and scripting with the wb.
josegegas
Posts: 255
Joined: Sat Feb 11, 2017 12:54 am
Location: New Zealand

Re: FreeCAD as pre-post processor for MBDyn

Post by josegegas »

I believe we are using more or less the same approach. I will descrive what I have done so far, firts the coincidences I have with your approach:

1.- I created a set of classes (each one is a FeeCAD scripted object) that implement the different "Objects" that MBDyn uses. Fo instance, one class implements a rigid body and contains the properties of rigid bodies. Another class is a structural dynamic node and implements all the properties of a dynamic node. Another class is a dummy node, etc. I also have classes for joints (revolute pin, hinge, in-line, prismatic, coincident, clamp, etc), gravity and forces, some actuators,a few drive callers (step, ramp, linear, sin and cossine, constant), etc.

2.- As you have done, I also store all the MBDyn model in the FreeCAD scripted objects, so that the model can be saved and open any time.

3.- One of my classes reads the scripted objects and automatically generates the input file for MBDyn. So that the user can open a saved model and simulate it any time, by re-generating the input file for MBDyn). I guess you have done the same?

4.- As you have done, my workbench also creates the nodes at the center of mass of the objects. Afterwards the user can move the node to any other location and orientation, and my "rigid body" class will update the offset of its centre or mass relative to its node.

5.- As with your workbench, the user also has to input the density of each body. The software will then calculate the inertia momments and mass using this density and the volume obtained from the volume property of the CAD part. My workbench requires an assembly made of simple (non-parametric) parts to work. This is not really a limitation. My approach is yo keep the parametric parts in one file and then export them as simple parts to perform MBDyn simulations. I started doing this in order to keep the tree and number of components as simple and small as possible.

6.- As you do, I also create a global reference system fixed to the 0,0,0 of the 3D scene. This works as the fiixed inertial frame for the simulations. Although I also use moving frames to animate and visualize local reaction forces, for example when there are moving joints.

Now, what we have done in very different ways:

1.- I do not use Assemby 4 or any other assembly workbench to visualize/animate the MBDyn simulation results. I thought of doing so for a long time, and even experimented with some scripts, but the animations were always terribly slow. Not very useful to visualize the results of the MBDyn simulation. In the end I decided to program my own class class to animate the models. This class reads the output file of the MBDyn simulation and stores all the data in a matrix (the computer´s ram). Then it changes the "placement" property of each body in the 3D scene to animate the model. It works well. The animations flow nicely even with tens of bodies in the assembly. I use the same approach to animate forces/torques using the arrows you mentioned. The limitation of this approach is that MBDyn can generate very large files, and all this data must be stored in a matrix before staring the annimation. The ram memory can suffer a lot by doing so, especially if the user has set tiny time intervals for the MBDyn simulation and if the simulation has lots of bodies. I believe the only solution for this is to properly set the simulation parameters so that the output file is not excessively large. I have been able to animate assemblies with more than 20 moving parts (such as the cuadriped robot I shared in the video) by properly setting the parameters for the MBDyn simulation. I achieved this even though my computer is pretty basic and old (and i3 processor with 4 gb ram).

I have some problems with animating (not simulating) assemblies with parts that move in 3D, (not in a plane). I believe there are some bugs in my animation algorithm that mess-up with the orientations of the parts while translating the MBDyn output file to the orientations in FreeCAD. This must be fixable though, it is just a matter of time and some work...

2.- I have implemented a few other classes. One interesting and which is working well is used to plot the MBDyn results. I use Matplotlib for this. All the results can be plotted straight from the workbench using both 2D and 3D plots. I have found this very useful especially to validate the simulations. Some times the animation looks fine and the mechanism seems to do what it is supposed to do, until one plots the result to find that something went terribly wrong, for instance, because of reaction forces that are well above or below what can be expected...

3.- Another useful class I have implemented exports the MBDyn simulationresults into a FreeCAD spreadsheet. I found this idea very useful too, because it gives easy access to the data from the simulation results. This only works with relatvely small simulations though. When I try to export the results of a large simmulation FreeCAD crashes.

I didn´t know there was a MBDyn installer for windows? I have not tested any of my classes on a windows machine because last time I tried I could not compile MBDyn on windows, not sure why. I tried to run the installer you provide in your GitHub, but didn´t work on windows 7. I think by now I will just stick to linux, although it would be nice to test all of this in windows because you know, most people use windows, I dont really know why....

I will take some time to install and use your workbench this week. I am courious to see what else we have done the same or differently and why...

What is the objective of using the assemby 4 workbench to animate the results?

Cheers.
josegegas
Posts: 255
Joined: Sat Feb 11, 2017 12:54 am
Location: New Zealand

Re: FreeCAD as pre-post processor for MBDyn

Post by josegegas »

Pd. I someone could give me some insight on how to get MBDyn running on a windows 7 machine would be much apprecuated :D
User avatar
mfasano
Posts: 122
Joined: Wed Apr 11, 2018 12:31 pm

Re: FreeCAD as pre-post processor for MBDyn

Post by mfasano »

josegegas

I used Assembly 4 because it is easy to create the FreeCAD model. I use the LCSs Assembly4 uses to assemble the model to help setup the MBDyn elements. How do you have the users create the MBDyn elements.

Animation is done as you do just reading the .mov file and setting the placements of the parts. I read the file as the animation progresses. This will get slow as the model gets complicated. Assembly4 does not facilitate the animation at all. I do have to use Gui.updateGiu command; recompute would apply the expressions used by assembly 4, and the placements would be reset to the initial assembly.

I look forward to viewing your code. It would be good if we were not duplicating our efforts.
josegegas
Posts: 255
Joined: Sat Feb 11, 2017 12:54 am
Location: New Zealand

Re: FreeCAD as pre-post processor for MBDyn

Post by josegegas »

Hi mfasano.

I downloaded and tried your workbench by doing your double pendulum example. It worked well.

The approach I am following to create the MBDyn model is quite different from yours. The first difference is that I avoid using pop-up windows to ask the user for information. My intention is to do something more like ANSIS or ADAMS, where you can define models mostly by clicking on entities from the 3D view. For instance to create a joint the user simply clicks a reference body from the 3D model, for example a cylinder for revolute pins and revolute hinges. So that to create a revolute hinge joint the user selects two bodies and a cylinder from the 3D view and click the "revolute hinge" icon and that is it, the revolute hinge will be created at the center of mass of the cylinder and using the cylinder orientation. The same for creating rigid bodies and nodes, the user just selects the bodies from the 3D view and clicks on the icon. Afterwards any property can be changed by simply changin the parameters of the scripted obtects. This approach has helped me keep my code as simple and short as possible...

I also do not use any other enititie in addition to simple (non-parametric) objects that form the assembly. I have done the same so that one can import models from any CAD package. For instance I downloaded a model of a tracktor from grabcad and applied some simulations over it. In fact one can do simulations from any model for instance in .step format one can import into FreeCAD.

I look forward to make my code public too, but it is not quite ready for that. The first obstacle is that some of my classes are still a "mess". They seem to work fine, but the code does not have many comments and is not nice and tidy, so will be hard for other people to understand. Another thing that stops me from making mu code public is that I am still working on the examples. I am now doing some examples including gears and racks... I really want a nice tutorial with plenty of working examples before I release my code. One of the most complex examples I want to include in that tutorial is the four-legged robot. I manage to simulate it every time, but the tutorial is still not very clear for a begginer, so I am still working on the tutorials. Another thing I want to implement before releasing the code is contact and some contact examples...

The objective is to get people exited about FreeCAD by providing a nice documentation with several examples on how to use the workbench :D

Another problem is that I have never tested my workbench on windows, and I would like it to work in windows before I release it. Most people use windows sadly, so I believe of I released a workbench that does not work on windows it will not have much acceptance, I fear.

I see your workbench works well in windors. Do you mind sharing with me the piece of code that executes MBDyn from FreeCAD? I see also you use the executable I downoaded from the link in your tutorial...

Cheers!
louisgag
Posts: 75
Joined: Fri Jan 17, 2020 9:59 am

Re: FreeCAD as pre-post processor for MBDyn

Post by louisgag »

Hi josegegas,
you are loading the results file in ram? I assume you are referring to the .mov file, or do you also work with netcdf files?
for the former, would it not save the ram when you read it line by line during the animation? Would that be too slow?
For one thing, the issue in the postprocessor from Zanoni is solved by implementing a frequency switch that allows to set a target frame rate.
No one actually wants to see 1000 frames per second, but a small timestep is often required, so here the frequency switch ends up saving reading most of the output file...

@josegegas @mafasano I have to say the approach of Jose sounds more intuitive and easy, but without actually trying it it's hard to say more...
User avatar
mfasano
Posts: 122
Joined: Wed Apr 11, 2018 12:31 pm

Re: FreeCAD as pre-post processor for MBDyn

Post by mfasano »

josegegas wrote: Tue Dec 15, 2020 12:56 am
The approach I am following to create the MBDyn model is quite different from yours. The first difference is that I avoid using pop-up windows to ask the user for information. My intention is to do something more like ANSIS or ADAMS, where you can define models mostly by clicking on entities from the 3D view. For instance to create a joint the user simply clicks a reference body from the 3D model, for example a cylinder for revolute pins and revolute hinges. So that to create a revolute hinge joint the user selects two bodies and a cylinder from the 3D view and click the "revolute hinge" icon and that is it, the revolute hinge will be created at the center of mass of the cylinder and using the cylinder orientation. The same for creating rigid bodies and nodes, the user just selects the bodies from the 3D view and clicks on the icon. Afterwards any property can be changed by simply changin the parameters of the scripted obtects. This approach has helped me keep my code as simple and short as possible...
I like the idea of selecting from the viewscreen or model tree. I just have not implemented it yet. I thought of using the feature python property propertyLink; you would link the MBDyn object to the parts and as the parts change the MBDyn objects change. The documentation for propertyLink is almost nonexistent, but some have used it. I have a problem with the initial position of the model. If it changes, I have to recreate the MBDyn objects.


I also do not use any other enititie in addition to simple (non-parametric) objects that form the assembly. I have done the same so that one can import models from any CAD package. For instance I downloaded a model of a tracktor from grabcad and applied some simulations over it. In fact one can do simulations from any model for instance in .step format one can import into FreeCAD.
My problem with imported parts is getting the CM, MoI, etc. I guess a program will need to be written for this.


Another problem is that I have never tested my workbench on windows, and I would like it to work in windows before I release it. Most people use windows sadly, so I believe of I released a workbench that does not work on windows it will not have much acceptance, I fear.

I see your workbench works well in windors. Do you mind sharing with me the piece of code that executes MBDyn from FreeCAD? I see also you use the executable I downoaded from the link in your tutorial...
T-Garnier wrote that section. I will let him answer.
josegegas
Posts: 255
Joined: Sat Feb 11, 2017 12:54 am
Location: New Zealand

Re: FreeCAD as pre-post processor for MBDyn

Post by josegegas »

louisgag wrote: Wed Dec 16, 2020 4:46 am Hi josegegas,
you are loading the results file in ram? I assume you are referring to the .mov file, or do you also work with netcdf files?
for the former, would it not save the ram when you read it line by line during the animation? Would that be too slow?
For one thing, the issue in the postprocessor from Zanoni is solved by implementing a frequency switch that allows to set a target frame rate.
No one actually wants to see 1000 frames per second, but a small timestep is often required, so here the frequency switch ends up saving reading most of the output file...

@josegegas @mafasano I have to say the approach of Jose sounds more intuitive and easy, but without actually trying it it's hard to say more...
Hi louisgag

I mean that I delcare a matrix, then open the .mov file and the .jnt file (which has the simulation results for the joints reaction forces and torques), and then I load all the results into this big matrix. After the results are in the matrix, I start the animation by reading the matrix and updating the placements of the bodies in the 3D scene and by animating the arrows I use to show forces and torques to the user (as in other multibody dynamics packages). I have tried the other approach, reading the file line by line and updating the placements as I read the file, but this is very slow. Animations look terrible if there are say 10 bodies or more. I decided to go for the first approach because today most computers people use to do CAD-CAE have at least 4 gigas ram, which will be enough to load a big simulation into a matrix. For really big simulations or for people who do not have a very big ram, it may be a good idea to split the .mov and .jnt files into parts and aminate the whole thing into two or three stages. The downside of this is that the animation will stop every time the next part of the .mov and .jnt files has to be loaded.

What do you think?
louisgag
Posts: 75
Joined: Fri Jan 17, 2020 9:59 am

Re: FreeCAD as pre-post processor for MBDyn

Post by louisgag »

I think Andrea Zanoni should give his opinion on that one, he's coded the same thing for Blender (Blendyn). I'll try to get a hold of him.
azanoni
Posts: 1
Joined: Wed Dec 16, 2020 9:42 pm

Re: FreeCAD as pre-post processor for MBDyn

Post by azanoni »

Hi louisgag, hi josegegas,

in Blendyn, I read the .mov file line by line, and skip the portions of it that I'm not interested in when the user wants to visualize the result with a different framerate with respect to the MBDyn output (which is actually the typical case, as Louis pointed out).

The relevant code can be found here
https://github.com/zanoni-mbdyn/blendyn ... ib.py#L714
if you want to take a look.

Nothing really fancy, but it works well and I did not experience problems even with very big models, with hundreds of nodes. If you start having problems with 10 nodes, I suspect that you are doing something that does not scale well, for example opening and closing the file each time you need to read, or seek every time from the start of the file to the line you are interested in, or something like that. This are blind accusations, though, since I did not look at your code :)

I find the use of cProfile very useful in this cases, it helped me find quite a lot of inefficient code through the latest development of Blendyn. I suggest you to try it out, if you're not doing so already: https://docs.python.org/3/library/profile.html

Cheers, and keep up the good work!
Post Reply