gfem.solve#
gfem.solve: the one verb that turns a model and a problem into a result.
Functions#
Free everything gfem keeps between solves in this process. |
|
|
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_memorybounds, 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=0keeps 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: seejax.clear_caches(). Undermpiexecorsrunevery 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
MaterialFieldarray):jax.grad,jax.jitandjax.vmap(sequential solves) apply. Each input then has a gradient (for an electrostatic capacitance: theMaterialFieldeps_rvalues), an exact zero gradient if the problem never reads it (conductivityin electrostatics), or raisesgfem.NonDifferentiableErrorwhenjax.grad,jax.jitorjax.vmaptraces 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_gradienton 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;
NoneisSolverOptions().comm – An mpi4py communicator.
Nonelets the launcher decide: one process without one, the world communicator undermpiexecorsrun, where every rank makes the same call. A communicator of size 1, such asMPI.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_memoryanddevice, 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 onmodel.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. Adevice="cuda"call that reads its result needs no GPU; one that solves does. A solve underjax.grad,jax.jitorjax.vmap, one that raises, and one with an input the cache cannot record (a subclass of a gfem class, with agfem.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.ElectrostaticResultfor anElectrostaticproblem.- Raises:
TypeError – An argument has the wrong type.
DistributedUnsupportedError – On several ranks, this solve has no distributed path;
featurenames 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
cachenames 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 Krylovksp_type) did not converge; the message gives the remedy. A uniformgfem.Drivensweep stops there, and the error’sresultholds the frequencies it solved,unsolvedthe 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.