FreeCAD as pre-post procesor for MBDyn

About the development of the FEM module/workbench.

Moderator: bernd

User avatar
Kunda1
Posts: 5763
Joined: Thu Jan 05, 2017 9:03 pm

Re: FreeCAD as pre-post procesor for MBDyn

Postby Kunda1 » Sat Oct 05, 2019 12:55 pm

josegegas wrote:
Sat Oct 05, 2019 9:13 am
Continuing with this project, found a problem. Can't import Pandas (import pandas as pd) from the FreeCAD python console... What could the solution be?
What's the error ?
Want to contribute back to FC? Checkout:
#lowhangingfruit | Use the Source, Luke. | How to Help FreeCAD | How to report FC bugs and features
josegegas
Posts: 30
Joined: Sat Feb 11, 2017 12:54 am
Location: New Zealand

Re: FreeCAD as pre-post procesor for MBDyn

Postby josegegas » Sat Oct 05, 2019 1:35 pm

[/quote]
What's the error ?
[/quote]

ImportError: No module named pandas. I'm sure pandas is installed aand works fine cause I don't have any issue using it from elsewhere than FreeCAD
vocx
Posts: 1598
Joined: Thu Oct 18, 2018 9:18 pm

Re: FreeCAD as pre-post procesor for MBDyn

Postby vocx » Sat Oct 05, 2019 4:35 pm

josegegas wrote:
Sat Oct 05, 2019 1:35 pm
ImportError: No module named pandas. I'm sure pandas is installed aand works fine cause I don't have any issue using it from elsewhere than FreeCAD
How did you install pandas? If you installed it through pip or some virtual Python environment, it's possible the base FreeCAD system doesn't find it.

Usually for these big packages it's best to install it through the package manager, for example,

Code: Select all

sudo apt install python3-pandas
josegegas
Posts: 30
Joined: Sat Feb 11, 2017 12:54 am
Location: New Zealand

Re: FreeCAD as pre-post procesor for MBDyn

Postby josegegas » Sun Oct 06, 2019 3:05 am

vocx wrote:
Wed Sep 11, 2019 4:22 pm
mfasano wrote:
Wed Sep 11, 2019 1:39 pm
From the video it looks like you open shell and run a command.
Maybe that was a prototype. I presume jose is planning on setting this up from a single button press, just like the FEM Workbench does.

In any case, yes, Python can run arbitrary programs. The very old way of doing it, is using

Code: Select all

os.system("mbdyn -options input.file")
But the recommended way nowadays is with the subprocess module.

Code: Select all

import subprocess
subprocess.run(["mbdyn", "-options", "input.file"], capture_output=True)
os.system was working nicely but when I do:

subprocess.run(["mbdyn", "-f",__dir__+"/MBDyn/MBDynCase.mbd"])

It just doesn't work, don't really know why. However when I do:

subprocess.call(["mbdyn", "-f",__dir__+"/MBDyn/MBDynCase.mbd"])

It executes MBDyn well, bit I'd like to capture the MBDyn output and show it in FreeCAD gui, any ideas?
vocx
Posts: 1598
Joined: Thu Oct 18, 2018 9:18 pm

Re: FreeCAD as pre-post procesor for MBDyn

Postby vocx » Sun Oct 06, 2019 3:33 am

josegegas wrote:
Sun Oct 06, 2019 3:05 am
It just doesn't work, don't really know why. However when I do:
"Just doesn't work" is not a good description of the problem. Please provide more information.

The subprocess functions have varied over the life of Python 3, so run() is not available before Python 3.5, but call() is. Maybe this is the problem?

Otherwise, check the different options in the manual.
It executes MBDyn well, bit I'd like to capture the MBDyn output and show it in FreeCAD gui, any ideas?
In this very thread we already told you to check the FEM code. In that case, they use Popen() which is a bit of a lower level call. Both run() and call() essentially use Popen().
vocx wrote:
Thu Sep 12, 2019 12:23 am

Code: Select all

