source: src/Actions/FragmentationAction/FragmentationAction.cpp@ ce0ca4

Last change on this file since ce0ca4 was d8821e, checked in by Frederik Heber <heber@…>, 11 years ago

Merge branch 'Fragmentation_Automation_wo_JobMarket' into stable

Conflicts:

src/Actions/ActionQueue.cpp
src/Actions/FragmentationAction/FragmentationAction.cpp
src/Actions/PotentialAction/FitParticleChargesAction.cpp

  • Property mode set to 100644
File size: 11.6 KB
RevLine 
[bcf653]1/*
2 * Project: MoleCuilder
3 * Description: creates and alters molecular systems
[0aa122]4 * Copyright (C) 2010-2012 University of Bonn. All rights reserved.
[5aaa43]5 * Copyright (C) 2013 Frederik Heber. All rights reserved.
[94d5ac6]6 *
7 *
8 * This file is part of MoleCuilder.
9 *
10 * MoleCuilder is free software: you can redistribute it and/or modify
11 * it under the terms of the GNU General Public License as published by
12 * the Free Software Foundation, either version 2 of the License, or
13 * (at your option) any later version.
14 *
15 * MoleCuilder is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 * GNU General Public License for more details.
19 *
20 * You should have received a copy of the GNU General Public License
21 * along with MoleCuilder. If not, see <http://www.gnu.org/licenses/>.
[bcf653]22 */
23
[97ebf8]24/*
25 * FragmentationAction.cpp
26 *
27 * Created on: May 9, 2010
28 * Author: heber
29 */
30
[bf3817]31// include config.h
32#ifdef HAVE_CONFIG_H
33#include <config.h>
34#endif
35
[ad011c]36#include "CodePatterns/MemDebug.hpp"
[112b09]37
[6f0841]38#include "Atom/atom.hpp"
[c7c615]39#include "CodePatterns/IteratorAdaptors.hpp"
[ad011c]40#include "CodePatterns/Log.hpp"
[79d0b9]41#include "Descriptors/AtomSelectionDescriptor.hpp"
[ca8bea]42#include "Fragmentation/Exporters/ExportGraph_ToFiles.hpp"
[ac9ca4]43#include "Fragmentation/Exporters/ExportGraph_ToJobs.hpp"
[246e13]44#include "Fragmentation/Fragmentation.hpp"
[b4f72c]45#include "Fragmentation/Graph.hpp"
[07a47e]46#include "Fragmentation/HydrogenSaturation_enum.hpp"
[13c5c1]47#include "Fragmentation/Interfragmenter.hpp"
[adb51ab]48#include "Fragmentation/KeySetsContainer.hpp"
49#include "Fragmentation/Summation/Containers/FragmentationResultContainer.hpp"
[0fad93]50#include "Graph/AdjacencyList.hpp"
[79d0b9]51#include "Graph/BondGraph.hpp"
[fe0cb8]52#include "Graph/CyclicStructureAnalysis.hpp"
[49c059]53#include "Graph/DepthFirstSearchAnalysis.hpp"
[9511c7]54#include "Helpers/defs.hpp"
[97ebf8]55#include "molecule.hpp"
56#include "World.hpp"
57
[c7c615]58#include <boost/shared_ptr.hpp>
[3aa8a5]59#include <boost/filesystem.hpp>
[c7c615]60#include <algorithm>
[97ebf8]61#include <iostream>
[2a0eb0]62#include <map>
[97ebf8]63#include <string>
[2a0eb0]64#include <vector>
[97ebf8]65
[1fd675]66#include "Actions/FragmentationAction/FragmentationAction.hpp"
[70d9b9]67
[ce7fdc]68using namespace MoleCuilder;
69
[1fd675]70// and construct the stuff
71#include "FragmentationAction.def"
72#include "Action_impl_pre.hpp"
73/** =========== define the function ====================== */
[b5b01e]74ActionState::ptr FragmentationFragmentationAction::performCall() {
[e4b5de]75 clock_t start,end;
[2a0eb0]76 int ExitFlag = -1;
77 World &world = World::getInstance();
[e4b5de]78
[2a0eb0]79 // inform about used parameters
[13c5c1]80 LOG(0, "STATUS: Fragmenting molecular system with current connection matrix up to "
[bae7bc]81 << params.order.get() << " order. ");
82 if (params.types.get().size() != 0)
83 LOG(0, "STATUS: Fragment files begin with "
84 << params.prefix.get() << " and are stored as: "
85 << params.types.get() << "." << std::endl);
[99b0dc]86
[2a0eb0]87 // check for selected atoms
88 if (world.beginAtomSelection() == world.endAtomSelection()) {
[26b4d62]89 STATUS("There are no atoms selected for fragmentation.");
[2a0eb0]90 return Action::failure;
91 }
92
93 // go through all atoms, note down their molecules and group them
94 typedef std::multimap<molecule *, atom *> clusters_t;
[3aa8a5]95 typedef std::vector<atomId_t> atomids_t;
96 atomids_t atomids;
[2a0eb0]97 clusters_t clusters;
98 for (World::AtomSelectionConstIterator iter = world.beginAtomSelection();
99 iter != world.endAtomSelection(); ++iter) {
100 clusters.insert( std::make_pair(iter->second->getMolecule(), iter->second) );
[3aa8a5]101 atomids.push_back(iter->second->getId());
[2a0eb0]102 }
[c7c615]103 {
104 std::vector<molecule *> molecules;
105 molecules.insert( molecules.end(), MapKeyIterator<clusters_t::const_iterator>(clusters.begin()),
106 MapKeyIterator<clusters_t::const_iterator>(clusters.end()) );
107 molecules.erase( std::unique(molecules.begin(), molecules.end()), molecules.end() );
108 LOG(1, "INFO: There are " << molecules.size() << " molecules to consider.");
109 }
[2a0eb0]110
[9511c7]111 // parse in Adjacency file
[3aa8a5]112 boost::shared_ptr<AdjacencyList> FileChecker;
113 boost::filesystem::path filename(params.prefix.get() + std::string(ADJACENCYFILE));
114 if (boost::filesystem::exists(filename) && boost::filesystem::is_regular_file(filename)) {
115 std::ifstream File;
116 File.open(filename.string().c_str(), ios::out);
117 FileChecker.reset(new AdjacencyList(File));
118 File.close();
119 } else {
120 LOG(1, "INFO: Could not open default adjacency file " << filename.string() << ".");
121 FileChecker.reset(new AdjacencyList);
122 }
[9511c7]123
[79d0b9]124 // make sure bond degree is correct
125 {
126 BondGraph *BG = World::getInstance().getBondGraph();
127 World::AtomComposite Set = World::getInstance().getAllAtoms(AtomsBySelection());
128 BG->CorrectBondDegree(Set);
129 }
130
[bfbd4a]131 // we parse in the keysets from last time if present
132 Graph StoredGraph;
133 StoredGraph.ParseKeySetFile(params.prefix.get());
134
[2a0eb0]135 start = clock();
136 // go through all keys (i.e. all molecules)
137 clusters_t::const_iterator advanceiter;
[b4f72c]138 Graph TotalGraph;
139 int keysetcounter = 0;
[2a0eb0]140 for (clusters_t::const_iterator iter = clusters.begin();
141 iter != clusters.end();
142 iter = advanceiter) {
143 // get iterator to past last atom in this molecule
144 molecule * mol = iter->first;
145 advanceiter = clusters.upper_bound(mol);
146
147 // copy molecule's atoms' ids as parameters to Fragmentation's AtomMask
148 std::vector<atomId_t> mols_atomids;
149 std::transform(iter, advanceiter, std::back_inserter(mols_atomids),
150 boost::bind( &atom::getNr,
151 boost::bind( &clusters_t::value_type::second, _1 ))
152 );
153 LOG(2, "INFO: Fragmenting in molecule " << mol->getName() << " in " << clusters.count(mol)
154 << " atoms, out of " << mol->getAtomCount() << ".");
[9291d04]155 const enum HydrogenTreatment treatment = params.HowtoTreatHydrogen.get() ? ExcludeHydrogen : IncludeHydrogen;
156 Fragmentation Fragmenter(mol, *FileChecker, treatment);
[2a0eb0]157
158 // perform fragmentation
159 LOG(0, std::endl << " ========== Fragmentation of molecule " << mol->getName() << " ========================= ");
160 {
[bfbd4a]161 Graph StoredLocalGraph(StoredGraph.getLocalGraph(mol));
[fe0cb8]162 const int tempFlag = Fragmenter.FragmentMolecule(mols_atomids, params.order.get(), params.prefix.get(), StoredLocalGraph);
[2a0eb0]163 if ((ExitFlag == 2) && (tempFlag != 2))
164 ExitFlag = tempFlag; // if there is one molecule that needs further fragmentation, it overrides others
165 if (ExitFlag == -1)
166 ExitFlag = tempFlag; // if we are the first, we set the standard
[e4b5de]167 }
[569e42]168 if (TotalGraph.empty()) {
169 TotalGraph = Fragmenter.getGraph();
170 keysetcounter = TotalGraph.size();
171 } else
172 TotalGraph.InsertGraph(Fragmenter.getGraph(), keysetcounter);
[ca8bea]173
[97ebf8]174 }
[fe0cb8]175 // add full cycles if desired
176 if (params.DoCyclesFull.get()) {
177 // get the BackEdgeStack from somewhere
178 DepthFirstSearchAnalysis DFS;
179 DFS();
180 std::deque<bond::ptr> BackEdgeStack = DFS.getBackEdgeStack();
181 // then we analyse the cycles and get them
182 CyclicStructureAnalysis CycleAnalysis(params.HowtoTreatHydrogen.get() ? ExcludeHydrogen : IncludeHydrogen);
183 CycleAnalysis(&BackEdgeStack);
184 CyclicStructureAnalysis::cycles_t cycles = CycleAnalysis.getAllCycles();
[adb51ab]185 // sort them according to KeySet::operator<()
186 std::sort(cycles.begin(), cycles.end());
187 // store all found cycles to file
188 {
189 boost::filesystem::path filename(params.prefix.get() + std::string(CYCLEKEYSETFILE));
190 std::ofstream File;
191 LOG(1, "INFO: Storing cycle keysets to " << filename.string() << ".");
192 File.open(filename.string().c_str(), ios::out);
193 for (CyclicStructureAnalysis::cycles_t::const_iterator iter = cycles.begin();
194 iter != cycles.end(); ++iter) {
195 for (CyclicStructureAnalysis::cycle_t::const_iterator cycleiter = (*iter).begin();
196 cycleiter != (*iter).end(); ++cycleiter) {
197 File << *cycleiter << "\t";
198 }
199 File << "\n";
200 }
201 File.close();
202 }
203 // ... and to result container
204 {
205 KeySetsContainer cyclekeys;
206 for (CyclicStructureAnalysis::cycles_t::const_iterator iter = cycles.begin();
207 iter != cycles.end(); ++iter) {
208 const CyclicStructureAnalysis::cycle_t &cycle = *iter;
209 const size_t order = cycle.size();
210 KeySetsContainer::IntVector temp_cycle(cycle.begin(), cycle.end());
211 cyclekeys.insert(temp_cycle, order);
212 }
213 FragmentationResultContainer::getInstance().addCycles(cyclekeys);
214 }
[fe0cb8]215 // Create graph and insert into TotalGraph
[adb51ab]216 LOG(0, "STATUS: Adding " << cycles.size() << " cycles.");
[fe0cb8]217 {
218 Graph CycleGraph;
219 for (CyclicStructureAnalysis::cycles_t::const_iterator iter = cycles.begin();
220 iter != cycles.end(); ++iter) {
221 const CyclicStructureAnalysis::cycle_t &currentcycle = *iter;
222 LOG(2, "INFO: Inserting cycle " << currentcycle << ".");
223#ifndef NDEBUG
224 std::pair< Graph::iterator, bool > inserter =
225#endif
226 CycleGraph.insert( std::make_pair(currentcycle, NumberValuePair(1,1.)) );
227 ASSERT( inserter.second,
228 "FragmentationFragmentationAction::performCall() - keyset "
229 +toString(currentcycle)+" inserted twice into CycleGraph.");
230 }
231 TotalGraph.InsertGraph(CycleGraph, keysetcounter);
232 }
233 }
234
[569e42]235 LOG(0, "STATUS: There are " << TotalGraph.size() << " fragments.");
[3aa8a5]236
[321470]237 {
238 // remove OrderAtSite file
239 std::string line;
240 std::ofstream file;
241 line = params.prefix.get() + ORDERATSITEFILE;
242 file.open(line.c_str(), std::ofstream::out | std::ofstream::trunc);
243 file << "";
244 file.close();
245 }
246
[13c5c1]247 // now add interfragments
248 if (params.InterOrder.get() != 0) {
249 LOG(0, "STATUS: Putting fragments together up to order "
250 << params.InterOrder.get() << " and distance of "
251 << params.distance.get() << ".");
252 Interfragmenter fragmenter(TotalGraph);
253 const enum HydrogenTreatment treatment = params.HowtoTreatHydrogen.get() ? ExcludeHydrogen : IncludeHydrogen;
254 fragmenter(params.InterOrder.get(), params.distance.get(), treatment);
255 LOG(0, "STATUS: There are now " << TotalGraph.size() << " fragments after interfragmenting.");
256 }
257
[bfbd4a]258 // store keysets to file
259 {
260 TotalGraph.StoreKeySetFile(params.prefix.get());
261 }
262
[3aa8a5]263 {
[9291d04]264 const enum HydrogenSaturation saturation = params.DoSaturation.get() ? DoSaturate : DontSaturate;
[276ac6]265 const enum HydrogenTreatment treatment = params.HowtoTreatHydrogen.get() ? ExcludeHydrogen : IncludeHydrogen;
[ac9ca4]266 if (params.types.get().size() != 0) {
267 // store molecule's fragment to file
268 ExportGraph_ToFiles exporter(TotalGraph, treatment, saturation);
269 exporter.setPrefix(params.prefix.get());
270 exporter.setOutputTypes(params.types.get());
271 exporter();
272 } else {
273 // store molecule's fragment in FragmentJobQueue
274 ExportGraph_ToJobs exporter(TotalGraph, treatment, saturation);
275 exporter.setLevel(params.level.get());
276 exporter();
277 }
[3aa8a5]278 }
279
280 // store Adjacency to file
281 {
282 std::string filename = params.prefix.get() + ADJACENCYFILE;
283 std::ofstream AdjacencyFile;
284 AdjacencyFile.open(filename.c_str(), ios::out);
285 AdjacencyList adjacency(atomids);
286 adjacency.StoreToFile(AdjacencyFile);
287 AdjacencyFile.close();
288 }
289
[2a0eb0]290 World::getInstance().setExitFlag(ExitFlag);
291 end = clock();
292 LOG(0, "STATUS: Clocks for this operation: " << (end-start) << ", time: " << ((double)(end-start)/CLOCKS_PER_SEC) << "s.");
293
[70d9b9]294 return Action::success;
[97ebf8]295}
296
[b5b01e]297ActionState::ptr FragmentationFragmentationAction::performUndo(ActionState::ptr _state) {
[70d9b9]298 return Action::success;
[97ebf8]299}
300
[b5b01e]301ActionState::ptr FragmentationFragmentationAction::performRedo(ActionState::ptr _state){
[70d9b9]302 return Action::success;
[97ebf8]303}
304
305bool FragmentationFragmentationAction::canUndo() {
[70d9b9]306 return true;
[97ebf8]307}
308
309bool FragmentationFragmentationAction::shouldUndo() {
[70d9b9]310 return true;
[97ebf8]311}
[1fd675]312/** =========== end of function ====================== */
Note: See TracBrowser for help on using the repository browser.