Saving and loading
A model is saved in one of two ways: as a pickle, the whole object with its problems, or as JSON, its data as plain text.
The samples on this page continue from one model:
from pathlib import Path
from xfea.model import BeamElement, ElasticIsotropic, Model, Node, Part, RectangularSection
here = Path(__file__).resolve().parent
model = Model(name="cantilever")steel = ElasticIsotropic(E=210_000, v=0.3, density=7.85e-9) # N, mm, tsection = RectangularSection(w=50, h=100, material=steel)
part = model.add_part(Part(name="beam"))nodes = [part.add_node(Node(xyz=[i * 100.0, 0, 0])) for i in range(21)]for a, b in zip(nodes, nodes[1:]): part.add_element(BeamElement(nodes=[a, b], section=section, orientation=[0, 1, 0]))model.add_fix_bc(nodes=[nodes[0]])JSON: the model as data
Section titled “JSON: the model as data”model.to_json(here / "cantilever.json", pretty=True)
same = Model.from_json(here / "cantilever.json")print(same.name, len(same.nodes), len(same.elements))The file is plain data, with points as lists of numbers: any tool can read it. The loaded model has the names and the uids of the one saved.
Pickle: the whole model
Section titled “Pickle: the whole model”from xfea.problem import Problem, StaticStepfrom xfea.results import DisplacementFieldResults
problem = model.add_problem(Problem(name="tip_load"))step = problem.add_step(StaticStep(name="load"))step.add_uniform_forcefield(nodes=[nodes[-1]], z=-1000)step.add_field_outputs([DisplacementFieldResults()])
model.to_pickle(here / "cantilever.pkl")
whole = Model.from_pickle(here / "cantilever.pkl")print([p.name for p in whole.problems])A pickle keeps everything, and objects that are shared stay shared. It is also the fast way to save and reload a large model.
What a solve saves
Section titled “What a solve saves”analyse_and_extract pickles the model to model.pkl in the folder given as path, next to the problem’s folder,
once the results are read, unless save_model=False. With the results database, that is all another script needs
to read the results without solving again:
problem.analyse_and_extract(path=here / "output")
again = Model.from_pickle(here / "output" / "model.pkl")step_again = list(again.problems)[0].steps[-1]for field in step_again.field_outputs: # the displacement field asked for above print(field.get_min_result("z").z) # -3.04 mmThe pickle holds the path of the results database, not the results: keep the folder together.
Copies
Section titled “Copies”other = part.copy() # a deep copy, with new uids and "_copy" after its namestwin = part.copy(duplicate=True) # keeping the names and the uidsprint(other.name, twin.name)Every object in the copy is copied with it: changing one does not change the other.
The data underneath
Section titled “The data underneath”Every xfea object gives its data as a dictionary, and can be made again from it. to_json and from_json are
built on this:
node = Node(xyz=[1.0, 2.0, 3.0], name="corner")
data = node.__data__print(Node.__from_data__(data).xyz)© 2026 Francesco Ranaudo · Text and pictures CC BY 4.0 · Code Elastic License 2.0 · how to reuse them