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