Attempt to program parameter sweep on model in COMSOL, using OOP in MATLAB
40 ビュー (過去 30 日間)
古いコメントを表示
Using COMSOL LiveLink and object-orientated programming (which I am new to) I am trying to develop a convenient way of passing vectorised parameters into a COMSOL model and running a parameter sweep efficiently without using the GUI on COMSOL.
I am aware that this code assumes that a is a 10-by-1 array, and that b, c and d are scalars.
After declaring this class, I try running:
Test = SimpleModel;
Test.a = 1:10; Test.b = 2; Test.c = 3; Test.d = 4;
Test.solveAll
However, I keep getting Java errors, along the lines of "com.comsol.util.exceptions.FlException: Variable refers to an object which is no longer in the model"
classdef SimpleModel
properties
a {mustBeNumeric}
b {mustBeNumeric}
c {mustBeNumeric}
d {mustBeNumeric}
model = mphopen('simpleModel')
end
methods
function obj = paramSet(obj)
obj.model.param.set("a",obj.a);
obj.model.param.set("b",obj.b);
obj.model.param.set("c",obj.c);
obj.model.param.set("d",obj.d);
end
function obj = solve(obj)
obj.model.study("std1").run()
obj.model.result.dataset("dset1");
end
function Solution = solution(obj)
Solution = obj.model.result.dataset("dset1");
end
function Vertices = meshPoints(obj,show)
[~ , data] = mphmeshstats(obj.model);
Vertices = data.vertex';
if show == true
plot(Vertices(1,:),Vertices(2,:),'.')
end
end
function Values = solValues(obj)
Values = mpheval(obj.model,"u").d1';
end
function Value = meanValue(obj)
Value = mphglobal(obj.model,"aveop1(u)");
end
% for parameter sweeps
function Strings = paramNames(obj)
Strings = ["a","b","c","d"];
end
function Values = paramValues(obj)
Names = obj.paramNames;
Values = cell(1,length(Names));
for n = 1:length(Names)
Values{n} = getfield(obj,Names(n));
end
end
function Int = howManyParametersAreBeingSwept(obj)
Int = 0;
Values = obj.paramValues();
for n = 1:length(Values)
if length(Values{n}) > 1
Int = Int + 1;
end
end
end
function Strings = whichParametersAreBeingSwept(obj)
Names = obj.paramNames;
Values = obj.paramValues;
Strings = [];
for n = 1:length(Values)
if length(Values{n}) > 1
Strings = [Names(n) Strings];
end
end
end
function Cell = paramIndices(obj)
Cell = {1:10};
end
function Cell = storageSpace(obj)
Names = obj.paramNames;
Values = obj.paramValues;
Cell = cell(1,10);
for idx = 1:10
Cell{idx} = obj;
Cell{idx}.a = obj.a(idx);
end
end
function Solutions = solveAll(obj)
Solutions = obj.storageSpace;
for idx = 1:10
% keyboard
Temp = Solutions{idx};
Temp.model = mphopen('simpleModel');
ParamValues = Temp.paramValues;
Temp.model.param.set("a",Temp.a);
Temp.model.param.set("b",Temp.b);
Temp.model.param.set("c",Temp.c);
Temp.model.param.set("d",Temp.d);
Temp.solve()
Solutions{idx} = Temp;
end
clear Temp
end
end
end
As I am new to object-orientated programming, I am aware that the code above might have quite a few shortcomings. If anybody could provide guidance on these I would be very grateful.
0 件のコメント
回答 (1 件)
chrisw23
2022 年 11 月 10 日
I suggest to use the constructor to load the model like.
...
methods
function obj = SimpleModel(modelFilePath)
obj.model = mphopen(modelFilePath)
end
...
2 件のコメント
chrisw23
2022 年 11 月 11 日
This suggestion was more in terms of having multiple objects (models) of the same class type. On the other hand you have path dependencies loading the model by file name and I would use the constructor to handle this by an argument or other options. I would also suggest to use the destructor (delete method) of your class to unload the model when the object lifetime ends. This could be importend for your comsol environment when a new model is to be loaded.
参考
カテゴリ
Help Center および File Exchange で Argument Definitions についてさらに検索
製品
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!