pyfemtet.opt.interface package

Module contents

class pyfemtet.opt.interface.AbstractFEMInterface[source]

Bases: object

close(*args, **kwargs)[source]
contact_to_optimizer(opt, global_data, ctx)[source]
Parameters:
  • opt (AbstractOptimizer) –

  • global_data (GlobalOptimizationData) –

  • ctx (OptimizationDataPerFEM) –

current_prm_values: dict[str, Variable]
kwargs: dict = {}
load_constraints(opt)[source]
Parameters:

opt (OptimizationDataPerFEM) –

load_objectives(opt)[source]
Parameters:

opt (OptimizationDataPerFEM) –

load_variables(opt)[source]
Parameters:

opt (OptimizationDataPerFEM) –

property object_pass_to_fun

The object pass to the first argument of user-defined objective functions.

Returns:

self (AbstractFEMInterface)

reopen()[source]
trial_postprocess()[source]
Return type:

None

trial_postprocess_per_fidelity()[source]
Return type:

None

trial_preprocess()[source]
Return type:

None

trial_preprocess_per_fidelity()[source]
Return type:

None

update()[source]
Return type:

None

update_parameter(x)[source]
Parameters:

x (dict[str, Variable]) –

Return type:

None

class pyfemtet.opt.interface.AbstractSurrogateModelInterfaceBase(history_path=None, train_history=None, _output_directions=None)[source]

Bases: AbstractFEMInterface

Parameters:
  • history_path (str | None) –

  • train_history (History) –

  • _output_directions (Sequence[DIRECTION] | dict[str, DIRECTION] | dict[int, DIRECTION] | None) –

contact_to_optimizer(opt, global_data, ctx)[source]
Parameters:
  • opt (AbstractOptimizer) –

  • global_data (GlobalOptimizationData) –

  • ctx (OptimizationDataPerFEM) –

current_obj_values: dict[str, float]
property object_pass_to_fun

The object pass to the first argument of user-defined objective functions.

Returns:

self (AbstractFEMInterface)

train_history: History
class pyfemtet.opt.interface.BoTorchInterface(history_path=None, train_history=None, _output_directions=None)[source]

Bases: AbstractSurrogateModelInterfaceBase

Parameters:
current_obj_std_values: dict[str, float]
update()[source]
Return type:

None

pyfemtet.opt.interface.BoTorchWithExcelSettingsInterface

alias of _WithExcelSettingsInterface

class pyfemtet.opt.interface.ExcelInterface(input_xlsm_path, input_sheet_name, output_xlsm_path=None, output_sheet_name=None, constraint_xlsm_path=None, constraint_sheet_name=None, procedure_xlsm_path=None, procedure_name=None, procedure_args=None, connect_method='new', procedure_timeout=None, setup_xlsm_path=None, setup_procedure_name=None, setup_procedure_args=None, teardown_xlsm_path=None, teardown_procedure_name=None, teardown_procedure_args=None, related_file_paths=None, visible=False, display_alerts=False, terminate_excel_when_quit=None, interactive=True, use_named_range=True, force_override_when_load=False)[source]

Bases: COMInterface

Excel を計算コアとして利用するためのクラス。

通常の有限要素法を Excel に 置き換えて使用することが可能です。

すでに Excel マクロと Femtet を 連携させた自動解析システムを 構築している場合、このクラスは それをラップします。これにより、 PyFemtet を用いた最適化を 行う際に便利な機能を提供します。

