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

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

Merged MoleculeListClass::DissectMoleculeIntoConnectedSubgraphs() into FragmenationSubgraphDissectionAction.

  • also rewrote the function a bit:
    • no more copying of atoms
    • we just take the molecule out of the MoleculeLeafs
    • molecule that contains all atoms is destroyed including all original atoms (they are copied by DFS).
  • Property mode set to 100644
File size: 7.7 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/MoleculeDescriptor.hpp"
18
19#include "atom.hpp"
20#include "bond.hpp"
21#include "bondgraph.hpp"
22#include "config.hpp"
23#include "Helpers/Log.hpp"
24#include "molecule.hpp"
25#include "stackclass.hpp"
26#include "verbose.hpp"
27#include "World.hpp"
28
29#include <iostream>
30#include <string>
31
32using namespace std;
33
34#include "UIElements/UIFactory.hpp"
35#include "UIElements/Dialog.hpp"
36#include "Actions/ValueStorage.hpp"
37
38const char FragmentationSubgraphDissectionAction::NAME[] = "subgraph-dissect";
39
40FragmentationSubgraphDissectionAction::FragmentationSubgraphDissectionAction() :
41 Action(NAME)
42{}
43
44FragmentationSubgraphDissectionAction::~FragmentationSubgraphDissectionAction()
45{}
46
47/** Dissects given \a *mol into connected subgraphs and inserts them as new molecules but with old atoms into \a this.
48 */
49void FragmentationSubgraphDissection() {
50 ActionRegistry::getInstance().getActionByName(FragmentationSubgraphDissectionAction::NAME)->call(Action::NonInteractive);
51};
52
53Dialog* FragmentationSubgraphDissectionAction::fillDialog(Dialog *dialog) {
54 ASSERT(dialog,"No Dialog given when filling action dialog");
55
56 dialog->queryEmpty(NAME, MapOfActions::getInstance().getDescription(NAME));
57
58 return dialog;
59}
60
61
62Action::state_ptr FragmentationSubgraphDissectionAction::performCall() {
63 DoLog(1) && (Log() << Verbose(1) << "Dissecting molecular system into a set of disconnected subgraphs ... " << endl);
64
65 MoleculeListClass *molecules = World::getInstance().getMolecules();
66 config * const configuration = World::getInstance().getConfig();
67
68 // 0a. remove all present molecules
69 vector<molecule *> allmolecules = World::getInstance().getAllMolecules();
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 vector <atom *> allatoms = World::getInstance().getAllAtoms();
78 for(vector<atom *>::iterator AtomRunner = allatoms.begin(); AtomRunner != allatoms.end(); ++AtomRunner) {
79 for(BondList::iterator BondRunner = (*AtomRunner)->ListOfBonds.begin(); !(*AtomRunner)->ListOfBonds.empty(); BondRunner = (*AtomRunner)->ListOfBonds.begin())
80 delete(*BondRunner);
81 mol->AddAtom(*AtomRunner);
82 }
83
84 // 1. create the bond structure of the single molecule
85 if (configuration->BG != NULL) {
86 if (!configuration->BG->ConstructBondGraph(mol)) {
87 World::getInstance().destroyMolecule(mol);
88 DoeLog(1) && (eLog()<< Verbose(1) << "There are no bonds." << endl);
89 return Action::failure;
90 }
91 } else {
92 DoeLog(1) && (eLog()<< Verbose(1) << "There is no BondGraph class present to create bonds." << endl);
93 return Action::failure;
94 }
95
96 // 2. scan for connected subgraphs
97 MoleculeLeafClass *Subgraphs = NULL; // list of subgraphs from DFS analysis
98 class StackClass<bond *> *BackEdgeStack = NULL;
99 Subgraphs = mol->DepthFirstSearchAnalysis(BackEdgeStack);
100 delete(BackEdgeStack);
101 if ((Subgraphs == NULL) || (Subgraphs->next == NULL)) {
102 World::getInstance().destroyMolecule(mol);
103 DoeLog(1) && (eLog()<< Verbose(1) << "There are no atoms." << endl);
104 return Action::failure;
105 }
106<<<<<<< HEAD
107
108 // 3. dissect (the following construct is needed to have the atoms not in the order of the DFS, but in
109 // the original one as parsed in)
110 // TODO: Optimize this, when molecules just contain pointer list of global atoms!
111
112 // 4a. create array of molecules to fill
113 const int MolCount = Subgraphs->next->Count();
114 char number[MAXSTRINGSIZE];
115 molecule **moleculelist = new molecule *[MolCount];
116 MoleculeLeafClass *MolecularWalker = Subgraphs;
117 for (int i=0;i<MolCount;i++) {
118 MolecularWalker = MolecularWalker->next;
119 moleculelist[i] = World::getInstance().createMolecule();
120 moleculelist[i]->ActiveFlag = true;
121 strncpy(moleculelist[i]->name, mol->name, MAXSTRINGSIZE);
122 if (MolCount > 1) {
123 sprintf(number, "-%d", i+1);
124 strncat(moleculelist[i]->name, number, MAXSTRINGSIZE - strlen(mol->name) - 1);
125 }
126 DoLog(1) && (Log() << Verbose(1) << "MolName is " << moleculelist[i]->name << ", id is " << moleculelist[i]->getId() << endl);
127 for (molecule::iterator iter = MolecularWalker->Leaf->begin(); iter != MolecularWalker->Leaf->end(); ++iter) {
128 DoLog(1) && (Log() << Verbose(1) << **iter << endl);
129 }
130 molecules->insert(moleculelist[i]);
131 }
132
133 // 4b. create and fill map of which atom is associated to which connected molecule (note, counting starts at 1)
134 int FragmentCounter = 0;
135 map<int, atom *> AtomToFragmentMap;
136 MolecularWalker = Subgraphs;
137 while (MolecularWalker->next != NULL) {
138 MolecularWalker = MolecularWalker->next;
139 for (molecule::iterator iter = MolecularWalker->Leaf->begin(); !MolecularWalker->Leaf->empty(); iter = MolecularWalker->Leaf->begin()) {
140 atom * Walker = *iter;
141 DoLog(1) && (Log() << Verbose(1) << "Re-linking " << Walker << "..." << endl);
142 MolecularWalker->Leaf->erase(iter);
143 moleculelist[FragmentCounter]->AddAtom(Walker); // counting starts at 1
144 }
145 FragmentCounter++;
146 }
147 // TODO: When DepthFirstSearchAnalysis does not use AddCopyAtom() anymore, we don't need to delete all original atoms.
148 // 4d. destroy the original molecule
149=======
150 int FragmentCounter = Subgraphs->Count();
151
152 // TODO: When DepthFirstSearchAnalysis does not use AddCopyAtom() anymore, we don't need to delete all original atoms
153 // 3. destroy the original molecule
154>>>>>>> Merged MoleculeListClass::DissectMoleculeIntoConnectedSubgraphs() into FragmenationSubgraphDissectionAction.
155 for (molecule::iterator AtomRunner = mol->begin(); !mol->empty(); AtomRunner = mol->begin())
156 World::getInstance().destroyAtom(*AtomRunner);
157 World::getInstance().destroyMolecule(mol);
158
159<<<<<<< HEAD
160 // 4d. we don't need to redo bonds, as they are connected subgraphs and still maintain their ListOfBonds, but we have to remove them from first..last list
161 // TODO: check whether this is really not needed anymore
162 // 4e. free Leafs
163 MolecularWalker = Subgraphs;
164=======
165 // 4. free Leafs
166 MoleculeLeafClass *MolecularWalker = Subgraphs;
167>>>>>>> Merged MoleculeListClass::DissectMoleculeIntoConnectedSubgraphs() into FragmenationSubgraphDissectionAction.
168 while (MolecularWalker->next != NULL) {
169 MolecularWalker = MolecularWalker->next;
170 delete(MolecularWalker->previous);
171 }
172 delete(MolecularWalker);
173<<<<<<< HEAD
174 delete[](moleculelist);
175=======
176>>>>>>> Merged MoleculeListClass::DissectMoleculeIntoConnectedSubgraphs() into FragmenationSubgraphDissectionAction.
177 DoLog(1) && (Log() << Verbose(1) << "I scanned " << FragmentCounter << " molecules." << endl);
178
179 return Action::success;
180}
181
182Action::state_ptr FragmentationSubgraphDissectionAction::performUndo(Action::state_ptr _state) {
183// ParserLoadXyzState *state = assert_cast<ParserLoadXyzState*>(_state.get());
184
185 return Action::failure;
186// string newName = state->mol->getName();
187// state->mol->setName(state->lastName);
188//
189// return Action::state_ptr(new ParserLoadXyzState(state->mol,newName));
190}
191
192Action::state_ptr FragmentationSubgraphDissectionAction::performRedo(Action::state_ptr _state){
193 return Action::failure;
194}
195
196bool FragmentationSubgraphDissectionAction::canUndo() {
197 return false;
198}
199
200bool FragmentationSubgraphDissectionAction::shouldUndo() {
201 return false;
202}
203
204const string FragmentationSubgraphDissectionAction::getName() {
205 return NAME;
206}
Note: See TracBrowser for help on using the repository browser.