Skip to content

Reference

Python

Build, open, analyze and save cSlope projects from Python, read the results, and run analysis documents with the cslope command.

The Python package cslope is the cSlope app's engine, compiled from the same Rust sources, with a cslope command. It builds a model from nothing or opens a .cslp project, runs it as the app runs it and saves a project the app opens with the results current. The examples on this page are files in the repository, run by its tests as they are shown.

Install

Python 3.10 or later, on Windows (x64), macOS and Linux (x86_64, aarch64); no other dependencies.

pip install cslope                             # or "cslope[numpy]" for the finite element arrays as numpy arrays
cslope info                                    # the package version, the engine build and the schema version

With uv, it runs without an install step: uvx cslope info, or uv run --with cslope python my_slope.py for a script.

From the source. With Rust (the version in packages/engine-rs/rust-toolchain.toml) installed:

git clone https://github.com/ZilanCiftci/cslope
cd cslope/packages/engine-rs/crates/cslope-py
pip install maturin==1.15.0
maturin build --release --out dist
pip install dist/cslope-*.whl

Build a project

Project, Model and their methods use the app's names: Exterior, Materials, Interior boundaries, Assign, Piezometric lines, Distributed loads, Line loads, Reinforcement, Search limits, Custom surfaces and Options. Units are m, kPa, kN/m (line loads), kN/m³ and degrees; points are (x, y) with x to the right and y up. A new model takes the app's defaults for a new model (Morgenstern-Price, 30 slices, composite surfaces).

packages/engine-rs/crates/cslope-py/examples/new_project.py
"""A project from nothing: one model built with the app's names, analyzed
and saved as a .cslp file that the cSlope app opens with the result current.

    python new_project.py          # writes site.cslp in this folder

The soil values are illustrative, not for use.
"""

import cslope

p = cslope.Project()
m = p.add_model("Section A")  # the app's defaults: Morgenstern-Price, 30 slices

# Exterior (m): x to the right, y up; the slope slides to the right.
m.exterior = [(0, 0), (0, 15), (15, 15), (35, 5), (50, 5), (50, 0)]

# Materials: c and phi (kPa, degrees) or su (kPa); unit weights (kN/m³).
sand = m.add_material("Sand", c=5, phi=32, gamma=19, gamma_sat=20)
clay = m.add_material("Clay", su=40, gamma=17, gamma_sat=18)

# An interior boundary splits the section; each region gets a material.
m.add_interior_boundary([(0, 8), (50, 8)])
m.assign(sand, at=(5, 12))
m.assign(clay, at=(25, 3))

# Water, a load on the crest and where slip surfaces enter and exit.
m.add_piezometric_line([(0, 12), (15, 11), (35, 5), (50, 5)])
m.add_distributed_load(x=(3, 10), q=20)  # kPa, vertical
m.search_limits(entry=(1, 14), exit=(30, 48))

r = m.analyze()
print(f"F {r.fos:.3f} ({r.fos_label}, {r.method})")
p.save("site.cslp")

Each call checks its input at once: a value the app would not keep, a boundary that does not reach the exterior or a load outside the model raises a ValueError that says what is wrong. m.data is the model as the app holds it, for what has no method of its own (the title block, a waternet, rapid drawdown, parameters).

Open, change, run and save

packages/engine-rs/crates/cslope-py/examples/change_and_rerun.py
"""Open a project, change a model, run it again and save it.

    python change_and_rerun.py site.cslp

Each model opens with the result saved in the file. A result whose model
has changed since is out of date: the app marks it so, and a save leaves it
out unless the model is run again.
"""

import sys

import cslope

p = cslope.Project.open(sys.argv[1] if len(sys.argv) > 1 else "site.cslp")
m = p.model("Section A")
print("saved:", m.result.fos, "current:", m.result_is_current)

m.material("Clay").su = 45  # the saved result is now out of date
m.options.method = "Bishop"  # the app's Options, snake_case
print("after the change, current:", m.result_is_current)

r = m.analyze(threads=4)  # as the app runs it; kept on the model
print(f"F {r.fos:.3f} ({r.method}), current: {m.result_is_current}")
p.save()  # back to the same file

for model in p.models:  # every model of a project
    print(model.name, model.result.fos if model.result else "no result")

