Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

The case file

At the end of this chapter you can read every group of a case, change a case safely and look up the allowed fields yourself. You need the Python binding of chapter 1: python -m pip install cfd-craft.

Setup

Save the case of chapter 1 as point-source.json in an empty directory:

{
  "schema_version": "cfd-craft.case/0.1",
  "name": "manual-point-source",
  "domain": {"size_m": [8.0, 4.0, 4.0]},
  "grid": {"cells": [16, 8, 8]},
  "boundaries": {"x": "open", "y": "periodic", "z": "periodic"},
  "flow": {"kind": "prescribed_uniform", "velocity_m_s": [1.0, 0.0, 0.0]},
  "geometry": [],
  "constituents": [{"id": "tracer", "diffusivity_m2_s": 0.05}],
  "initial_conditions": [],
  "sources": [{"kind": "point", "id": "S0", "constituent": "tracer",
    "point_m": [1.0, 2.0, 2.0], "rate_kg_s": 1e-7}],
  "receptors": [{"kind": "volume_average", "id": "R_down",
    "min_m": [5.0, 1.0, 1.0], "max_m": [6.0, 3.0, 3.0]}],
  "numerics": {
    "transport": "linear_upwind", "max_time_step_s": 0.1,
    "advective_cfl_max": 0.4, "pressure_relative_tolerance": 1e-10,
    "pressure_max_iterations": 1200, "steady_residual_tolerance": 1e-5,
    "steady_monitor_tolerance": 0.001, "monitor_window": 30
  },
  "run": {"mode": "steady", "end_time_s": 2.0, "max_steps": 2000,
    "wall_time_limit_s": 120.0, "threads": 1, "memory_limit_mib": 256},
  "output": {"fields": true, "checkpoint": true, "max_total_mib": 16}
}

Outputs on this page are real. The documentation check compares floating-point numbers with a relative tolerance of 1e-4; everything else must match exactly.

The groups of a case

Nothing outside the case file configures the solver. Its top-level fields are:

FieldsWhat they hold
schema_version, nameVersion of the case format; a label copied into the result
domain, gridLengths of the box in meters; three cell counts
boundariesOne condition per axis: periodic, open, wall, slip or, for z, ground_open_top
flow, moving_wallsPrescribed, laminar or neutral-RANS flow; moving lids
geometrySolid buildings, each with an ID: boxes, or prisms with a polygon footprint
constituents, initial_conditionsPassive gases and particle size classes with their diffusivity; their starting concentration
sources, receptorsWhere mass enters and where concentration is reported, each with an ID
numericsSchemes, time step limit, tolerances, monitor window
runMode (steady or transient), endpoint, and the budgets for steps, wall time, threads and memory
outputWhether fields and a checkpoint are kept; the output budget
vegetation, building_roughness_m, geometry_min_passage_cellsOptional fields for city blocks (chapter 22)

The six lists (moving_walls, geometry, constituents, initial_conditions, sources, receptors) are empty when omitted, and the opt-in numerics of chapter 9 have defaults. The fields of the last row may be omitted. Every other field is required. The case schema lists each field with its type, default and meaning.

Units and coordinates

Every quantity has one unit, and the field name carries it. Nothing is converted silently. This case uses:

FieldUnit or convention
size_m, point_m, min_m, max_mmeters; x east, y north, z up
velocity_m_sm/s
diffusivity_m2_sm2/s
rate_kg_skg/s
end_time_s, max_time_step_s, wall_time_limit_sseconds
receptors_kg_m3 (in the result)kg/m3; multiply by 1e9 for micrograms/m3

The grid is uniform: the cell size is the domain length divided by the cell count, here 8 m / 16 = 0.5 m along x and 4 m / 8 = 0.5 m along y and z. There is no mesh file and no mesh generator. The full table is in units and conventions.

Change a case without touching the original

Load the case, change a copy, give the copy a new name and check it. Here one copy has half the source rate and another has only a smaller step budget.

import copy
import json
import cfd_craft as cfd


def r_down(result):
    tracer = next(c for c in result["constituents"] if c["id"] == "tracer")
    return tracer["receptors_kg_m3"]["R_down"]


case = json.load(open("point-source.json"))
half = copy.deepcopy(case)
half["name"] = "point-half-rate"
half["sources"][0]["rate_kg_s"] *= 0.5
budget = copy.deepcopy(case)
budget["name"] = "point-other-budget"
budget["run"]["max_steps"] = 1000
print(cfd.check(half)["valid"], cfd.check(budget)["valid"])
base, changed, same = (cfd.simulate(c) for c in (case, half, budget))
print(changed["assessment"])
print(f"ratio = {r_down(changed) / r_down(base):.6g}")
print(changed["physics_sha256"] == base["physics_sha256"])
print(same["physics_sha256"] == base["physics_sha256"])
True True
steady_discrete_solution
ratio = 0.5
False
True

Half the rate gives half the concentration, and the run is numerically accepted. Every result carries physics_sha256, a hash of grid, flow, sources, receptors and numerics. A changed source is a different problem (False). A changed budget is not (True): name, run and output are outside the hash (chapter 8 builds on this).

A wrong field is an error

A field name from another CFD package is not accepted, and nothing is ignored. The coordinate of a point source is point_m. Rename it to position_m:

import json
import cfd_craft as cfd

bad = json.load(open("point-source.json"))
bad["sources"][0]["position_m"] = bad["sources"][0].pop("point_m")
report = cfd.check(bad)
print(report["valid"], [(d["code"], d["path"]) for d in report["diagnostics"]])
try:
    cfd.simulate(bad)
except cfd.CfdError as error:
    print("CfdError", [(d["code"], d["path"]) for d in error.diagnostics])
False [('CFD-E000', 'sources[0]')]
CfdError [('CFD-E000', 'sources[0]')]

check returns the diagnostic; simulate raises CfdError with the same diagnostics and never starts, so a typo cannot run silently with a default. Each diagnostic has a code, a severity, the path of the offending element and a message. Use code and path in programs; the message is for people and here names the allowed fields. All codes: diagnostics.

Find the allowed fields

describe returns the schemas of the installed version, generated from the types the solver uses.

import cfd_craft as cfd

described = cfd.describe()
schema = described["case_schema"]
definitions = schema["definitions"]
print([kind["properties"]["kind"]["enum"][0] for kind in definitions["Flow"]["oneOf"]])
print(definitions["Advection"]["enum"])
print("termination" in described["result_schema"]["properties"])
['prescribed_uniform', 'laminar', 'neutral_rans']
['linear_upwind', 'muscl']
True

These are the three flow kinds, the two schemes of numerics.transport, and a test that the result schema describes the summary. Both are JSON Schema (draft-07): jsonschema.validate(case, schema) checks names and types in your own tool. Ranges, units, geometry and budgets are checked only by check.

Next

  • Chapter 3: the command line and the files of a run.
  • Chapter 5: sources, receptors, schemes.