| 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 |  * FillWithMoleculeAction.cpp
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Created on: May 10, 2010
 | 
|---|
| 12 |  *      Author: heber
 | 
|---|
| 13 |  */
 | 
|---|
| 14 | 
 | 
|---|
| 15 | // include config.h
 | 
|---|
| 16 | #ifdef HAVE_CONFIG_H
 | 
|---|
| 17 | #include <config.h>
 | 
|---|
| 18 | #endif
 | 
|---|
| 19 | 
 | 
|---|
| 20 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #include "atom.hpp"
 | 
|---|
| 23 | #include "bondgraph.hpp"
 | 
|---|
| 24 | #include "boundary.hpp"
 | 
|---|
| 25 | #include "CodePatterns/Verbose.hpp"
 | 
|---|
| 26 | #include "config.hpp"
 | 
|---|
| 27 | #include "Descriptors/MoleculeIdDescriptor.hpp"
 | 
|---|
| 28 | #include "Descriptors/MoleculeOrderDescriptor.hpp"
 | 
|---|
| 29 | #include "molecule.hpp"
 | 
|---|
| 30 | #include "Parser/FormatParserStorage.hpp"
 | 
|---|
| 31 | #include "World.hpp"
 | 
|---|
| 32 | 
 | 
|---|
| 33 | 
 | 
|---|
| 34 | #include <iostream>
 | 
|---|
| 35 | #include <string>
 | 
|---|
| 36 | 
 | 
|---|
| 37 | using namespace std;
 | 
|---|
| 38 | 
 | 
|---|
| 39 | #include "Actions/MoleculeAction/FillWithMoleculeAction.hpp"
 | 
|---|
| 40 | 
 | 
|---|
| 41 | // and construct the stuff
 | 
|---|
| 42 | #include "FillWithMoleculeAction.def"
 | 
|---|
| 43 | #include "Action_impl_pre.hpp"
 | 
|---|
| 44 | /** =========== define the function ====================== */
 | 
|---|
| 45 | Action::state_ptr MoleculeFillWithMoleculeAction::performCall() {
 | 
|---|
| 46 | 
 | 
|---|
| 47 |   // obtain information
 | 
|---|
| 48 |   getParametersfromValueStorage();
 | 
|---|
| 49 | 
 | 
|---|
| 50 |   DoLog(1) && (Log() << Verbose(1) << "Filling Box with water molecules, "
 | 
|---|
| 51 |       << " minimum distance to molecules" << params.boundary
 | 
|---|
| 52 |       << ", random atom displacement " << params.RandAtomDisplacement
 | 
|---|
| 53 |       << ", random molecule displacement " << params.RandMoleculeDisplacement
 | 
|---|
| 54 |       << ", distances between fillers (" << params.distances[0] << "," << params.distances[1] << "," << params.distances[2]
 | 
|---|
| 55 |       << "), MinDistance " << params.MaxDistance
 | 
|---|
| 56 |       << ", DoRotate " << params.DoRotate << "." << endl);
 | 
|---|
| 57 |   // construct water molecule
 | 
|---|
| 58 |   std::vector<molecule *> presentmolecules = World::getInstance().getAllMolecules();
 | 
|---|
| 59 | //  DoLog(0) && (Log() << Verbose(0) << presentmolecules.size() << " molecules initially are present." << std::endl);
 | 
|---|
| 60 |   std::string FilenameSuffix = params.fillername.string().substr(params.fillername.string().find_last_of('.')+1, params.fillername.string().length());
 | 
|---|
| 61 |   ifstream input;
 | 
|---|
| 62 |   input.open(params.fillername.string().c_str());
 | 
|---|
| 63 |   ParserTypes type = FormatParserStorage::getInstance().getTypeFromSuffix(FilenameSuffix);
 | 
|---|
| 64 |   FormatParser &parser = FormatParserStorage::getInstance().get(type);
 | 
|---|
| 65 |   parser.load(&input);
 | 
|---|
| 66 | 
 | 
|---|
| 67 |   // search the filler molecule that has been just parsed
 | 
|---|
| 68 |   molecule *filler = World::getInstance().getMolecule(MoleculeByOrder(-1)); // get last molecule
 | 
|---|
| 69 |   filler->SetNameFromFilename(params.fillername.string().c_str());
 | 
|---|
| 70 |   World::getInstance().getConfig()->BG->ConstructBondGraph(filler);
 | 
|---|
| 71 |   // TODO: Remove the erasure of molecule when saving does not depend on them anymore.
 | 
|---|
| 72 |   World::getInstance().getMolecules()->erase(filler); // remove it, Parser adds it automatically
 | 
|---|
| 73 | 
 | 
|---|
| 74 |   // call routine
 | 
|---|
| 75 |   double distance[NDIM];
 | 
|---|
| 76 |   for (int i=0;i<NDIM;i++)
 | 
|---|
| 77 |     distance[i] = params.distances[i];
 | 
|---|
| 78 |   FillBoxWithMolecule(
 | 
|---|
| 79 |       World::getInstance().getMolecules(),
 | 
|---|
| 80 |       filler, *(World::getInstance().getConfig()),
 | 
|---|
| 81 |       params.MaxDistance,
 | 
|---|
| 82 |       distance,
 | 
|---|
| 83 |       params.boundary,
 | 
|---|
| 84 |       params.RandAtomDisplacement,
 | 
|---|
| 85 |       params.RandMoleculeDisplacement,
 | 
|---|
| 86 |       params.DoRotate);
 | 
|---|
| 87 |   for (molecule::iterator iter = filler->begin(); !filler->empty(); iter = filler->begin()) {
 | 
|---|
| 88 |     atom *Walker = *iter;
 | 
|---|
| 89 |     filler->erase(iter);
 | 
|---|
| 90 |     World::getInstance().destroyAtom(Walker);
 | 
|---|
| 91 |   }
 | 
|---|
| 92 |   World::getInstance().destroyMolecule(filler);
 | 
|---|
| 93 | 
 | 
|---|
| 94 |   // generate list of newly created molecules
 | 
|---|
| 95 |   // (we can in general remove more quickly from a list than a vector)
 | 
|---|
| 96 |   std::vector<molecule *> fillermolecules = World::getInstance().getAllMolecules();
 | 
|---|
| 97 | //  DoLog(0) && (Log() << Verbose(0) << fillermolecules.size() << " molecules are present." << std::endl);
 | 
|---|
| 98 |   std::list<molecule *> fillermolecules_list;
 | 
|---|
| 99 |   std::copy( fillermolecules.begin(),  fillermolecules.end(), std::back_inserter( fillermolecules_list ));
 | 
|---|
| 100 | //  DoLog(0) && (Log() << Verbose(0) << fillermolecules_list.size() << " molecules have been copied." << std::endl);
 | 
|---|
| 101 |   for (std::vector<molecule *>::const_iterator iter = presentmolecules.begin();
 | 
|---|
| 102 |       iter != presentmolecules.end();
 | 
|---|
| 103 |       ++iter) {
 | 
|---|
| 104 |     fillermolecules_list.remove(*iter);
 | 
|---|
| 105 |   }
 | 
|---|
| 106 | //  DoLog(0) && (Log() << Verbose(0) << fillermolecules_list.size() << " molecules left after removal." << std::endl);
 | 
|---|
| 107 |   fillermolecules.clear();
 | 
|---|
| 108 |   std::copy(fillermolecules_list.begin(), fillermolecules_list.end(), std::back_inserter( fillermolecules ));
 | 
|---|
| 109 | 
 | 
|---|
| 110 | //  DoLog(0) && (Log() << Verbose(0) << fillermolecules.size() << " molecules have been inserted." << std::endl);
 | 
|---|
| 111 | 
 | 
|---|
| 112 |   return Action::state_ptr(new MoleculeFillWithMoleculeState(fillermolecules,params));
 | 
|---|
| 113 | }
 | 