Parameters:
  • input_xlsm_path (str or Path) – 設計変数の定義を含む Excel ファイルのパスを指定 します。

  • input_sheet_name (str) – 設計変数の定義を含むシートの名前を指定します。

  • output_xlsm_path (str or Path, optional) – 目的関数の定義を含む Excel ファイルのパスを指定 します。指定しない場合は input_xlsm_path と 同じと見做します。

  • output_sheet_name (str, optional) – 目的関数の定義を含む含むシートの名前を指定します。 指定しない場合は input_sheet_name と同じと見 做します。

  • procedure_xlsm_path (str, optional) – 最適化ループ中に呼ぶ Excel マクロ関数を 含む xlsm のパスです。 指定しない場合は input_xlsm_path と 同じと見做します。

  • procedure_name (str, optional) – Excel マクロ関数名を指定します。指定しない場合は FemtetMacro.FemtetMain と見做します。

  • procedure_args (list or tuple, optional) – Excel マクロ関数に渡す引数をリストまたはタプルで 指定します。

  • connect_method (str, optional) – Excel との接続方法を指定します。 ‘auto’ または ‘new’ が利用可能です。デフォルトは ‘auto’ です。

  • procedure_timeout (float or None, optional) – Excel マクロ関数のタイムアウト時間を秒単位で指定 します。 None の場合はタイムアウトなしとなります。

  • setup_xlsm_path (str or Path, optional) – セットアップ時に呼ぶ関数を含む xlsm のパスです。 指定しない場合は input_xlsm_path と 同じと見做します。

  • setup_procedure_name (str, optional) – セットアップ時に呼ぶマクロ関数名です。 指定しない場合、セットアップ時に何もしません。

  • setup_procedure_args (list or tuple, optional) – セットアップ時に呼ぶマクロ関数の引数です。

  • teardown_xlsm_path (str or Path, optional) – 終了時に呼ぶ関数を含む xlsm のパスです。 指定しない場合は input_xlsm_path と 同じと見做します。

  • teardown_procedure_name (str, optional) – 終了時に呼ぶマクロ関数名です。 指定しない場合、終了時に何もしません。

  • teardown_procedure_args (list or tuple, optional) – 終了時に呼ぶマクロ関数の引数です。

  • visible (bool) – excel を可視化するかどうかです。 ただし、 True を指定した場合でもマクロの実行中は 不可視になります。 デフォルトは False です。

  • display_alerts (bool) – excel ダイアログを表示するかどうかです。 デバッグ目的の場合以外は True にしないでください。 デフォルトは False です。

  • terminate_excel_when_quit (bool) – 終了時に Excel を終了するかどうかです。 指定しない場合、 connect_method が ‘new’ の場合 True とふるまい ‘auto’ の場合 False と振舞います。

  • interactive (bool) – excel を対話モードにするかどうかです。 False にすると、 visible == True であっても 自動化プロセス中にユーザーが誤って Excel 本体を操作できないようにします。 デフォルトは True です。

  • constraint_xlsm_path (str) –

  • constraint_sheet_name (str) –

  • related_file_paths (list[str]) –

  • use_named_range (bool) –

  • force_override_when_load (bool) –

input_xlsm_path

設計変数の定義を含む Excel ファイルのパス。

Type:

Path

input_sheet_name

設計変数の定義を含むシートの名前。

Type:

str

output_xlsm_path

目的関数の定義を含む Excel ファイルのパス。

Type:

Path

output_sheet_name

目的関数の定義を含む含むシートの名前。

Type:

str

procedure_name

実行する Excel マクロ関数名。

Type:

str

procedure_args

Excel マクロ関数に渡す引数のリストまたはタプル。

Type:

list or tuple

excel_connect_method

接続方法。’new’ または ‘auto’。

Type:

str

procedure_timeout

Excel マクロ関数の実行タイムアウト。 Noneの場合は無制限。

Type:

float or None

terminate_excel_when_quit

プログラム終了時に Excel を終了するかどうか。 connect_method が ‘new’ の場合 True, ‘auto’ の場合 False。

Type:

bool

excel

Excel の COM オブジェクト。

Type:

CDispatch

static add_femtet_macro_reference(wb)[source]
close()[source]
com_members = {'excel': 'Excel.Application'}
connect_excel(connect_method)[source]
constraint_from_excel(_, name)[source]
Parameters:

name (str) –

