Discussion Closed This discussion was created more than 6 months ago and has been closed. To start a new discussion with a link back to this one, click here.

Creating Geometry with Loop in COMSOL with MATLAB

Please login with a confirmed email address before reporting spam

Dear COMSOL Team,

I am interested in creating a geometry in COMSOL 5.2 which consists of many spheres with their radii varying in unique manner iterated in a loop (example, r(i+1) =r(i)+1). I am trying to create this using COMSOL with MATLAB platform but not able to achieve the result. I think that the problem is that the COMSOL variable of the geometry name is not getting updated in COMSOL. Below is the script that i am running in MATLAB window.

model = ModelUtil.create('Model2');

geom1 = model.geom.create('geom1', 3);

r=1;C1=1;C2=1;C3=1; %matlab variables
for j=1:1:10

model.param.set('r',r,'radius');
model.param.set('C1',C1,'coordinate of center');
model.param.set('C2',C2,'coordinate of center');
model.param.set('C3',C3,'coordinate of center');
geom1.feature.create('sphj','Sphere');
model.geom('geom1').feature('sphj').label('Sphere j');
model.geom('geom1').feature('sphj').set('r', r);
model.geom('geom1').feature('sphj').set('pos', {'C1' 'C2' 'C3'});
model.geom('geom1').run('sphj');
r=r+1;
C1=C1+1;
C2=C2+1;
C3=C3+1;
end

geom1.run;

mphgeom(model)

On running this code i could only get the first sphere made and the remaining sphere is not getting created. I am getting the below error:

Java exception occurred:
Exception:
com.comsol.util.exceptions.FlException: An object with the given name already exists
(rethrown as com.comsol.util.exceptions.FlException)
(rethrown as com.comsol.util.exceptions.FlException)
Messages:
An object with the given name already exists

An object with the given name already exists

An object with the given name already exists.
- Tag: sphj


Stack trace:

at com.comsol.clientapi.engine.c.handleException(Unknown Source)

at com.comsol.client.interfaces.f$d.e(Unknown Source)

at com.comsol.client.interfaces.f.a(Unknown Source)

at com.comsol.client.interfaces.f.runAndWait(Unknown Source)

at com.comsol.clientapi.engine.APIEngine.runMethod(Unknown Source)

at com.comsol.clientapi.impl.GeomFeatureListClient.create(Unknown Source)

at com.comsol.clientapi.impl.GeomFeatureListClient.create(Unknown Source)

Caused by: Exception:
com.comsol.util.exceptions.FlException: An object with the given name already exists
(rethrown as com.comsol.util.exceptions.FlException)
Messages:
An object with the given name already exists

An object with the given name already exists.
- Tag: sphj


at com.comsol.clientapi.engine.c.handleException(Unknown Source)

at com.comsol.client.interfaces.f.a(Unknown Source)

at com.comsol.client.interfaces.f.processCommandAnswer(Unknown Source)

... 6 more

Caused by: Exception:
com.comsol.util.exceptions.FlException: An object with the given name already exists
Messages:
An object with the given name already exists.
- Tag: sphj


at com.comsol.model.method.ModelEntityListMethod.checkContains(Unknown Source)

at com.comsol.model.method.GeomFeatureListMethod.createInternal(Unknown Source)

at com.comsol.model.method.GeomFeatureListMethod.create(Unknown Source)

at com.comsol.model.internal.impl.GeomFeatureListImpl.b(Unknown Source)

at com.comsol.model.internal.impl.GeomFeatureListImpl$1.a(Unknown Source)

at com.comsol.model.internal.impl.GeomFeatureListImpl$1.execute(Unknown Source)

at com.comsol.model.clientserver.ClientManager$1.call(Unknown Source)

