Author a material schema¶
A material schema is an ordinary data type whose data schema describes the property set of a material family — a fluid, a solid, a coating. Materials declare it as their dependency and fill its generated data model; workflow types bind material slots to it. This recipe composes one from the shared property vocabulary, adds the physics constraints and tests them. About 20 minutes.
The curated schemas live in the workflow-components repository under
data-types/materials/schemas/ in the group
simscale.materials.schemas; read fluid.data-type.kts and
solid.data-type.kts there as complete worked examples.
Step 1 — Compose from the property fragments¶
The property vocabulary is the set of data types under
simscale.physics.material-properties.* (density, dynamic viscosity,
Young modulus, …). Each is an inline data type whose generated
declarator injects its field under the canonical name, so every schema
that uses the fragment speaks the same property name — which is what
lets a material authored against one schema serve a slot of another.
import simscale.physics.dimensionals.scalars.temperature
import simscale.physics.materialproperties.fluid.dynamicViscosity
import simscale.physics.materialproperties.general.density
import simscale.physics.materialproperties.thermal.heatOfFormation
dataType {
dependencies {
dataType("simscale.physics.dimensionals.scalars:temperature:1.0.1-SNAPSHOT-latest")
dataType("simscale.physics.material-properties.fluid:dynamic-viscosity:1.0.1-SNAPSHOT-latest")
dataType("simscale.physics.material-properties.general:density:1.0.1-SNAPSHOT-latest")
dataType("simscale.physics.material-properties.thermal:heat-of-formation:1.0.0-SNAPSHOT-latest")
}
metadata {
reference = "<your-namespace>:coolant:1.0.0"
name = "Coolant material"
description = "Material schema of a single-phase coolant with constant properties."
roles = setOf(MATERIAL)
}
dataSchema {
string("name") { label("Name") }
enum("fluidType", "FluidType", listOf("GAS", "LIQUID")) { label("Fluid type") }
density() // field `density`, the fragment's units
dynamicViscosity() // field `dynamicViscosity`
heatOfFormation(customMetricDefaultValue = 0.0) // optional: a schema default
temperature("referenceTemperature") { label("Reference temperature") }
}
}
roles = setOf(MATERIAL) declares what the data type is for (see
the roles of a data type):
it makes the build generate the material declarators and the data
model builder that a material's properties { } block is applied to,
and it is what lets the registry accept the data type as a material
schema. A schema that is also embedded as a fragment elsewhere adds
SCHEMA to the set.
A declarator takes the field name as its first argument, so one
fragment can appear under several names
(dynamicViscosity("referenceViscosity")). The generated declarator of
a dimensional fragment also accepts customMetricUnits,
customUsCustomaryUnits and customMetricDefaultValue; a real
fragment accepts defaultValue. A field with a default is optional in
the schema and becomes a constructor default of the generated data
model, so materials may leave it out.
Add a fragment to the vocabulary, rather than an ad-hoc field, whenever the property is a physical quantity other schemas could share.
Step 2 — Model variants as subtypes¶
A property set that differs by model is a polymorphic compound; each
variant is a subtype, and one variant may be the default:
compound("viscosityTemperatureDependence") {
label("Viscosity temperature dependence")
subtype("constant", typeName = "CONSTANT", default = true) {
label("Constant")
}
subtype("sutherland", typeName = "SUTHERLAND") {
label("Sutherland")
dynamicViscosity("referenceViscosity") { label("Reference viscosity") }
temperature("referenceTemperature") { label("Reference temperature") }
temperature("sutherlandTemperature") { label("Sutherland temperature") }
}
}
typeName is the wire discriminator ("type": "SUTHERLAND" in a
material's properties document); the subtype's name is what the generated
Kotlin names use (ViscosityTemperatureDependenceSutherlandDataModel).
A variant without fields is legitimate — it says "not modelled".
Give every field and subtype a label(...); the platform never derives
labels from identifiers.
Step 3 — Tabulate a property over a parameter¶
A property that depends on a parameter (viscosity over temperature, yield
stress over plastic strain) is a table: a list whose rows are indexed by
one or more axis columns and carry the tabulated quantity in the rest,
behind a CONSTANT / TABLE choice so the constant case stays a single
number. Declaring the axes is what makes the rows a table rather than a
list that happens to look like one: registration then rejects a repeated
or misordered combination, so no schema states that as a rule and nothing
reading the rows has to cope with either. Several axes tabulate over
several parameters, and the rows stay flat, one per combination. The shared
interpolation, extrapolationBelow and extrapolationAbove fragments
say how the table is read between its rows and beyond each end; the two
ends are stated apart because they differ physically, a property often
plateauing below the measured range while continuing above it:
compound("dynamicViscosityTemperatureDependence") {
label("Dynamic viscosity temperature dependence")
subtype("constant", typeName = "CONSTANT", default = true) { label("Constant") }
subtype("table", typeName = "TABLE") {
label("Table")
compoundTable("dynamicViscosityPoints") {
label("Dynamic viscosity over temperature")
axis { temperature() }
value { dynamicViscosity() }
}
interpolation() // LINEAR | LOGARITHMIC | STEP, default LINEAR
extrapolationBelow() // CONSTANT | LINEAR | NONE, default CONSTANT
extrapolationAbove()
}
}
A material fills the table with one dynamicViscosityPoints { } block per
row and states how it wants to be read; the stored document keeps the rows
in the units written. Reading it is the consumer's half of the bargain: a
slot that processes a single value derives one from the table (see add a
material slot), and a method receiving the whole
table honours the same settings itself. Tables of a few thousand
rows are ordinary documents; a property measured at a scale beyond that
belongs in a data component the material or workflow type references.
One interpolation fragment embedded by several tables of one schema
declares its TableInterpolation enum once, so every table shares the
constants.
Step 4 — Add the physics constraints¶
The schema's dataSchemaValidationRuleSet is enforced by the registry
when a material is registered, so a material violating it never enters
the library. Each rule case carries exactly one bound (above,
atLeast, below, atMost) or one condition; a range is two cases.
Rules over a subtype's field apply only to that variant through
appliesWhen:
import com.simscale.workflows.framework.measurementunits.dimensionals.kilogram_per_cubic_meter
import com.simscale.workflows.framework.measurementunits.dimensionals.pascal_second
import com.simscale.workflows.framework.values.isTypeOf
dataSchemaValidationRuleSet {
rule(valueModel.density, name = "Positive density", code = "positive_density") {
error(above = 0.0.kilogram_per_cubic_meter, message = "Density must be positive.", code = "density_not_positive")
}
rule(valueModel.poissonRatio, name = "Poisson ratio of a stable isotropic solid", code = "poisson_ratio_range") {
error(above = -1.0, message = "Poisson ratio must be above -1.", code = "poisson_ratio_too_low")
error(below = 0.5, message = "Poisson ratio must be below 0.5.", code = "poisson_ratio_too_high")
}
rule(
valueModel.viscosityTemperatureDependence.sutherland.referenceViscosity,
name = "Positive Sutherland reference viscosity",
code = "positive_sutherland_reference_viscosity",
appliesWhen = valueModel.viscosityTemperatureDependence isTypeOf "SUTHERLAND",
) {
error(above = 0.0.pascal_second, message = "The Sutherland reference viscosity must be positive.", code = "reference_viscosity_not_positive")
}
}
valueModel is the schema's generated value model; a subtype's fields
are reached through the subtype's name (.sutherland.), and
isTypeOf compares against the typeName.
Step 5 — Test the rule set¶
A sibling <id>.data-type.test.kts feeds complete documents to
validateData. Build the fixtures from the properties document of a
material bound to the schema (the properties object of its built
<ref>.material.json) and mutate them for the negative cases:
val water = """{ "name": "Water", "fluidType": "LIQUID", "density": { "value": 997.33, "unit": "kg/m³" }, … }"""
val waterWithNegativeDensity = """{ "name": "Water", "fluidType": "LIQUID", "density": { "value": -1.0, "unit": "kg/m³" }, … }"""
dataTypeTest {
test("a liquid with constant properties passes") {
validateData { data(water) }.shouldBeEmpty()
}
test("a negative density is rejected") {
validateData { data(waterWithNegativeDensity) }
.shouldHaveExactly(validationError("positive_density", "density_not_positive", message = "Density must be positive."))
}
}
The test evaluates the rule set only; structural validation and
default population happen at registration. Run it with
simkit component test <dir>; see the
component tests reference.
Step 6 — Build, register, release¶
simkit component build data-types/materials/schemas/coolant
simkit component register <your-namespace>:coolant:1.0.0.data-type.json
Release the schema before any material bound to it: a material's only
dependency is its schema, and the registry releases dependency graphs
leaves-first (see Release a component).
Materials name the schema version in schema(...), so a new schema
version does not move existing materials.
What the data model gives materials¶
The generated <Schema>DataModelBuilder is the scope of a material's
properties { } block: mandatory fields must be set, fields with schema
defaults are preset, a nested compound is filled in a block named after
it (it has no preset even when all its leaves do), a polymorphic
compound is chosen through the function named after the variant, and a
compound list takes one block call per row. Design
the schema with that shape in mind: what you make mandatory, every
material must state.
Cross-references¶
- Concepts: materials — the component kind.
- Add a material slot — consuming the schema in a workflow type.
- Contribute a library material — where materials of this schema live.
- Schema DSL reference — every field declaration.