|---|
| 114 | 
 | 
|---|
| 115 | Action::state_ptr MoleculeFillWithMoleculeAction::performUndo(Action::state_ptr _state) {
 | 
|---|
| 116 |   MoleculeFillWithMoleculeState *state = assert_cast<MoleculeFillWithMoleculeState*>(_state.get());
 | 
|---|
| 117 | 
 | 
|---|
| 118 |   MoleculeListClass *MolList = World::getInstance().getMolecules();
 | 
|---|
| 119 | 
 | 
|---|
| 120 |   BOOST_FOREACH(molecule *_mol, state->fillermolecules) {
 | 
|---|
| 121 |     MolList->erase(_mol);
 | 
|---|
| 122 |     if ((_mol != NULL) && (!(World::getInstance().getAllMolecules(MoleculeById(_mol->getId()))).empty())) {
 | 
|---|
| 123 |       for(molecule::iterator iter = _mol->begin();
 | 
|---|
| 124 |           !_mol->empty();
 | 
|---|
| 125 |           iter = _mol->begin()) {
 | 
|---|
| 126 |         atom *Walker = *iter;
 | 
|---|
| 127 |         _mol->erase(iter);
 | 
|---|
| 128 |         World::getInstance().destroyAtom(Walker);
 | 
|---|
| 129 |       }
 | 
|---|
| 130 |       World::getInstance().destroyMolecule(_mol);
 | 
|---|
| 131 |     }
 | 
|---|
| 132 |   }
 | 
|---|
| 133 | 
 | 
|---|
| 134 |   // as molecules and atoms from state are removed, we have to create a new one
 | 
|---|
| 135 |   std::vector<molecule *> fillermolecules;
 | 
|---|
| 136 |   return Action::state_ptr(new MoleculeFillWithMoleculeState(fillermolecules,state->params));
 | 
|---|
| 137 | }
 | 
|---|
| 138 | 
 | 
|---|
| 139 | Action::state_ptr MoleculeFillWithMoleculeAction::performRedo(Action::state_ptr _state){
 | 
|---|
| 140 |   //MoleculeFillWithMoleculeState *state = assert_cast<MoleculeFillWithMoleculeState*>(_state.get());
 | 
|---|
| 141 | 
 | 
|---|
| 142 |   return Action::failure;
 | 
|---|
| 143 |   //return Action::state_ptr(_state);
 | 
|---|
| 144 | }
 | 
|---|
| 145 | 
 | 
|---|
| 146 | bool MoleculeFillWithMoleculeAction::canUndo() {
 | 
|---|
| 147 |   return true;
 | 
|---|
| 148 | }
 | 
|---|
| 149 | 
 | 
|---|
| 150 | bool MoleculeFillWithMoleculeAction::shouldUndo() {
 | 
|---|
| 151 |   return true;
 | 
|---|
| 152 | }
 | 
|---|
| 153 | /** =========== end of function ====================== */
 | 
|---|