How to import Kratos from Python using the KratosMultiphysics module

From KratosWiki
Revision as of 09:42, 2 April 2012 by Jcotela (Talk | contribs)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to: navigation, search

Contents

Changing the main Python script

Import applications using the new module

The main difference between the KratosMultiphysics module and the old import style is that applications_interface is no longer used and that the Kernel is initialized internally by the module, so it is no longer required to initialize it in the Python script. Updating your main script to use the new format should be straightforward. Consider this script using the existing applications interface format:

 ##################################################################
 ##################################################################
 ## ATTENTION: here the order is important
 
 #including kratos path
 kratos_libs_path            = fluid_only_var.kratos_path + '/libs' ##kratos_root/libs
 kratos_applications_path    = fluid_only_var.kratos_path + '/applications' ##kratos_root/applications
 kratos_benchmarking_path = fluid_only_var.kratos_path +'/benchmarking' ##kratos_root/benchmarking
 import sys
 sys.path.append(kratos_libs_path)
 sys.path.append(kratos_applications_path)
 sys.path.append(kratos_benchmarking_path)
 
 import benchmarking
 
 #importing Kratos main library
 from Kratos import *
 kernel = Kernel()   #defining kernel
 
 #importing applications
 import applications_interface
 applications_interface.Import_IncompressibleFluidApplication = True
 applications_interface.Import_ExternalSolversApplication = True
 applications_interface.ImportApplications(kernel, kratos_applications_path)
 
 ## from now on the order is not anymore crucial
 ##################################################################
 ##################################################################
 from KratosIncompressibleFluidApplication import *
 from KratosExternalSolversApplication import *

The equivalent import using the KratosMultiphysics module is

 # including path to KratosMultiphysics
 import sys
 sys.path.append(fluid_only_var.kratos_path)
 
 # importing kratos
 from KratosMultiphysics import *
 from KratosMultiphysics.IncompressibleFluidApplication import *
 from KratosMultiphysics.ExternalSolversApplication import *

The order in which the applications are imported is not important, but they should be imported before we start defining the solvers or reading the model part.

In general, the name used to import the applications is the same as the one used now to import the .so, but adding the "Multiphysics." part. For example:

 from KratosStructuralApplication import * --> from KratosMultiphysics.StructuralApplication import *
 from KratosPFEMApplication import *       --> from KratosMultiphysics.PFEMApplication import *

A note about benckmarks

The benchmarking tools are not integrated in the KratosMultiphysics module, so the import of the benchmarking module should be left as usual:

 kratos_benchmarking_path = fluid_only_var.kratos_path +'/benchmarking' ##kratos_root/benchmarking
 sys.path.append(kratos_benchmarking_path)
 import benchmarking

If you need mpi

An additional feature of the KratosMultiphysics module is that it provides a minimal set of instructions to use MPI from Python, which will allow us to drop the dependency on Boost's Python MPI interface. If you are using MPI, it is strongly recommended that you update your code to use the new MPI interface. To do this, replace the existing imports of mpi:

 import mpi

or

 try:
   import boost.mpi as mpi
 except ImportError:
   import mpi

with

 from KratosMultiphysics.mpi import *

This will provide an object called "mpi" that can be used to perform basic mpi operations (currently rank, size, barrier, gather, allgather). In principle, no further changes are required to use it in existing code, but note that the capabilities of this object are much more limited that the ones in boost.mpi.

Changes in the Python solvers

All Python solver have to be updated to use the KratosMultiphysics module

Import applications using the new module

Check that the module was also used in the main script

Optional imports in particular functions

Adding new applications

Personal tools
Categories