feature: general initial value and body source, to be tested

About the development of the FEM module/workbench.

Moderator: bernd

qingfeng.xia
Posts: 153
Joined: Tue Sep 22, 2015 1:47 pm
Location: Oxford Uk
Contact:

feature: general initial value and body source, to be tested

Postby qingfeng.xia » Tue Jan 01, 2019 12:18 am

Happy new year 2019!
=========================

## features

Last year, I proposed a generalised body source (gravity, acceleration, heat source, ...) and generalised initial value for any variable, to reduce code duplication.


why? InitialValue is not important for linear elastic problem, but very useful for nonlinear PDE. Cfd workbench must have such obj. I see dicussion in Forum, using Macro to build a electroMagnetic simulation, I think there two objs could be helpful.


Finally, I have the time to implement: _FemBodySource, or _FemInitialValue.

It has the features:

1) apply to the whole domain or selected body (can be point source, internal face, body)

2) value support float value and string expression

3) generalised taskpanel "BodyConstraintWidget"

see a preview of body source, linear acceleration/gravity
FreecAD_generalised_bodysource.png
FreecAD_generalised_bodysource.png (247.89 KiB) Viewed 573 times
## testing

branch: bodyconstraint https://github.com/qingfengxia/FreeCAD
`git clone https://github.com/qingfengxia/FreeCAD.git --branch bodyconstraint --single-branch`

based on freecad-daily 20181229

I had a basic test on ubuntu 16.04

Currently, these two objs are tested Cfd module, without recompiling FreeCAD project.

But if you compile my branch, you should be enable to test in Fem workbench.

Apparently, there is no solver to use them, but UI should show up.



## implementation details



1) FemBodySource and FemInitialValue has their own viewprovider and taskpanel, but share widget

`Category = 'BodySource' 'InitialValue' # similar as FemMaterial for Solid and Fluid`



2) Properties: 'References' to select specific body

'InitialValue'/'BodySource' Python dict to provide flexible settings

Code: Select all


                   = {'Name': 'Temperature',  # name of physical field, used by commander to givev a better obj name

                   'Symbol': u'T',  # can be used to generate variable specific icon file

                   'ValueType'  'Quantity', # "Expression"`

                   'NumberOfComponents': 1, # 'Scaler', 'Vector', 'Tensor'. 

                   'Unit': 'C', # InputEdit of FreeCAD

                   'Value': [0]  # can be list/tuple or just float, len() = NumberOfComponents

                   }

                   #This python dict can be further inserted with "Category" and "ShapeType"

                
3) ObjectsFem has functions `makeInitialValue(doc, settings)` and `makeBodySource(doc, settings)`



4) commands.py has concrete command objects, I made 2 for the testing proposes

_FemCommandBodyAcceleration, which deprecates SelfWeight

Code: Select all


_DefaultInitialTemperature = "{'Name': 'Temperature', 'Symbol': u'T','ValueType': 'Expression', 'NumberOfComponents': 1, 'Unit': 'K', 'Value': 300}"

_DefaultBodyAcceleration = "{'Name': 'Acceleration', 'Symbol': u'g','ValueType': 'Quantity', 'NumberOfComponents': 3, 'Unit': 'm/s^2', 'Value': [0, 0, -9.8]}"



class _CommandFemInitialTemperature(CommandManager):

    "The FEM_InitialTemperature command definition"

    def __init__(self):

        super(_CommandFemInitialTemperature, self).__init__()

        self.resources = {

            'Pixmap': 'fem-add-initial-value',

            'MenuText': QtCore.QT_TRANSLATE_NOOP(

                "FEM_InitialTemperature",

                "initial temperature value"),

            'ToolTip': QtCore.QT_TRANSLATE_NOOP(

                "FEM_InitialTemperature",

                "Creates an initialtemperature value")}

        self.is_active = 'with_analysis'



    def Activated(self):

        FreeCAD.ActiveDocument.openTransaction("Create Fem InitialValue")

        FreeCADGui.addModule("ObjectsFem")

        # fill with a customised BodyConstrainSettings python dict

        FreeCADGui.doCommand("bcs = {}".format(_DefaultInitialTemperature))

        FreeCADGui.doCommand("FemGui.getActiveAnalysis().addObject(ObjectsFem.makeInitialValue(FreeCAD.ActiveDocument, bcs))")

        FreeCADGui.doCommand("FreeCADGui.ActiveDocument.setEdit(FreeCAD.ActiveDocument.ActiveObject.Name)")

        FreeCADGui.Selection.clearSelection()

        FreeCAD.ActiveDocument.recompute()



