Page MenuHomec4science

manual-python.tex
No OneTemporary

File Metadata

Created
Tue, Dec 24, 17:41

manual-python.tex

\chapter{Python interface\index{Python}\label{sect:python}}
Akantu as a python interface which allows to implement a finite
element case entirely in the Python language. The interface is
currently in \textcolor{red}{beta} version and bugs (including memory
leaks) can be expected.
The philosophy of the python wrappers is that it should follow, as
much as possible, the C++ interface. This is made possible thanks to
the \href{SWIG}{} software which can port C++ interfaces to Python.
However, the possibilities of the Python language have some
restrictions which do not allow the concept of template. For this
reason the templated classes and methods are currently hardly
supported in the Akantu Python interface (some are, but without any
warranty that the interface shall not change in a near future).
In order to use the python interface you want to import the
\code{akantu} module which is possibly located in directory where you
built or where you installed \akantu. The easiest way is to source
the file \code{akantu\_environement.sh} in your terminal environment:
\begin{command}
source AKANTU_BUILD_DIR/akantu_environement.sh
\end{command}
or for an installed akantu version:
\begin{command}
source AKANTU_INSTALL_DIR/share/akantuVERSION/akantu_environement.sh
\end{command}
Then, you can create a Python script, like \code{script.py} which
starts with:
\begin{python}
import akantu
\end{python}
Then, you can write the initialization sequence just like for the C++
case:
\begin{python}
akantu.initialize('materials.dat') spatial_dimension = 2
mesh = akantu.Mesh(spatial_dimension) mesh.read('mesh.msh')
model = akantu.SolidMechanicsModel(mesh)
model.initFull(_analysis_method = akantu._static)
\end{python}
Then for the dumpers declaration:
\begin{python}
model.setBaseName("example") model.addDumpFieldVector("displacement")
model.addDumpFieldVector("force") model.addDumpField("boundary")
model.addDumpField("strain") model.addDumpField("stress")
model.addDumpField("blocked_dofs")
\end{python}
The \akantu arrays can be retrieved at all time. For convenience there
is an automatic trans-typing operator which allows to use \akantu
Array, Vectors and Matrix as mere \textbf{Numpy}s.
\begin{python}
displacement = model.getDisplacement()
\end{python}
There is no copy made and the \textbf{Numpy} vectors are wrapped
around \akantu's memory pool.
\begin{python}
displacement[:,:] = 1.
displacement[0,1] = 10.
\end{python}
Also to assign an entire vector to another the '[]' operator must be
used:
\begin{python}
result = numpy.linspace(0.,1.,100)
# This works
displacement[:] = result[:]
# This does not
displacement = result
\end{python}
Then you can solve the problem for instance with:
\begin{python}
model.solveStaticDisplacement(1e-10,2);
\end{python}
And dump to Paraview:
\begin{python}
model.dump()
\end{python}
Do not forget to finalize at the end of the script:
\begin{python}
akantu.finalize()
\end{python}
Finally the python script can be launched with:
\begin{command}
python ./script.py
\end{command}
\vspace{1cm}
\textcolor{red}{Warning!} : We recall once more that the Python
interface is in \textbf{beta} state and is provided only for its nice
services: it avoids recompilation of akantu when coding the main function.
Several preliminary examples are provided
in the \code{examples} directory where it is described how to specify
boundary conditions and also constitutive behavior.

Event Timeline