Files
MotorSim/codex_product/probe_ansoft_integration.m
T

65 lines
2.6 KiB
Matlab

function probe_ansoft_integration
% Probe the installed Ansys Twin Builder S-function with the PMLSM model.
projectDir = fileparts(mfilename('fullpath'));
modelDir = fullfile(projectDir, 'LEM_slx');
modelFile = fullfile(modelDir, 'PMLSM.slx');
projectFile = fullfile(projectDir, 'LinearSimulink.aedt');
ansysEmRoot = getenv('ANSYSEM_ROOT252');
ansoftMatlabDir = fullfile(ansysEmRoot, 'cpl', 'matlab', 'r2024a');
assert(isfolder(ansoftMatlabDir), ...
'The AEDT MATLAB interface directory does not exist: %s', ansoftMatlabDir);
addpath(modelDir);
addpath(ansoftMatlabDir, '-begin');
rehash;
load_system(modelFile);
closeModel = onCleanup(@() close_system('PMLSM', 0));
blockPath = 'PMLSM/S-Function';
storedUserData = get_param(blockPath, 'UserData');
oldProjectFile = 'F:\Matlab_proj\LinearSimulink.aedt';
activeUserData = strrep(storedUserData, oldProjectFile, projectFile);
assert(~strcmp(storedUserData, activeUserData), ...
'Expected legacy project path was not found in the S-Function UserData.');
set_param(blockPath, 'UserData', activeUserData, 'UserDataPersistent', 'on');
fprintf('MATLAB_VERSION\t%s\n', version);
fprintf('MEX_EXTENSION\t%s\n', mexext);
fprintf('ANSYSEM_ROOT252\t%s\n', ansysEmRoot);
fprintf('ANSOFT_MATLAB_DIR\t%s\n', ansoftMatlabDir);
fprintf('ANSOFT_SFUNCTION\t%s\n', which('AnsoftSFunction'));
fprintf('ANSOFT_LINK_DIALOG\t%s\n', which('AnsoftLinkDialog'));
fprintf('ANSOFT_SFUNCTION_EXIST\t%d\n', exist('AnsoftSFunction', 'file'));
fprintf('STORED_PROJECT\t%s\n', oldProjectFile);
fprintf('ACTIVE_PROJECT\t%s\n', projectFile);
in = Simulink.SimulationInput('PMLSM');
in = in.setModelParameter('StopTime', '0.001', 'SimulationMode', 'normal');
try
out = sim(in);
catch ME
fprintf('SIMULATION_STATUS\tFAIL\n');
fprintf('ERROR_ID\t%s\n', ME.identifier);
fprintf('ERROR_CODEPOINTS\t%s\n', strjoin(string(double(ME.message)), ','));
couplingVars = {'Simulink_Coupling_Error', ...
'Simulink_Coupling_InstanceName', 'DSDEActiveDesignName'};
for k = 1:numel(couplingVars)
varName = couplingVars{k};
if evalin('base', sprintf('exist(''%s'', ''var'')', varName))
value = evalin('base', varName);
fprintf('BASE_VAR\t%s\t%s\n', varName, string(value));
end
end
errorFile = fullfile(projectDir, 'analysis_artifacts', 'ansoft_probe_error.txt');
fid = fopen(errorFile, 'w', 'n', 'UTF-8');
fprintf(fid, '%s\n', getReport(ME, 'extended', 'hyperlinks', 'off'));
fclose(fid);
rethrow(ME);
end
fprintf('SIMULATION_STATUS\tPASS\n');
fprintf('SIMULATION_OUTPUTS\t%s\n', strjoin(who(out), ', '));
end