A saved result carries a hash of the input it was computed for. When a model changes, its result is out of date (m.result_is_current is False), as the app marks it. Project.save writes what the app writes, with one rule: a model changed since its result and not run again is saved without the result, so that the file never shows an F for a model it was not computed for. For a model with the finite element analysis switched on, analyze gives the limit equilibrium result with a warning but does not keep it on the model: the finite element part runs in the app only.

Read the results

packages/engine-rs/crates/cslope-py/examples/read_results.py
"""Read a model's result: F and what it is, the critical surface, the
warnings and the slices, without running anything.

    python read_results.py site.cslp
"""

import sys

import cslope

p = cslope.Project.open(sys.argv[1] if len(sys.argv) > 1 else "site.cslp")
r = p.active_model.result
if r is None:
    sys.exit("The active model has no result: run it in the app or with m.analyze().")

print(r.fos, r.fos_label, r.method)  # F, "factor of safety", "Bishop"
print(r.critical_surface["text"])  # the critical surface, as the app describes it
for w in r.warnings:  # read every warning: one can mean F is not reliable
    print("warning:", w["message"])

for s in r.slices[:3]:  # the app's slice table, one dict per slice
    print(s["slice"], s["material"], round(s["alpha"], 1), round(s["normal_force"], 1))
r.to_csv("slices.csv")  # the whole table, as the app exports it

print(r.summary["engineBuild"])  # report the engine build with the result

fos_label says what F is: a factor of safety on characteristic values, or an over-design factor on design values when partial factors are applied (see Analysis). r.data is the whole result as the engine returns it, r.summary the summary the For agents page describes.

The command

cslope analyze site.cslp                       # the active model, as the app runs it
cslope analyze site.cslp --all --write         # every model, the results saved to the project
cslope export site.cslp --model "Section A" -o section-a.json
cslope validate section-a.json                 # the issues, each with its path in the document
cslope analyze section-a.json --json           # the summary and the full result as JSON
cslope schema analysis-document                # the JSON schema of a document

The exit status is 0 when the analysis ran, 1 when it failed and 2 when the input is not valid (each issue is printed with its path). --threads N limits the threads; the result is the same for any number.

Analysis documents (JSON)

An analysis document is one model and its options as JSON: {"format": "cslope-analysis", "formatVersion": 1, "definition": {...}, "options": {...}}. It is what an agent or a script writes and the cslope command runs. cslope.build.Definition writes one with the same names as above and checks it as it goes; cslope export and m.save_definition(path) write one of a project's model, and Model.from_definition(path) makes a model of one.

skills/cslope/examples/single_slope.py
"""A single slope: one drained soil, no water, sliding to the right.

The soil values are illustrative. For a real slope, use the values from the
site investigation; never guess them.

    python single_slope.py      # writes single_slope.json and analyzes it
"""

from pathlib import Path

from cslope.build import Definition


def build() -> Definition:
    d = Definition(orientation="ltr")  # the crest on the left, sliding to the right
    # The outline (m): 10 m high at 2 horizontal to 1 vertical.
    d.exterior = [(0, 0), (0, 15), (15, 15), (35, 5), (50, 5), (50, 0)]
    # c (kPa), phi (°), unit weights (kN/m³)
    d.add_material("Silty sand", c=5, phi=30, gamma=19, gamma_sat=20)
    # Slip surfaces enter behind the crest and exit at or beyond the toe.
    d.search_limits(entry=(2, 15), exit=(30, 48))
    # The options are set in full so the result does not depend on defaults.
    d.options.method = "Morgenstern-Price"
    d.options.slices = 30
    return d


if __name__ == "__main__":
    from cslope.cli import main

    path = Path(__file__).with_suffix(".json")
    build().save(path)
    raise SystemExit(main(["analyse", str(path)]))

cslope.analyze(doc) and cslope.summarize(result, doc) run a document from Python and give the result and its summary as dicts. The JSON schemas of the document, the options, the result and the summary are in packages/engine/schema, and cslope schema <name> prints them; their names are listed by cslope info. A key the schema does not define is an error, not ignored.

The full list of names, the functions of the engine (finite elements, reliability, the Slope class) and the notes on threads, progress and cancelling are in the package's README. The format of a project file is in Project files.

Suggest a change to this page