Looking for help changing linked class objects in parallel

1 ビュー (過去 30 日間)
Marc Laub
Marc Laub 2020 年 6 月 12 日
Hey guys,
I'am looking for help with following problem.
In my code I have defindes vertices, edges and polygons as class handle objects and each of them are linked to each other.
So the vertices knows its position and knows to which are its neighbour vertices and to which edges it belongs and which are its adjacend polygons. Same for the polygons which knows what its limiting vertices and edges are and ame for edges how know to whcih polygos its adjacend and of which vertices it consist of.
Now i need to refine my mesh by adding more vertices on the edges. So i add a new vertex on one edge and I have to relinked to previous neighbour vertices to the new vertex and cut the link to each other, i have to create new linkages to the polygons and same for polygon i need new linkages to its new limiting vertex and new edge.
Since thats pretty time consuming , especially whne i need to do it multiple times in a row, I wanted to do in parrallel since I expect a huge benefit from that.
It tried it with a parfor but it didnt work and i got the error not enough input arguments and I guess the error is that while for example look at the vertices a,b,c and the edges AB and BC, that while I am adding a vertex to AB, like AD, DB , I am changing the properties of B, more precisely the linkes of B to its edges and vertices and that the problem that I run into problems when another worker wants to calculate the edge BC at the same time.
Does anybody has an idea how to do a workaround? I thought about somehow saving the new links that can be applied later but i have no idea of how to do that.
In the code below you can see the loop that is splittig the edges and the split fuction which is a hndle class of the edges itsself.
for j=1:6
new_edges=Edge.empty(length(edge),0);
new_vertex = Vertex.empty(length(edge),0);
disp('refining edges')
for i =1:length(edge) %% here I want a parfor
[new_edges(i),new_vertex(i)]=split_edge(edge(i));
end
edge=[edge,new_edges];
vertex=[vertex,new_vertex];
end
classdef Edge < handle
properties
adjacend_vertex;
adjacend_polygon;
end
methods
function ed=Edge(v)
ed.adjacend_vertex = Vertex.empty(0,0);
ed.adjacend_polygon = Polygon.empty(0,0);
ed.adjacend_vertex(end+1)=v(1);
ed.adjacend_vertex(end+1)=v(2);
v(1).adjacend_edge(end+1)=ed;
v(2).adjacend_edge(end+1)=ed;
for i=1:numel(v(1).adjacend_polygon)
p1=v(1).adjacend_polygon(i);
for j=1:numel(v(2).adjacend_polygon)
if p1==v(2).adjacend_polygon(j)
ed.adjacend_polygon(end+1)=p1;
p1.adjacend_edge(end+1)=ed;
end
end
end
end
function [new_edge,v]=split_edge(ed)
P1=ed.adjacend_vertex(1).position;
P2=ed.adjacend_vertex(2).position;
new_vert_pos=(P1(:) + P2(:)).'/2;
% generate new vertex
v = Vertex(new_vert_pos);
v1=ed.adjacend_vertex(1);
v2=ed.adjacend_vertex(2);
% re assign old edge vertices to new vertex
v1.neighbour_vertex(find(ismember(v1.neighbour_vertex,v2)))=v;
v2.neighbour_vertex(find(ismember(v2.neighbour_vertex,v1)))=v;
% assign new vertex to old edge vertices
v.neighbour_vertex(1)=v1;
v.neighbour_vertex(2)=v2;
% assign adjacend polygon to new vertex
for i=1:length(ed.adjacend_polygon)
v.adjacend_polygon(i)=ed.adjacend_polygon(i);
end
% insert new vertex to both adjacend polygons
for k=1:length(ed.adjacend_polygon)
id=find(ismember(ed.adjacend_polygon(k).adjacend_vertex,[v1,v2]));
if id(1)==1 && id(1)+1~=id(2)
ed.adjacend_polygon(k).adjacend_vertex=[v,ed.adjacend_polygon(k).adjacend_vertex];
else
ed.adjacend_polygon(k).adjacend_vertex=[ed.adjacend_polygon(k).adjacend_vertex(1:id(1)),v,ed.adjacend_polygon(k).adjacend_vertex(id(2):end)];
end
end
for i=1:length(v1.adjacend_edge)
if find(ismember(v1.adjacend_edge(i).adjacend_vertex,v2))>0
v1.adjacend_edge(i)=[];
break
end
end
% renerate new edge
if ed.adjacend_vertex(1)==v1
new_edge=Edge([v1,v]);
else
new_edge=Edge([v2,v]);
end
% re assign one egde
ed.adjacend_vertex(1)=v;
v.adjacend_edge(end+1)=ed;
end
end
end
Maybe somebody can help my with this.
Many Thanks in advance
Best regards

回答 (0 件)

製品


リリース

R2019b

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by