Skip to content

Add a material slot to a workflow type

A material slot is a configuration field whose value is a material: picked from the library by reference, picked and adjusted, or spelled out inline. This recipe adds one, restricts what fits it, feeds the effective material to a method and tests the result. About 20 minutes. The end-to-end fixture is e2e/components/workflow-types/test-material-workflow/ in the workflows repository.

Step 1 — Depend on the material schema

The slot is bound to a material schema data type. Declare it like any nested data type; the declaration pins the version, the slot refers to it version-less.

dependencies {
    dataType("simscale.materials.schemas:fluid:1.0.0")
}

Step 2 — Declare the slot

Every data-type dependency yields a material declarator named <camelCaseId>Material(name). The body receives candidate, the schema's value model over a candidate material, for the suitability rules:

configurationSchema {
    fluidMaterial("fluid") { candidate ->
        label("Working fluid")
        doc("The fluid filling the geometry, picked from the material library or spelled out inline.")
        suitability {
            rule(candidate.fluidType, name = "Liquids only", code = "liquid_only") {
                error(condition = candidate.fluidType equal FluidType.LIQUID.asValueModel(), message = "This workflow handles liquids only.", code = "not_a_liquid")
            }
        }
    }
}

FluidType is the schema's generated enum; it comes with the dependency's generated package, which the build imports automatically. A material failing a suitability rule with error severity does not suit the slot; the findings say why, both in configuration validation and in the picker queries below. Set allowsCustomization = false in the body to accept pure references only.

Several materials assigned to regions is a list of compounds holding a slot and an assignment each; the list, not the slot, carries the cardinality:

compoundList("materials") {
    label("Materials")
    fluidMaterial("material") { candidate -> label("Fluid material") }
    bodyList(allowedSourceTypes = setOf(SourceType.CAD, SourceType.MESH), name = "regions") { label("Assigned regions") }
}

A slot cannot live inside an expert { } section, since it has no default.

Step 3 — Know the three value shapes

The configuration holds one of:

{ "fluid": { "configuration_reference_type": "material",
             "material": "simscale.materials.library.fluids:water:1.0.0" } }
{ "fluid": { "configuration_reference_type": "material",
             "material": "simscale.materials.library.fluids:water:1.0.0",
             "overrides": { "density": { "value": 998.2, "unit": "kg/m³" } } } }
{ "fluid": { "name": "Site-measured brine",
             "fluidType": "LIQUID",
             "density": { "value": 1027.0, "unit": "kg/m³" },
             "dynamicViscosity": { "value": 0.00108, "unit": "Pa⋅s" } } }

Overrides are a partial document in the schema's shape, deep-merged onto the referenced material's values; a polymorphic compound with a different type replaces the referenced variant. The last two shapes require allowsCustomization.

Step 4 — Read what validation reports

All findings sit at the slot's path (/fluid, or /materials/0/material in a list):

Code When
invalid_material_reference the reference node is malformed (details.reason)
material_customization_not_allowed overrides or an inline material on a slot with allowsCustomization = false
non_existent_material the referenced version does not exist (details.material_reference)
material_not_accessible the caller may not use the referenced version
material_without_properties the version carries no properties
material_resolution_unavailable the reference could not be resolved in this context
missing_value, unknown_value, … ordinary structural findings on the effective document, e.g. a property the slot needs but the material lacks

After materialization the slot holds a plain document, so the schema's rule set, the slot's suitability rules and the workflow type's own rules all judge the effective material.

Step 5 — Judge and use the effective material

The slot's value model is the schema's value model. Rules and mappings read it like any nested compound:

validationRuleSet {
    rule(configValueModel.fluid.density, name = "Dense fluid", code = "dense_fluid") {
        warning(above = 900.0.kilogram_per_cubic_meter, message = "Light fluids produce low Reynolds numbers in this geometry", code = "light_fluid")
    }
}

In the workflow definition, map the effective properties into the method's configuration field by field (here for a method solve whose configuration declares density and dynamicViscosity), or assign the whole value model when the method embeds the same data type:

workflowDefinition {
    solve(geometry = geometry, result = result) {
        density = configValueModel.fluid.density
        dynamicViscosity = configValueModel.fluid.dynamicViscosity
    }
}

Methods never see slots: by the time an operation runs, the runner has materialized every reference, and the method receives a document. Solver-specific restructuring of the properties is ordinary mapping code in the workflow type, versioned with it.

Step 6 — Take a single value from a tabulated material

A slot that processes one value where a material may carry a table derives it while materializing: the table is read at the parameter the analysis works at, the way the material itself asks to be read.

fluidMaterial("fluid") { candidate ->
    label("Working fluid")
    derive(
        candidate.dynamicViscosity,
        interpolate(
            points = candidate.dynamicViscosityTemperatureDependence.table.dynamicViscosityPoints,
            elementModelFactory = { DynamicViscosityPointsValueModel.fromReference(it) },
            at = DimensionalScalarReference(ConfigValueReference("/referenceTemperature")),
            by = { it.temperature },
            value = { it.dynamicViscosity },
            stepwise = candidate.dynamicViscosityTemperatureDependence.table.interpolation equal TableInterpolation.STEP.asValueModel(),
            logarithmic = candidate.dynamicViscosityTemperatureDependence.table.interpolation equal TableInterpolation.LOGARITHMIC.asValueModel(),
            holdBelowRange = candidate.dynamicViscosityTemperatureDependence.table.extrapolationBelow equal TableExtrapolation.CONSTANT.asValueModel(),
            holdAboveRange = candidate.dynamicViscosityTemperatureDependence.table.extrapolationAbove equal TableExtrapolation.CONSTANT.asValueModel(),
        ),
    )
}

