Hi,
Time for me to explore Sfepy, again! Examples on the website are great, but I would prefer working in more familiar and interactive environments, like Jupiter notebooks. If anyone here has worked in such environments and would share a notebook to start with, it would be greatly appreciated.
Thanks!
Serge-Étienne Parent
Hi,
Try this:
SFEPY
!conda update -n base conda -y
!conda update --all --yes
!conda config --add channels conda-forge
!conda install vtk mayavi -y
!conda install -c conda-forge sfepy -y
!jupyter nbextension install --py mayavi --user
!jupyter nbextension enable mayavi --user --py
!conda install qt pyqt -y !conda install scikit-umfpack -y
Create Mesh
!pip install gmsh In [1]:
import gmsh gmsh.initialize() gmsh.option.setNumber("General.Terminal", 1) gmsh.model.add("Ex_p51_api") p1 = gmsh.model.geo.addPoint( 0, 0, 1.0) p2 = gmsh.model.geo.addPoint(75, 0, 1.0) p3 = gmsh.model.geo.addPoint(0, 75, 1.0) c1 = gmsh.model.geo.addCircleArc(p2, p1, p3) l1 = gmsh.model.geo.addLine(p1,p2) l2 = gmsh.model.geo.addLine(p3,p1) l3 = gmsh.model.geo.addCurveLoop([c1, l1, l2]) s1 = gmsh.model.geo.addPlaneSurface([l3]) gmsh.model.geo.synchronize() gmsh.model.mesh.generate(2) #2D gmsh.option.setNumber("Mesh.SaveAll", 1) gmsh.write("Ex_p51_original.mesh") #gmsh.fltk.run() #pour visualiser le résultat gmsh.finalize()
Convert Mesh for Sfepy
In [2]:
import os python_prog = '"/home/francois/anaconda3/bin/python"' convert_2d_prog = ("/home/francois/anaconda3/lib/python3.8/site-packages/sfepy/"+ "script/convert_mesh.py --2d") ordre = " ".join([python_prog,convert_2d_prog,"Ex_p51_original.mesh", "Ex_p51.mesh"]) os.system(ordre)
Out[2]:
0
SfePy
Define problem
In [3]:
def define(): from sfepy.mechanics.matcoefs import stiffness_from_youngpoisson from sfepy.mechanics.matcoefs import lame_from_youngpoisson from sfepy.discrete.fem.utils import refine_mesh from sfepy import data_dir # Fix the mesh file name if you run this file outside the SfePy directory. filename_mesh = "Ex_p51.mesh" refinement_level = 0 filename_mesh = refine_mesh(filename_mesh, refinement_level) output_dir = '.' # set this to a valid directory you have write access to young = 2000.0 # Young's modulus [MPa] poisson = 0.4 # Poisson's ratio options = { 'output_dir' : output_dir, } regions = { 'Omega' : 'all', 'Left' : ('vertices in (x < 0.001)', 'facet'), 'Bottom' : ('vertices in (y < 0.001)', 'facet'), 'Top' : ('vertex 2', 'vertex'), } materials = { 'Asphalt' : ({'D': stiffness_from_youngpoisson(2, young, poisson)},), 'Load' : ({'.val' : [0.0, -1000.0]},), } fields = { 'displacement': ('real', 'vector', 'Omega', 1), } equations = { 'balance_of_forces' : """dw_lin_elastic.2.Omega(Asphalt.D, v, u) = dw_point_load.0.Top(Load.val, v)""", } variables = { 'u' : ('unknown field', 'displacement', 0), 'v' : ('test field', 'displacement', 'u'), } #conditions limites ebcs = { 'XSym' : ('Bottom', {'u.1' : 0.0}), 'YSym' : ('Left', {'u.0' : 0.0}), } solvers = { 'ls' : ('ls.scipy_direct', {}), 'newton' : ('nls.newton', { 'i_max' : 1, 'eps_a' : 1e-6, }), } return locals()
In [4]:
import inspect lines = inspect.getsource(define) with open("definition.py","w") as f: f.write(lines)
Solve problem
In [5]:
from sfepy.discrete import Problem problem = Problem.from_conf_file("definition.py")
#Setup output directory according to options above. problem.setup_default_output() problem.solve()
sfepy: left over: ['stiffness_from_youngpoisson', 'lame_from_youngpoisson', 'refine_mesh', 'data_dir', 'refinement_level', 'output_dir', 'young', 'poisson', 'verbose', '_filename'] sfepy: reading mesh (/mnt/c/Users/Francois/Notebooks/Python/Ex_p51.mesh)... sfepy: number of vertices: 76 sfepy: number of cells: sfepy: 2_3: 122 sfepy: ...done in 0.01 s sfepy: creating regions... sfepy: Omega sfepy: Left sfepy: Bottom sfepy: Top sfepy: ...done in 0.01 s sfepy: equation "balance_of_forces": sfepy: dw_lin_elastic.2.Omega(Asphalt.D, v, u) = dw_point_load.0.Top(Load.val, v) sfepy: using solvers: ts: no ts nls: newton ls: ls sfepy: updating variables... sfepy: ...done sfepy: setting up dof connectivities... sfepy: ...done in 0.00 s sfepy: matrix shape: (134, 134) sfepy: assembling matrix graph... sfepy: ...done in 0.00 s sfepy: matrix structural nonzeros: 1592 (8.87e-02% fill) sfepy: updating variables... sfepy: ...done sfepy: updating materials... sfepy: Asphalt sfepy: Load sfepy: ...done in 0.00 s sfepy: nls: iter: 0, residual: 1.000000e+03 (rel: 1.000000e+00) sfepy: residual: 0.02 [s] sfepy: matrix: 0.00 [s] sfepy: solve: 0.01 [s] sfepy: nls: iter: 1, residual: 6.306955e-12 (rel: 6.306955e-15) sfepy: solved in 1 steps in 0.05 seconds
Out[5]:
State
In [6]:
from sfepy.base.base import output output('results saved in %s' % problem.get_output_name(suffix = '*'))
sfepy: results saved in ./Ex_p51.*.vtk
View
In [ ]:
from sfepy.postprocess.viewer import Viewer view = Viewer(problem.get_output_name()) view(vector_mode='warp_norm', rel_scaling=2,is_scalar_bar=True,is_wireframe=True)
In [ ]:
Le 07/05/2021 à 15:28, Serge-Étienne Parent a écrit :
Hi,
Time for me to explore Sfepy, again! Examples on the website are great, but I would prefer working in more familiar and interactive environments, like Jupiter notebooks. If anyone here has worked in such environments and would share a notebook to start with, it would be greatly appreciated.
Thanks!
Serge-Étienne Parent
SfePy mailing list -- sfepy@python.org To unsubscribe send an email to sfepy-leave@python.org https://mail.python.org/mailman3/lists/sfepy.python.org/ Member address: francoisbrest@hotmail.fr
Hi Serge-Étienne,
On 07. 05. 21 15:28, Serge-Étienne Parent wrote:
Hi,
Time for me to explore Sfepy, again! Examples on the website are great, but I would prefer working in more familiar and interactive environments, like Jupiter notebooks. If anyone here has worked in such environments and would share a notebook to start with, it would be greatly appreciated.
See the message from Francois, and check also the examples [1] that have "interactive" in the name - those use the imperative way of creating problems, suitable for interactive work.
r.
participants (3)
-
francois
-
Robert Cimrman
-
Serge-Étienne Parent