## to discuss:



1) if there is any name you dislike, please tell me, especially, the 'InitialValue'/'BodySource'

2) I provided default python settings dict, which file should I put them?

3) I was considering add "PhysicalField" property, "Temperature", "Pressure", "Velocity".

to make it easy to create concrete command object.

4) for body source, I am considering "BodySourceType", to support more advanced type like "Rotating acceleratoin",

Depends on BodySourceType, different taskpanel can be loaded.

5) any features you would like?



## Todo:



1) icon file, I am thinking about overlaying a symbol on svg icon file, but I have not got such technique.

2) Gui.InputField, I can not import InputField into python? currently it is a QDoubleSpinBox, min value is zero (this is a bug)

3) adoption of these 2 classes should be done by solver devoloper, there are some existing objects could be deprecated.

`SelfWeight = gravity/linear acceleration`

4) test on multi-body mesh and geometry. I do not have the time.

5) code review and pep8
Ubuntu LTS 64bit in virtualbox, always work with latest FreeCAD git version
Mainly work on OpenFOAM + Fenics for CFD, FSI
User avatar
bernd
Posts: 7892
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: feature: general initial value and body source, to be tested

Postby bernd » Thu Jan 03, 2019 4:48 pm

Welcome back to FreeCAD FEM development! :) This looks good. I will have a closer look on this after I have finished the bug fixing for 0.18 release I'm working ATM.

cheers bernd
User avatar
bernd
Posts: 7892
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: feature: general initial value and body source, to be tested

Postby bernd » Tue Jan 15, 2019 2:15 pm

This is a bug fix commit for FreeCAD master Am I right? https://github.com/qingfengxia/FreeCAD/commit/986b853

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

Re: feature: general initial value and body source, to be tested

Postby bernd » Tue Jan 15, 2019 7:55 pm

qingfeng.xia wrote:
Tue Jan 01, 2019 12:18 am
5) pep8
done

https://github.com/berndhahnebach/FreeC ... constraint
User avatar
bernd
Posts: 7892
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: feature: general initial value and body source, to be tested

Postby bernd » Thu Jan 17, 2019 6:11 am

unit tests do not pass ...
qingfeng.xia
Posts: 153
Joined: Tue Sep 22, 2015 1:47 pm
Location: Oxford Uk
Contact:

Re: feature: general initial value and body source, to be tested

Postby qingfeng.xia » Sun Jan 20, 2019 9:07 pm

thank you Bernd, for the pep8 and code review.

1) what do you think on the feature/code quality? Do you have a timeline for this feature to be merged?
How can we work together on this feature, e.g. I want to push some fix to your branch, what should I do?

Code: Select all

# I will rename my branch with the same name
git branch -m  bodyconstrain_xia
git clone https://github.com/berndhahnebach/FreeCAD.git --branch bodyconstraint --single-branch`
# I work on this branch, make some commit, ...
# how can I push some fix to your branch?
2) yes there is a bug fix for my fluidboundary, do you suggest splitting it out for pull request directly to master by myself?

3) I run a unit test with 1 failure

Code: Select all

Traceback (most recent call last):
  File "/usr/lib/python2.7/unittest/case.py", line 329, in run
    testMethod()
  File "/opt/FreeCAD/build/Mod/Fem/femtest/testobject.py", line 116, in test_femobjects_make
    self.assertEqual(len(analysis.Group), testtools.get_defmake_count(fem_vtk_post) - 1)  # because of the analysis itself count -1
  File "/usr/lib/python2.7/unittest/case.py", line 513, in assertEqual
    assertion_func(first, second, msg=msg)
  File "/usr/lib/python2.7/unittest/case.py", line 506, in _baseAssertEqual
    raise self.failureException(msg)
AssertionError: 47 != 49
My understanding is I added 2 more "makeXXX()" functions, but I have not added test function in `testobject.py`. Add "makeXXX()" testing is compulsory, as the total "makeXXX()" function is automatically detected. It is not easy to fix it, since I have not provided the default parameter value for initialValue and BodySource. I can provide a default initialValue settings dict , but it may be not a decent solution.

4) I see "from FreeCAD import Units" is commented out, is there any recent change to Units module's package path/location?

I would have some time in the first week of Feb to complete more work.

