Fem constraint contact

About the development of the FEM module/workbench.

Moderator: bernd

User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Fem constraint contact

Post by bernd »

the code still still has quite some pep8 code formatting issues ...

try on a linux machine ...

Code: Select all

find src/Mod/Fem/ -name "*\.py" | grep -v convert2TetGen.py | grep -v InitGui.py | grep -v TestFem.py | xargs -I [] flake8 --ignore=E266,E402,E722,W503 --max-line-length=200 []
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Fem constraint contact

Post by bernd »

for the cube example I get a exception ...

Code: Select all

Traceback (most recent call last):
  File "/usr/lib/python3/dist-packages/numpy/core/fromnumeric.py", line 56, in _wrapfunc
    return getattr(obj, method)(*args, **kwds)
TypeError: Partition index must be integer

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/hugo/Documents/dev/freecad/freecadbhb_dev/build/Mod/Fem/femguiobjects/_ViewProviderFemConstraintAutoContact.py", line 136, in add_contact
    femutils.AddAutoContact(self.slope,self.friction,self.facenum)
  File "/home/hugo/Documents/dev/freecad/freecadbhb_dev/build/Mod/Fem/femtools/femutils.py", line 308, in AddAutoContact
    results = np.array(get_all_close_surfaces(locations,num))
  File "/home/hugo/Documents/dev/freecad/freecadbhb_dev/build/Mod/Fem/femtools/femutils.py", line 197, in get_all_close_surfaces
    store.append(find_index_of_nearest(array, i, num))    
  File "/home/hugo/Documents/dev/freecad/freecadbhb_dev/build/Mod/Fem/femtools/femutils.py", line 188, in find_index_of_nearest
    idxmin = np.argpartition(np.array(distance), num)
  File "/usr/lib/python3/dist-packages/numpy/core/fromnumeric.py", line 808, in argpartition
    return _wrapfunc(a, 'argpartition', kth, axis=axis, kind=kind, order=order)
  File "/usr/lib/python3/dist-packages/numpy/core/fromnumeric.py", line 66, in _wrapfunc
    return _wrapit(obj, method, *args, **kwds)
  File "/usr/lib/python3/dist-packages/numpy/core/fromnumeric.py", line 46, in _wrapit
    result = getattr(asarray(obj), method)(*args, **kwds)
TypeError: Partition index must be integer
User avatar
makkemal
Posts: 395
Joined: Wed Apr 29, 2015 12:41 pm
Location: South Africa
Contact:

Re: Fem constraint contact

Post by makkemal »

Sorry about the Flake8 formatting errors somehow the auto formatting in my pydev got disabled.
That is fixed now
https://github.com/makkemal/FreeCAD.git

Code: Select all

Freecad/FreeCAD$ find src/Mod/Fem/ -name "*\.py" | grep -v convert2TetGen.py | grep -v InitGui.py | grep -v TestFem.py | xargs -I [] flake8 --ignore=E266,E402,E722,W503 --max-line-length=200 []
src/Mod/Fem/femguiobjects/_ViewProviderFemConstraintAutoContact.py:36:1: F401 '_FemConstraintAutoContact' imported but unused
My test prob still run without errors, you just have to be sure analysis is active files attached again.
I also tested on windows machine

Code: Select all

OS: Windows 10
Word size of OS: 64-bit
Word size of FreeCAD: 64-bit
Version: 0.18.15652 +2 (Git)
Build type: Release
Branch: femautocontact
Hash: cd70f86543101b6440f65ce82fc14e88daca778e
Python version: 2.7.8
Qt version: 4.8.7
Coin version: 4.0.0a
OCC version: 7.1.0
Locale: English/SouthAfrica (en_ZA)
Now why do you get that error ?
Attachments
contact_test2.FCStd
(9.07 KiB) Downloaded 69 times
contact_test.FCStd
(10.47 KiB) Downloaded 61 times
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Fem constraint contact

Post by bernd »

great ... https://github.com/makkemal/FreeCAD/com ... utocontact

I will give it a try. It might be because I'm on Py3 already. I will fix the problem in this regard then ...

bernd
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Fem constraint contact

Post by bernd »

there is a nice real example for this auto contact utility: https://forum.freecadweb.org/viewtopic.php?f=18&t=34052
Qumbetlian
Posts: 4
Joined: Fri Mar 15, 2019 6:28 am

Re: Fem constraint contact

Post by Qumbetlian »

Hi,

This auto contact feature would be very useful when doing an FEA of an assembly of multiple body. I like to have this feature in 0.18 stable release. I tested yesterday this utility but not worked for me. I compiled the project from branch linked earlier in this topic. When compiled with debugging option, FreeCAD crashed at loading the file. I compiled again withouth debugging option then I done a test. Waht happening is that when push the button "Create auto contact for compound object" thei apear the form in the "Tasks" tab and apear "ConstraintAutoContact" in the model tree. Pushing "Add Automatic Contacts" button within the form, the cubes are highlited and nothing heappen. When push "Ok" button the "ConstraintAutoContact" object disapear from the model tree. Attached some screenshots.Image
Attachments
FreeCAD-autocontact.jpg
FreeCAD-autocontact.jpg (277.47 KiB) Viewed 2293 times
User avatar
bernd
Veteran
Posts: 12849
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland
Contact:

Re: Fem constraint contact

Post by bernd »

Hi and welcome to FreeCAD FEM.

It will for sure not be in 0.18, since 0.18 has been soft released already.

I have a branch which I regularly rebase on master. Means you should try this branch: https://github.com/berndhahnebach/FreeC ... utocontact

