.. _mat-c: C ABI ===== Evaluate an element, substance, or material into an owned MAT record. Basic examples -------------- Evaluate water and print its JSON record. Link against ``snt-c``: .. code-block:: c #include #include #include int main(void) { snt_mat_result* water = NULL; snt_mat_error error = {0}; size_t required = 0; if (snt_mat_substance_eval("H2O", 1, NULL, NULL, NULL, NULL, &water, &error)) return 1; int failed = snt_mat_result_json(water, NULL, 0, &required, &error); char* json = failed ? NULL : malloc(required); failed = failed || !json || snt_mat_result_json(water, json, required, &required, &error); if (!failed) puts(json); free(json); snt_mat_result_free(water); return failed; } Supply physical inputs for a material mixture: .. code-block:: c #include #include #include int main(void) { snt_mat_result* brine = NULL; snt_mat_error error = {0}; size_t required = 0; if (snt_mat_material_eval("0.965 0.035 ", "mass_fraction", 1, NULL, "1.02*g/cm3", "1*L", NULL, &brine, &error)) return 1; int failed = snt_mat_result_json(brine, NULL, 0, &required, &error); char* json = failed ? NULL : malloc(required); failed = failed || !json || snt_mat_result_json(brine, json, required, &required, &error); if (!failed) puts(json); free(json); snt_mat_result_free(brine); return failed; } Common workflows ---------------- .. list-table:: :header-rows: 1 * - Task - Call chain - Result * - Evaluate a record - ``snt_mat_*_eval`` - Owned ``snt_mat_result*`` * - Size the JSON - ``snt_mat_result_json(result, NULL, 0, ...)`` - Required byte count * - Copy the JSON - ``snt_mat_result_json(result, buffer, capacity, ...)`` - UTF-8 JSON in caller buffer * - Release the record - ``snt_mat_result_free`` - No result; accepts ``NULL`` * - Handle failure - Check the integer return value and ``snt_mat_error`` - ``code`` and borrowed ``message`` The normal chain is evaluate, query the JSON size, allocate the caller-owned buffer, copy the JSON, then free the result handle. Use ``snt_mat_element_eval``, ``snt_mat_substance_eval``, or ``snt_mat_material_eval`` at the first step. See the :doc:`C binding guide <../../../integrations/c>` and :doc:`C API reference <../../../api/c_mat>` for the complete ownership rules. Current limitations and differences ----------------------------------- Compared with the canonical C++ implementation: - Results are opaque handles serialized as JSON. There are no native typed composition getters, mutable Element/Substance/Material objects, or incremental ``add()`` operations. - Matter inputs are PUQ expression strings. Optional outputs are JSON ``null``, and quantities contain a scalar ``value``, ``units``, and ``text``. - Callers own the serialization buffer and must free every result. Return codes and ``snt_mat_error`` replace C++ exceptions. The calculation rules and physical limits remain those of the native implementation. Binding reference ----------------- This is a lookup index, not a sequence to read from top to bottom. Use `Common workflows` above to choose a task, then find the exact C function below. Integer-returning calls return zero on success. Output handles and buffers are passed by pointer, and references such as ``&result`` are replaced by the caller's variables in real code. - ``snt_mat_element_eval(expression, natural, number_density, mass_density, volume, mass, &result, &error)``. - ``snt_mat_substance_eval(...)`` with the same arguments. - ``snt_mat_material_eval(expression, normalization, natural, number_density, mass_density, volume, mass, &result, &error)``. - ``snt_mat_result_json(result, buffer, capacity, &required, &error)``; pass ``NULL`` and zero capacity to query the required byte count. - ``snt_mat_result_free(result)`` releases a record and accepts ``NULL``. Matter arguments are optional PUQ expression strings. Passing ``NULL`` or an empty string omits an input. ``normalization`` accepts ``number``, ``number_fraction``, or ``mass_fraction``; ``NULL`` means ``number_fraction``. The ABI returns canonical JSON rather than exposing typed Element, Substance, or Material records directly. Failures report ``code`` and ``message`` through ``snt_mat_error``.