### Install FFCx from Source Source: https://github.com/fenics/ffcx/blob/main/README.md Use this command to install FFCx when working with the source code directly. ```bash $ pip install . ``` -------------------------------- ### Installing Header File Source: https://github.com/fenics/ffcx/blob/main/cmake/CMakeLists.txt Installs the main header file (ufcx.h) to the system include directory. ```cmake install(FILES ${PROJECT_SOURCE_DIR}/../ffcx/codegeneration/ufcx.h TYPE INCLUDE) ``` -------------------------------- ### Install FFCx with Documentation Dependencies Source: https://github.com/fenics/ffcx/blob/main/doc/README.md Install FFCx with the optional 'docs' dependency set to enable documentation building. This command should be run from the project's root directory. ```bash python -m pip install .[docs] ``` -------------------------------- ### Install FFCx from PyPI Source: https://github.com/fenics/ffcx/blob/main/README.md Use this command to install the FFCx package from the Python Package Index. ```bash $ pip install fenics-ffcx ``` -------------------------------- ### Full FFCx Compilation and File Output Example Source: https://github.com/fenics/ffcx/blob/main/_autodocs/api-reference/naming-formatting.md An end-to-end example showing the complete workflow from defining a UFL form to generating, formatting, and writing the code to files, including obtaining the final C name of the form. ```python import ufl from pathlib import Path from ffcx.analysis import analyze_ufl_objects from ffcx.ir.representation import compute_ir from ffcx.codegeneration.codegeneration import generate_code from ffcx.formatting import format_code, write_code from ffcx.options import get_options from ffcx.naming import form_name # Create form cell = ufl.triangle V = ufl.FiniteElement("P", cell, 1) u, v = ufl.TrialFunction(V), ufl.TestFunction(V) a = ufl.inner(ufl.grad(u), ufl.grad(v)) * ufl.dx # Compile options = get_options() ufl_data = analyze_ufl_objects([a], options["scalar_type"]) ir = compute_ir(ufl_data, {}, "myprefix", options, False) # Generate and format code code_blocks, suffixes = generate_code(ir, options) formatted_code = format_code(code_blocks) # Write to files write_code( formatted_code, "myform", suffixes, "./generated_code" ) # Get the generated form name form_c_name = form_name(a, 0, "myprefix") print(f"Generated form C name: {form_c_name}") ``` -------------------------------- ### Example: Compute IR for a simple form Source: https://github.com/fenics/ffcx/blob/main/_autodocs/api-reference/ir.md This example demonstrates how to compute the IR for a basic UFL form. It involves analyzing UFL objects, getting compilation options, and then calling `compute_ir`. ```python import ufl from ffcx.analysis import analyze_ufl_objects from ffcx.ir.representation import compute_ir from ffcx.options import get_options # Create forms cell = ufl.triangle V = ufl.FiniteElement("P", cell, 1) u, v = ufl.TrialFunction(V), ufl.TestFunction(V) a = ufl.inner(ufl.grad(u), ufl.grad(v)) * ufl.dx # Analyze options = get_options() ufl_data = analyze_ufl_objects([a], options["scalar_type"]) # Compute IR ir = compute_ir( ufl_data, object_names={}, prefix="myform", options=options, visualise=False ) # Access IR components print(f"Number of integrals: {len(ir.integrals)}") print(f"Number of forms: {len(ir.forms)}") print(f"Number of expressions: {len(ir.expressions)}") for form_ir in ir.forms: print(f"Form {form_ir.name}: rank={form_ir.rank}, " f"coefficients={form_ir.num_coefficients}") ``` -------------------------------- ### Configuring and Installing Pkg-config File Source: https://github.com/fenics/ffcx/blob/main/cmake/CMakeLists.txt Configures a pkg-config file template and installs the generated file to the pkg-config data directory. ```cmake configure_file(ufcx.pc.in ufcx.pc @ONLY) install(FILES ${PROJECT_BINARY_DIR}/ufcx.pc DESTINATION ${CMAKE_INSTALL_DATADIR}/pkgconfig) ``` -------------------------------- ### Bash/Shell Example for FFCx Compilation Source: https://github.com/fenics/ffcx/blob/main/_autodocs/README.md A basic example of using the FFCx command-line tool to compile a UFL file, specifying scalar type and output directory. ```bash # Bash/shell example ffcx myform.ufl --scalar_type float32 ``` -------------------------------- ### Installing Library Targets Source: https://github.com/fenics/ffcx/blob/main/cmake/CMakeLists.txt Installs the library targets (archive, library, runtime) to their respective destinations based on GNUInstallDirs. ```cmake install(TARGETS ${PROJECT_NAME} EXPORT ${PROJECT_NAME}_Targets ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR} LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR} RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR}) ``` -------------------------------- ### JSON Configuration File Format Source: https://github.com/fenics/ffcx/blob/main/_autodocs/configuration.md Example of the JSON format for ffcx_options.json configuration files. ```json { "epsilon": 1e-7, "scalar_type": "float64", "sum_factorization": true, "table_rtol": 1e-6, "language": "C" } ``` -------------------------------- ### Compile Basic Forms to C Code Source: https://github.com/fenics/ffcx/blob/main/_autodocs/quick-reference.md Compile UFL forms (bilinear and linear) to C code for later use. This example demonstrates defining forms, getting default options, and writing the compiled code to files. ```python import ufl from ffcx.compiler import compile_ufl_objects from ffcx.options import get_options from ffcx.formatting import write_code # Define domain and function spaces cell = ufl.triangle V = ufl.FiniteElement("P", cell, 1) # Define trial and test functions u, v = ufl.TrialFunction(V), ufl.TestFunction(V) # Define bilinear form: a(u,v) = ∫(∇u · ∇v) dx a = ufl.inner(ufl.grad(u), ufl.grad(v)) * ufl.dx # Define linear form: L(v) = ∫(f · v) dx f = ufl.Coefficient(V) L = ufl.inner(f, v) * ufl.dx # Get default options options = get_options() # Compile forms code, suffixes = compile_ufl_objects( [a, L], options, namespace="poisson" ) # Write to files write_code(code, "poisson", suffixes, "./generated") # Output files: # ./generated/poisson.h # ./generated/poisson.c ``` -------------------------------- ### Install FFCx Interface Header with CMake Source: https://github.com/fenics/ffcx/blob/main/README.md This sequence of CMake commands builds and installs only the `ufcx.h` interface header file, which is useful for DOLFINx integration. ```bash $ cmake -B build-dir -S cmake/ $ cmake --build build-dir $ cmake --install build-dir ``` -------------------------------- ### Example: Formatting and Printing Code Output Source: https://github.com/fenics/ffcx/blob/main/_autodocs/api-reference/naming-formatting.md Shows how to use `format_code` after code generation to prepare the code for output. It then prints the sizes of the header and source content. ```python from ffcx.codegeneration.codegeneration import generate_code from ffcx.formatting import format_code # After code generation ir = compute_ir(...) # From IR stage code_blocks, suffixes = generate_code(ir, options) # Format code formatted_code = format_code(code_blocks) # Returns: [header_content, source_content] print(f"Header size: {len(formatted_code[0])} bytes") print(f"Source size: {len(formatted_code[1])} bytes") ``` -------------------------------- ### Simulate Command-Line Call with Python API Source: https://github.com/fenics/ffcx/blob/main/_autodocs/api-reference/main.md This example demonstrates how to simulate a command-line call to the `ffcx.main.main()` function using Python. It specifies input files, scalar type, and output directory. ```python from ffcx.main import main # Simulate command-line call exit_code = main(["myform.ufl", "--scalar_type", "float32", "--dir", "./output"]) ``` -------------------------------- ### Example Usage of compile_ufl_objects Source: https://github.com/fenics/ffcx/blob/main/_autodocs/api-reference/compiler.md Demonstrates how to compile a simple UFL form using compile_ufl_objects. This example shows form creation, option retrieval, and code compilation. ```python import ufl from ffcx.compiler import compile_ufl_objects from ffcx.options import get_options # Create a simple UFL form cell = ufl.triangle V = ufl.FiniteElement("P", cell, 1) u, v = ufl.TrialFunction(V), ufl.TestFunction(V) a = ufl.inner(ufl.grad(u), ufl.grad(v)) * ufl.dx # Get default options options = get_options() # Compile the form code, suffixes = compile_ufl_objects([a], options, namespace="myform") # code is a list of strings (header and source content) # suffixes is (".h", ".c") for s, suffix in zip(code, suffixes): print(f"Generated {suffix} file with {len(s)} characters") ``` -------------------------------- ### Interface Include Directories Source: https://github.com/fenics/ffcx/blob/main/cmake/CMakeLists.txt Configures interface include directories for the library, specifying both build and installation paths. ```cmake target_include_directories(${PROJECT_NAME} INTERFACE $ $) ``` -------------------------------- ### Element Analysis and Identification Example Source: https://github.com/fenics/ffcx/blob/main/_autodocs/types.md Shows how to analyze UFL objects to obtain element data and access elements by their index. ```python from ffcx.analysis import analyze_ufl_objects from ffcx.options import get_options # Analyze forms containing multiple elements ufl_data = analyze_ufl_objects(forms, "float64") # Get element by index for i, element in enumerate(ufl_data.unique_elements): assert ufl_data.element_numbers[element] == i # element_hashes would contain hashes of element ``` -------------------------------- ### Integral Code Generation Example Source: https://github.com/fenics/ffcx/blob/main/_autodocs/compilation-pipeline.md Example C function generated for integral computation, including Jacobian, quadrature loops, and accumulation. ```c void integral_cell_abc123( ufcx_tabulate_tensor_float64 f, const double *restrict A, // coefficients const double *restrict w, // quad weights const double *restrict c, // constants // ... other params ) { // Jacobian computation const double J_01 = x[1][0] - x[0][0]; // ... more Jacobian entries // Quadrature loop for (int q = 0; q < nqp; ++q) { // Basis function evaluation const double phi_0_0 = J_det * w[q] * (1.0); // Accumulation A[0] += phi_0_0 * dphi_1_0; } } ``` -------------------------------- ### Python Example for FFCx Compilation Source: https://github.com/fenics/ffcx/blob/main/_autodocs/README.md Demonstrates importing and using the `compile_ufl_objects` function from the FFCx compiler with custom options. ```python # Python example from ffcx.compiler import compile_ufl_objects from ffcx.options import get_options ``` -------------------------------- ### Example: Writing Generated Code to Files Source: https://github.com/fenics/ffcx/blob/main/_autodocs/api-reference/naming-formatting.md Demonstrates the usage of `write_code` to save formatted code content into specified files. It creates a directory and generates files with given prefixes and extensions. ```python from ffcx.formatting import write_code from pathlib import Path code = [header_content, source_content] suffixes = (".h", ".c") prefix = "myform" output_dir = "./output" write_code(code, prefix, suffixes, output_dir) # Creates: # ./output/myform.h # ./output/myform.c ``` -------------------------------- ### FFCX Command-Line Compilation Source: https://github.com/fenics/ffcx/blob/main/_autodocs/configuration.md Examples of using the ffcx command-line tool to compile UFL files with custom options. Demonstrates setting scalar type, epsilon, output directory, namespace, and enabling visualization or profiling. ```bash # Compile a UFL file with custom options ffcx myform.ufl --scalar_type float32 --epsilon 1e-12 --sum_factorization # Specify output directory and namespace ffcx myform.ufl --dir ./output --namespace my_form # Enable visualisation of IR graph ffcx myform.ufl --visualise # Enable profiling ffcx myform.ufl --profile # Multiple files with explicit options ffcx -i file1.ufl file2.ufl -n ns1 ns2 -o out1 out2 ``` -------------------------------- ### FFCx CLI Interface Source: https://github.com/fenics/ffcx/blob/main/_autodocs/DOCUMENTATION_SUMMARY.txt Documentation for the FFCx Command Line Interface (CLI), including arguments, options, and usage examples. ```APIDOC ## CLI Interface ### Description Provides access to FFCx functionality through a command-line interface. All command-line arguments and options are documented, along with usage examples and configuration file details. ### Method CLI invocation ### Endpoint N/A (Command Line Interface) ### Parameters #### Command-line Arguments - **(Various)**: All command-line arguments are documented. #### Options - **(Various)**: All configuration options are explained with examples. ### Request Example ```bash ffcx --output-dir=./build --language=c++ path/to/your/form.ufl ``` ### Response #### Success Response - Output files generated based on specified options. #### Response Example (No specific response body, output is file-based) ``` -------------------------------- ### Get Default and Custom FFCx Options Source: https://github.com/fenics/ffcx/blob/main/_autodocs/quick-reference.md Demonstrates retrieving default FFCx compilation options and custom options for specific settings like single precision. ```python from ffcx.options import get_options # All default options options = get_options() # Single precision options = get_options({"scalar_type": "float32"}) ``` -------------------------------- ### Example: Generating and Comparing Expression Names Source: https://github.com/fenics/ffcx/blob/main/_autodocs/api-reference/naming-formatting.md Demonstrates how to use `expression_name` with a UFL expression and different sets of evaluation points to show that distinct inputs result in distinct names. ```python import numpy as np import ufl from ffcx.naming import expression_name cell = ufl.triangle V = ufl.FiniteElement("P", cell, 1) f = ufl.Coefficient(V) expr = ufl.grad(f) # Evaluation points points = np.array([[0.0, 0.0], [0.5, 0.0], [0.5, 0.5]]) name = expression_name((expr, points), "myexpr") # Returns: "expression_" # Different points, same expression = different name points2 = np.array([[0.25, 0.25]]) name2 = expression_name((expr, points2), "myexpr") assert name != name2 ``` -------------------------------- ### TensorPart Enum Conversion Example Source: https://github.com/fenics/ffcx/blob/main/_autodocs/types.md Demonstrates how to convert a string representation to a TensorPart enum value. ```python part = TensorPart.from_str("diagonal") ``` -------------------------------- ### Example Compiled Form Kernel Source: https://github.com/fenics/ffcx/blob/main/_autodocs/api-reference/codegen.md This C code represents a compiled form kernel, showing the computation of local variables, loop over quadrature points, and accumulation of tensor contributions. ```c // Compiled form kernel void integral_abc123( ufcx_tabulate_tensor_float64 restrict f, const double *restrict A, const double *restrict w, // ... other parameters ) { // Compute local variables const double J_01 = x[1][0] - x[0][0]; const double J_02 = x[2][0] - x[0][0]; const double J_11 = x[1][1] - x[0][1]; // ... determinant, inverse calculations // Loop over quadrature points for (int q = 0; q < nqp; ++q) { // Compute basis function values at quadrature point const double phi_0_0 = // ... const double dphi_0_0 = // ... // Accumulate tensor contributions A[0] += phi_0_0 * dphi_1_0 * J_det * w[q]; } } ``` -------------------------------- ### Get FFCx Options Source: https://github.com/fenics/ffcx/blob/main/_autodocs/README.md Retrieves FFCx configuration options. Options can be specified via JSON files, command-line arguments, or Python API parameters. ```python from ffcx.options import get_options options = get_options({ "scalar_type": "float64", # float32, float64, complex64, complex128 "sum_factorization": True, # Optimize high-order elements "language": "C", # C or numba "epsilon": 1e-14, # Precision for table values "table_rtol": 1e-6, # Relative tolerance "table_atol": 1e-9, # Absolute tolerance "verbosity": 20, # Logging level "part": "full", # full or diagonal }) ``` -------------------------------- ### Example Usage of generate_code Source: https://github.com/fenics/ffcx/blob/main/_autodocs/api-reference/codegen.md Demonstrates the complete workflow from UFL objects to generated C code using FFCx. This includes analyzing UFL objects, computing IR, and finally generating code. ```python import ufl from ffcx.analysis import analyze_ufl_objects from ffcx.ir.representation import compute_ir from ffcx.codegeneration.codegeneration import generate_code from ffcx.options import get_options # Create a form cell = ufl.triangle V = ufl.FiniteElement("P", cell, 1) u, v = ufl.TrialFunction(V), ufl.TestFunction(V) a = ufl.inner(ufl.grad(u), ufl.grad(v)) * ufl.dx # Compile through stages 1-2 options = get_options() ufl_data = analyze_ufl_objects([a], options["scalar_type"]) ir = compute_ir(ufl_data, {}, "myform", options, False) # Stage 3: Generate code code_blocks, suffixes = generate_code(ir, options) # code_blocks is CodeBlocks named tuple with generated C code # suffixes is (".h", ".c") for C backend ``` -------------------------------- ### Configure Compilation Options Source: https://github.com/fenics/ffcx/blob/main/_autodocs/README.md Configure compilation options to customize the code generation process. This example sets the scalar type to float32 and enables sum factorization. ```python options = get_options({"scalar_type": "float32", "sum_factorization": True}) ``` -------------------------------- ### Example UFCX Form Structure Source: https://github.com/fenics/ffcx/blob/main/_autodocs/api-reference/codegen.md This C code shows the structure of a ufcx_form, including its signature, rank, coefficient information, and integral definitions. It is generated by the form generator. ```c ufcx_form form_abc123 = { .signature = "abc123...", .rank = 2, .num_coefficients = 1, .original_coefficient_positions = { 0 }, .coefficient_names = { "u" }, .num_constants = 0, .constant_names = { NULL }, .num_integrals = 3, .integrals = { integral_cell, integral_ext_facet, integral_int_facet }, .integral_ids = { UFCX_CELL, UFCX_EXTERIOR_FACET, UFCX_INTERIOR_FACET }, .subdomain_ids = { "everywhere", "everywhere", "everywhere" }, // ... other fields }; ``` -------------------------------- ### CLI Entry Point Source: https://github.com/fenics/ffcx/blob/main/_autodocs/module-index.md The `ffcx` command-line tool serves as a primary entry point for users to interact with the FFCx functionality. ```APIDOC ## CLI ### Description The `ffcx` command-line tool provides a direct interface for users to leverage FFCx capabilities. ### Usage `ffcx [arguments]` ``` -------------------------------- ### Build FFCx Documentation with Sphinx Source: https://github.com/fenics/ffcx/blob/main/doc/README.md Run the Sphinx build command from the 'docs' directory to generate HTML documentation. The '-W' flag treats warnings as errors, ensuring documentation quality. ```bash python -m sphinx -W -b html source/ build/html/ ``` -------------------------------- ### Configure FFCx Options Source: https://github.com/fenics/ffcx/blob/main/_autodocs/quick-reference.md Use `get_options` to set various compiler parameters for precision, optimization, and backend. ```python options = get_options({ "scalar_type": "float64", "epsilon": 1e-15, "table_rtol": 1e-10, "table_atol": 1e-12 }) ``` ```python options = get_options({ "sum_factorization": True, "part": "diagonal" }) ``` ```python options = get_options({"language": "numba"}) ``` ```python options = get_options({"verbosity": 10}) ``` -------------------------------- ### Package Configuration File Generation Source: https://github.com/fenics/ffcx/blob/main/cmake/CMakeLists.txt Generates and installs CMake package configuration files (Config.cmake and ConfigVersion.cmake) for the project. ```cmake include(CMakePackageConfigHelpers) write_basic_package_version_file("${PROJECT_NAME}ConfigVersion.cmake" VERSION ${PROJECT_VERSION} COMPATIBILITY AnyNewerVersion) configure_package_config_file("${PROJECT_NAME}Config.cmake.in" "${PROJECT_NAME}Config.cmake" INSTALL_DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake) install(EXPORT ${PROJECT_NAME}_Targets FILE ${PROJECT_NAME}Targets.cmake NAMESPACE ${PROJECT_NAME}:: DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake) install(FILES "${PROJECT_BINARY_DIR}/${PROJECT_NAME}Config.cmake" "${PROJECT_BINARY_DIR}/${PROJECT_NAME}ConfigVersion.cmake" DESTINATION ${CMAKE_INSTALL_DATAROOTDIR}/${PROJECT_NAME}/cmake) ``` -------------------------------- ### Compile a Form from the Command Line Source: https://github.com/fenics/ffcx/blob/main/_autodocs/README.md Shows how to compile a UFL form using the FFCx command-line interface, specifying output directory and namespace. ```bash ffcx myform.ufl --namespace myns --dir ./output ``` -------------------------------- ### FFCx Configuration File Usage Source: https://github.com/fenics/ffcx/blob/main/_autodocs/quick-reference.md Demonstrates using JSON configuration files for persistent FFCx settings. Local configuration overrides user-level settings. ```json { "scalar_type": "float64", "epsilon": 1e-12, "verbosity": 20 } ``` ```json { "sum_factorization": true } ``` ```bash ffcx myform.ufl # Uses merged config from both files ``` -------------------------------- ### UFL Form Definition Source: https://github.com/fenics/ffcx/blob/main/_autodocs/README.md Example of a high-level UFL notation for variational forms. FFCx compiles these forms into efficient code. ```python a(u, v) = ∫(∇u · ∇v + u·v) dx L(v) = ∫(f · v) dx ``` -------------------------------- ### Import Quadrature Rule Utilities Source: https://github.com/fenics/ffcx/blob/main/_autodocs/api-reference/ir.md Imports necessary components for creating quadrature rules from the representation utilities module. ```python from ffcx.ir.representationutils import ( QuadratureRule, create_quadrature_points_and_weights, ) ``` -------------------------------- ### Get Reference Cell Vertices Source: https://github.com/fenics/ffcx/blob/main/_autodocs/api-reference/element-interface.md Retrieves the coordinates of vertices for a given reference cell type. Returns an array of vertex coordinates. ```python from ffcx.element_interface import reference_cell_vertices # Triangle vertices tri_verts = reference_cell_vertices("triangle") # Returns: [[0., 0.], [1., 0.], [0., 1.]] # Tetrahedron vertices tet_verts = reference_cell_vertices("tetrahedron") # Returns: [[0., 0., 0.], [1., 0., 0.], [0., 1., 0.], [0., 0., 1.]] # Interval vertices int_verts = reference_cell_vertices("interval") # Returns: [[0.], [1.]] ``` -------------------------------- ### Define Forms with High-Order Elements Source: https://github.com/fenics/ffcx/blob/main/_autodocs/quick-reference.md Example of defining forms using high polynomial order elements (P5 and DG4). Sum factorization can be enabled for efficiency. ```python import ufl cell = ufl.triangle # High-order elements P5 = ufl.FiniteElement("P", cell, 5) DG4 = ufl.FiniteElement("DG", cell, 4) V = ufl.FunctionSpace( ufl.Mesh(ufl.VectorElement("P", cell, 1)), P5 ) u, v = ufl.TrialFunction(V), ufl.TestFunction(V) # Optional: Enable sum factorization for efficiency # compile with --sum_factorization flag a = ufl.inner(ufl.grad(u), ufl.grad(v)) * ufl.dx ``` -------------------------------- ### Create Finite Element with Basix/UFL Source: https://github.com/fenics/ffcx/blob/main/_autodocs/api-reference/element-interface.md Demonstrates creating a finite element using Basix and UFL. This is a foundational step for defining element spaces. ```python import basix import basix.ufl # Create an element via Basix/UFL element = basix.ufl.FiniteElement("Lagrange", "triangle", 1) # Get cell type celltype = basix.CellType.triangle # Get geometry (vertices) geom = basix.geometry(celltype) # Get topology topo = basix.topology(celltype) ``` -------------------------------- ### Define Linear Form in UFL Source: https://github.com/fenics/ffcx/blob/main/_autodocs/glossary.md Example of defining a linear form (L) using UFL, which assembles into a vector. This is used for the right-hand side of a variational problem. ```python L(v) = ∫(f · v) dx ``` -------------------------------- ### UFL Functional Definition Source: https://github.com/fenics/ffcx/blob/main/_autodocs/glossary.md Example of defining a functional in UFL, which is a variational form of rank 0 that produces a scalar output. This is often used for energy minimization problems. ```python E = ufl.inner(f, f) * ufl.dx # functional (rank 0) ``` -------------------------------- ### Compiler API Source: https://github.com/fenics/ffcx/blob/main/_autodocs/module-index.md The `ffcx.compiler.compile_ufl_objects()` function is the recommended entry point for compiling UFL objects. ```APIDOC ## Compile UFL Objects ### Description Compiles UFL objects using the FFCx compiler. ### Method `ffcx.compiler.compile_ufl_objects()` ### Parameters (No specific parameters documented in the source) ### Request Example (No request example provided in the source) ### Response (No specific response details documented in the source) ``` -------------------------------- ### Example Usage of analyze_ufl_objects Source: https://github.com/fenics/ffcx/blob/main/_autodocs/api-reference/analysis.md Demonstrates how to use analyze_ufl_objects with a simple UFL form and expression. Accesses the results from the returned UFLData object, such as the number of forms and unique elements. ```python import ufl import numpy as np from ffcx.analysis import analyze_ufl_objects from ffcx.options import get_options # Create a simple form cell = ufl.triangle V = ufl.FiniteElement("P", cell, 1) u, v = ufl.TrialFunction(V), ufl.TestFunction(V) a = ufl.inner(ufl.grad(u), ufl.grad(v)) * ufl.dx # Create an expression for evaluation f = ufl.Coefficient(V) expr = ufl.grad(f) points = np.array([[0.0, 0.0], [0.5, 0.0]]) # Analyze options = get_options() ufl_data = analyze_ufl_objects( [a, expr, f.ufl_element()], scalar_type=options["scalar_type"] ) # Access results print(f"Number of forms: {len(ufl_data.form_data)}") print(f"Number of unique elements: {len(ufl_data.unique_elements)}") print(f"Element indices: {ufl_data.element_numbers}") ``` -------------------------------- ### FFCx Main Entry Point Function Signature Source: https://github.com/fenics/ffcx/blob/main/_autodocs/api-reference/main.md The `main` function serves as the primary entry point for the FFCx command-line interface. It accepts an optional sequence of strings representing command-line arguments. ```python from ffcx.main import main exit_code = main(args: Sequence[str] | None = None) -> int ``` -------------------------------- ### Import FFCx IR element table functions Source: https://github.com/fenics/ffcx/blob/main/_autodocs/api-reference/ir.md Import key functions for generating tabulated finite element basis functions. This includes `compute_element_tables` and `compute_element_matrix`. ```python from ffcx.ir.elementtables import ( compute_element_tables, compute_element_matrix, # ... and other element table functions ) ``` -------------------------------- ### UFLData Usage Example Source: https://github.com/fenics/ffcx/blob/main/_autodocs/api-reference/analysis.md Shows how to access fields within the UFLData named tuple after calling analyze_ufl_objects. Iterates through form data and unique elements to print relevant information. ```python # After calling analyze_ufl_objects(...) ufl_data = analyze_ufl_objects(objects, "float64") # Access individual fields for fd in ufl_data.form_data: # fd is a FormData object from UFL print(f"Form has {len(fd.integral_data)} integral groups") for elem in ufl_data.unique_elements: idx = ufl_data.element_numbers[elem] print(f"Element {idx}: {elem}") ``` -------------------------------- ### Compile Expressions for Evaluation Source: https://github.com/fenics/ffcx/blob/main/_autodocs/quick-reference.md Compile UFL expressions for evaluation at specific points using JIT compilation. This example demonstrates compiling the gradient of a coefficient and preparing it for evaluation at given coordinates. ```python import numpy as np import ufl from ffcx.codegeneration.jit import compile_expressions # Create coefficient and expression cell = ufl.triangle V = ufl.FiniteElement("P", cell, 1) f = ufl.Coefficient(V) expr = ufl.grad(f) # Define evaluation points (in reference triangle) points = np.array([ [0.0, 0.0], # Vertex 1 [1.0, 0.0], # Vertex 2 [0.0, 1.0], # Vertex 3 [0.333, 0.333] # Interior point ]) # Compile expression expressions, module, source = compile_expressions( [(expr, points)], options={"scalar_type": "float64"} ) expr_obj = expressions[0] # expr_obj can evaluate gradient of f at specified points ``` -------------------------------- ### Import FFCx IR integral computation functions Source: https://github.com/fenics/ffcx/blob/main/_autodocs/api-reference/ir.md Import necessary components for integral IR computation, such as `CommonExpressionIR`, `TensorPart`, and `compute_integral_ir`. ```python from ffcx.ir.integral import ( CommonExpressionIR, TensorPart, compute_integral_ir, ) ``` -------------------------------- ### Compile UFL form with options using CLI Source: https://github.com/fenics/ffcx/blob/main/_autodocs/README.md Compile a UFL form file using the ffcx command-line tool with specified options for scalar type, sum factorization, and output directory. ```bash ffcx myform.ufl --scalar_type float32 --sum_factorization --dir ./output ``` -------------------------------- ### Define Test Function in UFL Source: https://github.com/fenics/ffcx/blob/main/_autodocs/glossary.md Example of defining a test function 'v' within a finite element space 'V' using UFL. Test functions are used in variational formulations. ```python v = ufl.TestFunction(V) ``` -------------------------------- ### Project Definition and Versioning Source: https://github.com/fenics/ffcx/blob/main/cmake/CMakeLists.txt Defines the minimum CMake version, project name, version, description, and programming language. Sets the homepage URL for the project. ```cmake cmake_minimum_required(VERSION 3.19) # Development version: x.x.x.0 # Release version: x.x.x project(ufcx VERSION 0.11.0.0 DESCRIPTION "UFCx interface header for finite element kernels" LANGUAGES C HOMEPAGE_URL https://github.com/fenics/ffcx) ``` -------------------------------- ### Example Expression Evaluation Function Source: https://github.com/fenics/ffcx/blob/main/_autodocs/api-reference/codegen.md This C code defines the signature for an expression evaluation function. It takes an expression object, input arrays, and output parameters to compute and store expression results. ```c void expression_abc123( ufcx_expression *expression, const double *restrict A, const double *restrict w, const double *restrict c, // ... other parameters ); ``` -------------------------------- ### Integrate with Code Generation Source: https://github.com/fenics/ffcx/blob/main/_autodocs/api-reference/ir.md Demonstrates the integration of the FFCx IR with the code generation stage by calling the generate_code function. ```python # Compiler pipeline integration: from ffcx.codegeneration.codegeneration import generate_code code_blocks, suffixes = generate_code(ir, options) ``` -------------------------------- ### Compile UFL objects using Python API Source: https://github.com/fenics/ffcx/blob/main/_autodocs/README.md Programmatically compile UFL objects using the ffcx.compiler.compile_ufl_objects function. This requires defining the form, getting options, and then writing the generated code to files. ```python import ufl from ffcx.compiler import compile_ufl_objects from ffcx.options import get_options from ffcx.formatting import write_code # Define form cell = ufl.triangle V = ufl.FiniteElement("P", cell, 1) u, v = ufl.TrialFunction(V), ufl.TestFunction(V) a = ufl.inner(ufl.grad(u), ufl.grad(v)) * ufl.dx # Compile options = get_options() code, suffixes = compile_ufl_objects([a], options, namespace="myform") # Write to files write_code(code, "myform", suffixes, "./output") ``` -------------------------------- ### Compile UFL Form from Command Line Source: https://github.com/fenics/ffcx/blob/main/_autodocs/quick-reference.md Basic command-line compilation of a UFL form file. Use custom options for specific compilation behaviors. ```bash # Basic compilation ffcx myform.ufl # With custom options ffcx myform.ufl \ --scalar_type float32 \ --sum_factorization \ --dir ./output \ --namespace my_form # Multiple files with explicit options ffcx -i form1.ufl form2.ufl \ -n form1_ns form2_ns \ -o form1_out form2_out # With visualization ffcx myform.ufl --visualise # With profiling ffcx myform.ufl --profile # High verbosity debugging ffcx myform.ufl --verbosity 10 ``` -------------------------------- ### Define Trial Function in UFL Source: https://github.com/fenics/ffcx/blob/main/_autodocs/glossary.md Example of defining a trial function 'u' within a finite element space 'V' using UFL. Trial functions represent the unknown solution in variational formulations. ```python u = ufl.TrialFunction(V) ``` -------------------------------- ### Multiple Files with Explicit Options Source: https://github.com/fenics/ffcx/blob/main/_autodocs/api-reference/main.md Compiles multiple UFL files, specifying corresponding namespaces and output filenames, along with a common output directory. ```bash # Compile multiple files with corresponding namespaces and outputs ffcx -i form1.ufl form2.ufl -n form1_ns form2_ns -o form1_out form2_out --dir ./generated # Output: # ./generated/form1_out.h, ./generated/form1_out.c # ./generated/form2_out.h, ./generated/form2_out.c ``` -------------------------------- ### Compile UFL form using CLI Source: https://github.com/fenics/ffcx/blob/main/_autodocs/README.md Use the ffcx command-line tool to compile a UFL form file into C code. Outputs are typically myform.h and myform.c. ```bash ffcx myform.ufl ``` -------------------------------- ### Compile a Form using Python API Source: https://github.com/fenics/ffcx/blob/main/_autodocs/README.md Illustrates compiling UFL objects into code using the FFCx Python API, retrieving generated code and suffixes. ```python from ffcx.compiler import compile_ufl_objects from ffcx.options import get_options code, suffixes = compile_ufl_objects([form], get_options()) ``` -------------------------------- ### Get FFCX Options Source: https://github.com/fenics/ffcx/blob/main/_autodocs/configuration.md Retrieve FFCX options, either using default values or by overriding specific options. This function merges options from various sources, including default, config files, and priority options. ```python from ffcx.options import get_options # Use defaults options = get_options() # Override specific options options = get_options({"scalar_type": "float32", "epsilon": 1e-10}) # Typical API call from ffcx.compiler import compile_ufl_objects code, suffixes = compile_ufl_objects(forms, options) ``` -------------------------------- ### C Integral Function Signature Source: https://github.com/fenics/ffcx/blob/main/_autodocs/api-reference/codegen.md Example signature for a generated C integral function, compatible with the UFCx interface. It outlines the parameters required for tabulating tensor functions and handling coefficient values, weights, and mesh data. ```c void integral_( ufcx_tabulate_tensor_float64 f, const double *restrict A, const double *restrict b, const double *restrict w, const double *restrict c, const int *restrict entity_local_index, const uint8_t *restrict quadrature_permutation, const int *restrict mesh_cell_index, const int *restrict cell_permutation ); ``` -------------------------------- ### UFL Trial and Test Functions Source: https://github.com/fenics/ffcx/blob/main/_autodocs/glossary.md Demonstrates the creation of trial and test functions in UFL, which serve as arguments in variational forms. Trial functions are typically argument 1 and test functions are argument 0 in bilinear forms. ```python u = ufl.TrialFunction(V) # argument with number 1 v = ufl.TestFunction(V) # argument with number 0 a = ufl.inner(u, v) * ufl.dx # form with 2 arguments (bilinear) ```