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

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 faa1c9 was faa1c9, checked in by Frederik Heber <heber@…>, 14 years ago

BUGFIX: SubgraphDissectionAction() does not copy system anymore, has Undo/Redo.

  • This fixes ticket #88.
  • Added Undo/Redo, needed World::changeMoleculeId() for that
  • BUGFIX: performCall() let DepthFirstSearchAnalysis() copy atoms and copied the molecules once more.
  • BUGFIX: performCall() did not reset fathers of copied atoms to point to themselves (BUG with redo arose).
  • TEST: Added Undo/Redo part to Graph/2 (subgraph dissection).
  • TESTFIX: Analysis/4 uses --unselect-molecule-by-formula and id of this molecule is 1 (as system is not copied anymore).
  • Property mode set to 100644
File size: 7.2 KB
Line 
1/*
2 * SubgraphDissectionAction.cpp
3 *
4 * Created on: May 9, 2010
5 * Author: heber
6 */
7
8// include config.h
9#ifdef HAVE_CONFIG_H
10#include <config.h>
11#endif
12
13#include "Helpers/MemDebug.hpp"
14
15#include "Actions/FragmentationAction/SubgraphDissectionAction.hpp"
16#include "Actions/ActionRegistry.hpp"
17#include "Descriptors/AtomIdDescriptor.hpp"
18#include "Descriptors/MoleculeDescriptor.hpp"
19
20#include "atom.hpp"
21#include "bond.hpp"
22#include "bondgraph.hpp"
23#include "config.hpp"
24#include "Helpers/Log.hpp"
25#include "Helpers/Verbose.hpp"
26#include "molecule.hpp"
27#include "stackclass.hpp"
28#include "World.hpp"
29
30#include <iostream>
31#include <string>
32
33using namespace std;
34
35#include "UIElements/UIFactory.hpp"
36#include "UIElements/Dialog.hpp"
37#include "Actions/ValueStorage.hpp"
38
39// memento to remember the state when undoing
40
41typedef std::map< moleculeId_t, std::vector<atomId_t> > MolAtomList;
42
43class FragmentationSubgraphDissectionState : public ActionState {
44public:
45 FragmentationSubgraphDissectionState(const MolAtomList &_moleculelist) :
46 moleculelist(_moleculelist)
47 {}
48 MolAtomList moleculelist;
49};
50
51const char FragmentationSubgraphDissectionAction::NAME[] = "subgraph-dissect";
52
53FragmentationSubgraphDissectionAction::FragmentationSubgraphDissectionAction() :
54 Action(NAME)
55{}
56
57FragmentationSubgraphDissectionAction::~FragmentationSubgraphDissectionAction()
58{}
59
60/** Dissects given \a *mol into connected subgraphs and inserts them as new molecules but with old atoms into \a this.
61 */
62void FragmentationSubgraphDissection() {
63 ActionRegistry::getInstance().getActionByName(FragmentationSubgraphDissectionAction::NAME)->call(Action::NonInteractive);
64};
65
66Dialog* FragmentationSubgraphDissectionAction::fillDialog(Dialog *dialog) {
67 ASSERT(dialog,"No Dialog given when filling action dialog");
68
69 dialog->queryEmpty(NAME, MapOfActions::getInstance().getDescription(NAME));
70
71 return dialog;
72}
73
74
75Action::state_ptr FragmentationSubgraphDissectionAction::performCall() {
76 DoLog(1) && (Log() << Verbose(1) << "Dissecting molecular system into a set of disconnected subgraphs ... " << endl);
77
78 // first create undo state
79 MolAtomList moleculelist;
80 vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
81 for (vector<molecule *>::const_iterator moliter = allmolecules.begin(); moliter != allmolecules.end(); ++moliter) {
82 std::vector<atomId_t> atomlist;
83 atomlist.resize((*moliter)->size());
84 for (molecule::const_iterator atomiter = (*moliter)->begin(); atomiter != (*moliter)->end(); ++atomiter) {
85 atomlist.push_back((*atomiter)->getId());
86 }
87 moleculelist.insert( std::pair< moleculeId_t, std::vector<atomId_t> > ((*moliter)->getId(), atomlist));
88 }
89 FragmentationSubgraphDissectionState *UndoState = new FragmentationSubgraphDissectionState(moleculelist);
90
91 MoleculeListClass *molecules = World::getInstance().getMolecules();
92 config * const configuration = World::getInstance().getConfig();
93
94 // 0a. remove all present molecules
95 for (vector<molecule *>::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) {
96 molecules->erase(*MolRunner);
97 World::getInstance().destroyMolecule(*MolRunner);
98 }
99
100 // 0b. remove all bonds and construct a molecule with all atoms
101 molecule *mol = World::getInstance().createMolecule();
102 {
103 vector <atom *> allatoms = World::getInstance().getAllAtoms();
104 for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
105 for(BondList::iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); !(*AtomRunner)->ListOfBonds.empty(); BondRunner = (*AtomRunner)->ListOfBonds.begin())
106 delete(*BondRunner);
107 mol->AddAtom(*AtomRunner);
108 }
109 }
110
111 // 1. create the bond structure of the single molecule
112 if (configuration->BG != NULL) {
113 if (!configuration->BG->ConstructBondGraph(mol)) {
114 World::getInstance().destroyMolecule(mol);
115 DoeLog(1) && (eLog()<< Verbose(1) << "There are no bonds." << endl);
116 return Action::failure;
117 }
118 } else {
119 DoeLog(1) && (eLog()<< Verbose(1) << "There is no BondGraph class present to create bonds." << endl);
120 return Action::failure;
121 }
122
123 // 2. scan for connected subgraphs
124 MoleculeLeafClass *Subgraphs = NULL; // list of subgraphs from DFS analysis
125 class StackClass<bond *> *BackEdgeStack = NULL;
126 Subgraphs = mol->DepthFirstSearchAnalysis(BackEdgeStack);
127 delete(BackEdgeStack);
128 if ((Subgraphs == NULL) || (Subgraphs->next == NULL)) {
129 //World::getInstance().destroyMolecule(mol);
130 DoeLog(1) && (eLog()<< Verbose(1) << "There are no atoms." << endl);
131 return Action::failure;
132 }
133 int FragmentCounter = Subgraphs->next->Count();
134
135 // TODO: When DepthFirstSearchAnalysis does not use AddCopyAtom() anymore, we don't need to delete all original atoms
136 {
137 // 3a. destroy the original molecule
138 for (molecule::iterator AtomRunner = mol->begin(); !mol->empty(); AtomRunner = mol->begin())
139 World::getInstance().destroyAtom(*AtomRunner);
140 World::getInstance().destroyMolecule(mol);
141 // 3b. correct fathers (AddCopyAtom sets father to original atom, which has been destroyed).
142 vector <atom *> allatoms = World::getInstance().getAllAtoms();
143 for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
144 (*AtomRunner)->father = *AtomRunner;
145 }
146 }
147
148 // 4. free Leafs
149 MoleculeLeafClass *MolecularWalker = Subgraphs;
150 while (MolecularWalker->next != NULL) {
151 MolecularWalker->Leaf = NULL;
152 MolecularWalker = MolecularWalker->next;
153 delete(MolecularWalker->previous);
154 }
155 MolecularWalker->Leaf = NULL;
156 delete(MolecularWalker);
157 DoLog(1) && (Log() << Verbose(1) << "I scanned " << FragmentCounter << " molecules." << endl);
158
159 return Action::state_ptr(UndoState);
160}
161
162Action::state_ptr FragmentationSubgraphDissectionAction::performUndo(Action::state_ptr _state) {
163 FragmentationSubgraphDissectionState *state = assert_cast<FragmentationSubgraphDissectionState*>(_state.get());
164
165 {
166 // remove all present molecules
167 MoleculeListClass *molecules = World::getInstance().getMolecules();
168 vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
169 for (vector<molecule *>::iterator MolRunner = allmolecules.begin(); MolRunner != allmolecules.end(); ++MolRunner) {
170 molecules->erase(*MolRunner);
171 World::getInstance().destroyMolecule(*MolRunner);
172 }
173 }
174
175 {
176 // construct the old state
177 molecule *mol = NULL;
178 for (MolAtomList::const_iterator iter = state->moleculelist.begin(); iter != state->moleculelist.end(); ++iter) {
179 mol = World::getInstance().createMolecule();
180 if (mol->getId() != (*iter).first)
181 World::getInstance().changeMoleculeId(mol->getId(), (*iter).first);
182 for (std::vector<atomId_t>::const_iterator atomiter = (*iter).second.begin(); atomiter != (*iter).second.end(); ++atomiter)
183 mol->AddAtom(World::getInstance().getAtom(AtomById(*atomiter)));
184 }
185 }
186
187 return Action::state_ptr(_state);
188}
189
190Action::state_ptr FragmentationSubgraphDissectionAction::performRedo(Action::state_ptr _state){
191 return performCall();
192}
193
194bool FragmentationSubgraphDissectionAction::canUndo() {
195 return true;
196}
197
198bool FragmentationSubgraphDissectionAction::shouldUndo() {
199 return true;
200}
201
202const string FragmentationSubgraphDissectionAction::getName() {
203 return NAME;
204}
Note: See TracBrowser for help on using the repository browser.