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.

Extracting and Sorting Entity Numbers based on their Coordinates

Clarissa Amaral da Silva

Please login with a confirmed email address before reporting spam

Hi,

I'm working on a model in Comsol+MATLAB and when trying to automate my process I came across a problem that I haven't been able to solve.

I create a semi-circle using a polygon geometry with the following command:

model.geom('geom1').create('pol1', 'Polygon');

model.geom('geom1').feature('pol1').set('x', 'r*sin({range(0,dtheta,pi)})') ;

model.geom('geom1').feature('pol1').set('y', 'r*cos({range(0,dtheta,pi)})');

model.geom('geom1').run;

Later on my model I need to find the coordinates of those points that I created and sort them out so that they go from 0 to pi. For this I use the following:

% Indices of membrane points and edges from theta = 0 to theta = pi

mempoints_raw = intersect(mphgetadj(model,'geom1','point','domain',intdomain),mphgetadj(model,'geom1','point','domain',extdomain));

memedges_raw = intersect(mphgetadj(model,'geom1','boundary','domain',intdomain),mphgetadj(model,'geom1','boundary','domain',extdomain));

if mod(length(mempoints_raw),2) == 0 

    mempoints = [mempoints_raw(2:2:end) mempoints_raw(end-1:-2:1)];

    memedges = [memedges_raw(2:2:end-1) memedges_raw(end:-2:1)];

else 

    mempoints = [mempoints_raw(2:2:end-1) mempoints_raw(end:-2:1)];

    memedges = [memedges_raw(2:2:end) memedges_raw(end-1:-2:1)];

end

end

mempoints_coords = mphgetcoords(model,'geom1','point',mempoints);

Now, this was working fine because I had symetric points, but now I need a finner discretization in only one of those segments (let's say that only between pi-dtheta and pi I add more points) and the code that I was using to sort my points and segments from 0 to pi doesn't hold anymore (because of the way Comsol defines the entity numbers).

So, my question is: Is there a simple way to extract the indeces of the points, find their coordinates and sort them (the indeces) based on those coordinates?

I thought of extracting the coordinates of my raw points, sort those coordinates in descending order for the y axis (using the sort command from MATLAB), then fix both my x axis and my number entities based on the indeces found with the sort function. So, basically doing:

mempoints_raw = intersect(mphgetadj(model,'geom1','point','domain',intdomain),... mphgetadj(model,'geom1','point','domain',extdomain));

mempoints_coords = mphgetcoords(model,'geom1','point',mempoints);

x = mempoints_coords(1,:);

y = mempoints_coords(2,:);

[y_sorted, index] = sort(y,' descend');

x_sorted = x(index);

mempoints_sorted = mempoints(index);

But I was hoping there would be a more elegant way of doing this.

Thanks for your help.

Clarissa


1 Reply Last Post Oct 27, 2017, 5:45 a.m. EDT
Lars Gregersen COMSOL Employee

Please login with a confirmed email address before reporting spam

Posted: 7 years ago Oct 27, 2017, 5:45 a.m. EDT

Hi Clarissa

I don't think there is any reason for trying a more elegant way to code this if it works and runs fast.

Anyway, here are some ideas:

1) If you calculate the coordinates in Matlab you don't have to extract the coordinates from the model afterwards. Just calculate x and y and send them to Comsol like this: model.geom('geom1').feature('pol1').set('x', x) ; model.geom('geom1').feature('pol1').set('y', y);

2) You can use atan2 for sorting. Here is a small example that you'll have to fit to you own code:

t = rand(100,1)*pi;
x = cos(t);
y = sin(t);
figure(1)
plot(x,y) % looks awfull

a = atan2(y,x);
[as,idx] = sort(a);
figure(2)
plot(x(idx), y(idx)) % much better

3) Once you have the coordinates of the points you can use mphselectcoords to find the entity numbers.

-------------------
Lars Gregersen
Comsol Denmark
Hi Clarissa I don't think there is any reason for trying a more elegant way to code this if it works and runs fast. Anyway, here are some ideas: 1) If you calculate the coordinates in Matlab you don't have to extract the coordinates from the model afterwards. Just calculate x and y and send them to Comsol like this: model.geom('geom1').feature('pol1').set('x', x) ; model.geom('geom1').feature('pol1').set('y', y); 2) You can use atan2 for sorting. Here is a small example that you'll have to fit to you own code: ``` t = rand(100,1)*pi; x = cos(t); y = sin(t); figure(1) plot(x,y) % looks awfull a = atan2(y,x); [as,idx] = sort(a); figure(2) plot(x(idx), y(idx)) % much better ``` 3) Once you have the coordinates of the points you can use mphselectcoords to find the entity numbers.

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.