constraint_sheet_name: str
constraint_xlsm_path: str
display_alerts: bool
excel: CDispatch
force_override_when_load: bool
input_sheet_name: str
input_xlsm_path: str
interactive: bool
load_constraints(opt, raise_if_no_keyword=False)[source]
Parameters:

opt (AbstractOptimizer) –

load_objectives(opt, raise_if_no_keyword=True)[source]
Parameters:

opt (AbstractOptimizer) –

load_variables(opt, raise_if_no_keyword=True)[source]
Parameters:

opt (AbstractOptimizer) –

Return type:

None

property object_pass_to_fun

The object pass to the first argument of user-defined objective functions.

Returns:

COM object of Microsoft Excel.

Return type:

excel (CDispatch)

objective_from_excel(_, name)[source]
Parameters:

name (str) –

open_femtet_ref_xla()[source]
output_sheet_name: str
output_xlsm_path: str
procedure_args: list
procedure_name: str
related_file_paths: list[str]
static remove_femtet_ref_xla(wb)[source]
setup_procedure_args: list
setup_procedure_name: str
setup_xlsm_path: str
property sh_constraint: CDispatch
property sh_input: CDispatch
property sh_output: CDispatch
teardown_procedure_args: list
teardown_procedure_name: str
teardown_xlsm_path: str
terminate_excel_when_quit: bool
update()[source]
Return type:

None

update_parameter(x)[source]
Parameters:

x (dict[str, Variable]) –

Return type:

None

use_named_range: bool
visible: bool
property wb_constraint: CDispatch
property wb_input: CDispatch
property wb_output: CDispatch
property wb_procedure: CDispatch
property wb_setup: CDispatch
property wb_teardown: CDispatch
class pyfemtet.opt.interface.FEMListInterface[source]

Bases: AbstractFEMInterface

append(fem)[source]
Parameters:

fem (AbstractFEMInterface) –

close(*args, **kwargs)[source]
contact_to_optimizer(opt, global_data, ctx)[source]
Parameters:
  • opt (AbstractOptimizer) –

  • global_data (GlobalOptimizationData) –

  • ctx (OptimizationDataPerFEM) –

load_constraints(opt)[source]
Parameters:

opt (OptimizationDataPerFEM) –

load_objectives(opt)[source]
Parameters:

opt (OptimizationDataPerFEM) –

load_variables(opt)[source]
Parameters:

opt (OptimizationDataPerFEM) –

property object_pass_to_fun

The object pass to the first argument of user-defined objective functions.

Returns:

self (AbstractFEMInterface)

pop(index)[source]
Parameters:

index (int) –

Return type:

AbstractFEMInterface

remove(fem)[source]
Parameters:

fem (AbstractFEMInterface) –

reopen()[source]
trial_postprocess()[source]
Return type:

None

trial_postprocess_per_fidelity()[source]
Return type:

None

trial_preprocess()[source]
Return type:

None

trial_preprocess_per_fidelity()[source]
Return type:

None

update()[source]
update_parameter(x)[source]
Parameters:

x (dict[str, Variable]) –

Return type:

None

class pyfemtet.opt.interface.FemtetInterface(femprj_path=None, model_name=None, connect_method='auto', save_pdt='all', strictly_pid_specify=True, allow_without_project=False, open_result_with_gui=True, always_open_copy=False, parametric_output_indexes_use_as_objective=None)[source]

Bases: COMInterface

Control Femtet from optimizer.

Parameters:
  • femprj_path (str, optional) – The path to the project file. If not specified, get it from connected Femtet.

  • model_name (str, optional) – The name of the model. If not specified, get it from connected Femtet or the first model when Femtet open the project file.

  • connect_method (str, optional) – The connection method. Default is ‘auto’. Other valid values are ‘new’ or ‘existing’.

  • save_pdt (str, optional) – The type to save result file. Valid values are ‘all’, ‘none’ or ‘optimal’. Default is ‘all’.

  • strictly_pid_specify (bool, optional) – Whether to strictly specify the PID in Femtet connection. Default is True.

  • allow_without_project (bool, optional) – Whether to allow without a project. Default is False.

  • open_result_with_gui (bool, optional) – Whether to open the result with GUI. Default is True.

  • parametric_output_indexes_use_as_objective (dict[int, str or float], optional) –

    A list of parametric output indexes and its direction to use as the objective function. If not specified, it will be None and no parametric outputs are used as objectives.

    Note

    Indexes start at 0, but the parametric analysis output settings in the Femtet dialog box indicate setting numbers starting at 1.

    Warning

    Setting this argument deletes the parametric analysis swept table set in the femprj file. If you do not want to delete the swept table, make a copy of the original file.

