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

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

Added ifdef HAVE_CONFIG and config.h include to each and every cpp file.

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