.. _Chap:ImplemLanguage: Reading models in C++ ===================== A program reads a ``.ebg`` model (see :ref:`Chap:Language`) with ``Source/EBGeometry_Language.hpp``, which ``EBGeometry.hpp`` does not include: a program that reads no models compiles none of it. Everything here runs on the host, except the tables, which a device reads. :ref:`Chap:ExampleLanguageDomains` shows the whole path, a device query included. .. contents:: On this page :local: :depth: 1 Reading ------- `Language::read() `__ reads a ``.ebg`` file and the files it includes, and ``Language::parse()`` reads text, into a `Language::Model `__. Both take the pool that the meshes the model reads are built into, which must outlive the object, and overrides for its params, written as in a file: .. code-block:: c++ #include Pool pool(hostMemoryResource()); const auto model = Language::read("cable/setup.ebg", pool, {{"r_core", "0.6"}}); // throws Language::Error const Tape tape(pool, model.object()); // the program chooses the tape's features and user primitives A model with errors throws a `Language::Error `__ holding every `Diagnostic `__, each with its file, line and column; ``model.warnings()`` lists the warnings of a model that was read. The precision is the program's: the model is built in ``T``. What a model holds ------------------ * ``object()``: the object, an ``Expr`` to compile into a tape, and ``index()``, its `ExprIndex `__: the regions, materials and labels by name (see :ref:`Chap:ImplemRegions`). * ``domain(name)`` and ``domain(i)``: its domains (``ModelDomain``, below); ``domain(name)`` throws a ``Language::Error`` listing the domains when there is none of that name. * ``materialProperties(material)``: a material's properties; ``couplings()``: the couplings (`Coupling `__: their kind, the two domains and parts they join, their properties). * ``params()``: the params (`Param `__: name, value after the overrides, type, where it is written), so that a program can show what it may change. * ``files()``: every file the model read, and ``sources()``: their parsed text, for a program that watches them or edits them. * ``view()``: the view block's properties, for a program that opens a window. * ``shapes()`` and ``siteOfLeaf(leaf)``: the shapes the text builds and where (`ShapeSite `__: the call, its file and line, its parts and their labels), so that a program can name a surface by the line that made it. * ``siteOfNode(node)``: the shape value that makes a node of the object (`NodeSite `__: its names, where its call is written, how many places of the object use it), so that a program can write a level of the object as ``surfaces(name)``. * ``names()``: the names a viewer shows (``RegionNames``: the regions, materials and labels, each surface by the shape call it comes from, the domains and their groups, and the view's colors). Domains and their properties ---------------------------- A domain of the model, `ModelDomain `__, holds its ``DomainSetup`` with the names and properties of its subdomains and surface groups. The program builds the domain for its tape from the setup (see :ref:`Chap:ImplemRegions`), with the same numbers: subdomain *i* is domain label *i*, surface group *g* is group *g*. .. code-block:: c++ const auto& cfd = model.domain("cfd"); const Domain fluid(pool, tape, cfd.setup()); A solver of one domain can compile the domain's own object instead of the whole model's: ``domainObject(name)`` gives a `DomainObject `__, the domain's regions and the regions whose bounding boxes meet theirs (``regionNeighborhood()``, whose rule and caveats are on :ref:`Chap:ImplemRegions`), with the domain's setup and the model's names translated to it. Its subdomains and surface groups keep their numbers, so the domain's tables (below) still apply; its regions and surfaces are numbered anew, mapped to the model's through ``neighborhood()``. .. code-block:: c++ const auto own = model.domainObject("conduction"); const Tape cable(pool, own.object()); const Domain heat(pool, cable, own.setup()); `Properties `__ are what a material, subdomain, surface group or coupling carries: ``number()``, ``vector()``, ``string()`` and ``flag()`` (true or false) by key, each with an optional fallback, and ``code(key, {"no_slip", "slip"})``, a string as its index among names, for a kernel to switch on. A missing key, or a value of another type, throws an ``Error`` at the line in the file. A subdomain's properties are its own over its regions' material's. Tables on a device ------------------ The program declares its own row type and fills it on the host, from the properties; the rows go into a pool as a `Language::Table `__, a pool handle like the tape (the pool must outlive it, and ``rebasedView()`` moves it to a mirror). A kernel indexes it by the numbers EBGeometry computes there: * ``model.regions(pool, fill)``: a row per region, by ``tape.region(p)`` (a region with no material, ``outside`` included, gets empty properties: give fallbacks); * ``cfd.volumes(pool, fill)``: a row per subdomain, by ``domain.subdomainAt(p)`` (``Domain::Excluded`` has no row); * ``cfd.surfaces(pool, fill)``: a row per surface group, by ``domain.surfaceGroupOf(surface)``. .. code-block:: c++ struct Gas { double rho, mu; }; const auto gas = cfd.volumes(pool, [](const Language::Properties& p) { return Gas{p.number("density"), p.number("viscosity")}; }); pool.freeze(); const Pool device = Pool::mirror(pool, GPU::memoryResource()); const auto d = fluid.rebasedView(device); const auto g = gas.rebasedView(device); // in a kernel: const uint32_t l = d.subdomainAt(x); if (l != Domain::Excluded) { g[l].rho ... } Geometry and properties live in the one pool: one mirror, one lifetime. A program's other data is its own. Checks and text --------------- * ``Language::completeness(model, tape, "cfd")`` checks a domain (see :ref:`Chap:LanguageDomains`): a `Completeness `__, whether it is complete (``m_complete``) and what is missing, as diagnostics at the lines to change (``m_findings``). ``Language::completeness(model, tape, "cfd", resolution)`` also searches the domain's subdomains for their connected components at that resolution, and warns of a subdomain in several and of an enclosed cavity (see :ref:`Chap:ImplemRegions`). * ``inventory(model)`` lists every name the model offers, as text: its regions and materials, its labels, each shape's parts and their labels, and its domains. * ``partNames("box")`` gives a built-in shape's part names. * ``print(model)`` writes the model in canonical form; ``print(model, true)`` resolves the includes into one text. The namespace ``Language::Syntax`` holds the language's text as a tool sees it: ``tokenize()``, ``parse()`` into a syntax tree (``Syntax::File``) with every position, and ``isReserved()``. It is what a program that edits ``.ebg`` files, rather than reading models, builds on. Reading a model reports errors instead of ending the program. The checks the library makes of the values it is given (a negative radius, say) abort a program built in C++, where a value is the program's own; the reader turns them into errors at their call by building inside a `ThrowOnRequire `__ scope, where a failed check throws ``RequireError``. A program that builds objects from input of its own can do the same. The scope holds on the thread that opens it only, and a check in a ``noexcept`` function still ends the program. From C++ to .ebg ---------------- The language writes what the C++ API builds: .. list-table:: :header-rows: 1 :widths: 45 55 * - C++ - .ebg * - ``SphereSDF(center, radius)``, ``BoxSDF(lo, hi)``, ... - ``sphere(center, radius)``, ``box(lo, hi)``, ... * - ``Parser::readIntoTriMeshSDF(file, pool)`` - ``mesh(file)`` * - ``Union(a, b)``, ``Difference(a, b)``, ``Translate(a, v)``, ... - ``union(a, b)``, ``difference(a, b)``, ``translate(a, v)`` or ``a.translate(v)``, ... * - ``Union(objects, Accelerate::BVH)`` - ``bvh_union(...)`` * - ``SurfaceLabel(a, "name")``, labels per part - ``a.label("name")``, ``a.label(xmin = "inlet")`` * - ``Overlay(RegionName(MaterialLabel(a, "m"), "r"), ...)`` - ``overlay { region r = a material m ... }`` * - ``DomainSetup::addSubdomain()``, ``addSurfaceGroup()`` and ``add(group, selector)`` - ``domain d { subdomain ... group ... }`` * - ``SurfaceSelector::label()``, ``surfaces()``, ``interface()``, ``region()``, ``materials()``, ``rest()`` - ``label()``, ``surfaces(name)``, ``interface()``, ``region()``, ``materials()``, ``remaining()`` * - per-material and per-subdomain data in the program's own arrays - ``material`` and subdomain properties, read into a ``Language::Table`` What the language does not express is the program's: a tape's features and user primitives (the reader builds the built-in shapes, and the program chooses the tape type it compiles the object into), and shapes of your own.