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 »

saso wrote: Tue Nov 12, 2019 3:56 pm
josegegas wrote: Tue Nov 12, 2019 12:23 pm I actually think that MBDyn can do as the definitive asembly workbench for FreeCAD. As you can see in my videos, the parts move perfectly because MBDyn takes care of the assembly, it looks for all the constraints and everything. I'm quite existed about this.
Yes, lately I was actually thinking about something similar, this is why I asked this questions :)

I was however thinking about doing it with Modelica, since Modelica is a language, we could even write our own libraries with it, if some of the existing ones would not be useful for our needs... But I am not preferring one solution over the other, similar to how we can use different solvers for FEM/CFD it would be IMO good to also have support for different solutions for simulations.

But directly using something like this for the assembly solver I am not sure if it is really possible. For assembling the model (from different parts and sub-assemblies) the user does not really need all the power of such simulation engines, what s/he needs however is a very fast (real time) solver, simulation engines however normally need some time (in complex models it can actually be a very long time) to "solve" the simulation. So, for the basic assembly we would need a simplified version of such a simulation engine with a sort of JIT compiler OR a combination of a more standard assembly constraints/mates and a more advanced ones for simulations (and I think most commercial parametric cad software is doing it like this).

This is however actually also one of the reasons for this post https://forum.freecadweb.org/viewtopic.php?f=8&t=40775, if Modelica, as a language, is a step further from the simulation packages and libraries, then DifferentialEquations.jl and SUNDIALS projects are another step further in to research of the best algorithms and mathematics that are behind this.
I agree with you, an MBDyn simulation is far too complex for the much simpler task of an assembly, and will be terribly slow if an MBDyn simulation has to be performed every time the user updates one part, however, I would suspect that with MBDyn it may be possible to solve exclusively the kinematics of an assembly, and skip the dynamics. Since for an asembly workbench we are only interested on the kinematics, and skipping all the dynamics simulations will make MBDyn much faster. I still have to learn Modelica. I've heard of it but have no experience with it.

Pd. What about using the same FreeCAD sketch solver to solve assemblies? The sketch solver is able to "solve basic assemblies" quite fast. This is actually what I started doing some time ago:

https://www.youtube.com/watch?v=BHbnOlBvpLI&t=327s

It's pretty straightforward. The script just changes the one constraint in a sketch, then read the values of reference constraints, and then move the 3D parts... Here's the basic idea:

https://www.youtube.com/watch?v=uHEJ3IyaSFk&t=145s
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 Nov 12, 2019 8:05 pm Pd. What about using the same FreeCAD sketch solver to solve assemblies? The sketch solver is able to "solve basic assemblies" quite fast. This is actually what I started doing some time ago:...
Yes, this has been done before, but it isn't perfect for every case.

There are several issues such as numerical instabilities when the dimensions vary by several orders of magnitude, that is, millimeters and kilometers.

See these threads
* Adventure of fixing sketcher solver for large sketches
* Call for team! Reimplementing constraint solver
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 »

Testing the constraints so far added: revolute hige, revolute pin, in-line, clamp, prismatic, and axial rotation... Seems all work well. I'll do a couple more examples and then I'll include a few more joints and constraints. This is getting really fun, enjoy this:

https://www.youtube.com/watch?v=8dWsutq ... e=youtu.be
vocx
Veteran
Posts: 5197
Joined: Thu Oct 18, 2018 9:18 pm

Re: FreeCAD as pre-post procesor for MBDyn

Post by vocx »

By the way, this bothers me although it doesn't seem to be a problem. In the tree view all your solids appear with the blue checkmark. This means that the body is in a "touched" state, and needs to be re-computed. Can you do a recompute before running the simulation? It just bothers me that it is in a permanent state of "needs recompute".

Code: Select all

App.ActiveDocument.recompute()
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 »

vocx wrote: Thu Nov 14, 2019 4:38 pm
By the way, this bothers me although it doesn't seem to be a problem. In the tree view all your solids appear with the blue checkmark. This means that the body is in a "touched" state, and needs to be re-computed. Can you do a recompute before running the simulation? It just bothers me that it is in a permanent state of "needs recompute".

Code: Select all

