Optimization#

Directory#

class OptimizationDirectory#

Provides a collection of methods related to optimizations.

This class is accessed through client.optimizations.

Example

import ansys.simai.core

simai = ansys.simai.core.from_config()
simai.optimizations.run_parametric(
    ...
)  # or simai.optimizations.run_non_parametric(...)
get(optimization_id: str) Optimization#

Get a specific optimization object from the server.

Parameters:

optimization_id (str) – ID of the optimization.

Returns:

Optimization.

Return type:

Optimization

run_non_parametric(geometry: Geometry | str, bounding_boxes: List[List[float]], n_iters: int, symmetries: List[Literal['x', 'y', 'z', 'X', 'Y', 'Z']] | None = None, axial_symmetry: Literal['x', 'y', 'z'] | None = None, boundary_conditions: Dict[str, float] | None = None, minimize: List[str] | None = None, maximize: List[str] | None = None, max_displacement: List[float] | None = None, show_progress: bool = False) OptimizationResult#

Run an optimization loop to generate geometries, server-side, using automorphing. Automorphing is a non-parametric deformation of a surface geometry.

Parameters:
  • geometry (Geometry | str) – Required. The object (Geometry) or the ID (str) of the baseline geometry on which to perform the non-parametric optimization and that has already been used to build an AI model. The optimization will run in the same workspace as the model.

  • bounding_boxes (List[List[float]]) –

    Required. The list of the bounds of the different boxes that will define the absolute locations of the geometry to optimize. It is a list of lists, and each sub-list must have exactly six items with the following expected order: [x_min, x_max, y_min, y_max, z_min, z_max].

    Example format:

    [
    [box1_xmin, box1_xmax, box1_ymin, box1_ymax, box1_zmin, box1_zmax],
    [box2_xmin, box2_xmax, box2_ymin, box2_ymax, box2_zmin, box2_zmax],
    ...
    ]
    

  • n_iters (int) – Required. The number of optimization iterations. This number must be a strictly positive integer. It will define the number of automorphed geometries uploaded to the SimAI workspace.

  • symmetries (List[Literal['x', 'y', 'z', 'X', 'Y', 'Z']] | None) –

    Optional. The list of symmetry axes, axes being x, y, and z, defining a plane around which the geometry is mirrored.

    • The planar symmetry is applied to all the bounding_boxes defined.

    • symmetries and axial_symmetry are mutually exclusive parameters.

  • axial_symmetry (Literal['x', 'y', 'z'] | None) –

    Optional. The axis, defined by a scalar, along which axial symmetry is applied. For the deformation to be axially symmetrical:

    • The center of the bounding box must coincide with the targeted central axis.

    • The mesh should be axially symmetrical in the bounding box.

    • The axial_symmetry is applied to all the bounding_boxes defined.

    • symmetries and axial_symmetry are mutually exclusive parameters.

  • boundary_conditions (Dict[str, float] | None) – Optional. The values of the boundary conditions to perform the optimization at. The values must correspond to existing boundary conditions already defined in your SimAI workspace.

  • minimize (List[str] | None) – Required if no maximize parameter is defined. A list of one global coefficient to minimize. This global coefficient must correspond to one of the existing coefficients defined in your model configuration.

  • maximize (List[str] | None) –

    Required if no minimize parameter is defined. A list of one global coefficient to maximize. This global coefficient must correspond to one of the existing coefficients defined in your model configuration.

    Note

    • minimize and maximize are mutually exclusive objectives; define only one.

    • The defined objective must be computed from the surface fields because mesh nodes must be involved.

  • max_displacement (List[float] | None) –

    Optional. User-defined constraint on the maximum allowable deformation of the initial mesh in non-parametric optimization. It is specified as a list (max_displacement) matching the number of bounding boxes (bounding_boxes).

    For example, for two bounding boxes:

    • bounding_boxes = [[0,1,0,2,0,4],[10,2,10,4,10,5]]

    • max_displacement = [0.002, 0.001]

    Each value limits the displacement within the corresponding bounding box, using the same metric as the bounding box coordinates.

  • show_progress (bool) – Optional. Whether to print progress bar on stdout. It is updated each time a new iteration is completed.

Example

import ansys.simai.core

simai = ansys.simai.core.from_config(workspace="optimization-workspace")
geometry = simai.geometries.list()[0]

