gfem.solve#

gfem.solve: the one verb that turns a model and a problem into a result.

Functions#

release_memory()

Free everything gfem keeps between solves in this process.

solve(…)

Solve one problem type on a model.

Module Contents#

release_memory()#

Free everything gfem keeps between solves in this process.

That is what SolverOptions.retain_memory bounds, and the meshes and spaces it does not: what differentiated solves keep for their gradients, the solver plans, factorizations, assembled parts and wave port couplings kept for later designs, and the last discretizations of each mesh.

Results, fields, meshes and models you hold are not touched, and no later result changes. A gradient whose solve was released is recomputed from the design when it runs, and the next solve builds its mesh, spaces and solver plan again: this costs time, never accuracy. Call it between two studies in one process, for example before a check at a higher order after an optimization loop.

retain_memory=0 keeps nothing from then on; this call drops what is kept now. It does not touch a solve cache on disk (cache=). JAX’s own compilation caches are not touched: see jax.clear_caches(). Under mpiexec or srun every rank must call it at the same point.

Example

After an optimization loop, before another study:

gfem.release_memory()
Return type:

None

solve(model: Model, problem: Electrostatic, *, options: SolverOptions | None = None, comm: Any = None, cache: str | PathLike | None = None) → ElectrostaticResult#
solve(model: Model, problem: DCConduction, *, options: SolverOptions | None = None, comm: Any = None, cache: str | PathLike | None = None) → DCConductionResult
solve(model: Model, problem: SteadyThermal, *, options: SolverOptions | None = None, comm: Any = None, cache: str | PathLike | None = None) → SteadyThermalResult
solve(model: Model, problem: TransientThermal, *, options: SolverOptions | None = None, comm: Any = None, cache: str | PathLike | None = None) → TransientThermalResult
solve(model: Model, problem: Driven, *, options: SolverOptions | None = None, comm: Any = None, cache: str | PathLike | None = None) → DrivenResult
solve(model: Model, problem: Eigenmode, *, options: SolverOptions | None = None, comm: Any = None, cache: str | PathLike | None = None) → EigenmodeResult
solve(model: Model, problem: PortMode, *, options: SolverOptions | None = None, comm: Any = None, cache: str | PathLike | None = None) → PortModeResult
solve(model: Model, problem: Piezoelectric, *, options: SolverOptions | None = None, comm: Any = None, cache: str | PathLike | None = None) → PiezoelectricResult
solve(model: Model, problem: MechanicalEigenmode, *, options: SolverOptions | None = None, comm: Any = None, cache: str | PathLike | None = None) → MechanicalEigenmodeResult

Solve one problem type on a model.

A pure function of its inputs: the model and problem are not changed, and the same inputs give the same result.

It is also a JAX function of the model’s arrays (the mesh node coordinates and every MaterialField array): jax.grad, jax.jit and jax.vmap (sequential solves) apply. Each input then has a gradient (for an electrostatic capacitance: the MaterialField eps_r values), an exact zero gradient if the problem never reads it (conductivity in electrostatics), or raises gfem.NonDifferentiableError when jax.grad, jax.jit or jax.vmap traces the function, if the problem reads it but gfem cannot differentiate it (for example a problem value such as a frequency, or a field-derived output). jax.lax.stop_gradient on an input or an output is the opt-out. Only reverse mode is supported.

Example

S-parameters of a model over a band:

result = gfem.solve(model, gfem.Driven(frequency=[4e9, 5e9, 6e9]))
s21 = result.s["p2", "p1"]  # S21 over frequency
Parameters:
  • model – The device.

  • problem – What to solve, e.g. gfem.Electrostatic.

  • options – The numerics; None is SolverOptions().

  • comm – An mpi4py communicator. None lets the launcher decide: one process without one, the world communicator under mpiexec or srun, where every rank makes the same call. A communicator of size 1, such as MPI.COMM_SELF, runs one independent copy.

  • cache – A directory for the solve cache. The result is stored there under its solve key, a hash of everything it depends on (the mesh, materials, problem, the solver options except threads, frequency_workers, retain_memory and device, gfem’s version and code, and the versions of NGSolve and, when the options select them, PETSc or SLEPc), and the same call again reads it back instead of solving: the same result class and values, its fields on model.mesh, and the solve’s warnings shown again at your line. A path that does not exist, or an empty directory, becomes a cache; delete the directory to clear it. A result read back may have been solved on another machine, device, thread count or rank count: the same answer to the precision gfem documents, not bit-for-bit. A device="cuda" call that reads its result needs no GPU; one that solves does. A solve under jax.grad, jax.jit or jax.vmap, one that raises, and one with an input the cache cannot record (a subclass of a gfem class, with a gfem.SolveCacheWarning) are solved and store nothing, as is one that keeps fields on more than one rank (with the same warning). The logger "gfem.solve_cache" reports each read and store at INFO. None (the default) solves without a cache. The save and export recipe, “Solve once, read it back”, shows it at work.

Returns:

The problem type’s result, e.g. gfem.ElectrostaticResult for an Electrostatic problem.

Raises:
  • TypeError – An argument has the wrong type.

  • DistributedUnsupportedError – On several ranks, this solve has no distributed path; feature names what, the message the remedy.

  • SolverUnavailableError – Launched on several ranks without a usable mpi4py and Open MPI.

  • RuntimeError – On several ranks, the ranks hold different inputs; the message names one.

  • RegionNotFoundError – The problem names a region the mesh lacks.

  • ValueError – The problem does not fit the model; the message gives the remedy. Or cache names a non-empty directory that is not a solve cache.

  • NonDifferentiableError – Under jax.grad, an input or output gfem does not differentiate is perturbed or used.

  • LinearSolverConvergenceError – An iterative linear solve (linear_solver="bddc_gmres" or "pmg_gmres", or "petsc" with a Krylov ksp_type) did not converge; the message gives the remedy. A uniform gfem.Driven sweep stops there, and the error’s result holds the frequencies it solved, unsolved the rest.

  • NotImplementedError – On a 2D mesh, an input with no 2D path (the problem type, wave_vector, the adaptive sweep, comm, an iterative solver, a GPU, moved nodes); the message names the remedy.