scimba_torch.numerical_solvers.abstract_projector

An abstract class for a nonlinear projector.

Classes

AbstractNonlinearProjector(space, rhs[, has_bc])

Abstract class for a nonlinear projector.

class AbstractNonlinearProjector(space, rhs, has_bc=False, **kwargs)[source]

Bases: ABC

Abstract class for a nonlinear projector.

This class defines a nonlinear projector with various projection options and an optimization method. It is used to solve projection problems in a given approximation space, using optimization methods.

Parameters:
  • space (AbstractApproxSpace) – The approximation space where the projection will take place.

  • rhs (Optional[Callable[..., Tensor]]) – The function representing the right-hand side of the problem.

  • has_bc (bool) – Whether boundary conditions are present.

  • **kwargs – Additional parameters, such as the type of projection, losses, and optimizers.

type_projection: str

The type of projection (default is “L2”).

losses: GenericLosses

The losses to minimize during optimization.

optimizer: OptimizerData

The optimizer used for parameter updates.

bool_linesearch: bool

Whether to use line search during optimization.

type_linesearch: str

The type of line search to use.

data_linesearch: dict

Parameters for line search.

projection_data: dict

Data related to the projection process.

default_lr: float

Default learning rate for the optimizer.

ridge

Ridge regularization parameter.

bool_preconditioner

Whether to use a preconditioner.

nb_epoch_preconditioner_computing

Number of epochs for computing the preconditioner.

preconditioner

The preconditioner used during optimization.

best_loss

Best loss achieved during optimization.

abstract get_dof(flag_scope='all', flag_format='list')[source]

Retrieves the degrees of freedom (DoF) of the approximation space.

Parameters:
  • flag_scope (str) – Specifies the scope of the parameters to return. (Default value = “all”)

  • flag_format (str) – The format for returning the parameters. (Default value = “list”)

Return type:

list | Tensor

Returns:

The degrees of freedom in the specified format.

get_gradient()[source]

Retrieves the gradient of the parameters.

Return type:

Tensor

Returns:

The gradient of the parameters.

Raises:

RuntimeError – If no gradient is available.

set_gradient(grads)[source]

Sets the gradient of the parameters.

Parameters:

grads (Tensor) – The gradient values to set.

Raises:

RuntimeError – If no gradient is available.

Return type:

None

get_loss_grad_loss(data=None, **kwargs)[source]

Returns functions to compute the loss and its gradient.

Uses fixed data and non-fixed parameter’s values.

Parameters:
  • data (None | tuple[LabelTensor, ...]) – The data to use for computing the loss and gradient. (Default value = None)

  • **kwargs – Additional arguments.

Return type:

tuple[Callable[[Tensor], Tensor], Callable[[Tensor], Tensor]]

Returns:

A tuple of functions (L, gradL) to compute the loss and its gradient.

abstract sample_all_vars(**kwargs)[source]

Samples values in the domains of the arguments of the function to project.

Parameters:

**kwargs (Any) – Additional arguments for sampling.

Return type:

tuple[LabelTensor, ...]

Returns:

A tuple of sampled tensors.

abstract assembly_post_sampling(data)[source]

Assembles the projection problem after sampling.

Parameters:

data (tuple[LabelTensor, ...]) – The sampled data.

Return type:

tuple[tuple[Tensor, ...], tuple[Tensor, ...]]

Returns:

A tuple of tuples containing the assembled left and right-hand sides.

abstract assembly(**kwargs)[source]

Abstract method to assemble the terms of the projection problem.

Parameters:

**kwargs (Any) – Additional arguments required for the assembly.

Return type:

tuple[tuple[Tensor, ...], tuple[Tensor, ...]]

Returns:

A tuple of tuples containing the assembled left and right-hand sides.

solve_nnstep(first_call=True, **kwargs)[source]

Solves the projection problem using optimization and the defined losses.

This method performs optimization to minimize the loss function over a given number of epochs, or until a specified target for the loss is reached. At each epoch, it updates the parameters based on the calculated loss and the optimizer.

Parameters:
  • first_call (bool) – Whether this is the first call to the method. (Default value = True)

  • **kwargs – Additional parameters such as the number of epochs and verbosity.

Return type:

None

solve_linearstep(**kwargs)[source]

Find the optimal values for the parameters of the last layer.

Targets the neural network of the approximation space with a least square solver.

Parameters:

**kwargs (Any) – Additional parameters for solving the linear step.

Raises:

NotImplementedError – If the method is called for a non-scalar equation.

Return type:

None

solve(**kwargs)[source]

Solves numerically, with optimization, the projection problem.

This method performs interleaved optimization steps on the parameters of the inner layers and least square fitting steps on the parameters of the last layer.

Parameters:

**kwargs (Any) – Additional parameters for solving the projection problem.

Return type:

None

make_tuple(x)[source]

Converts input to a tuple of tensors.

Parameters:

x (Tensor | tuple[Tensor, ...]) – Input tensor or tuple of tensors.

Return type:

tuple

Returns:

A tuple of tensors.

dict_for_save()[source]

Returns a dictionary representing the state of the projector for saving.

Return type:

dict[str, dict | float]

Returns:

A dictionary containing the state of the projector.

load_from_dict(state_dict, **kwargs)[source]

Loads the state of the projector from a dictionary.

Parameters:
  • state_dict (dict[str, Any]) – The dictionary containing the state to load.

  • **kwargs (dict) – Additional parameters for loading the state.

Return type:

None

save(scriptname, postfix='', path=PosixPath('/home/scimba'), folder_name='.scimba/scimba_torch', verbose=None)[source]

Saves the current state of the projector to a file.

Parameters:
  • scriptname (str) – The name of the script.

  • postfix (str) – An optional postfix for the filename. (Default value = “”)

  • path (Path) – (Default value = DEFAULT_PATH_FOR_SAVING)

  • folder_name (str) – (Default value = FOLDER_FOR_SAVED_PROJECTORS)

  • verbose (bool | None) – if None then global verbosity of scimba is used; otherwise the wanted verbosity (Default value = None)

Return type:

None

load(scriptname, postfix='', path=PosixPath('/home/scimba'), folder_name='.scimba/scimba_torch', verbose=None)[source]

Loads the current state of the projector from a file.

Parameters:
  • scriptname (str) – The name of the script.

  • postfix (str) – An optional postfix for the filename. (Default value = “”)

  • path (Path) – (Default value = DEFAULT_PATH_FOR_SAVING)

  • folder_name (str) – (Default value = FOLDER_FOR_SAVED_PROJECTORS)

  • verbose (bool | None) – if None then global verbosity of scimba is used; otherwise the wanted verbosity (Default value = None)

Return type:

bool

Returns:

Whether the load was successful.