Thank you!
Last edited by qingfeng.xia on Thu Jan 24, 2019 8:51 pm, edited 1 time in total.
Ubuntu LTS 64bit in virtualbox, always work with latest FreeCAD git version
Mainly work on OpenFOAM + Fenics for CFD, FSI
User avatar
bernd
Posts: 7892
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: feature: general initial value and body source, to be tested

Postby bernd » Tue Jan 22, 2019 7:41 pm

your generall constraints are improvements we should get into master. About a time line ... I do not know, we it is ready ... I will have another look before february.

I will separete the fixes commit and add it to my next PR.

We might need to change the unit tests. I will have a look at this too.
User avatar
bernd
Posts: 7892
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: feature: general initial value and body source, to be tested

Postby bernd » Wed Jan 23, 2019 5:35 am

qingfeng.xia wrote:
Sun Jan 20, 2019 9:07 pm
2) yes there is a bug fix for my fluidboundary, do you suggest splitting it out for pull request directly to master by myself?
https://github.com/FreeCAD/FreeCAD/pull/1925
User avatar
bernd
Posts: 7892
Joined: Sun Sep 08, 2013 8:07 pm
Location: Zürich, Switzerland

Re: feature: general initial value and body source, to be tested

Postby bernd » Thu Jan 24, 2019 6:31 am

bernd wrote:
Wed Jan 23, 2019 5:35 am
qingfeng.xia wrote:
Sun Jan 20, 2019 9:07 pm
2) yes there is a bug fix for my fluidboundary, do you suggest splitting it out for pull request directly to master by myself?
https://github.com/FreeCAD/FreeCAD/pull/1925
it compiles for me, but it fails on travis with

Code: Select all

[ 76%] [32mBuilding CXX object src/Mod/Fem/Gui/CMakeFiles/FemGui.dir/TaskFemConstraintPressure.cpp.o[0m
/home/travis/build/FreeCAD/FreeCAD/src/Mod/Fem/Gui/TaskFemConstraintFluidBoundary.cpp: In member function ‘void FemGui::TaskFemConstraintFluidBoundary::onBoundaryTypeChanged()’:
/home/travis/build/FreeCAD/FreeCAD/src/Mod/Fem/Gui/TaskFemConstraintFluidBoundary.cpp:634:108: error: cannot pass objects of non-trivially-copyable type ‘std::string {aka class std::basic_string<char>}’ through ‘...’
         Base::Console().Error("Fluid boundary recomputationg failed for boundaryType `%s` \n", boundaryType);
                                                                                                            ^
make[2]: *** [src/Mod/Fem/Gui/CMakeFiles/FemGui.dir/TaskFemConstraintFluidBoundary.cpp.o] Error 1
make[2]: *** Waiting for unfinished jobs....
[ 76%] [34m[1mGenerating ui_PropertiesDialog.h[0m
see https://api.travis-ci.org/v3/job/483739833/log.txt

would you have a look at this quing feng?
qingfeng.xia
Posts: 153
Joined: Tue Sep 22, 2015 1:47 pm
Location: Oxford Uk
Contact:

Re: feature: general initial value and body source, to be tested

Postby qingfeng.xia » Thu Jan 24, 2019 7:39 pm

I will have a look today and tomorrow to solve it. thank you.

error: cannot pass objects of non-trivially-copyable type ‘std::string {aka class std::basic_string<char>}’ through ‘...’
Base::Console().Error("Fluid boundary recomputationg failed for boundaryType `%s` \n", boundaryType);

I guess the error is due to Error() does not support std::string in ... variable length parameter, maybe for some compilers.
I replace ```Error("Fluid boundary recomputation failed for boundaryType: `%s`", boundaryType);``` with

Code: Select all

        std::string boundaryType = ui->comboBoundaryType->currentText().toStdString();
        Base::Console().Error("Fluid boundary recomputation failed for  changing boundaryType to `%s` \n", boundaryType.c_str());
I found in line 500, using c_str() seems work.

I see it has been merged. sorry for that. do we need to fix in a new PR?

you can manually fix this line, or cherry pick my "bodyconstraint" last commit. Thank you
Last edited by qingfeng.xia on Thu Jan 24, 2019 9:22 pm, edited 1 time in total.
Ubuntu LTS 64bit in virtualbox, always work with latest FreeCAD git version
Mainly work on OpenFOAM + Fenics for CFD, FSI