Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

making python-modules work #3217

Merged
merged 10 commits into from
Dec 3, 2018
Merged

making python-modules work #3217

merged 10 commits into from
Dec 3, 2018

Conversation

philbucher
Copy link
Member

With this modification the python-scripts can be subdivided into subfolders
=> now they behave as real python modules!
=> the changes necessary are very small and can be done app-by-app without breaking anything
i.e. these changes do NOT break the current mechanism, it is fully backwards compatible and can be adapted slowly

e.g. now one can do:

import KratosMultiphysics
import KratosMultiphysics.StructuralMechanicsApplication as KSM

from KratosMultiphysics import new_linear_solver_factory

from KratosMultiphysics.StructuralMechanicsApplication import structural_mechanics_analysis

# for demonstration:
from KratosMultiphysics.StructuralMechanicsApplication.solvers import custom_solver

solves #2655

I will discuss this with @jcotela in the next days, afterwards you can discuss it in the comittee

I think this also works in python2, but I have not tried it

FYI @adityaghantasala @oberbichler

@loumalouomega
Copy link
Member

imagen

@loumalouomega
Copy link
Member

Totally support from here

Copy link
Member

@roigcarlo roigcarlo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Wow nice job! 👍 from my side to!

@bodhinandach
Copy link
Member

This is a very nice feature!

Copy link
Member Author

@philbucher philbucher left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Some comments abt things that I am not sure are working yet

Also I think currently if there is a __init__.py in AppName/python_scripts I think it would not be executed
=> also this I think could be solved if we want this feature

@@ -196,4 +196,6 @@ install(TARGETS KratosStructuralMechanicsCore DESTINATION libs )
install(TARGETS KratosStructuralMechanicsApplication DESTINATION libs )

# Add to the KratosMultiphisics Python module
install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/StructuralMechanicsApplication.py" DESTINATION KratosMultiphysics )
install(DIRECTORY DESTINATION "KratosMultiphysics/StructuralMechanicsApplication")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this works in Win (using / for separating the path)
=> if not I think it can be easily fixed

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Usually you are safe with "/". It is also a valid separator for windows.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so this is not any longer in

KratosMultiphysics/applications/StructuralMechanicsApplication?

this means that you can only make installation outside of where you compile?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t understand, can you please explain in more detail what you mean?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

right now on my computer i normally keep the python files in the same directory as they normally are. This is useful since when i change a file i do not need to reinstall.

I am asking if this will change after this PR or if the python files will be left where they are

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

clarifying on my point, it may even be good if this is changed, since it may be the first step towards propoer python packaging of Kratos...however the need of installing every time a python file is modified is a little annoying, which is the reason for which i ask

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah now I understand
Answer: the current behavior is NOT changed, i.e. you DON'T have to reinstall after changing python files
Only the folder-structure changes a bit in the KratosMultiphysics folder
from:

KratosMultiphysics
    - __init__.py
    - StructuralMechanicsApplication.py
    - FluidDynamicsApplication.py

to:

KratosMultiphysics
    - __init__.py
    - StructuralMechanicsApplication
        __init__.py # this is the "StructuralMechanics.py"-file
    - FluidDynamicsApplication.py

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

oh, i see.

thx!

install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/StructuralMechanicsApplication.py" DESTINATION KratosMultiphysics )
install(DIRECTORY DESTINATION "KratosMultiphysics/StructuralMechanicsApplication")

install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/StructuralMechanicsApplication.py" DESTINATION "KratosMultiphysics/StructuralMechanicsApplication" RENAME "__init__.py")
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above, I am not sure if this works in Win (using / for separating the path)
=> if not I think it can be easily fixed

@@ -4,7 +4,7 @@
application_folder = "StructuralMechanicsApplication"

# The following lines are common for all applications
from . import application_importer
from .. import application_importer
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am not sure if this also works in python2
=> if not I think it can be fixed with little effort

@oberbichler
Copy link
Contributor

Nice work!

Can I also import stuff from the native (C++) module inside my python script?

@philbucher
Copy link
Member Author

Can I also import stuff from the native (C++) module inside my python script?

I guess ...
I mean, the C-modules stuff has not changed, I think it should be possible

I will also test it again

@philbucher
Copy link
Member Author

philbucher commented Nov 6, 2018