simai.optimizations.run_non_parametric(
    geometry,
    bounding_boxes=[[0, 1, 0, 1, 0, 1]],
    boundary_conditions={"VelocityX": 10.5},
    symmetries=["y"],
    n_iters=10,
    minimize=["TotalForceX"],
    show_progress=True,
)
Returns:

An object containing the results of the optimization.

Return type:

OptimizationResult

run_parametric(geometry_generation_fn: Callable[[...], Path | str | PathLike | Tuple[BinaryIO | RawIOBase | BufferedIOBase | Path | str | PathLike, str]], geometry_parameters: Dict[str, Tuple[float, float]], n_iters: int, boundary_conditions: Dict[str, float] | None = None, minimize: List[str] | None = None, maximize: List[str] | None = None, outcome_constraints: List[str] | None = None, show_progress: bool = False, workspace: Workspace | str | None = None) List[Dict]#

Run an optimization loop to generate a parametric geometry, client-side.

Warning

This feature is deprecated.

Parameters:
  • geometry_generation_fn (Callable[[...], Path | str | PathLike | Tuple[BinaryIO | RawIOBase | BufferedIOBase | Path | str | PathLike, str]]) – Function to call to generate a new geometry with the generated parameters. This parameter should return a NamedFile object.

  • geometry_parameters (Dict[str, Tuple[float, float]]) – Name of the geometry parameters and their bounds or possible values (choices).

  • boundary_conditions (Dict[str, float] | None) – Values of the boundary conditions to perform the optimization at. The values should map to existing boundary conditions in your project/workspace.

  • n_iters (int) – Number of iterations of the optimization loop.

  • minimize (List[str] | None) – List of global coefficients to minimize. The global coefficients should map to existing coefficients in your project/workspace.

  • maximize (List[str] | None) – List of global coefficients to maximize. The global coefficients should map to existing coefficients in your project/workspace.

  • outcome_constraints (List[str] | None) –

    List of strings representing a linear inequality constraint on a global coefficient. The outcome constraint should be in the form gc >= x, where:

    • gc is a valid global coefficient name.

    • x is a float bound.

    • The comparison operator is >= or <=.

  • show_progress (bool) – Whether to print progress on stdout.

  • workspace (Workspace | str | None) – Workspace to run the optimization in. If a workspace is not specified, the default is the configured workspace.

Returns:

List of dictionaries representing the result of each iteration. when constraints are specified, the list can be shorter than the number of iterations.

Return type:

List[Dict]

Warning

This is a long-running process and your computer must be powered on to generate the iterations. This method attempts to prevent your computer from sleeping, keeping your computer open during the process.

Example

import ansys.simai.core


# Function takes the parameters
def my_geometry_generation_function(param_a, param_b):
    # Implementation
    return "/path/to/generated/geometry.stl"


simai = ansys.simai.core.from_config(workspace="optimization-workspace")

simai.optimizations.run_parametric(
    geometry_generation_fn=my_geometry_generation_function,
    geometry_parameters={
        "param_a": {"bounds": (-12.5, 12.5)},
        "param_b": {"choices": (0, 1)},
    },
    minimize=["TotalForceX"],
    boundary_conditions={"VelocityX": 10.5},
    outcome_constraints=["TotalForceY <= 10"],
    n_iters=100,
)

Model#

class Optimization#

Provides the local representation of an optimization definition object.

reload() None#

Refresh the object with its representation from the server.

wait(timeout: float | None = None) bool#

Wait for all jobs concerning the object to either finish or fail.

Parameters:

timeout (float | None) – Maximum amount of time in seconds to wait. The default is None, it means that there is no maximum on the time to wait.

Returns:

True if the computation has finished, False if the operation timed out.

Return type:

bool

property failure_reason#

Optional message giving the causes for why the creation of the object failed.

See also

property fields: dict#

Dictionary containing the raw object representation.

property has_failed#

Boolean indicating if the creation of the object failed.

property id: str#

ID of the object on the server.

property is_pending#

Boolean indicating if the object is still in creation. The value becomes False once object creation is either successful or has failed.

property is_ready#

Boolean indicating if the object has finished creating without error.

class OptimizationResult#

Result for a non-parametric optimization.

list_geometries() List[Geometry]#

List of geometries generated by the optimization.

list_objectives() List[Dict]#

List of objectives generated by the optimization.

property optimization: Optimization#

Optimization object of the optimization run.