at java.util.concurrent.FutureTask.run(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor.runWorker(Unknown Source)

at java.util.concurrent.ThreadPoolExecutor$Worker.run(Unknown Source)

at java.lang.Thread.run(Unknown Source)

----------------------------------------------------------------------------------------------------------
Could you please let me know how to overcome this issue.

With regards,
--
Uma Shankar

4 Replies Last Post 17.01.2017, 07:40 GMT-5
Henrik Sönnerlind COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 8 years ago 16.01.2017, 17:34 GMT-5
Hi,

There are several issues in your code.

You must give a unique name to the objects you create. The creation of the sphere will fail because of this.

After fixing that, all spheres will however get the same radius and location (the last one) since you update the values of the parameters. Using parameters to set values of a lot of programmed objects may not be a good idea; probably it is better to set the values directly.

An alternative approach is discussed in

www.comsol.com/community/forums/general/thread/123442/

but that does not really change the observations above.

Regards,
Henrik
Hi, There are several issues in your code. You must give a unique name to the objects you create. The creation of the sphere will fail because of this. After fixing that, all spheres will however get the same radius and location (the last one) since you update the values of the parameters. Using parameters to set values of a lot of programmed objects may not be a good idea; probably it is better to set the values directly. An alternative approach is discussed in https://www.comsol.com/community/forums/general/thread/123442/ but that does not really change the observations above. Regards, Henrik

Please login with a confirmed email address before reporting spam

Posted: 8 years ago 16.01.2017, 23:03 GMT-5
Hi Henrik,

Thanks for your reply.
I will try to fix these issues in my code.
Also I will go through the thread.

Also if you have any example of creating geometry in a loop in MATLAB without giving any specific name, then kindly let me know.

Have a nice day!

With regards,

--
Uma Shankar
Hi Henrik, Thanks for your reply. I will try to fix these issues in my code. Also I will go through the thread. Also if you have any example of creating geometry in a loop in MATLAB without giving any specific name, then kindly let me know. Have a nice day! With regards, -- Uma Shankar

Please login with a confirmed email address before reporting spam

Posted: 8 years ago 17.01.2017, 03:38 GMT-5
Hi Henrik,

I have corrected my code to generate different spheres with different radii but i am unable to change their location. Is there anyway to improve my code so that I could change the sphere positions using the matlab variables. Below is the code:

model = ModelUtil.create('Model2');

geom1 = model.geom.create('geom1', 3);

r=1;C1=1;C2=1;C3=1; %matlab variables
model.param.set('r',r,'radius');
model.param.set('C1',C1,'coordinate of center');
model.param.set('C2',C2,'coordinate of center');
model.param.set('C3',C3,'coordinate of center');

prompt= 'Enter the value N \n';

N=input(prompt);%This may vary based on user input

for j=1:1:N
tag=model.geom('geom1').feature().uniquetag('sph');
model.geom('geom1').feature().create(tag,'Sphere');
model.geom('geom1').feature(tag).set('r', r);
model.geom('geom1').feature(tag).set('pos', {'C1' 'C2' 'C3'});
model.geom('geom1').run(tag);
r=r+1;
C1=C1+1;C2=C2+1;C3=C3+1;
end

mphgeom(model)

With regards,

--
Uma Shankar
Hi Henrik, I have corrected my code to generate different spheres with different radii but i am unable to change their location. Is there anyway to improve my code so that I could change the sphere positions using the matlab variables. Below is the code: model = ModelUtil.create('Model2'); geom1 = model.geom.create('geom1', 3); r=1;C1=1;C2=1;C3=1; %matlab variables model.param.set('r',r,'radius'); model.param.set('C1',C1,'coordinate of center'); model.param.set('C2',C2,'coordinate of center'); model.param.set('C3',C3,'coordinate of center'); prompt= 'Enter the value N \n'; N=input(prompt);%This may vary based on user input for j=1:1:N tag=model.geom('geom1').feature().uniquetag('sph'); model.geom('geom1').feature().create(tag,'Sphere'); model.geom('geom1').feature(tag).set('r', r); model.geom('geom1').feature(tag).set('pos', {'C1' 'C2' 'C3'}); model.geom('geom1').run(tag); r=r+1; C1=C1+1;C2=C2+1;C3=C3+1; end mphgeom(model) With regards, -- Uma Shankar

Please login with a confirmed email address before reporting spam

Posted: 8 years ago 17.01.2017, 07:40 GMT-5
Hi Henrik,

I could now solve my issue by improving my MATLAB code as below:

model = ModelUtil.create('Model2');

geom1 = model.geom.create('geom1', 3);

r=1;C1=1;C2=1;C3=1; %matlab variables
model.param.set('r',r,'radius');
model.param.set('C1',C1,'coordinate of center');
model.param.set('C2',C2,'coordinate of center');
model.param.set('C3',C3,'coordinate of center');
prompt= 'Enter the value N \n';
N=input(prompt);%This may vary based on user input
for j=1:1:N
tag=model.geom('geom1').feature().uniquetag('sph');
model.geom('geom1').feature().create(tag,'Sphere');
model.geom('geom1').feature(tag).set('r', r);
model.geom('geom1').feature(tag).set('pos', [C1 C2 C3]);
model.geom('geom1').run(tag);
r=r+1;
C1=C1+1;C2=C2+1;C3=C3+1;
end
model.label('Model2.mph');
mphgeom(model)

I would like to mention that the blog by Lars Gregersen at the link: www.comsol.com/blogs/automatically-handling-selections-comsol-multiphysics/ was very helpful to me.

Thanking you,

With regards,
--
Uma Shankar
Hi Henrik, I could now solve my issue by improving my MATLAB code as below: model = ModelUtil.create('Model2'); geom1 = model.geom.create('geom1', 3); r=1;C1=1;C2=1;C3=1; %matlab variables model.param.set('r',r,'radius'); model.param.set('C1',C1,'coordinate of center'); model.param.set('C2',C2,'coordinate of center'); model.param.set('C3',C3,'coordinate of center'); prompt= 'Enter the value N \n'; N=input(prompt);%This may vary based on user input for j=1:1:N tag=model.geom('geom1').feature().uniquetag('sph'); model.geom('geom1').feature().create(tag,'Sphere'); model.geom('geom1').feature(tag).set('r', r); model.geom('geom1').feature(tag).set('pos', [C1 C2 C3]); model.geom('geom1').run(tag); r=r+1; C1=C1+1;C2=C2+1;C3=C3+1; end model.label('Model2.mph'); mphgeom(model) I would like to mention that the blog by Lars Gregersen at the link: https://www.comsol.com/blogs/automatically-handling-selections-comsol-multiphysics/ was very helpful to me. Thanking you, With regards, -- Uma Shankar

Note that while COMSOL employees may participate in the discussion forum, COMSOL® software users who are on-subscription should submit their questions via the Support Center for a more comprehensive response from the Technical Support team.