@KratosMultiphysics/technical-committee I played with this some more, for me it works as intended
Summing up again the changes I propose here:

  • Fully backwards compatible (see first post)
  • minimal changes necessary
  • NO need to reinstall after python-files were changed!
  • the AppName.py-scripts are renamed to __init__.py and moved to the python-scripts-folder of the corresponding app => standard for python-modules (Note: this could also be achieved through CMake, e.g. if an app does not have the python_scripts dir so far) => therefore up to you. Moving and renaming seems to be the more natural/pythonic way
  • as Riccardo asked earlier, the folder-structure in the KratosMultiphysics-folder slightly changes:

from:

KratosMultiphysics
    - __init__.py
    - StructuralMechanicsApplication.py
    - FluidDynamicsApplication.py

to:

KratosMultiphysics
    - __init__.py
    - StructuralMechanicsApplication
        __init__.py # this is the "StructuralMechanicsApplication.py"-file
    - FluidDynamicsApplication.py

What is left to do: Testing in other environments => for this I pushed some "pseudo-submodules", ofc they will be removed again
Here is the file to test this. If it runs through then it means that the imports are working
python-modules-test.zip

I think we should test it on WIndows (@oberbichler @roigcarlo ) and with python2 (@maceligueta @adityaghantasala )
Thanks for your help

@adityaghantasala
Copy link
Member

I am working and using this branch extensively as I write here. I will soon summarize my experience here. As of now for me it looks ok. except for a few points (supporting recursively including of subfolders ).

@pooyan-dadvand
Copy link
Member

Does it work with python 2.7?

@oberbichler
Copy link
Contributor

Works on Windows 10/Python 3.6

@philbucher
Copy link
Member Author

@maceligueta could you please try with python 2.7?

@adityaghantasala
Copy link
Member

@philbucher I get an error with python2.7 .. I think it not related to the imports but some other ...

Traceback (most recent call last):
  File "python-modules-test.py", line 12, in <module>
    import KratosMultiphysics
  File "/home/aditya/software/kratos_python2/kratos/Kratos/KratosMultiphysics/__init__.py", line 13, in <module>
    from Kratos import *
ImportError: /home/aditya/software/kratos_python2/kratos/Kratos/libs/Kratos.so: undefined symbol: _ZN6Kratos25SimpleMortarMapperProcessILi3ELi4ENS_8VariableINS_8array_1dIdLm3EEEEELNS_16HistoricalValuesE1ELS5_0EE7ExecuteEv

@philbucher
Copy link
Member Author

philbucher commented Nov 13, 2018

We checked in personl, seems like @adityaghantasala currently cannot get his python2 installation running (unrelated to this PR)

@maceligueta it would be great if you (or someone else that has python2) can try this out 👍

@maceligueta
Copy link
Member

maceligueta commented Nov 14, 2018

I just compiled the branch and tested it. I tried the tests, by doing
kratos/kratos/python_scripts> python run_tests.py
but got this typical error for Python 2.7:

/home/maceli/kratos/kratos/tests/test_KratosCore.py
Traceback (most recent call last):
  File "/home/maceli/kratos/kratos/tests/test_KratosCore.py", line 12, in <module>
    import test_model_part_io
  File "/home/maceli/kratos/kratos/tests/test_model_part_io.py", line 9, in <module>
    import KratosMultiphysics.StructuralMechanicsApplication as StructuralMechanicsApplication
  File "/home/maceli/kratos/KratosMultiphysics/StructuralMechanicsApplication/__init__.py", line 10, in <module>
    application_importer.ImportApplication(application,application_name,application_folder,caller, __path__)
  File "/home/maceli/kratos/KratosMultiphysics/application_importer.py", line 21, in ImportApplication
    raise RuntimeError(msg)

File "/home/maceli/kratos/KratosMultiphysics/application_importer.py", line 21, in ImportApplication
    raise RuntimeError(msg)
RuntimeError: 
***
*    Python file /home/maceli/kratos/kratos/tests/test_model_part_io.py
*    requires KratosStructuralMechanicsApplication
*    Please import it from your main Python script, /home/maceli/kratos/kratos/tests/test_KratosCore.py
*    If your main Python script is already importing it, you may need to re-order the imports
***

The same for the Fluid Dynamics:

RuntimeError: 
***
*    Python file /home/maceli/kratos/applications/FluidDynamicsApplication/tests/artificial_compressibility_test.py
*    requires KratosExternalSolversApplication
*    Please import it from your main Python script, /home/maceli/kratos/applications//FluidDynamicsApplication/tests/test_FluidDynamicsApplication.py
*    If your main Python script is already importing it, you may need to re-order the imports
***

Solid Mechanics:

