Skip to content

salvus.opt.models.block_model

salvus.opt.models.block_model salvus opt models block_model

A model using Salvus’ native mesh format.

Functions

get_mass_matrix_meshblock()

def get_mass_matrix_meshblock(mesh_block: MeshBlock) -> npt.NDArray: ...

Compute the global mass matrix of a mesh block.

This function computes the global mass matrix of a mesh using the GLL quadrature points and weights. The mass matrix is computed as the integral of the product of the shape functions over the element volume.

Parameters
  • mesh_block MeshBlock — The mesh block for which to compute the mass matrix.
Returns npt.NDArray — The mass matrix of the mesh as a 2D numpy array of shape (n_elem, n_nodes_per_element).

Classes

BlockModel

class BlockModel(salvus.opt.models.base_model.BaseModel):
    def __init__(
        self,
        name: str,
        model: str | pathlib.Path | Mesh | list[DataBlock],
        fields: list[str],
        compute_auxiliary_data: bool = True,
    ): ...

Salvus mesh inversion model.

Parameters
  • name str — Name of the model.
  • model str | pathlib.Path | Mesh | list[DataBlock] — The model either as a list of DataBlock objects, a Mesh object, or a path to a serialized BlockModel object.
  • fields list[str] — Names of the elemental fields to use.
  • compute_auxiliary_data bool = True — Whether to compute auxiliary data for the blocks.
Attributes
model list[DataBlock]

The actual dataset representing the model.

name str

Model name.

status ModelStatus

Model status to keep track of whether the model has been modified.

vector npt.NDArray[np.float64]

The block model as a vector.

The vector is ordered by block, then by field within each block. Does not include fields that are not part of a given block’s material.

Methods
from_disk()
def from_disk(parent_folder: pathlib.Path, d: dict) -> BlockModel: ...

Load a model from disk. This will use the dictionary and path provided.

Parameters
  • parent_folder pathlib.Path — Path to store the model.
  • d dict — Dictionary containing meta information of the model.
Returns BlockModel
from_json()
def from_json(d: dict) -> Self: ...

Create a model from a dictionary.

Parameters
  • d dict — Dictionary containing meta information of the model.
Returns Self
update_notification_count()
def update_notification_count() -> None: ...

Update the notification count in the decorator info.

Returns None
copy()
def copy(self, name: str) -> BlockModel: ...

Copy the model.

Will load the model from disk if necessary, and leave it in memory.

Parameters
  • name str — Name of the new model.
Returns BlockModel — A copy of the model with the new name. Always fully in memory.
dot()
def dot(self, other: BlockModel) -> float: ...

Compute the dot product between this model and another model.

Parameters
  • other BlockModel — The other model to compute the dot product with.
Returns float — The scalar dot product as a float.
expand_scalar_fields()
def expand_scalar_fields(self) -> None: ...

Expand any scalar fields in the model to full arrays.

This modifies the model in place.

Returns None
get_block()
def get_block(self, block_id: int) -> DataBlock | None: ...

Get a block by its ID.

Parameters
  • block_id int — The ID of the block to retrieve.
Returns DataBlock | None — The DataBlock with the specified ID, or None if not found.
get_data_from_block()
def get_data_from_block(
    self, block: MeshBlock, global_material_fields: set[str]
) -> dict[str, npt.NDArray | types.scalar_]: ...

Extract the data from a single mesh block.

If the block has a material defined, the extracted data will only contain the fields that are required for this material. Any missing data will be a scalar zero.

Parameters
  • block MeshBlock — The mesh block to extract the data from.
  • global_material_fields set[str] — Set of all material fields defined in the mesh.
Returns dict[str, npt.NDArray | types.scalar_] — Dictionary mapping field names to their data arrays.
get_metric()
def get_metric(self) -> dict[str, npt.NDArray]: ...

Get the metric for the model.

The data is stored once per mesh point, so the metric is the assembled mass matrix of every block.

Returns dict[str, npt.NDArray] — A dictionary with the per-block weights of the metric.
max()
def max(self, other: BlockModel) -> BlockModel: ...

In-place point-wise maximum.

Parameters
  • other BlockModel — Model to compare with.
Returns BlockModel — A reference to the modified input model.
min()
def min(self, other: BlockModel) -> BlockModel: ...

In-place point-wise minimum.

Parameters
  • other BlockModel — Model to compare with.
Returns BlockModel — A reference to the modified input model.
norm()
def norm(self) -> float: ...

Return the model norm.

Returns float
norm_inf()
def norm_inf(self) -> float: ...

Compute the infinity norm of the model.

Returns float
to_dict()
def to_dict(self) -> dict[str, dict[str, npt.NDArray]]: ...

Convert the model to a dictionary representation.

Constant fields are expanded to full arrays in the returned dictionary. The model itself is not modified.

Returns dict[str, dict[str, npt.NDArray]] — A dictionary representation of the model.
to_disk()
def to_disk(self, parent_folder: pathlib.Path | str) -> dict: ...

Serialize the model to JSON (and an associated pair of an npy file and a toml file containt some basic information on the blocks).

Parameters
  • parent_folder pathlib.Path | str — Parent folder where the JSON + npy files should be stored.
Returns dict — A dictionary containing meta information of the serialized model.
to_json()
def to_json(
    self,
    external_file_hash: Optional[str] = None,
    timer: Optional[salvus._core.timer.timer.Timer] = None,
    log_to_logger: bool = False,
    comm: Optional[int] = None,
) -> Dict: ...

Serialize the object to a dictionary that can be written to JSON.

Parameters
  • external_file_hash Optional[str] = None — Hash of any external files associated with this object. Can be passed here in which case it will be stored in a centralized location in the JSON file.
  • timer Optional[salvus._core.timer.timer.Timer] = None — Execution timer.
  • log_to_logger bool = False — Log timings to the logger.
  • comm Optional[int] = None — MPI communicator, if any.
Returns Dict
validate_consistency()
def validate_consistency(
    self, other: BlockModel, description: str = "other model"
) -> None: ...

Check that another model has the same parameters and array shapes.

Parameters
  • other BlockModel — The model to compare to, e.g., a weight or a reference model.
  • description str = 'other model' — Name of other in error messages.
Returns None