LocalMechanicalPool#

class ansys.mechanical.core.pool.LocalMechanicalPool(n_instances, wait=True, port=MECHANICAL_DEFAULT_PORT, progress_bar=True, restart_failed=True, **kwargs)#

Create a pool of Mechanical instances.

Parameters:
n_instanceint

Number of Mechanical instances to create in the pool.

waitbool, optional

Whether to wait for the pool to be initialized. The default is True. When False, the pool starts in the background, in which case all resources might not be immediately available.

starting_portint, optional

Starting port for the instances. The default is 10000.

progress_barbool, optional

Whether to show a progress bar when starting the pool. The default is True, but the progress bar is not shown when wait=False.

restart_failedbool, optional

Whether to restart any failed instances in the pool. The default is True.

**kwargsdict, optional

Additional keyword arguments. For a list of all keyword arguments, use the ansys.mechanical.core.launch_mechanical() function. If the exec_file keyword argument is found, it is used to start instances. PyPIM is used to create instances if the following conditions are met:

  • PyPIM is configured.

  • version is specified.

  • exec_file is not specified.

Examples

Create a pool of 10 Mechanical instances.

>>> from ansys.mechanical.core import LocalMechanicalPool
>>> pool = LocalMechanicalPool(10)
Creating Pool: 100%|########| 10/10 [00:01<00:00,  1.43it/s]

On Windows, create a pool while specifying the Mechanical executable file.

>>> exec_file = 'C:/Program Files/ANSYS Inc/v242/aisol/bin/winx64/AnsysWBU.exe'
>>> pool = LocalMechanicalPool(10, exec_file=exec_file)
Creating Pool: 100%|########| 10/10 [00:01<00:00,  1.43it/s]

On Linux, create a pool while specifying the Mechanical executable file.

>>> exec_file = '/ansys_inc/v242/aisol/.workbench'
>>> pool = LocalMechanicalPool(10, exec_file=exec_file)
Creating Pool: 100%|########| 10/10 [00:01<00:00,  1.43it/s]

In the PyPIM environment, create a pool.

>>> pool = LocalMechanicalPool(10, version="242")
Creating Pool: 100%|########| 10/10 [00:01<00:00,  1.43it/s]

Overview#

map

Run a user-defined function on each Mechanical instance in the pool.

run_batch

Run a batch of input files on the Mechanical instances in the pool.

next_available

Wait until a Mechanical instance is available and return this instance.

exit

Exit all Mechanical instances in the pool.

ports

Get a list of the ports that are used.

__del__

Clean up when complete.

__len__

Get the number of instances in the pool.

__getitem__

Get an instance by an index.

__iter__

Iterate through active instances.

__str__

Get the string representation of this object.

Import detail#

from ansys.mechanical.core.pool import LocalMechanicalPool

Property detail#

property LocalMechanicalPool.ports#

Get a list of the ports that are used.

Examples

Get the list of ports used by the pool of Mechanical instances.

>>> pool.ports
[10001, 10002]

Method detail#

LocalMechanicalPool.map(func, iterable=None, clear_at_start=True, progress_bar=True, close_when_finished=False, timeout=None, wait=True)#

Run a user-defined function on each Mechanical instance in the pool.

Parameters:
funcfunction

Function with mechanical as the first argument. The subsequent arguments should match the number of items in each iterable (if any).

iterablelist, tuple, optional

An iterable containing a set of arguments for the function. The default is None, in which case the function runs once on each instance of Mechanical.

clear_at_startbool, optional

Clear Mechanical at the start of execution. The default is True. Setting this to False might lead to instability.

progress_barbool, optional

Whether to show a progress bar when running the batch of input files. The default is True, but the progress bar is not shown when wait=False.

close_when_finishedbool, optional

Whether to close the instances when the function finishes running on all instances in the pool. The default is False.

timeoutfloat, optional

Maximum runtime in seconds for each iteration. The default is None, in which case there is no timeout. If you specify a value, each iteration is allowed to run only this number of seconds. Once this value is exceeded, the batch process is stopped and treated as a failure.

waitbool, optional

Whether block execution must wait until the batch process is complete. The default is True.

Returns:
list

A list containing the return values for the function. Failed runs do not return an output. Because return values are not necessarily in the same order as the iterable, you might want to add some sort of tracker to the return of your function.

Examples

Run several input files while storing the final routine. Note how the function to map must use mechanical as the first argument. The function can have any number of additional arguments.

>>> from ansys.mechanical.core import LocalMechanicalPool
>>> pool = LocalMechanicalPool(10)
>>> completed_indices = []
>>> def function(mechanical, name, script):
        # name, script = args
        mechanical.clear()
        output = mechanical.run_python_script(script)
        return name, output
>>> inputs = [("first","2+3"), ("second", "3+4")]
>>> output = pool.map(function, inputs, progress_bar=False, wait=True)
[('first', '5'), ('second', '7')]
LocalMechanicalPool.run_batch(files, clear_at_start=True, progress_bar=True, close_when_finished=False, timeout=None, wait=True)#

Run a batch of input files on the Mechanical instances in the pool.

Parameters:
fileslist

List of input files.

clear_at_startbool, optional

Whether to clear Mechanical when execution starts. The default is True. Setting this parameter to False might lead to instability.

progress_barbool, optional

Whether to show a progress bar when running the batch of input files. The default is True, but the progress bar is not shown when wait=False.

close_when_finishedbool, optional

Whether to close the instances when running the batch of input files is finished. The default is False.

timeoutfloat, optional

Maximum runtime in seconds for each iteration. The default is None, in which case there is no timeout. If you specify a value, each iteration is allowed to run only this number of seconds. Once this value is exceeded, the batch process is stopped and treated as a failure.

waitbool, optional

Whether block execution must wait until the batch process is complete. The default is True.

Returns:
list

List of text outputs from Mechanical for each batch run. The outputs are not necessarily listed in the order of the inputs. Failed runs do not return an output. Because the return outputs are not necessarily in the same order as iterable, you might want to add some sort of tracker or note within the input files.

Examples

Run 20 verification files on the pool.

>>> files = [f"test{index}.py" for index in range(1, 21)]
>>> outputs = pool.run_batch(files)
>>> len(outputs)
20
LocalMechanicalPool.next_available(return_index=False)#

Wait until a Mechanical instance is available and return this instance.

Parameters:
return_indexbool, optional

Whether to return the index along with the instance. The default is False.

Returns:
pymechanical.Mechanical

Instance of Mechanical.

int

Index within the pool of Mechanical instances. This index is not returned by default.

Examples

>>> mechanical = pool.next_available()
>>> mechanical
Ansys Mechanical [Ansys Mechanical Enterprise]
Product Version:242
Software build date: 06/03/2024 14:47:58
LocalMechanicalPool.__del__()#

Clean up when complete.

LocalMechanicalPool.exit(block=False)#

Exit all Mechanical instances in the pool.

Parameters:
blockbool, optional

Whether to wait until all processes close before exiting all instances in the pool. The default is False.

Examples

>>> pool.exit()
LocalMechanicalPool.__len__()#

Get the number of instances in the pool.

LocalMechanicalPool.__getitem__(key)#

Get an instance by an index.

LocalMechanicalPool.__iter__()#

Iterate through active instances.

LocalMechanicalPool.__str__()#

Get the string representation of this object.