Reputation: 699
I want to create a finite element object oriented program. I have a class Node. Since the nodes in a finite element mesh (represented by class Mesh) are distinct, I created the Node class to be a value class. When I instantiate the array of objects from class Node, I assign that object array to the nodes property of Mesh. I have an Element class too, representing a finite element. I also create an object array from this class and assign it to the element property of Mesh. It is clear up to now.
Since the finite element nodes also belong to the elements, I want to assign some of the nodes to the appropriate elements. But copying the nodes results in data redundancy, therefore I want to assign pointers to the Node objects so that the localNodes property of Element contains an array of pointers to the specific nodes. How should I modify my classes below to achieve it?
The Node class:
classdef Node
properties
coordinate;
end
methods
% Not interesting for this example
end
end
The Element class:
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
end
The Mesh class:
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
Upvotes: 2
Views: 1391
Reputation: 36710
Finally, following some discussion here is a starting point how I would solve this:
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
And finally a short demonstration:
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.
Upvotes: 3