votca 2024.2-dev
Loading...
Searching...
No Matches
beadstructure.cc
Go to the documentation of this file.
1/*
2 * Copyright 2009-2020 The VOTCA Development Team (http://www.votca.org)
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 *
16 */
17
18// Standard includes
19#include <cassert>
20
21// VOTCA includes
25
26// Local VOTCA includes
28
29using namespace std;
30using namespace votca;
31using namespace votca::csg;
32using namespace votca::tools;
33
34/**********************
35 * Internal Functions *
36 **********************/
37
39 if (!graphUpToDate) {
40 std::vector<tools::Edge> connections_vector;
41 for (const tools::Edge &edge : connections_) {
42 connections_vector.push_back(edge);
43 }
44
45 for (std::pair<const Index, BeadInfo> &id_bead_ptr_pair : beads_) {
46 graphnodes_[id_bead_ptr_pair.first] =
47 BeadInfoToGraphNode_(id_bead_ptr_pair.second);
48 }
49 graph_ = tools::Graph(connections_vector, graphnodes_);
50 graphUpToDate = true;
51 }
52}
53
62
64 const BeadInfo &bead_info) {
65 std::unordered_map<std::string, double> attributes1;
66 std::unordered_map<std::string, std::string> attributes2;
67
68 attributes1["Mass"] = bead_info.mass;
69 attributes2["Name"] = bead_info.name;
70
72 tools::GraphNode graphnode;
73 graphnode.setDouble(attributes1);
74 graphnode.setStr(attributes2);
75
76 return graphnode;
77}
78
79/***************************
80 * Public Facing Functions *
81 ***************************/
82
84 const std::vector<Index> &bead_ids,
85 const std::vector<tools::Edge> &connections) const {
86 BeadStructure new_beadstructure;
87 for (const Index &bead_id : bead_ids) {
88 if (beads_.count(bead_id) == 0) {
89 string error_msg =
90 "Cannot get bead substructure from current "
91 "BeadStructure, bead with id " +
92 to_string(bead_id) +
93 " is not found in"
94 " the BeadStructure";
95 throw runtime_error(error_msg);
96 }
97 new_beadstructure.beads_[bead_id] = beads_.at(bead_id);
98 }
99 for (const tools::Edge &edge : connections) {
100 if (connections_.count(edge) == 0) {
101 string error_msg =
102 "Cannot get bead substructure from current "
103 "BeadStructure, edge between beads " +
104 to_string(edge.getEndPoint1()) + " and " +
105 to_string(edge.getEndPoint2()) +
106 " is not found in the "
107 "BeadStructure";
108 throw runtime_error(error_msg);
109 }
110 new_beadstructure.ConnectBeads(edge.getEndPoint1(), edge.getEndPoint2());
111 }
112 return new_beadstructure;
113}
114
115void BeadStructure::ConnectBeads(const Index &bead1_id, const Index &bead2_id) {
116 if (!(beads_.count(bead1_id)) || !(beads_.count(bead2_id))) {
117 std::string err =
118 "Cannot connect beads in bead structure that do not exist";
119 throw std::invalid_argument(err);
120 }
121 if (bead1_id == bead2_id) {
122 std::string err = "Beads cannot be self-connected";
123 throw std::invalid_argument(err);
124 }
125 size_t numberOfConnections = connections_.size();
126 connections_.insert(tools::Edge(bead1_id, bead2_id));
127 if (numberOfConnections != connections_.size()) {
129 graphUpToDate = false;
130 structureIdUpToDate = false;
131 }
132}
133
138
140
142 if (single_structureUpToDate_ == false) {
143 std::vector<Index> vertices = graph_.getVertices();
144 if (vertices.size() == 0) {
145 single_structure_ = false;
146 return single_structure_;
147 }
148 // Choose first vertex that is actually in the graph as the starting vertex
149 tools::Graph_BF_Visitor gv_breadth_first;
150 gv_breadth_first.setStartingVertex(vertices.at(0));
151 if (!singleNetwork(graph_, gv_breadth_first)) {
152 single_structure_ = false;
153 return single_structure_;
154 }
155 if (beads_.size() == 0) {
156 single_structure_ = false;
157 return single_structure_;
158 }
159 if (vertices.size() != beads_.size()) {
160 single_structure_ = false;
161 return single_structure_;
162 }
163 single_structure_ = true;
165 }
166 return single_structure_;
167}
168
170 if (!structureIdUpToDate) {
172 }
173 if (!beadstructure.structureIdUpToDate) {
174 beadstructure.CalculateStructure_();
175 }
176 return structure_id_.compare(beadstructure.structure_id_) == 0;
177}
178
179std::vector<Index> BeadStructure::getNeighBeadIds(const Index &index) {
180 if (!graphUpToDate) {
182 }
183 std::vector<Index> neighbor_ids = graph_.getNeighVertices(index);
184 return neighbor_ids;
185}
186
187std::vector<Index> BeadStructure::getBeadIds() const {
190 vector<Index> bead_ids;
191 for (auto &id_and_bead_info : beads_) {
192 bead_ids.push_back(id_and_bead_info.first);
193 }
194 return bead_ids;
195}
Designed to determine if the structure beads passed in.
tools::GraphNode BeadInfoToGraphNode_(const BeadInfo &)
std::set< tools::Edge > connections_
std::vector< Index > getNeighBeadIds(const Index &index)
Return a vector of all the ids of the beads neighboring the index.
virtual void ConnectBeads(const Index &bead1_id, const Index &bead2_id)
Create a connection between two beads in the structure.
std::unordered_map< Index, tools::GraphNode > graphnodes_
std::unordered_map< Index, BeadInfo > beads_
bool isStructureEquivalent(BeadStructure &beadstructure)
Compare the topology of two bead structures.
BeadStructure getSubStructure(const std::vector< Index > &idx, const std::vector< tools::Edge > &edges) const
Given indices and edges that exist are a subset of beadstructure, return the sub-beadstructure.
std::vector< Index > getBeadIds() const
Return the ids of the beads that are in the structure.
bool isSingleStructure()
Determine if the bead structure consists of a single connected structure.
Connects to vertices.
Definition edge.h:42
A graph node that will take a variety of different values.
Definition graphnode.h:46
void setDouble(const std::unordered_map< std::string, double > double_vals)
Definition graphnode.cc:137
void setStr(const std::unordered_map< std::string, std::string > str_vals)
Definition graphnode.cc:142
void setStartingVertex(Index vertex)
std::vector< Index > getNeighVertices(Index vertex) const
Definition graph.h:95
std::vector< Index > getVertices() const
Returns all the vertices in the graph.
Definition graph.cc:166
STL namespace.
bool singleNetwork(Graph &graph, GraphVisitor &graph_visitor)
Determine if every vertex is connected to every other one through some combination of edges.
std::string findStructureId(Graph &graph)
Find a unique identifier that describes graph structure.
base class for all analysis tools
Definition basebead.h:33
Eigen::Index Index
Definition types.h:26
Small structure to help store bead info relevant to the structure.