Any feedback what does work and what does not work would be great. For sure it has still some Py3 problems. These needs to be fixed befor we can make a PR. Because of the release 0.18 I did not have the time to look at this. Any help in this regard is highly apreteated.

bernd
Qumbetlian
Posts: 4
Joined: Fri Mar 15, 2019 6:28 am

Re: Fem constraint contact

Post by Qumbetlian »

I ran the script "contact_apply_01.py" that i suppose to run behind of button "Create auto contact for compound object". It worked, but it put contacts also between surfaces of the same body. I think that these contacts are redundant for Calculix and should be avoided. I coded another script, that can check the contacts only between faces of a body and faces of the remaining bodies. It has also the posibility to specify a distance for that two faces is considered in contact.
I tested the script with a little more complex model and it worked. It can be improved, of course and one of improvements would be to check if two faces in contact are paralele (for flat faces), or tangent for cilindrical and spherical faces. I didn't found in FreeCAD documentation a method to do this checking.
If somebody consider my script better, it can be used in FreeCAD source code. If someone can tell me how to bind my scrypt to "Create auto contact for compound object" button, I would be gratefully.
Here is the script:

Code: Select all

from PySide import QtCore, QtGui
import FreeCAD
import Fem

minDist=0.5     # minimum distance between faces for which they are considered in contact
Slope = 1000000.000000  # Contact stiffness
Friction = 0.300000     # Friction 
Scale = 1   # Scale

def findContacts(bodiesGroup): # find the contacts between each surface of first body and surfaces of the rest of bodies
    contacts=[]
    if len(bodiesGroup) <= 1:
        return contacts
    firstMember=bodiesGroup[0]
    restOfMembers=bodiesGroup[1:len(bodiesGroup)]
    contacts=findContacts(restOfMembers) # recursively find contacts
    c=len(contacts)
    i=0
    for face in firstMember.Shape.Faces:  # browse the list of shapes of the first body
        j=0
        for neighbourBody in restOfMembers: # browse the list of neighbour body
            for neighbourFace in neighbourBody.Shape.Faces: # browse the list of shapes for each body
                dist=face.distToShape(neighbourFace)[0]
#                print("Distance from face ", face, " of ", firstMember.Name, " to the face ", neighbourFace, " of ", neighbourBody.Name, " is ", dist)
                if dist <= minDist:
                    contacts.append([i,j]) # Appending indexes of faces in contact - indexes are according to number of faces checked in current instance of function
                j=j+1
        i=i+1
    for k in range(0,c): # Correcting the indexes returned from the previous instance according to number of faces founded in the current instance
        contacts[k][0]=contacts[k][0]+i
        contacts[k][1]=contacts[k][1]+i
    for k in range(c,len(contacts)): # Correcting the index of faces belonging to the rest of bodies according to number of faces belonging to the first body
        contacts[k][1]=contacts[k][1]+i
    return contacts

def addContacts(facePairs, group): # Adding contacts according to the faces pairs given in first argument
    active = FemGui.getActiveAnalysis()
    for pair in facePairs:
        obj = FreeCAD.activeDocument().addObject("Fem::ConstraintContact","FemConstraintContact")
        obj.Slope = Slope
        obj.Friction = Friction
        obj.Scale = Scale
        obj.References = [(group,"Face"+str(pair[0]+1)),(group,"Face"+str(pair[1]+1))]
        active.addObject(obj)
        
def errorDialog(msg):
    # Create a simple dialog QMessageBox
    # The first argument indicates the icon used: one of QtGui.QMessageBox.{NoIcon, Information, Warning, Critical, Question} 
    diag = QtGui.QMessageBox(QtGui.QMessageBox.Warning, 'Error in AutoContacts', msg)
    diag.setWindowModality(QtCore.Qt.ApplicationModal)
    diag.exec_()

class Error(Exception):
   """Base class for other exceptions"""
   pass

class ActiveAnalysis(Error):
   """Raised when something go wrong with analyse"""
   pass

try:
    if not FemGui.getActiveAnalysis():
        raise ActiveAnalysis
    for obj in FreeCAD.ActiveDocument.Objects:  # seach all objects in document
        contacts=[]
        objName = obj.Name
        objLabel = obj.Label
        if 'Compound' in objName or 'Fusion' in objName: # detect if an oject is compound or fussion
            bodiesGroup=[]
            group=App.ActiveDocument.getObject(objName).OutList # get the list of member objects
            for member in group:
                if member.Shape.Faces:  # is a face then
                    bodiesGroup.append(member)
                else:
                    print("Not a Shape")
            contacts=findContacts(bodiesGroup)
            addContacts(contacts, obj)
except ActiveAnalysis:
    errorDialog("There is not an active analysis.\nPlease create a FEM analyse first!")
except Exception:
    print("Not object")
And a screenshot
AutoContacts.jpg
AutoContacts.jpg (132.93 KiB) Viewed 2247 times
User avatar
makkemal
Posts: 395
Joined: Wed Apr 29, 2015 12:41 pm
Location: South Africa
Contact:

Re: Fem constraint contact

Post by makkemal »

I also had some problem with the following example

https://forum.freecadweb.org/viewtopic ... 18&t=34052
I have not had time to dig yet I will see what is going on
You are also welcome to have a look at the code ;)

Thanks
Michael
Qumbetlian
Posts: 4
Joined: Fri Mar 15, 2019 6:28 am

Re: Fem constraint contact

Post by Qumbetlian »

Hi Michael,

Can you provide me this model!?
gear.PNG
gear.PNG (159.98 KiB) Viewed 2180 times
I'd like to test my code on it.
Thank you!
Post Reply