out = subprocess.run(["ls", "-l", "/usr/share/freecad"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf8")
print(out.stdout)
It seems FEM uses Popen(). https://github.com/berndhahnebach/FreeC ... ks.py#L105

This is the internal class, while the high level functions are run() (for Python 3.5 and above), or the older call(), check_call(), and check_output() (for Python 3.4 and below).

Code: Select all

process = subprocess.Popen(["ls", "-l", "/usr/share/freecad"], stdout=subprocess.PIPE, stderr=subprocess.PIPE, encoding="utf8")
out, err = process.communicate()
print(out)
print(err)
The standard output or standard error then can be printed in the terminal, the FreeCAD console, or used in some Qt dialogs.
User avatar
bernd
Posts: 8420
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: FreeCAD as pre-post procesor for MBDyn

Postby bernd » Sun Oct 06, 2019 11:29 am

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

Re: FreeCAD as pre-post procesor for MBDyn

Postby josegegas » Mon Oct 07, 2019 8:48 am

Hi all.

Solved the problem of calculating moments of inertia for complex bodies!! Now simulations can be done with any body/assembly. Have a look at my first try:

https://www.youtube.com/watch?v=BN07ZRm ... e=youtu.be

I know you've told me to have a loon at other workbenches, but I don't get the point of telling me where the answer is, if you probably already have the answer in mind and can save me time, time I can invest in getting this workbench done instead of reading someone else's code... So here goes the question: how do I put the messages MBDyn generates in the FreeCAD gui? Here's how I'm executing MBDyn:

process = subprocess.Popen(["mbdyn", "-f", __dir__+"/MBDyn/MBDynCase.mbd"], stdout=subprocess.PIPE, stderr=subprocess.PIPE)
out, err = process.communicate()
print(out)

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

Re: FreeCAD as pre-post procesor for MBDyn

Postby josegegas » Mon Oct 07, 2019 9:18 am

Here's another video, with the reaction force vector and the data plotted for both, the nodes (bodies) and the joints:

https://www.youtube.com/watch?v=75jERiv ... e=youtu.be
User avatar
bernd
Posts: 8420
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: FreeCAD as pre-post procesor for MBDyn

Postby bernd » Mon Oct 07, 2019 9:54 am

We just try to attrac your attention to FEM solver code. In this code all (most) probplem you have with your subprocess code are solved allready and thre are smart Task panels and python API commands already. You just need to add a new FEM solver object and add the FEM solver task and writer modules and could use all atvantages the FEM solver system has.

Eventually it is up to you to write you own solver code.

I try to avoid coding with subprocess because I have run in to trouble a few times (like you). Thus I am not of a big help if it comes to subprocess. Happily the FEM solver framework which uses subprocess was done by someone else.
vocx
Posts: 1598
Joined: Thu Oct 18, 2018 9:18 pm

Re: FreeCAD as pre-post procesor for MBDyn

Postby vocx » Mon Oct 07, 2019 5:40 pm

josegegas wrote:
Mon Oct 07, 2019 8:48 am
.... So here goes the question: how do I put the messages MBDyn generates in the FreeCAD gui?
Well, this is a separate question. Your first question was how to get the output. Can you do this now? Okay. Now, how do we put this output inside a graphical interface? That depends. What type of interface? A message box, a task panel, a text editor? There are various ways. I think it would be good to put it in a task panel, just like FEM does.

Code: Select all

output="""
A bunch of output here.
A bunch of output here.
A bunch of output here.
A bunch of output here.
A bunch of output here.
A bunch of output here.
A bunch of output here.
A bunch of output here."""

import PySide
Text = PySide.QtGui.QTextEdit()
Text.setText(output)
Text.setFont(PySide.QtGui.QFont("Mono"))
Text.show()
By the way, when you are building paths, it's best to use Python's os.path functions. I would also avoid variables with double underscores as these are typically reserved for special internal properties.

Code: Select all

__dir__ + "/MBDyn/MBDynCase.mbd"

os.path.join(__dir__, "MBDyn/MBDynCase.mbd")
Last edited by vocx on Wed Oct 09, 2019 1:35 am, edited 1 time in total.