RuntimeError: 
***
*    Python file /home/maceli/kratos/applications/SolidMechanicsApplication/python_scripts/MainSolid.py
*    requires KratosExternalSolversApplication
*    Please import it from your main Python script, /home/maceli/kratos/applications//SolidMechanicsApplication/tests/test_SolidMechanicsApplication.py
*    If your main Python script is already importing it, you may need to re-order the imports
***

A different one from Structural Mechanics:

File "/home/maceli/kratos/applications//StructuralMechanicsApplication/tests/test_StructuralMechanicsApplication.py", line 43, in <module>
    from test_loading_conditions_line import TestLoadingConditionsLine as TTestLoadingConditionsLine
  File "/home/maceli/kratos/applications/StructuralMechanicsApplication/tests/test_loading_conditions_line.py", line 43
SyntaxError: Non-ASCII character '\xc2' in file /home/maceli/kratos/applications/StructuralMechanicsApplication/tests/test_loading_conditions_line.py on line 43, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details

Constitutive Models Application:

======================================================================
ERROR: test_execution (ValidationTests.Shear_Test_Von_Misses_Model)
----------------------------------------------------------------------
Traceback (most recent call last):
  File "/home/maceli/kratos/applications/ConstitutiveModelsApplication/tests/TestFactory.py", line 31, in test_execution
    self.test.Run()
  File "/home/maceli/kratos/applications/ConstitutiveModelsApplication/python_scripts/MainMaterial.py", line 62, in Run
    self.Initialize()
  File "/home/maceli/kratos/applications/ConstitutiveModelsApplication/python_scripts/MainMaterial.py", line 77, in Initialize
    self._construct_material_processes(self.materials)
  File "/home/maceli/kratos/applications/ConstitutiveModelsApplication/python_scripts/MainMaterial.py", line 171, in _construct_material_processes
    return (process_factory.KratosProcessFactory(self.model).ConstructListOfProcesses(material_parameters))
  File "/home/maceli/kratos/kratos/python_scripts/process_factory.py", line 14, in ConstructListOfProcesses
    kratos_module = __import__(item["kratos_module"].GetString())
ImportError: No module named ConstitutiveModelsApplication

After this, the tests hang up during PfemApplication tests. :(

Sorry if some of these errors are my bad!

@philbucher
Copy link
Member Author

@maceligueta yes it is possible that you had to import these apps, I didn't think abt that at the time.
This does not depend on the changes of this PR though
Thanks for testing!

Iit seems to work for the configurations we are having
@KratosMultiphysics/technical-committee I guess it is worth taking a closer look now :D

@philbucher
Copy link
Member Author

ping @KratosMultiphysics/technical-committee

jcotela
jcotela previously approved these changes Nov 30, 2018
Copy link
Member

@jcotela jcotela left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The technical committee approves. We still need to decide how to treat subfolders, but that is a separate discussion.

Attention everyone: there may be some runtime issues when switching between branches that have/don't have this change. If python complains about being unable to import some application, please delete the KratosMultiphysics folder and reinstall.

@adityaghantasala
Copy link
Member

After merging this PR all the applications should be changed to adapt the CMakeLists file or ?
Will these changes be made as a part of this PR or as individual PRs ?

@philbucher
Copy link
Member Author

@adityaghantasala please leave this PR clean, I will merge it tmr
How to proceed we can discuss in person

@adityaghantasala
Copy link
Member

@philbucher sure we can discuss in person ! But ya I wanted to know about other applications as (StructuralMechanicsApplciation) is changed here ...

@jcotela
Copy link
Member

jcotela commented Dec 3, 2018

@philbucher probably it would be smart to write a short how-to (here or on the wiki) so that people can port their applications on their own.

@philbucher
Copy link
Member Author

@philbucher probably it would be smart to write a short how-to (here or on the wiki) so that people can port their applications on their own.

sure, will do

Copy link
Member

@jcotela jcotela left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Re-approving.

@philbucher
Copy link
Member Author

wiki added:
https://github.com/KratosMultiphysics/Kratos/wiki/Applications-as-python-modules

@philbucher
Copy link
Member Author

I will merge this now
If anyone has problems please let me know

I suggest to wait one or two days until using this in other places, just to see if it has some unwanted side-effects

NOTE the structuring of subfolders was not yet discussed, I suggest to wait a bit more and let the @KratosMultiphysics/technical-committee decide how the subfolders should look like

@ddiezrod
Copy link
Contributor

ddiezrod commented Dec 6, 2018

FYI @KratosMultiphysics/altair

1 similar comment
@ddiezrod
Copy link
Contributor

ddiezrod commented Dec 6, 2018

FYI @KratosMultiphysics/altair

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.