source: src/Actions/FragmentationAction/SubgraphDissectionAction.cpp@ db7cb0

Action_Thermostats Add_AtomRandomPerturbation Add_FitFragmentPartialChargesAction Add_RotateAroundBondAction Add_SelectAtomByNameAction Added_ParseSaveFragmentResults AddingActions_SaveParseParticleParameters Adding_Graph_to_ChangeBondActions Adding_MD_integration_tests Adding_ParticleName_to_Atom Adding_StructOpt_integration_tests AtomFragments Automaking_mpqc_open AutomationFragmentation_failures Candidate_v1.5.4 Candidate_v1.6.0 Candidate_v1.6.1 ChangeBugEmailaddress ChangingTestPorts ChemicalSpaceEvaluator CombiningParticlePotentialParsing Combining_Subpackages Debian_Package_split Debian_package_split_molecuildergui_only Disabling_MemDebug Docu_Python_wait EmpiricalPotential_contain_HomologyGraph EmpiricalPotential_contain_HomologyGraph_documentation Enable_parallel_make_install Enhance_userguide Enhanced_StructuralOptimization Enhanced_StructuralOptimization_continued Example_ManyWaysToTranslateAtom Exclude_Hydrogens_annealWithBondGraph FitPartialCharges_GlobalError Fix_BoundInBox_CenterInBox_MoleculeActions Fix_ChargeSampling_PBC Fix_ChronosMutex Fix_FitPartialCharges Fix_FitPotential_needs_atomicnumbers Fix_ForceAnnealing Fix_IndependentFragmentGrids Fix_ParseParticles Fix_ParseParticles_split_forward_backward_Actions Fix_PopActions Fix_QtFragmentList_sorted_selection Fix_Restrictedkeyset_FragmentMolecule Fix_StatusMsg Fix_StepWorldTime_single_argument Fix_Verbose_Codepatterns Fix_fitting_potentials Fixes ForceAnnealing_goodresults ForceAnnealing_oldresults ForceAnnealing_tocheck ForceAnnealing_with_BondGraph ForceAnnealing_with_BondGraph_continued ForceAnnealing_with_BondGraph_continued_betteresults ForceAnnealing_with_BondGraph_contraction-expansion FragmentAction_writes_AtomFragments FragmentMolecule_checks_bonddegrees GeometryObjects Gui_Fixes Gui_displays_atomic_force_velocity ImplicitCharges IndependentFragmentGrids IndependentFragmentGrids_IndividualZeroInstances IndependentFragmentGrids_IntegrationTest IndependentFragmentGrids_Sole_NN_Calculation JobMarket_RobustOnKillsSegFaults JobMarket_StableWorkerPool JobMarket_unresolvable_hostname_fix MoreRobust_FragmentAutomation ODR_violation_mpqc_open PartialCharges_OrthogonalSummation PdbParser_setsAtomName PythonUI_with_named_parameters QtGui_reactivate_TimeChanged_changes Recreated_GuiChecks Rewrite_FitPartialCharges RotateToPrincipalAxisSystem_UndoRedo SaturateAtoms_findBestMatching SaturateAtoms_singleDegree StoppableMakroAction Subpackage_CodePatterns Subpackage_JobMarket Subpackage_LinearAlgebra Subpackage_levmar Subpackage_mpqc_open Subpackage_vmg Switchable_LogView ThirdParty_MPQC_rebuilt_buildsystem TrajectoryDependenant_MaxOrder TremoloParser_IncreasedPrecision TremoloParser_MultipleTimesteps TremoloParser_setsAtomName Ubuntu_1604_changes stable
Last change on this file since db7cb0 was 7d8342, checked in by Frederik Heber <heber@…>, 15 years ago

SubgraphDissectionAction does not delete bonds anymore and bugfixes regarding its redo.

  • Only creates BondGraph of system when no bonds are present.
  • redo was not working correctly due to wrong end state of performCall():
  • Property mode set to 100644
