Skip to content

Applications as python modules

Philipp Bucher edited this page Jan 22, 2019 · 8 revisions

https://github.com/KratosMultiphysics/Kratos/pull/3217/ enables the applications can be used as python-modules.

This means that the python-scripts in the Applications can now be used e.g. as:

# old:
import KratosMultiphysics
import analysis_stage # this works because the folder "python_scripts" is added to the python_path

# new, pythonic way
from KratosMultiphysics import analysis_stage
import KratosMultiphysics.FluidDynamicsApplication.navier_stokes_solver_vmsmonolithic import NavierStokesSolverMonolithic

The new way is the standard already for new applications created with the Application-Generator. For already existing applications currently both ways are supported, but it is recommended to the developers to use the new way (pythonic way) of importing python-scripts because at some point the old import-mechanism (which adds the python_scripts to the python-path) will be removed eventually, and then all python-files should be using the new way already.

The following three steps are recommended for developers:

1. Modify the CMakeLists.txt in your Application (replacing DummyApplication with the real name of your Application):

Change the location where the DummyApplication.py is being installed to:

  • from: install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/DummyApplication.py" DESTINATION KratosMultiphysics )
  • to install(FILES "${CMAKE_CURRENT_SOURCE_DIR}/DummyApplication.py" DESTINATION "KratosMultiphysics/DummyApplication" RENAME "__init__.py")

2. The second step depends if a large Application (which needs backwards-compatibility) or a small Application (where backwards-compatibility is not a problem e.g. because only few people use it)/Applications that have no python-files

Small Applications or Applications with no python-files (all Applications as a long-term goal)

The DummyApplication.py should look like this:

# makes KratosMultiphysics backward compatible with python 2.6 and 2.7
from __future__ import print_function, absolute_import, division

# Application dependent names and paths
import KratosMultiphysics as KM
from KratosDummyApplication import *
application = KratosDummyApplication()
application_name = "KratosDummyApplication"
application_folder = "DummyApplication"

KM._ImportApplicationAsModule(application, application_name, application_folder, __path__)

Large Applications (intermediate for transition-period)

The DummyApplication.py should look like this:

# makes KratosMultiphysics backward compatible with python 2.6 and 2.7
from __future__ import print_function, absolute_import, division

from KratosDummyApplication import *
application = KratosDummyApplication()
application_name = "KratosDummyApplication"
application_folder = "DummyApplication"

# The following lines are common for all applications
from .. import application_importer
import inspect
caller = inspect.stack()[1] # Information about the file that imported this, to check for unexpected imports
application_importer.ImportApplication(application,application_name,application_folder,caller, __path__)

3. Update all python-files to use the new/pythonic import-mechanism

As shown above

The changes result in the following structure in the KratosMultiphysics-Folder:

previously:

KratosMultiphysics
 |-- __init__.py
 |-- application_importer.py    
 |-- kratos_globals.py
 |-- ... (other kratos files)
 |-- DummyApplication.py

new:

KratosMultiphysics
 |-- __init__.py
 |-- application_importer.py    
 |-- kratos_globals.py
 |-- ... (other kratos files)
 |-- DummyApplication
      |-- __init__.py

Project information

Getting Started

Tutorials

Developers

Kratos structure

Conventions

Solvers

Debugging, profiling and testing

HOW TOs

Utilities

Kratos API

Kratos Structural Mechanics API

Clone this wiki locally