Python API: building a model¶
How to build a model in code: materials, sections, nodes, elements, geometry objects, supports, springs, constraints and fields. Loads and analysis are on Python API: loads and analysis; solving, reading the results, designing and editing are on Python API: results and design. The conventions that apply to all of them, and an end-to-end example, are on Scripting with the engine.
The signatures on this page are written from the engine, so they are complete: every parameter, with its type and default. Python API: reference lists every call.
| To create… | Shortcut | Fundamental |
|---|---|---|
| A material | create_rc_material, create_steel_material, create_timber_material |
add_material, add_concrete_material |
| A section | create_bar_section, create_area_section, create_rc_section, create_steel_section, create_timber_bar_section |
add_section, add_tri_section, add_plate_section, add_quad_section, add_area_section |
| A node, a bar, a triangle or a quad | create_node, create_node_list, create_bar_element, create_area_element |
add_node, add_bar_element, add_tri_element, add_quad_element, add_area_element |
| A wall or a slab region | create_polygon (alias create_panel) |
add_geo_rectangle, add_geo_polygon |
| A line or an arc | add_geo_segment, add_geo_arc, add_geo_arc_3pts, add_geo_multisegment |
|
| A support | pin, fix, roller, symm, create_support; on an edge support_edge and its relatives |
add_support, assign_support |
| A spring | add_node_spring, add_element_spring, add_line_element_spring, add_area_spring |
|
| A constraint or a field | add_rigid_link, add_equal_dof, add_constraint_equation, add_field |
|
| A whole standard structure | create_from_template |
Materials¶
Convenience calls (create_*)¶
create_material(
name: str, elastic_modulus: float, unit_weight: float, alpha: float = 1e-05, unit_mass=None,
material_type=None, design=None, poisson: float = 0.2) -> Material
# == add_material, pure alias
create_rc_material(
concrete: str = 'C30/37', steel: str = 'B500B', name: str | None = None) -> Material
# get-or-create, deduped by name
create_steel_material(steel: str = 'S275', name: str | None = None) -> Material
create_timber_material(timber: str = 'C24', name: str | None = None) -> Material
name=None derives the name from the class string (e.g. "C30/37").
Get-or-create: two calls with the same class dedupe to one Material.
Fundamental calls (add_*)¶
add_material(
name: str, elastic_modulus: float, unit_weight: float, alpha: float = 1e-05, unit_mass=None,
material_type=None, design=None, poisson: float = 0.2) -> Material
add_concrete_material(
material_name: str, concrete_class: str, steel_class: str, gamma_c=1.5, gamma_s=1.15,
alpha_cc=1.0) -> ConcreteMaterial
add_concrete_material links EC2 design metadata onto an already-existing
material by name — a different job from create_rc_material, which makes
the physical material itself.
Sections¶
Convenience calls (create_*)¶
create_bar_section(name: str, material_name: str | Material, b: float, h: float, **kw) -> Section
# material_name resolves an unrecognised Eurocode class automatically first
create_area_section(
name: str, material_name: str | Material, thickness: float = 0.2, **kw) -> TriSection
# domain-aware pair: plane -> CST (tri) / QM6 (quad), plate -> MITC3 (tri) / MITC4 (quad)
# add_area_section under the hood: a same-named TriSection AND QuadSection,
# so create_area_element resolves whichever shape it ends up building
create_rc_section(
name: str, b: float, h: float, concrete: str = 'C30/37', steel: str = 'B500B',
cover: float = 0.045) -> Section
# material + section, one call
create_steel_section(name: str, profile: str, grade: str = 'S275') -> Section
# profile e.g. "IPE300"
create_timber_bar_section(
name: str, b: float, h: float, timber: str = 'C24', service_class: str = 'SC1') -> Section
# no add_* predecessor
create_rc_section/create_steel_section/create_timber_bar_section are
the canonical names for a material-plus-section shortcut per family;
add_rc_section/add_steel_section are kept working as aliases (see Names and aliases).
Fundamental calls (add_*)¶
add_section(
name: str, material_name: str, b: float, h: float, area_override=None, inertia_override=None,
profile_name=None, section_type=None, rc_cover=0.045, rc_alpha_s=90.0, rc_bar_phi=16.0,
shape=None, tw=0.0, tf=0.0, rc_shear_min=True, torsion_override=None, angle=0.0,
inertia_minor_override=None, wel_y_override=None, wpl_y_override=None, wel_z_override=None,
wpl_z_override=None, av_y_override=None, av_z_override=None, warping_override=None,
wt_override=None, curve_y_override=None, curve_z_override=None, timber_service_class='SC1',
is_column: bool = False, rc_phi_ef: float = 2.0, rc_n_bars: int = 4, rc_n_bars_y: int = 2,
rc_n_bars_z: int = 2, rc_second_order_method: str = 'nominal_curvature',
rc_torsion_distribution: str = 'top_bottom', beam_overrides: Optional[dict] = None) -> Section
# beam / grid section (J)
add_tri_section(
name: str, material_name: str, thickness: float = 0.1, plane_strain: bool = False,
formulation: str = 'CST', rc_cover: float = 0.045, rc_alpha_s: float = 90.0,
rc_bar_phi: float = 16.0, rc_shear_min: bool = True, rc_cover_top_x: float = None,
rc_cover_top_y: float = None, rc_cover_bot_x: float = None,
rc_cover_bot_y: float = None) -> TriSection
# panel
add_plate_section(
name: str, material_name: str, thickness: float = 0.2, formulation: str = 'MITC3',
**kw) -> TriSection
# slab
add_quad_section(
name: str, material_name: str, thickness: float = 0.1, plane_strain: bool = False,
formulation: str = 'MITC4', rc_cover: float = 0.045, rc_alpha_s: float = 90.0,
rc_bar_phi: float = 16.0, rc_shear_min: bool = True, rc_cover_top_x: float = None,
rc_cover_top_y: float = None, rc_cover_bot_x: float = None,
rc_cover_bot_y: float = None) -> QuadSection
# 4-node area section. formulation: "Q4"/"QM6" (plane wall) | "MITC4"/"DKT4" (plate slab)
add_area_section(
name: str, material_name: str, thickness: float = 0.1, plane_strain: bool = False,
formulation: str = 'CST', quad_formulation: str = 'QM6', rc_cover: float = 0.045,
rc_alpha_s: float = 90.0, rc_bar_phi: float = 16.0, rc_shear_min: bool = True,
rc_cover_top_x: float = None, rc_cover_top_y: float = None, rc_cover_bot_x: float = None,
rc_cover_bot_y: float = None) -> TriSection
# add_tri_section + add_quad_section under one name; returns the TriSection,
# the QuadSection is self.quad_sections[name]. A bad quad_formulation rolls
# the tri half back rather than leaving an unpaired section.
The quad section fields mirror the triangle section's; a wall uses "QM6"
(recommended) or "Q4", a slab uses "MITC4" (recommended) or "DKT4". Edge,
area (pz), temperature and Winkler-spring loads all have quad targets, via the
same add_area_load / create_temperature / add_area_spring dispatchers (the
id resolves to the triangle or quad list) plus add_quad_edge_load /
add_quad_temperature_load.
material_name must already exist. These are what every create_* call above forwards to.
Nodes and elements¶
Convenience calls (create_*)¶
create_node(x: float, y: float, id: str | None = None) -> Node
# id auto-assigned: "1", "2", ...
create_node_list(outline, id_prefix: str | None = None) -> list[Node]
# flexible outline, see Geometry objects
create_bar_element(
section: str | Section, node_i: str | Node, node_j: str | Node, id: str | None = None,
**kw) -> BarElement
# id auto: "B1", "B2", ...; section first, then the nodes it applies to;
# node_i/node_j: id or Node; section: name or Section
create_area_element(
section: str | TriSection | QuadSection, node_i: str | Node, node_j: str | Node,
node_k: str | Node, node_l: str | Node | None = None, id: str | None = None,
**kw) -> TriElement | QuadElement
# id auto: "T1", "T2", ... (triangle) or "Q1", "Q2", ... (quad); section first;
# node_l is the only thing that decides triangle (3 nodes) vs quad (4 nodes) —
# one call covers both instead of picking add_tri_element/add_quad_element
# up front
id optional (auto-assigned), and node/section arguments accept the object
returned by another create_* call as well as a raw id/name. Section comes
first in both calls — the target it belongs to precedes the nodes it applies
to, one order across the whole create_* element layer:
n = s.create_node_list([0,0, 5,0, 10,0])
sec = s.create_rc_section("B30x60", 0.30, 0.60)
s.create_bar_element(sec, n[0], n[1])
Fundamental calls (add_*)¶
add_node(id: str, x: float, y: float) -> Node
add_bar_element(
id: str, node_i: str, node_j: str, section_name: str, hinge_i=False, hinge_j=False,
rc_design=False, rc_cover=0.025, rc_cotg_theta=1.0, rc_alpha_s=90.0, sd_ky=None, sd_kz=None,
sd_klt=None, sd_ltb=True, stage: int = 1, is_column: Optional[bool] = None,
beam: Optional[str] = None) -> BarElement
# Beam (plane) / Grid (plate)
add_tri_element(id: str, node_i: str, node_j: str, node_k: str, section_name: str) -> TriElement
# Panel/Slab
add_quad_element(
id: str, node_i: str, node_j: str, node_k: str, node_l: str, section_name: str) -> QuadElement
# 4-node area element; nodes counter-clockwise; Panel/Slab (see add_quad_section)
add_area_element(
id: str, node_i: str, node_j: str, node_k: str, node_l: str | None = None, *,
section_name: str) -> TriElement | QuadElement
# 3 nodes -> add_tri_element, 4 (node_l given) -> add_quad_element;
# section_name is keyword-only (its slot would otherwise shift depending
# on which branch runs)
Element ids are unique across kinds — a bar, a triangle and a quad may not share the same id. A quad is validated at creation (4 distinct nodes, counter-clockwise winding, convex, positive Jacobian); a bad quad raises rather than being silently repaired.
add_area_element dispatches to the other two: three nodes make a triangle, four a quad. section_name is keyword-only on it, because its position would otherwise shift with the branch.
Geometry objects (meshed at solve time)¶
Convenience call: create_polygon¶
create_polygon(
outline, thickness: float, material: str = 'C30/37', target_size: float = 0.5,
id: str | None = None)
# exactly 2 points -> opposite corners of a rectangle; 3 or more -> a polygon
A region and its section in one call: the domain picks a slab (plate) or a wall (plane). create_panel and add_panel are the same
call under older names. Keep the return: .node_ids are the real corner nodes (for a rectangle, counter-clockwise: bottom-left,
bottom-right, top-right, top-left), for example to put create_node_load on one specific corner. Named edges (support_edge,
below) work on this outline as long as the 4 points are given in that order.
material is an existing material name or a Material, or an unresolved Eurocode concrete class ("C30/37"), which is created for
you with its steel fixed at "B500B". id names the geometry object, and its section <id>_sec; it is "Panel1", "Panel2", … when
omitted.
outline (and any list of points, including create_node_list above) is flexible: [(x, y), …] pairs, a flat [x1, y1, x2, y2, …],
{"x": …, "y": …} dicts, or two columns [[x1, x2, …], [y1, y2, …]] (for 3 or more points). Except in the flat form, an entry may
instead be the id of an existing node, or a Node, mixed freely with coordinates.
Fundamental calls (add_*)¶
add_geo_segment(
id: str, x1: float, y1: float, x2: float, y2: float, section_name: str = '',
divisions: int = 1, max_chord: float = 0.0) -> GeoSegment
add_geo_arc(
id: str, cx: float, cy: float, radius: float, start_angle: float, end_angle: float,
section_name: str = '', divisions: int = 16, max_chord: float = 0.0) -> GeoArc
add_geo_arc_3pts(
id: str, p1, pm, p2, section_name: str = '', divisions: int = 16,
max_chord: float = 0.0) -> GeoArc
add_geo_multisegment(
id: str, vertices, closed: bool = False, section_name: str = '', divisions: int = 1,
max_chord: float = 0.0, span_divisions: list | None = None) -> GeoMultisegment
add_geo_rectangle(
id: str, corner_a, corner_c, section_name: str = '', target_size: float = 0.5,
prefer_quad: bool = True, *, tri_section_name: str | None = None) -> GeoRectangle
add_geo_polygon(
id: str, vertices, section_name: str = '', target_size: float = 0.5, prefer_quad: bool = True,
*, tri_section_name: str | None = None) -> GeoPolygon
remove_geo_object(id: str)
The old names add_geo_line / add_geo_polyline / add_geo_surface remain as
aliases of add_geo_segment / add_geo_multisegment / add_geo_polygon. For
a rectangle/polygon the section argument is section_name (the old keyword
tri_section_name= still works); the stored attribute and the .x2d field
keep the name tri_section_name.
For what an object is and how it is meshed, see Geometry objects.
A whole standard structure¶
create_from_template(kind: str, **params) -> Structure2D
# on an EMPTY model
The parametric structures of File ▸ New from Template… as one call, with plain parameters: the material is a name (material='C30/37',
'S275', 'C24'), the lengths are the ones the problem states (spans=[4, 5, 6]), and a profile is profile='IPE300'. Linear
structures come as nodes and bars, walls and slabs as one geometry object that is meshed when the model is solved. It fills an empty model
only, and validates everything before building anything; an error says what the call takes.
s = Structure2D(domain='plane')
s.create_from_template('continuous_beam', spans=[4, 5, 6])
The kinds are listed by xdfem2d.template_api.template_kinds(): beam, continuous_beam, portal_frame, frame, truss_warren, truss_howe, truss_pratt, truss_long, truss_kingpost, truss_town, arch, wall, slab, wall_beam, wall_frame, slab_ribbed, flat_slab, slab_on_grade, slab_sector, beam_grid, bridge_grillage. The parameters of each are in xdfem2d.template_api.TEMPLATE_KINDS, and
kind_signature(kind) prints one.
Supports¶
Convenience calls (create_*)¶
pin(nodes: str | Node | list)
# plane: ux,uy · plate: w
fix(nodes: str | Node | list)
# plane: ux,uy,θz · plate: w,θx,θy
roller(nodes: str | Node | list, free: str = 'x')
# plane only — frees one translation
symm(nodes: str | Node | list, axis: str = 'x')
# plate only — symmetry line (w free, θ about axis fixed)
create_support(
nodes: str | Node | list, ux=False, uy=False, tz=False, w=None, tx=None, ty=None,
name: str | None = None)
nodes: a single id/Node, or a list of either (same list-or-single rule as
loads). pin/fix/roller/symm cover the common cases with an
auto-assigned canonical support name ("PIN", "FIXED", "ROLLER-X", "SYM-X", …);
create_support is the generic one-call create-and-assign underneath them.
True fixes that DOF, False frees it — every flag defaults to False,
so create_support(nodes) on its own restrains nothing. A model that
means to fully fix a node has to say ux=True, uy=True, tz=True (or just
call fix(nodes)); writing all three explicitly as False "to be clear"
restrains nothing and turns the structure into a mechanism — a real mistake
a script has made. w/tx/ty are the plate-domain aliases of the same
three (ux,uy,tz) slots.
Fundamental calls¶
add_support(name: str, ux=False, uy=False, tz=False, w=None, tx=None, ty=None) -> Support
assign_support(node_id: str, support_name: str) -> SupportAssignment
The two calls every convenience wrapper above forwards to: create the named
restraint, then assign it to a node. Reach for these directly to reuse one
named Support across many assign_support calls without re-specifying its
flags each time.
On the edge of a region¶
support_edge(object_id: str, edge, kind: str = 'pin')
# the general call
pin_edge(object_id: str, edge)
# = support_edge(…, 'pin') plate SIMPLE · plane PIN
fix_edge(object_id: str, edge)
# = support_edge(…, 'fix') plate CLAMPED · plane FIXED
roller_edge(object_id: str, edge, free: str = 'x')
# = support_edge(…, 'roller-x'/'roller-y') plane only
symm_edge(object_id: str, edge, axis: str = 'x')
# = support_edge(…, 'symm-x'/'symm-y') plate only
support_object_edge(object_id: str, edge, support_name: str)
# any named support / 'free'
free_edge(object_id: str, edge)
# remove — clear that edge
support_edge is the general call; pin_edge/fix_edge/roller_edge/
symm_edge are it with a fixed kind. edge: 'all' |
'bottom'/'right'/'top'/'left' | an integer index (vertex i → i+1,
wrapping) — the same vocabulary create_edge_load (see loads) uses.
kind: pin | fix | symm-x | symm-y | roller-x | roller-y. All are
domain-aware (roller-* is plane-only, symm-* plate-only, exactly as the node
methods are) and resolve to the canonical support (SIMPLE/CLAMPED/…).
They all write the object's per-edge edge_supports list, so every mesh node
the edge generates is restrained, and restraints can differ per edge (e.g.
clamped / simply / clamped / simply around a rectangle).
support_object_edge takes an explicit support name instead of a kind: any
support already defined (or a canonical name such as SIMPLE / CLAMPED), or
'free' to clear that edge. free_edge is the inverse of the whole family — it
frees an edge whether it was restrained through the edge_supports list or by
corner-node assignments.
In a JSON model the same thing is the object's "edge_supports" key: a list
with one entry per edge in perimeter order (a rectangle is bottom, right, top,
left), each a support name or "free". For example, a slab simply supported on
two opposite edges:
{ "kind": "rectangle", "id": "R1", "tri_section_name": "Slab",
"node_ids": ["R1.p0", "R1.p1", "R1.p2", "R1.p3"],
"edge_supports": ["SIMPLE", "free", "SIMPLE", "free"] }
When edge_supports is absent, edge_support_mode ("common" default /
"equal" / "none") instead propagates each edge from whatever supports its
two corner nodes carry — the older corner-propagation fallback, used when the
corners are supported directly (assign_support/pin on the corner nodes)
rather than through edge_supports.
Springs¶
add_node_spring(
node_id: str, kx=0.0, ky=0.0, kt=0.0, mode_x='both', mode_y='both', kz=None, ktx=None,
kty=None) -> NodeSpring
add_element_spring(
element_id: str, kx=0.0, ky=0.0, coord_sys='global', mode_x='both',
mode_y='both') -> ElementSpring
add_line_element_spring(
object_id: str, kx=0.0, ky=0.0, kx_field='', ky_field='', coord_sys='global', mode_x='both',
mode_y='both') -> LineElementSpring
add_area_spring(target_id: str, kz: float = 0.0)
# Winkler slab-on-grade (plate)
No create_* layer — springs have no single "common case" to shortcut the
way a pinned/fixed support does.
Constraints and fields¶
add_rigid_link(master: str, slaves, id: str = '') -> Constraint
# slaves rigidly follow master
add_equal_dof(nodes, components, id: str = '') -> Constraint
# ties listed DOF components equal across nodes
add_constraint_equation(terms, value: float = 0.0, id: str = '') -> Constraint
# general linear MPC: sum(terms) = value
add_field(name: str, expression: str = '0.0')
# f(x, y) sampled at nodes