我想创建一个面向对象的有限元程序。我有一个Node类。由于有限元网格中的节点(由类 mesh 表示)是不同的,所以我创建了Node类作为一个值类。当我从类Node实例化对象数组时,我将该对象数组分配给网格的节点属性。我也有一个元素类,表示一个有限元。我还从这个类创建了一个对象数组,并将其分配给Mesh的element属性。到目前为止还是很晴朗的。
由于有限元节点也属于单元,所以我想将其中的一些节点分配给适当的单元。但是复制节点会导致数据冗余,因此我希望为节点对象分配指针,以便元素的localNodes属性包含指向特定节点的指针数组。我应该如何修改下面的类来实现它?
Node类:
classdef Node
properties
coordinate;
end
methods
% Not interesting for this example
end
end元素类:
classdef Element
properties
localNodes; % the object instantiated from the class Element
% will store an array of pointers to the
% appropriate elements of the object array stored
% in Mesh.nodes. How can I assign these pointers
% to Element.localNodes?
end
methods
% Not interesting for this example
end
endMesh类:
classdef Mesh
properties
nodes; % its object will contain an object array of Node
elements; % its object will contain an object array of Element
end
methods
% Not interesting for this example
end
end发布于 2015-11-05 16:06:25
最后,下面的讨论是我如何解决这个问题的一个起点:
classdef Node < handle
properties
coordinate;
end
methods
function obj=Node(id)
obj.coordinate=id;
end
end
end。
classdef Mesh < handle
properties
nodes; % its object will contain an object array of Node
elements; % its object will contain an object array of Element
end
methods
function obj=Mesh(nodes,elements)
pnodes=cell(1,nodes);
for idx=1:nodes
pnodes{idx}=Node(idx);
end
obj.nodes=[pnodes{:}];
pelements=cell(1,numel(elements));
for idx=1:numel(elements)
pelements{idx}=Element(obj.nodes(elements{idx}));
end
obj.elements=[pelements{:}];
end
function non_deleted_nodes=get.nodes(obj)
%getter to return only not-deleted nodes
obj.nodes=obj.nodes(arrayfun(@isvalid,(obj.nodes)));
non_deleted_nodes=obj.nodes;
end
function non_deleted_nodes=get.elements(obj)
%getter to return only not-deleted nodes
obj.elements=obj.elements(arrayfun(@isvalid,(obj.elements)));
non_deleted_nodes=obj.elements;
end
end
end。
classdef Element < handle
properties
localNodes; % the object instantiated from the class Element
% will store an array of pointers to the
% appropriate elements of the object array stored
% in Mesh.nodes. How can I assign these pointers
% to Element.localNodes?
end
methods
function obj=Element(localNodes)
obj.localNodes=localNodes;
end
function non_deleted_nodes=get.localNodes(obj)
%getter to return only not-deleted nodes
obj.localNodes=obj.localNodes(arrayfun(@isvalid,(obj.localNodes)));
non_deleted_nodes=obj.localNodes
end
function delete(obj)
for ix=1:numel(obj.localNodes)
%The 1 is not a typo, we will delete always the first
%element until the list is empty
obj.localNodes(1).delete();
end
delete@handle(obj);
end
end
end最后是一个简短的演示:
m=Mesh(10,{[1,2],[2,3],[3,4]})
m.elements(1).localNodes
m.elements(1).localNodes(1).delete()
%now the node is deleted from the element and the mesh
m.elements(2).delete()
%now element 2 together with the nodes is deleted.发布于 2015-11-05 12:15:45
您必须使用超类handle,让我们使用引用而不是复制数据。
x=MyValueClass(); %assume handle not superclass
y=x; %creates a copy
x=MyHandleClass(); %assume handle is superclass
y=x; %creates a reference to the same instancehttps://stackoverflow.com/questions/33543906
复制相似问题