File size: 8.2 KB
Line 
1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
4 * Copyright (C) 2010 University of Bonn. All rights reserved.
5 * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
6 */
7
8/*
9 * SubgraphDissectionAction.cpp
10 *
11 * Created on: May 9, 2010
12 * Author: heber
13 */
14
15// include config.h
16#ifdef HAVE_CONFIG_H
17#include <config.h>
18#endif
19
20#include "Helpers/MemDebug.hpp"
21
22#include "Descriptors/AtomIdDescriptor.hpp"
23#include "Descriptors/MoleculeDescriptor.hpp"
24
25#include "atom.hpp"
26#include "bond.hpp"
27#include "bondgraph.hpp"
28#include "config.hpp"
29#include "Helpers/Log.hpp"
30#include "Helpers/Verbose.hpp"
31#include "molecule.hpp"
32#include "stackclass.hpp"
33#include "World.hpp"
34
35#include <iostream>
36#include <string>
37
38typedef std::map< moleculeId_t, std::vector<atomId_t> > MolAtomList;
39typedef std::map< atomId_t, atomId_t > AtomAtomList;
40
41using namespace std;
42
43#include "Actions/FragmentationAction/SubgraphDissectionAction.hpp"
44
45// and construct the stuff
46#include "SubgraphDissectionAction.def"
47#include "Action_impl_pre.hpp"
48/** =========== define the function ====================== */
49Action::state_ptr FragmentationSubgraphDissectionAction::performCall() {
50 // obtain information
51 getParametersfromValueStorage();
52
53 DoLog(1) && (Log() << Verbose(1) << "Dissecting molecular system into a set of disconnected subgraphs ... " << endl);
54
55 // first create stuff for undo state
56 MolAtomList moleculelist;
57 vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
58 for (vector<molecule *>::const_iterator moliter = allmolecules.begin(); moliter != allmolecules.end(); ++moliter) {
59 std::vector<atomId_t> atomlist;
60 atomlist.resize((*moliter)->size());
61 for (molecule::const_iterator atomiter = (*moliter)->begin(); atomiter != (*moliter)->end(); ++atomiter) {
62 atomlist.push_back((*atomiter)->getId());
63 }
64 moleculelist.insert( std::pair< moleculeId_t, std::vector<atomId_t> > ((*moliter)->getId(), atomlist));
65 }
66 FragmentationSubgraphDissectionState *UndoState = new FragmentationSubgraphDissectionState(moleculelist, params);
67
68 // 0a. remove all present molecules
69 MoleculeListClass *molecules = World::getInstance().getMolecules();
70 for (vector<molecule *>::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) {
71 molecules->erase(*MolRunner);
72 World::getInstance().destroyMolecule(*MolRunner);
73 }
74
75 // 0b. remove all bonds and construct a molecule with all atoms
76 molecule *mol = World::getInstance().createMolecule();
77 int BondCount = 0;
78 {
79 vector <atom *> allatoms = World::getInstance().getAllAtoms();
80 for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
81 BondCount += (*AtomRunner)->ListOfBonds.size();
82// for(BondList::iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); !(*AtomRunner)->ListOfBonds.empty(); BondRunner = (*AtomRunner)->ListOfBonds.begin())
83// delete(*BondRunner);
84 mol->AddAtom(*AtomRunner);
85 }
86 }
87
88 // 1. create the bond structure of the single molecule
89 if (BondCount == 0) {
90 config * const configuration = World::getInstance().getConfig();
91 if ((configuration->BG != NULL)) {
92 if (!configuration->BG->ConstructBondGraph(mol)) {
93 World::getInstance().destroyMolecule(mol);
94 DoeLog(1) && (eLog()<< Verbose(1) << "There are no bonds." << endl);
95 return Action::failure;
96 }
97 } else {
98 DoeLog(1) && (eLog()<< Verbose(1) << "There is no BondGraph class present to create bonds." << endl);
99 return Action::failure;
100 }
101 }
102
103 // 2. scan for connected subgraphs
104 MoleculeLeafClass *Subgraphs = NULL; // list of subgraphs from DFS analysis
105 class StackClass<bond *> *BackEdgeStack = NULL;
106 Subgraphs = mol->DepthFirstSearchAnalysis(BackEdgeStack);
107 delete(BackEdgeStack);
108 if ((Subgraphs == NULL) || (Subgraphs->next == NULL)) {
109 //World::getInstance().destroyMolecule(mol);
110 DoeLog(1) && (eLog()<< Verbose(1) << "There are no atoms." << endl);
111 return Action::failure;
112 }
113
114 //int FragmentCounter = Subgraphs->next->Count();
115
116 // TODO: When DepthFirstSearchAnalysis does not use AddCopyAtom() anymore, we don't need to delete all original atoms
117 {
118 {
119 atom **ListOfAtoms = NULL;
120 // 3a. re-create bond structure and insert molecules into general MoleculeListClass
121 MoleculeLeafClass *MoleculeWalker = Subgraphs->next;
122 while (MoleculeWalker->next != NULL) {
123 ListOfAtoms = NULL;
124 MoleculeWalker->FillBondStructureFromReference(mol, ListOfAtoms, true); // we want to keep the created ListOfLocalAtoms
125 molecules->insert(MoleculeWalker->Leaf);
126 MoleculeWalker = MoleculeWalker->next;
127 }
128 molecules->insert(MoleculeWalker->Leaf);
129 ListOfAtoms = NULL;
130 MoleculeWalker->FillBondStructureFromReference(mol, ListOfAtoms, true); // we want to keep the created ListOfLocalAtoms
131 }
132
133 // 3b. store map from new to old ids for 3d
134 vector <atom *> allatoms = World::getInstance().getAllAtoms();
135 AtomAtomList newtooldlist;
136 for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner)
137 if ((*AtomRunner)->father != (*AtomRunner))
138 newtooldlist.insert( std::pair<atomId_t, atomId_t> ((*AtomRunner)->getId(),(*AtomRunner)->father->getId()) );
139
140 {
141 // 3c. destroy the original molecule
142 for (molecule::iterator AtomRunner = mol->begin(); !mol->empty(); AtomRunner = mol->begin())
143 World::getInstance().destroyAtom(*AtomRunner);
144 World::getInstance().destroyMolecule(mol);
145 }
146
147 {
148 // 3d. convert to old Ids and correct fathers (AddCopyAtom sets father to original atom, which has been destroyed).
149 vector <atom *> allatoms = World::getInstance().getAllAtoms();
150 for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
151 World::getInstance().changeAtomId((*AtomRunner)->getId(), newtooldlist[(*AtomRunner)->getId()]);
152 (*AtomRunner)->father = *AtomRunner;
153 }
154 }
155 }
156
157 // 4. free Leafs
158 MoleculeLeafClass *MolecularWalker = Subgraphs;
159 while (MolecularWalker->next != NULL) {
160 MolecularWalker->Leaf = NULL;
161 MolecularWalker = MolecularWalker->next;
162 delete(MolecularWalker->previous);
163 }
164 MolecularWalker->Leaf = NULL;
165 delete(MolecularWalker);
166 DoLog(1) && (Log() << Verbose(1) << "I scanned " << molecules->ListOfMolecules.size() << " molecules." << endl);
167
168 return Action::state_ptr(UndoState);
169}
170
171Action::state_ptr FragmentationSubgraphDissectionAction::performUndo(Action::state_ptr _state) {
172 FragmentationSubgraphDissectionState *state = assert_cast<FragmentationSubgraphDissectionState*>(_state.get());
173
174 {
175 // remove all present molecules
176 MoleculeListClass *molecules = World::getInstance().getMolecules();
177 vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
178 for (vector<molecule *>::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) {
179 molecules->erase(*MolRunner);
180 World::getInstance().destroyMolecule(*MolRunner);
181 }
182 }
183
184 {
185 // construct the old state
186 MoleculeListClass *molecules = World::getInstance().getMolecules();
187 molecule *mol = NULL;
188 for (MolAtomList::const_iterator iter = state->moleculelist.begin(); iter != state->moleculelist.end(); ++iter) {
189 mol = World::getInstance().createMolecule();
190 if (mol->getId() != (*iter).first)
191 World::getInstance().changeMoleculeId(mol->getId(), (*iter).first);
192 for (std::vector<atomId_t>::const_iterator atomiter = (*iter).second.begin(); atomiter != (*iter).second.end(); ++atomiter) {
193 atom *Walker = World::getInstance().getAtom(AtomById(*atomiter));
194 mol->AddAtom(Walker);
195 }
196 molecules->insert(mol);
197 }
198 }
199
200 return Action::state_ptr(_state);
201}
202
203Action::state_ptr FragmentationSubgraphDissectionAction::performRedo(Action::state_ptr _state){
204 return performCall();
205}
206
207bool FragmentationSubgraphDissectionAction::canUndo() {
208 return true;
209}
210
211bool FragmentationSubgraphDissectionAction::shouldUndo() {
212 return true;
213}
214
215const string FragmentationSubgraphDissectionAction::getName() {
216 return NAME;
217}
218/** =========== end of function ====================== */
Note: See TracBrowser for help on using the repository browser.