An Example Using Nested Loops
In this example, a MATLAB function is created that solves the busbar model for different values of the length and thickness of the busbar in addition to the input electric potential. The function evaluates and returns the maximum temperature in the busbar, the generated heat, and the current through the busbar. For each variation of the model, the function stores these results in a file and saves the model object in an MPH file. To make it easy to identify the name of the MPH file, it contains the values of the different parameters used to set up the model.
The input argument of the M-function is the busbar model object and the path to the directory where the data and models are saved.
1
2
function modelParam(model,filepath)
The function first creates a file and then sets the header of the output file format.
3
filename = fullfile(filepath,'results.txt');
fid=fopen(filename,'wt');
fprintf(fid,'*** run parametric study ***\n');
fprintf(fid,'L[m] | tbb[m] | Vtot[V] | ');
fprintf(fid,'MaxT[K] | TotQ[W] | Current[A]\n');
4
model.hist.disable;
5
Now start a for loop to parameterize the parameter L and set the corresponding parameter in the model object. Enter the commands:
for L = [9e-2 15e-2]
model.param.set('L',L);
6
Continue by creating a second for loop, this time to parameterize the parameter tbb and to modify its value in the model object:
for tbb = [5e-3 10e-3]
model.param.set('tbb',tbb);
7
Define the last for loop to parameterize the applied potential Vtot:
for Vtot = [20e-3 40e-3]
model.param.set('Vtot',Vtot);
8
fprintf(fid,[num2str(L),' | ',num2str(tbb),...
' | ',num2str(Vtot),' | ']);
9
mphrun(model,'study');
10
MaxT = mphmax(model,'T',3,'selection',1);
TotQ = mphint2(model,'ht.Qtot',3,'selection',1);
Current = mphint2(model,'ec.normJ','surface','selection',43);
11
fprintf(fid,[num2str(MaxT),' | ',num2str(TotQ),...
' | ',num2str(Current),' \n']);
12
modelName = fullfile(filepath,['busbar_L=',num2str(L),...
'_tbb=',num2str(tbb),...
'_Vtot=',num2str(Vtot),'.mph']);
mphsave(model,modelName);
13
Terminate the for loops:
end
end
end
14
fclose(fid);
The script in the text editor should now look like this text:
function modelParam(model,filepath)
filename = fullfile(filepath,'results.txt');
fid=fopen(filename,'wt');
fprintf(fid,'*** run parametric study ***\n');
fprintf(fid,'L[m] | tbb[m] | Vtot[V] | ');
fprintf(fid,'MaxT[K] | TotQ[W] | Current[A]\n');
model.hist.disable;
for L = [9e-2 15e-2]
  model.param.set('L',L);
  for tbb = [5e-3 10e-3]
    model.param.set('tbb',tbb);
    for Vtot = [20e-3 40e-3]
      model.param.set('Vtot',Vtot);
      fprintf(fid,[num2str(L),' | ',num2str(tbb),' | ',...
        num2str(Vtot),' | ']);
      mphrun(model,'study');
      MaxT = mphmax(model,'T',3,'selection',1);
      TotQ = mphint2(model,'ht.Qtot',3,'selection',1);
      Current = mphint2(model,'ec.normJ','surface','selection',43);
      fprintf(fid,[num2str(MaxT),' | ',num2str(TotQ), ' | ',...
        num2str(Current),' \n']);
      modelName = fullfile(filepath,...
        ['busbar_L=',num2str(L),'_tbb=',num2str(tbb),...
        '_Vtot=',num2str(Vtot),'.mph']);
      mphsave(model,modelName);
    end
  end
end
fclose(fid);
15
Save the M-file as modelParam.m in a directory known by MATLAB.
16
model = mphopen('busbar');
17
Run the function and substitute 'filepath' with the directory of your choice:
modelParam(model,'filepath')
18
Open the file results.txt in a text editor to look at a summary of the results:
*** run parametric study ***
L[m] | tbb[m] | Vtot[V] |  MaxT[K]  | TotQ[W]  | Current[A]
0.09 | 0.005  | 0.02    | 323.0784  | 0.2419   | 161.5116
0.09 | 0.005  | 0.04    | 413.0169  | 0.9676   | 323.0232
0.09 | 0.01   | 0.02    | 308.5303  | 0.0472   |  95.761
0.09 | 0.01   | 0.04    | 354.7501  | 0.1887   | 191.5219
0.15 | 0.005  | 0.02    | 315.7229  | 0.3249   | 156.3937
0.15 | 0.005  | 0.04    | 383.5576  | 1.2999   | 312.7875
0.15 | 0.01   | 0.02    | 305.1014  | 0.0643   |  94.84
0.15 | 0.01   | 0.04    | 340.9558  | 0.2571   | 189.6799
Although this example duplicates functionality that is readily accessible in the COMSOL Desktop®, you can use a similar technique to couple a COMSOL Multiphysics model to your customized optimization script.