XIAN-FEM-2026June/3D opticsfem-master/compare/run_export_comsol.m

60 lines
1.9 KiB
Matlab
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

%RUN_EXPORT_COMSOL 一键从 COMSOL mph 导出 z=0.5 切面到 data/comsol_z050.txt
%
% 用法:在 MATLAB 里打开本文件,点“运行”(F5),不要只输入 export_comsol_slice。
%
% 需要:
% 1) 本机已安装 COMSOL + LiveLink for MATLAB有 mphload 命令)
% 2) 有 SBC.mph 模型文件(算例已求解)
clear; clc;
thisDir = fileparts(mfilename('fullpath'));
projDir = fileparts(thisDir);
outFile = fullfile(thisDir, 'data', 'comsol_z050.txt');
zCut = 0.5;
gridN = 121;
%% 1. 检查 LiveLink
if exist('mphload', 'file') ~= 2
error(['未找到 COMSOL LiveLinkmphload 命令不存在)。\n\n' ...
'方法二需要 LiveLink。若没有请用 COMSOL 界面手动导出:\n' ...
' Results → Cut Plane (z=0.5) → Export → 保存为\n' ...
' %s\n\n' ...
'详见 compare/README.md'], outFile);
end
%% 2. 查找 SBC.mph
candidates = {
fullfile(projDir, '..', '三维matlab代码', 'matlab 3D一阶散射问题', 'SBC.mph')
fullfile(projDir, 'SBC.mph')
fullfile(thisDir, 'SBC.mph')
'SBC.mph'
};
mphPath = '';
for k = 1:numel(candidates)
if isfile(candidates{k})
mphPath = candidates{k};
break;
end
end
if isempty(mphPath)
fprintf('自动搜索未找到 SBC.mph请手动选择...\n');
[fname, fpath] = uigetfile('*.mph', '选择 COMSOL 模型文件 SBC.mph');
if isequal(fname, 0)
error(['未选择 mph 文件。\n' ...
'项目目录里目前只有 SBC.mph.lock没有模型本体。\n' ...
'请从 COMSOL 另存 SBC.mph或改用 GUI 手动导出 comsol_z050.txt。']);
end
mphPath = fullfile(fpath, fname);
end
fprintf('COMSOL 模型: %s\n', mphPath);
fprintf('输出文件: %s\n', outFile);
%% 3. 导出
export_comsol_slice(mphPath, outFile, zCut, gridN, gridN);
fprintf('\n导出成功接下来运行:\n compare_normE_zslice\n');