App.ActiveDocument.recompute()
Hi, yes, but the recompute is not required, as you said. One thing on what I'm doing is that I only use simple copies of the parmetric bodies and plenty of scripted objects. If I call the recompute method all gets pretty slow...
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: Sat Nov 16, 2019 8:49 am ...
Hi, yes, but the recompute is not required, as you said. ... If I call the recompute method all gets pretty slow...
Sure but you should probably still do it, at least once. I mean, recomputing is there for a reason, to make sure the objects have run the code that is assigned to them (even if that code is empty). If it gets slow you should probably investigate why, not just ignore it.
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
microelly2
Veteran
Posts: 4688
Joined: Tue Nov 12, 2013 4:06 pm
Contact:

Re: FreeCAD as pre-post procesor for MBDyn

Post by microelly2 »

josegegas wrote: Sat Nov 16, 2019 8:49 am Hi, yes, but the recompute is not required, as you said. ... If I call the recompute method all gets pretty slow...
vocx wrote: Sat Nov 16, 2019 9:00 am Sure but you should probably still do it, at least once. I mean, recomputing is there for a reason, to make sure the objects have run the code that is assigned to them (even if that code is empty). If it gets slow you should probably investigate why, not just ignore it.
If you are sure that you do not need a recompute, you can call purgeTouched for the objects.
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 »

Will the recompute make any difference for a simple (non-parametric) object? To my understanding you recompute a parametric object every time one of it's parameters changes, so that the final object is actually affected by the parameter change. If the objects are non-parametric, as they are in my MBD simulation, then a recompute won't change anything... Please let me know if this is correct. So far I haven't needed to call any recompute, and when I do, all that happens is that my animations get very slow. I could probably call the recompute() only some times so that the "touched" thing disapears?
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 »

Hi. Every time I make a change to the code, to test if it works as intended, I have to close FreeCAD and open it again, so that it "loads" the module with the changes... This is making me loose too much time. Is there a way to avoid this? Like forcing FreeCAD to load the module without closing it and opening again? I've tested to go from one workbench to another, but it seems FreeCAD loads all only once. How do you guys work?
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: Wed Nov 20, 2019 5:04 am Hi. Every time I make a change to the code, to test if it works as intended, I have to close FreeCAD and open it again, so that it "loads" the module with the changes... This is making me loose too much time. Is there a way to avoid this? Like forcing FreeCAD to load the module without closing it and opening again? I've tested to go from one workbench to another, but it seems FreeCAD loads all only once. How do you guys work?
This is a general feature of Python, that Python modules are only imported once, and then are held in a Python cache. If you'd like to reload the module you have to do it manually. However, for FreeCAD this may be a bit more challenging as you may want to reload icons, buttons, menu entries, etc. I think it's not really possible to reload these graphical elements, only Python modules themselves (functions and classes) which can be used from the command line.

Python workbenches debugging
ediloren wrote: Mon Apr 01, 2019 9:46 pm ...
Reloading workbenches
...
There are two main posts that deal with the topic:

https://forum.freecadweb.org/viewtopic.php?t=320
https://forum.freecadweb.org/viewtopic. ... 5&p=296289

First of all a note: as far as my understanding and my experience goes, you cannot really reload a full workbench, as there are some functions that loaded by C/C++ code. In particular:
  • the Workbench description class is transfered to FreeCAD while start
  • FreeCADGui.addCommand('MyModule', MyModule())
To reload everything else,... in Python 3:

Code: Select all

import someModule
from importlib import reload
reload(someModule)
...
If you are prototyping something I suggest you use the macro editor. You can paste an entire file there, and test it while running a current instance of FreeCAD. Once you are satisfied with the behavior, then you can take it out, and add it to your real workbench sources.

But more important than that is that you should try to write your code in a modular way so that you can see the effects of small snippets of code without even running them. That is, if the code is written correctly, it should work on first try.

If you are one of those programmers who writes small things, then runs the code once, then makes small changes, then runs the code again, then goes back and does other small changes, then tests again, etc., I feel maybe you are too impatient in writing the code. I would try to plan what I want to do a bit longer before writing the functions. Just taking some extra minutes to think and reason the best way to accomplish what you want may save time in the long term. I'd try to write the documentation strings in the functions because this makes you think about what you are implementing, and maybe you get ideas that you didn't think about originally.
Last edited by vocx on Wed Nov 20, 2019 3:28 pm, edited 1 time in total.
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.
Post Reply