Changeset e0b960 for src/Graph


Ignore:
Timestamp:
Jul 12, 2017, 7:10:32 PM (7 years ago)
Author:
Frederik Heber <frederik.heber@…>
Branches:
Action_Thermostats, Adding_Graph_to_ChangeBondActions, Adding_MD_integration_tests, Adding_StructOpt_integration_tests, AutomationFragmentation_failures, Candidate_v1.6.1, ChemicalSpaceEvaluator, Enhanced_StructuralOptimization, Enhanced_StructuralOptimization_continued, Exclude_Hydrogens_annealWithBondGraph, Fix_Verbose_Codepatterns, ForceAnnealing_with_BondGraph, ForceAnnealing_with_BondGraph_continued, ForceAnnealing_with_BondGraph_continued_betteresults, ForceAnnealing_with_BondGraph_contraction-expansion, Gui_displays_atomic_force_velocity, JobMarket_RobustOnKillsSegFaults, JobMarket_StableWorkerPool, PythonUI_with_named_parameters, Recreated_GuiChecks, StoppableMakroAction, TremoloParser_IncreasedPrecision
Children:
4a6ef3
Parents:
966ce7
git-author:
Frederik Heber <frederik.heber@…> (05/19/17 13:13:41)
git-committer:
Frederik Heber <frederik.heber@…> (07/12/17 19:10:32)
Message:

Edges may be added and removed in BoostGraphCreator.

  • TESTS: added unit test case on this.
Location:
src/Graph
Files:
4 edited

Legend:

Unmodified
Added
Removed
  • src/Graph/BoostGraphCreator.cpp

    r966ce7 re0b960  
    102102}
    103103
     104BoostGraphCreator::Edge BoostGraphCreator::findEdge(const atomId_t &_firstid, const atomId_t &_secondid)
     105{
     106  edge_iter i, end;
     107  const name_map_t name_map = boost::get(boost::vertex_name, graph);
     108  for (boost::tie(i, end) = boost::edges(graph); i != end; ++i) {
     109    const BoostGraphCreator::Edge &e = *i;
     110    const Vertex u = source(e, graph);
     111    const Vertex v = target(e, graph);
     112    const atomId_t atomid_u = boost::get(name_map, u);
     113    const atomId_t atomid_v = boost::get(name_map, v);
     114    if (atomid_u == _firstid) {
     115      if (atomid_v == _secondid)
     116        break;
     117    } else if (atomid_u == _secondid) {
     118      if (atomid_v == _firstid)
     119        break;
     120    }
     121  }
     122  if (i != end) {
     123    BoostGraphCreator::Edge e = *i;
     124    return e;
     125  }
     126
     127  return BoostGraphCreator::Edge();
     128}
     129
     130bool BoostGraphCreator::removeEdge(const atomId_t &_firstid, const atomId_t &_secondid)
     131{
     132  // look through all edges
     133  BoostGraphCreator::Edge e = findEdge(_firstid, _secondid);
     134
     135  // edge was found
     136  if (e != BoostGraphCreator::Edge()) {
     137    const Vertex u = source(e, graph);
     138    const Vertex v = target(e, graph);
     139    if (DoLog(3)) {
     140      const name_map_t name_map = boost::get(boost::vertex_name, graph);
     141      LOG(3, "DEBUG: Found edge " << boost::get(name_map, u) <<  " <-> "
     142          << boost::get(name_map, v) << ", removing.");
     143    }
     144    boost::remove_edge(e, graph);
     145
     146    return true;
     147  }
     148  return false;
     149}
     150
     151bool BoostGraphCreator::addEdge(const atomId_t &_firstid, const atomId_t &_secondid)
     152{
     153  // look through all edges
     154  BoostGraphCreator::Edge e = findEdge(_firstid, _secondid);
     155
     156  if (e != BoostGraphCreator::Edge()) {
     157    return false;
     158  } else {
     159    const nodeId_t leftnodeid = getNodeId(_firstid);
     160    const nodeId_t rightnodeid = getNodeId(_secondid);
     161
     162    boost::add_edge(leftnodeid, rightnodeid, graph);
     163
     164    return true;
     165  }
     166}
  • src/Graph/BoostGraphCreator.hpp

    r966ce7 re0b960  
    6161  //!> typedef for vertex iterator
    6262  typedef boost::graph_traits<UndirectedGraph>::vertex_iterator vertex_iter;
     63  //!> typedef for a Edge
     64  typedef boost::graph_traits<UndirectedGraph>::edge_descriptor Edge;
     65  //!> typedef for edge iterator
     66  typedef boost::graph_traits<UndirectedGraph>::edge_iterator edge_iter;
    6367
    6468  //!> typedef for a node id
     
    145149      );
    146150
     151  /** Finds a given edge by its two atomic indices.
     152   *
     153   * \param _firstid first atomic id of edge
     154   * \param _secondid second atomic id of edge
     155   * \return edge descriptor in graph or empty descriptor
     156   */
     157  Edge findEdge(const atomId_t &_firstid, const atomId_t &_secondid);
     158
     159  /** Allows to remove a present edge in the graph.
     160   *
     161   * \param _firstid first atomic id of edge
     162   * \param _secondid second atomic id of edge
     163   * \return true - edge found and removed, false - else
     164   */
     165  bool removeEdge(const atomId_t &_firstid, const atomId_t &_secondid);
     166
     167  /** Adds an edge to the graph if not already present.
     168   *
     169   * \param _firstid first atomic id of edge
     170   * \param _secondid second atomic id of edge
     171   * \return true - edge not found thus added, false - else
     172   */
     173  bool addEdge(const atomId_t &_firstid, const atomId_t &_secondid);
     174
    147175private:
    148176  //!> internal graph that is created by creator functions
  • src/Graph/unittests/BoostGraphCreatorUnitTest.cpp

    r966ce7 re0b960  
    172172  CPPUNIT_ASSERT_EQUAL ((size_t)5, BGCreator->getNumEdges());
    173173};
     174
     175/** Tests whether adding and removing edges works.
     176 */
     177void BoostGraphCreatorTest::addremoveEdgeTest()
     178{
     179  typedef std::pair<int,int> E;
     180
     181  E edges[] = { E(0,1), E(1,2), E(2,3), E(3,4) };
     182  const size_t no_nodes = 5;
     183  BGCreator->graph =
     184      BoostGraphCreator::UndirectedGraph(edges, edges + sizeof(edges) / sizeof(E), no_nodes);
     185  BGCreator->atomids_nodeids +=
     186      make_pair(0,0), make_pair(1,1), make_pair(2,2), make_pair(3,3), make_pair(4,4);
     187  for (size_t i=0;i<no_nodes;++i)
     188    boost::put(boost::get(boost::vertex_name, BGCreator->graph), boost::vertex(i, BGCreator->graph), i);
     189
     190  CPPUNIT_ASSERT_EQUAL ((size_t)5, BGCreator->getNumVertices());
     191  CPPUNIT_ASSERT_EQUAL ((size_t)4, BGCreator->getNumEdges());
     192
     193  bool status;
     194  // remove a valid edge
     195  {
     196    status = BGCreator->removeEdge((atomId_t)0,(atomId_t)1);
     197    CPPUNIT_ASSERT_EQUAL (true, status);
     198    status = BGCreator->addEdge((atomId_t)0,(atomId_t)1);
     199    CPPUNIT_ASSERT_EQUAL (true, status);
     200    // check again whether edge has really been added again
     201    status = BGCreator->removeEdge((atomId_t)0,(atomId_t)1);
     202    CPPUNIT_ASSERT_EQUAL (true, status);
     203    status = BGCreator->addEdge((atomId_t)0,(atomId_t)1);
     204    CPPUNIT_ASSERT_EQUAL (true, status);
     205  }
     206
     207  // remove an invalid edge
     208  {
     209    status = BGCreator->removeEdge((atomId_t)0,(atomId_t)2);
     210    CPPUNIT_ASSERT_EQUAL (false, status);
     211  }
     212
     213  // add a present edge
     214  {
     215    status = BGCreator->addEdge((atomId_t)0,(atomId_t)1);
     216    CPPUNIT_ASSERT_EQUAL (false, status);
     217  }
     218};
  • src/Graph/unittests/BoostGraphCreatorUnitTest.hpp

    r966ce7 re0b960  
    2828    CPPUNIT_TEST ( createFromMoleculeTest );
    2929    CPPUNIT_TEST ( createFromAtomsTest );
     30    CPPUNIT_TEST ( addremoveEdgeTest );
    3031    CPPUNIT_TEST_SUITE_END();
    3132
     
    3738      void createFromMoleculeTest();
    3839      void createFromAtomsTest();
     40      void addremoveEdgeTest();
    3941
    4042private:
Note: See TracChangeset for help on using the changeset viewer.