From: Benjamin S. K. <be...@cf...> - 2003-10-27 17:27:13
|
The problem is that there is no default constructor for Node objects. The reason for this is that the Mesh creates nodes, you probably just want to use the ones in the Mesh. The code you have written might have unexpected consequences. You are in effect copying the Nodes and all their associated data into your ElectrPos vector. The library may, at some later time, decide to change the Node-DOF association, and you would be stuck with old values in your vector that don't apply to the modified mesh. This could occur as a consequence of mesh refinement, for example. I suggest that instead of copying the nodes outright you get pointers to them. This will have two beneficial consequences: 1.) You avoid the "stale information" problem I describe above 2.) It saves space. A pointer is much smaller than a Node. I would change the code to this: const Mesh& mesh = nonconst_mesh; std::vector<const Node*> ElectrPos; unsigned int n_nodes = mesh.n_nodes(); for (unsigned int n_cnt=0; n_cnt<n_nodes; n_cnt++) { const Node* curr_node_ptr = mesh.node_ptr(n_cnt); if (mesh.data.has_data (curr_node_ptr)) { const std::vector<Number>& node_data = mesh.data.get_data (curr_node_ptr); if (node_data[0]==-99) ElectrPos.push_back(curr_node_ptr); } } Note that std::vector<>::resize() handles the sizing issues for you, so there is no need to have the ElectrCount variable. Of course, after the loop ElectrPos.size() will give you exactly the same information. Since ElectrPos now contains pointers the syntax of how you use it will be changed, but everything else should be fine... For example, ElectrPos[num].dof_number(...) becomes ElectrPos[num]->dof_number(...) Hope this helps. -Ben Jens Oeser wrote: > Hi, > > I've written a direct current modelling program for geophysical purposes > with libmesh. Till now every thing seems to work quit proper. So far so > good. > > But now to my problem, which is quit simple so I think, but I could not > solve it. I have data associated to each node. Now I would like to > create a vector of nodes, in which the nodes marked as a electrode > position are stored (the marker is -99). To solve this I've done the > following: > > const Mesh& const_mesh = mesh; > > std::vector<Number> node_data; > std::vector<Node> ElectrPos; > unsigned int ElectrCount=1; > > unsigned int n_nodes = mesh.n_nodes(); > > for (unsigned int n_cnt=0; n_cnt<n_nodes; n_cnt++) > { > const Node& curr_node = const_mesh.node(n_cnt); > const Node* curr_node_ptr = const_mesh.node_ptr(n_cnt); > > if (mesh.data.has_data (curr_node_ptr)) > { > node_data = mesh.data.get_data (curr_node_ptr); > if (node_data[0]==-99) > { > ElectrPos.resize(ElectrCount); > ElectrPos.push_back(curr_node); > ElectrCount++; > } > } > } > > The problem is, that it's not possible to resize the vector ElectrPos. > The compiler complains: > /usr/include/c++/3.3/bits/stl_vector.h: In member function `void > std::vector<_Tp, _Alloc>::resize(unsigned int) [with _Tp = Node, > _Alloc = std::allocator<Node>]': > InOut.C:163: instantiated from here > /usr/include/c++/3.3/bits/stl_vector.h:452: error: no matching function > for call to `Node::Node()' > /home/oeser/DA/libmesh/include/node.h:150: error: candidates are: > Node::Node(const Point&, unsigned int = DofObject::invalid_id) > /home/oeser/DA/libmesh/include/node.h:139: error: > Node::Node(const Node&) > /home/oeser/DA/libmesh/include/node.h:130: error: > Node::Node(double, double, double, unsigned int) > > If I change ElectrPos.resize(ElectrCount) to > ElectrPos.resize(curr_node) as the compiler mentioned (with absence of > understanding why I should do this), the compiler complains: > InOut.C:163: error: no matching function for call to `std::vector<Node, > std::allocator<Node> >::resize(const Node&)' > /usr/include/c++/3.3/bits/stl_vector.h:434: error: candidates are: void > std::vector<_Tp, _Alloc>::resize(unsigned int, const _Tp&) [with _Tp > = Node, _Alloc = std::allocator<Node>] > /usr/include/c++/3.3/bits/stl_vector.h:452: error: void > std::vector<_Tp, _Alloc>::resize(unsigned int) [with _Tp = Node, > _Alloc = std::allocator<Node>] > > What's going on, where is the problem? Because I'm currently new to OOP > and C++ I could not solve the problem. But I hope you can give me some > hints or a other way to solve the problem. > > I think I need to store the reference, because I need to call > DofObject::dof_number for these nodes. > > Best regards. > Jens. > |