The derived value is written into the effective material that the workflow type's rules judge and that a run executes, so the value judged is the value used — and the value shown: the effective configuration of a workflow version is what a user interface renders for the slot, with the provenance of every field (DERIVED for this one). A user who customizes it keeps what they typed: customization wins over derivation. A material stating the value directly rather than tabulating it keeps its own, so one slot serves both kinds. See the effective material for a version with simkit workflow version effective-configuration <version-id>, or on the version's page in the SimKit UI.

The parameter is addressed by path because the configuration's own value model is generated from this schema and so does not exist yet while the schema is being declared; everything inside the material is typed.

A material asking not to be read beyond its range says so with the NONE extrapolation. Reading always yields a value, so state the refusal as a rule and the finding explains itself:

validationRuleSet {
    rule(
        configValueModel.referenceTemperature,
        name = "Reference temperature within the tabulated range",
        code = "reference_temperature_tabulated",
        appliesWhen = configValueModel.fluid.dynamicViscosityTemperatureDependence.table.extrapolationAbove equal TableExtrapolation.NONE.asValueModel(),
    ) {
        error(
            condition = withinTableRange(
                points = configValueModel.fluid.dynamicViscosityTemperatureDependence.table.dynamicViscosityPoints,
                elementModelFactory = { DynamicViscosityPointsValueModel.fromReference(it) },
                at = configValueModel.referenceTemperature,
                by = { it.temperature },
            ),
            message = "This material is not to be read outside the temperatures it tabulates.",
            code = "outside_tabulated_range",
        )
    }
}

interpolate and withinTableRange work wherever value models do, so the same reading serves a mapping expression feeding a method that takes a single value.

A material tabulating over more parameters than the slot takes

A slot declaring a table over temperature, fed by a material tabulating over temperature and pressure, takes a section of it at the pressure the analysis works at:

derive(
    candidate.densityPoints,
    sectionAt(
        rows = candidate.densityPoints,
        elementModelFactory = { DensityPointsValueModel.fromReference(it) },
        at = DimensionalScalarReference(ConfigValueReference("/referencePressure")),
        by = { it.pressure },
        value = { it.density },
    ),
)

What comes back is a table over the remaining parameter, which reads like any other, so sectioning and then reading takes a single value from a material of two parameters. Sectioning the last parameter leaves one row.

The rows are grouped by whatever the section leaves, and each group is read on its own, so the parameters need not form a full grid — measured data often does not. A group already carrying the sectioned value is taken as it stands rather than read, so a section landing on tabulated rows preserves them exactly.

Where a group does not span the sectioned parameter at all — a strain measured at one temperature only, a temperature measured at one pressure only — it has no variation of its own to read. Holding that single measurement would claim the quantity does not depend on the sectioned parameter, which every neighbouring group contradicts. Such a group is completed first: at each value of the sectioned parameter the group lacks, the table's rows there are read along the remaining axis. A group that does span the parameter keeps its own measurements, so completing never overrides data that is actually there.

Reducing two parameters to a single number is the two steps taken in turn — section, then read:

derive(
    candidate.density,
    interpolate(
        points =
            sectionAt(
                rows = candidate.densityPoints,
                elementModelFactory = { DensityPointsValueModel.fromReference(it) },
                at = DimensionalScalarReference(ConfigValueReference("/referencePressure")),
                by = { it.pressure },
                value = { it.density },
            ),
        elementModelFactory = { DensityPointsValueModel.fromReference(it) },
        at = DimensionalScalarReference(ConfigValueReference("/referenceTemperature")),
        by = { it.temperature },
        value = { it.density },
    ),
)

The section is an ordinary table, so nothing about the second step knows it came from a larger one — which is why the same two steps reduce a table of any number of parameters, one at a time.

The derivation sees the material whole rather than the slot's projection, which is what lets it read a pressure column the slot does not declare.

Step 6 — Test

The workflow type's own rules run in a .workflow-type.test.kts against an inline material (the test context has no registry, so use the inline shape):

workflowTypeTest {
    test("a light fluid is warned about") {
        validate {
            config("""{ "fluid": { "name": "Air", "fluidType": "GAS", "density": { "value": 1.2, "unit": "kg/m³" }, "dynamicViscosity": { "value": 0.0000181, "unit": "Pa⋅s" } } }""")
        }.shouldHaveExactly(validationWarning("dense_fluid", "light_fluid"))
    }
}

Suitability rules are judged by the platform, not by the captured rule set; check them against registered materials:

simkit component suitable-materials <your-namespace>:my-workflow:1.0.0 /fluid --all
simkit component suitable-materials <your-namespace>:my-workflow:1.0.0 /materials/*/material --states SNAPSHOT,RELEASED

The slot path is the configuration path with * for list elements. Without --all only suitable materials are listed; --states defaults to RELEASED. The reverse question — which slots accept a material — is simkit component material-slots <material-version-reference>.

Cross-references