Warning

Even if you specify strictly_pid_specify=True on the constructor, the connection behavior is like strictly_pid_specify=False in parallel processing because of its large overhead. So you should close all Femtet processes before running FEMOpt.optimize() if n_parallel >= 2.

Tip

If you search for information about the method to connect python and Femtet, see connect_femtet().

close(timeout=15, force=True)[source]

Destructor.

May not quit Femtet. If you want to quit certainly, Please use quit() method.

com_members = {'Femtet': 'FemtetMacro.Femtet'}
connect_femtet(connect_method='auto', pid=None)[source]

Connects to a Femtet process.

Parameters:
  • connect_method (str, optional) – The connection method. Can be ‘new’, ‘existing’, or ‘auto’. Defaults to ‘auto’.

  • pid (int or None, optional) – The process ID of an existing Femtet process and wanted to connect.

Note

When connect_method is ‘new’, starts a new Femtet process and connects to it. pid will be ignored.

Note

When ‘existing’, connect to an existing Femtet process. However, if there are no Femtets to which it can connect (i.e. already connected to another Python or Excel process), it throws an exception.

Note

When set to ‘auto’, first tries ‘existing’, and if that fails, connects with ‘new’. If pid is specified and failed to connect, it will not try existing another Femtet process.

current_prm_values: TrialInput
femtet_is_alive()[source]

Returns connected femtet process is existing or not.

Return type:

bool

load_objectives(*args, **kwargs)
property object_pass_to_fun

The object pass to the first argument of user-defined objective functions.

Returns:

COM object of Femtet.

Return type:

Femtet (CDispatch)

open(femprj_path, model_name=None)[source]

Open specific analysis model with connected Femtet.

Parameters:
  • femprj_path (str) –

  • model_name (str | None) –

Return type:

None

parametric_output_indexes_use_as_objective: dict[int, str | float]
postprocess(*args, **kwargs)
preprocess(*args, **kwargs)
quit(timeout=15, force=True)[source]

Force to terminate connected Femtet.

reopen()[source]
save_screenshot: Literal['result', 'model', 'none']
solve(*args, **kwargs)
update(*args, **kwargs)
update_model(*args, **kwargs)
update_parameter(*args, **kwargs)
use_parametric_output_as_objective(number, direction='minimize')[source]

Use output setting of Femtet parametric analysis as an objective function.

Parameters:
  • number (int) – The index of output settings tab in parametric analysis dialog of Femtet. Starts at 1.

  • direction (str | float) – Objective direction. Valid input is one of ‘minimize’, ‘maximize’ or a specific value. Defaults to ‘minimize’.

Returns:

None

Return type:

None

pyfemtet.opt.interface.FemtetWithExcelSettingsInterface

alias of _WithExcelSettingsInterface

class pyfemtet.opt.interface.FemtetWithNXInterface(prt_path, femprj_path=None, model_name=None, connect_method='auto', save_pdt='all', strictly_pid_specify=True, allow_without_project=False, open_result_with_gui=True, parametric_output_indexes_use_as_objective=None, always_open_copy=False, export_curves=None, export_surfaces=None, export_solids=None, export_flattened_assembly=None)[source]

Bases: FemtetInterface, _NXInterface

Control Femtet and NX.

Using this class, you can import CAD files created in NX through the Parasolid format into a Femtet project. It allows you to pass design variables to NX, update the model, and perform analysis using the updated model in Femtet.

