Scripting with the engine¶
The calculation engine, xdfem2d, is a Python library that needs no interface: it builds a model, solves it and gives
back numbers. Everything the application does with a model can be done from a script, and a model written by a script can
be opened in the application (and the other way round: the application exports its models as scripts).
pip install xdfem2d
The engine needs Python 3.12 or newer, and uses numpy, scipy and eurocodepy. It is distributed under the LGPLv3.
A first script¶
A simply supported beam of 5 m with 10 kN/m:
from xdfem2d import Structure2D
s = Structure2D(domain='plane')
s.add_material('C30', elastic_modulus=30e6, unit_weight=25.0)
s.add_section('S1', material_name='C30', b=0.3, h=0.6)
s.add_node('N1', 0.0, 0.0)
s.add_node('N2', 5.0, 0.0)
s.add_bar_element('E1', node_i='N1', node_j='N2', section_name='S1')
s.add_support('PIN', ux=True, uy=True)
s.add_support('ROLLER', uy=True)
s.assign_support('N1', 'PIN')
s.assign_support('N2', 'ROLLER')
s.add_load_case('LC1', self_weight_factor=0.0)
s.add_distributed_load('E1', 'LC1', fye=-10.0, fyd=-10.0) # 10 kN/m downward
r = s.calculate()
print(r['reactions']['LC1']['N1']) # [Rx, Ry, Mz]: Ry = 25 kN
s.save('beam.x2d') # opens in the application
The same model with the shortcuts (create_*), which have sensible defaults and fewer arguments:
s = Structure2D(domain='plane')
n = s.create_node_list([0, 0, 5, 0])
sec = s.create_rc_section('B30x60', 0.30, 0.60)
s.create_bar_element(sec, n[0], n[1])
s.pin(n[0]); s.roller(n[1])
s.create_uniform_load(s.bar_elements[0], 'LC1', 10.0) # 10 kN/m downward
r = s.calculate()
The reference for every call is in the three pages of the Python API:
building a model, loads and analysis and
results and design, and the complete list of calls with their full signatures. Each call also has a docstring: help(Structure2D.add_section). The recipes, short and with one task each, are the
Recipes.
Conventions¶
These apply to every call of the API.
- Units are metres, kN, kN·m and kN/m². Mass is in tonnes. Angles are in degrees where an angle is named.
- A model has a domain, fixed for its life:
plane(frames and walls:ux,uy,θz) orplate(slabs and grillages:w,θx,θy). The three degrees of freedom of a node keep the same positions in both, only their meaning changes. In a plate model the aliasesfz,mx,my(loads),w,tx,ty(supports) andkz,ktx,kty(springs) fill the same slots asfx,fy,mz,ux,uy,tzandkx,ky,kt. See Domains. - Signs. A load takes a positive value in the direction of the axis, so gravity is negative.
create_*andadd_*. Most things can be built two ways. Acreate_*shortcut takes a magnitude or a name, has sensible defaults, and where it makes sense gets or creates (two calls for the same concrete class give one material). Prefer it. Theadd_*call it forwards to spells every field out: use it when a shortcut cannot say what you need (a load in a local frame with both components, a trapezoidal axial load). Each page of the API lists the shortcuts and the fundamental calls of a topic together.- Ids. A
create_*call gives an id when you do not:"1","2"for nodes,"B1"for bars,"T1"and"Q1"for triangles and quads,"LC1"for load cases,"AC1"for analysis cases. Element ids are unique across kinds. Where a call takes a node, a section or an element, it accepts the object another call returned or its id as text. - Geometry object, surface object and region all mean an item of
geometry_objects(a rectangle, a polygon, a segment, …), discretised into bars, triangles or quads when the model is solved. The canonical name is geometry object. - Errors. A call that cannot do what it says raises, and changes nothing. A model that cannot be analysed is reported by
check_references(),domain_problems()andxdfem2d.workflows.validate_for_analysis(struc). The removal calls (delete_*) are forgiving: removing what is not there does nothing.
Names and aliases¶
Every name below still works as it always has, and none is deprecated: they are permanent aliases.
| Still works | Canonical name | |
|---|---|---|
add_rc_section |
create_rc_section |
same signature |
add_steel_section |
create_steel_section |
same signature |
add_panel, create_panel |
create_polygon |
same signature |
add_geo_line |
add_geo_segment |
|
add_geo_polyline |
add_geo_multisegment |
|
add_geo_surface |
add_geo_polygon |
|
the keyword tri_section_name= |
section_name= |
on add_geo_rectangle and add_geo_polygon; the stored attribute and the file keep the old name |
the keywords element_id=, delta_t_uniform=, delta_t_gradient= |
bar_id=, dt_uniform=, dt_gradient= |
on add_temperature_load |
The add_* calls of the fundamental layer (add_material, add_section, add_node, add_bar_element, add_tri_element,
add_quad_element, and every other add_* of loads, supports and cases) are not aliases: the create_* shortcuts forward to them.
Six calls were renamed outright, with no alias: add_bar_load, add_bar_point_load, add_node_load, add_pressure,
add_self_weight and add_temperature are now create_uniform_load (for the first and the fourth), create_bar_point_load,
create_node_load, create_self_weight and create_temperature.
Examples¶
The examples/ folder of the repository has runnable scripts, each checked against a closed-form solution where one exists:
example.py (a simply supported beam), example-slab.py, example-slab-mitc3.py, example-slab-quad.py,
example-grillage.py and example-grillage-cantilever.py for plate models, example-cuts.py for section cuts and
example-sequences.x2d for a construction sequence.
From the application¶
- File ▸ Export… with the file type Python script writes the open model as a commented script that rebuilds it. See Exporting.
- The Script editor (File ▸ Script editor…) runs a script against the open model.
- The files the engine reads and writes,
.x2dand.json, are described on File Formats and JSON data structure. - The Assistant, the Agent and the MCP server drive the same model through a short command language, without a script.