标签归档:Matlab

COMSOL Matlablive 多参数研究,批量化运行+导出+自动整理

COMSOL软件的研究求解模块其实提供了比较丰富的参数化运行结构,但是对不同参数的研究的结果导出方面,还是需要很多手动设置操作的才能导出批量化数据。

比如以下情况:研究对线是一个催化反应器,需要入口温度、入口流速、反应器尺寸、反应物初始孔隙率等四个不同的输入参数进行单变量的不同输入值的影响研究。那么如果直接使用COMSOL来做的话,就需要使用参数化扫描对单个参数进行扫描,然后导出动画进行分解,并保存四个不同的mph文件,用于分别研究四个参数。

针对这种情况,其实可以用Matlab livelink来进行批量的单个参数的修改,并运行求解导出相关结果(mph、图、报告、表数据)。

% 1、定义要运行的参数组
model = mphopen('param_run.mph');  % 打开模型
% 设定参数组
param_name = ["T_in" "U_in" "d_ball" "eps_init"]; % 参数名称
param_unit = ["degC" "m/s" "um" ""]; % 参数单位
param_Value = [600 650 700 750 800;...  %参数值
                0.1 0.2 0.3 0.4 0.5;...
                75 150 200 300 500;...
                0.03 0.05 0.1 0.15 0.2];

% 2、 For循环运行所有参数组
for name=1:length(param_name) % 循环研究参数名
   for value=1:size(param_Value,2) % 参数水平
       % 记录输出
       tic;
       fprintf('Start simulation: %s=%s [%s]\n',param_name(name),num2str(param_Value(name,value)),param_unit(name));
       % 设定参数
       model.param.set(param_name(name),num2str(param_Value(name,value)),param_unit(name));
       % 运行计算
       model.sol('sol1').runAll;
       % 导出数据
       model.result.report('rpt1').run; % 导出报告
       model.result.export('anim1').run; % 导出动画
       model.result.export('anim2').run;
       model.result.export('anim3').run;
       model.result.export('anim4').run;
       model.result.export('anim5').run;
       model.result.export('anim6').run;
       model.result.export('anim7').run;
       model.result.export('plot1').run; % 导出出图数据表
       model.result.export('plot2').run;
       model.result.export('plot3').run;
       % 整理导出数据
       movefile("export_dir", strcat(param_name(name),"_", num2str(param_Value(name,value))));
       copyfile("export_file",strcat(param_name(name),"_", num2str(param_Value(name,value))));
       % 完成
       toc
   end
   % 设定回原参数
   model.param.set(param_name(name),num2str(param_Value(name,1)),param_unit(name));
end

Matlab 对csv数据持续写入的方式

Matlab计算的数据保存有很多种方式,可以直接试用xlswrite函数讲数据保存成,也可以用csvwrite写成csv。

但是在你如果在某些循环计算中持续生成数据后然后实时写入csv中,dlmwrite函数是个比较好的方式。比以上两种方式速度快非常多,可以大量优化for循环效率。

dlmwrite('data.csv',data,'delimiter',',','-append');

COMSOL 参数化仿真自动导出报告

在使用COMSOL进行研究工作的过程中,涉及到很多需要对多组参数进行仿真,输出对应参数仿真结果的报告,在这个过程中可以使用COMSOL 提供的matlab脚本进行参数化结果的输出。

%% 0 清除所有数据
clear all;clc;
%% 1 打开mph文件
model = mphopen('FuelCell0825b.mph');

%% 2 输入需要参数化研究的参数
spacing_Outlet = [4000 1800 823 378 170 74.7 30.9];
width_Outlet = [992  625  394  248  156  98  62];

%% 3 for循环,对所有参数进行参数化研究仿真。
for N=3:9
    %% 3.1 设定参数
    model.param('par2').set('N', num2str(N));
    model.param('par2').set('spacing_Outlet', strcat(num2str(spacing_Outlet(N-2)),'[um]'));
    model.param('par2').set('width_Outlet', strcat(num2str(width_Outlet(N-2)),'[um]'));
    %% 3.2 运行研究
    model.sol('sol1').study('std1');
    model.sol('sol1').attach('std1');
    model.sol('sol1').runAll;
    %% 3.3 导出word报告
    model.result.report('rpt1').set('filename', ['E:\08 Science Compass\2208082536 ' native2unicode(hex2dec({'9e' 'c4'}), 'unicode')  native2unicode(hex2dec({'63' '2f'}), 'unicode')  native2unicode(hex2dec({'4e' '91'}), 'unicode') ' ' native2unicode(hex2dec({'71' 'c3'}), 'unicode')  native2unicode(hex2dec({'65' '99'}), 'unicode')  native2unicode(hex2dec({'75' '35'}), 'unicode')  native2unicode(hex2dec({'6c' '60'}), 'unicode') '\N=' num2str(N) '.docx']);
    model.result.report('rpt1').run;
    fprintf("Export N = %d",N)
end

小广告:COMSOL相关问题可以加琳泓大佬的群去找群友讨论

开放群:566811107
群一:836281296
群二:594368389
群三:1080606488
群四: 678357196