Parameters:
  • prt_path (str) – The path to .prt file containing the CAD data from which the import is made.

  • export_curves (bool or None, optional) – Defaults to None.

  • export_surfaces (bool or None, optional) – Defaults to None.

  • export_solids (bool or None, optional) – Defaults to None.

  • export_flattened_assembly (bool or None, optional) – Defaults to None.

  • femprj_path (str) –

  • model_name (str) –

  • connect_method (str) –

  • save_pdt (str) –

  • strictly_pid_specify (bool) –

  • allow_without_project (bool) –

  • open_result_with_gui (bool) –

  • parametric_output_indexes_use_as_objective (dict[int, str | float]) –

Notes

`export_*` arguments sets parasolid export setting of NX. If None, PyFemtet does not change the current setting of NX.

It is recommended not to change these values from the settings used when exporting the Parasolid that was imported into Femtet.

update_model()[source]
pyfemtet.opt.interface.FemtetWithNXWithExcelSettingsInterface

alias of _WithExcelSettingsInterface

class pyfemtet.opt.interface.FemtetWithSolidworksInterface(sldprt_path, femprj_path=None, model_name=None, connect_method='auto', save_pdt='all', strictly_pid_specify=True, allow_without_project=False, open_result_with_gui=True, parametric_output_indexes_use_as_objective=None, always_open_copy=False, close_solidworks_on_terminate=False, solidworks_visible=True)[source]

Bases: FemtetInterface, SolidworksInterface, AbstractFEMInterface

Interface class integrating Femtet and SolidWorks operations.

This class combines the capabilities of both Femtet and SolidWorks interfaces, allowing coordinated operation between simulation (Femtet) and CAD manipulation (SolidWorks).

Parameters:
  • sldprt_path (str) – Path to the SolidWorks part file (.sldprt).

  • femprj_path (str, optional) – Path to the Femtet project file (.femprj). Defaults to None.

  • model_name (str, optional) – Name of the model in the project. Defaults to None.

  • connect_method (str, optional) – Connection method for Femtet. Defaults to “auto”.

  • save_pdt (str, optional) – Specifies which data to save. Defaults to “all”.

  • strictly_pid_specify (bool, optional) – Whether to strictly specify parameter IDs. Defaults to True.

  • allow_without_project (bool, optional) – If True, allows operation without a project file. Defaults to False.

  • open_result_with_gui (bool, optional) – If True, open results with GUI. Defaults to True.

  • parametric_output_indexes_use_as_objective (dict[int, str | float], optional) – Indexes for parametric output used as objectives. Defaults to None.

  • always_open_copy (bool, optional) – If True, always open a copy of the project. Defaults to False.

  • close_solidworks_on_terminate (bool, optional) – If True, SolidWorks will close when this object is destroyed. Defaults to False.

  • solidworks_visible (bool, optional) – If True, SolidWorks will be started in visible mode. Defaults to True.

close(timeout=15, force=True)[source]

Destructor.

May not quit Femtet. If you want to quit certainly, Please use quit() method.

update_model()[source]

Update .sldprt

update_parameter(x, with_warning=False)[source]
Parameters:

x (dict[str, Variable]) –

Return type:

None

pyfemtet.opt.interface.FemtetWithSolidworksWithExcelSettingsInterface

alias of _WithExcelSettingsInterface

class pyfemtet.opt.interface.NoFEM[source]

Bases: AbstractFEMInterface

update()[source]
Return type:

None

class pyfemtet.opt.interface.PoFBoTorchInterface(history_path, train_history=None, observation_noise=None, feasibility_noise=None, feasibility_cdf_threshold=0.5, _output_directions=None)[source]

Bases: BoTorchInterface, AbstractSurrogateModelInterfaceBase

Parameters:
calc_pof()[source]
update()[source]
Return type:

None

pyfemtet.opt.interface.PoFBoTorchWithExcelSettingsInterface

alias of _WithExcelSettingsInterface