/* * Project: MoleCuilder * Description: creates and alters molecular systems * Copyright (C) 2010 University of Bonn. All rights reserved. * Please see the LICENSE file or "Copyright notice" in builder.cpp for details. */ /* * FillWithMoleculeAction.cpp * * Created on: May 10, 2010 * Author: heber */ // include config.h #ifdef HAVE_CONFIG_H #include #endif #include "CodePatterns/MemDebug.hpp" #include "atom.hpp" #include "Graph/BondGraph.hpp" #include "boundary.hpp" #include "CodePatterns/Verbose.hpp" #include "config.hpp" #include "Descriptors/MoleculeIdDescriptor.hpp" #include "Descriptors/MoleculeOrderDescriptor.hpp" #include "molecule.hpp" #include "Parser/FormatParserStorage.hpp" #include "World.hpp" #include #include #include "Actions/MoleculeAction/FillWithMoleculeAction.hpp" using namespace MoleCuilder; // and construct the stuff #include "FillWithMoleculeAction.def" #include "Action_impl_pre.hpp" /** =========== define the function ====================== */ Action::state_ptr MoleculeFillWithMoleculeAction::performCall() { // obtain information getParametersfromValueStorage(); DoLog(1) && (Log() << Verbose(1) << "Filling Box with water molecules, " << " minimum distance to molecules" << params.boundary << ", random atom displacement " << params.RandAtomDisplacement << ", random molecule displacement " << params.RandMoleculeDisplacement << ", distances between fillers (" << params.distances[0] << "," << params.distances[1] << "," << params.distances[2] << "), MinDistance " << params.MaxDistance << ", DoRotate " << params.DoRotate << "." << endl); // construct water molecule std::vector presentmolecules = World::getInstance().getAllMolecules(); // DoLog(0) && (Log() << Verbose(0) << presentmolecules.size() << " molecules initially are present." << std::endl); std::string FilenameSuffix = params.fillername.string().substr(params.fillername.string().find_last_of('.')+1, params.fillername.string().length()); ifstream input; input.open(params.fillername.string().c_str()); ParserTypes type = FormatParserStorage::getInstance().getTypeFromSuffix(FilenameSuffix); FormatParser &parser = FormatParserStorage::getInstance().get(type); parser.load(&input); // search the filler molecule that has been just parsed molecule *filler = World::getInstance().getMolecule(MoleculeByOrder(-1)); // get last molecule filler->SetNameFromFilename(params.fillername.string().c_str()); molecule *Filling = NULL; // atom *first = NULL, *second = NULL, *third = NULL; // first = World::getInstance().createAtom(); // first->type = World::getInstance().getPeriode()->FindElement(1); // first->x = Vector(0.441, -0.143, 0.); // filler->AddAtom(first); // second = World::getInstance().createAtom(); // second->type = World::getInstance().getPeriode()->FindElement(1); // second->x = Vector(-0.464, 1.137, 0.0); // filler->AddAtom(second); // third = World::getInstance().createAtom(); // third->type = World::getInstance().getPeriode()->FindElement(8); // third->x = Vector(-0.464, 0.177, 0.); // filler->AddAtom(third); // filler->AddBond(first, third, 1); // filler->AddBond(second, third, 1); molecule::atomVector Set = filler->getAtomSet(); World::getInstance().getBondGraph()->CreateAdjacency(Set); // filler->SetNameFromFilename("water"); // TODO: Remove the erasure of molecule when saving does not depend on them anymore. World::getInstance().getMolecules()->erase(filler); // remove it, Parser adds it automatically // call routine double distance[NDIM]; for (int i=0;ibegin(); !filler->empty(); iter = filler->begin()) { atom *Walker = *iter; World::getInstance().destroyAtom(Walker); } World::getInstance().destroyMolecule(filler); // generate list of newly created molecules // (we can in general remove more quickly from a list than a vector) std::vector fillermolecules = World::getInstance().getAllMolecules(); // DoLog(0) && (Log() << Verbose(0) << fillermolecules.size() << " molecules are present." << std::endl); std::list fillermolecules_list; std::copy( fillermolecules.begin(), fillermolecules.end(), std::back_inserter( fillermolecules_list )); // DoLog(0) && (Log() << Verbose(0) << fillermolecules_list.size() << " molecules have been copied." << std::endl); for (std::vector::const_iterator iter = presentmolecules.begin(); iter != presentmolecules.end(); ++iter) { fillermolecules_list.remove(*iter); } // DoLog(0) && (Log() << Verbose(0) << fillermolecules_list.size() << " molecules left after removal." << std::endl); fillermolecules.clear(); std::copy(fillermolecules_list.begin(), fillermolecules_list.end(), std::back_inserter( fillermolecules )); // DoLog(0) && (Log() << Verbose(0) << fillermolecules.size() << " molecules have been inserted." << std::endl); return Action::state_ptr(new MoleculeFillWithMoleculeState(fillermolecules,params)); } Action::state_ptr MoleculeFillWithMoleculeAction::performUndo(Action::state_ptr _state) { MoleculeFillWithMoleculeState *state = assert_cast(_state.get()); MoleculeListClass *MolList = World::getInstance().getMolecules(); BOOST_FOREACH(molecule *_mol, state->fillermolecules) { MolList->erase(_mol); if ((_mol != NULL) && (!(World::getInstance().getAllMolecules(MoleculeById(_mol->getId()))).empty())) { for(molecule::iterator iter = _mol->begin(); !_mol->empty(); iter = _mol->begin()) { atom *Walker = *iter; World::getInstance().destroyAtom(Walker); } World::getInstance().destroyMolecule(_mol); } } // as molecules and atoms from state are removed, we have to create a new one std::vector fillermolecules; return Action::state_ptr(new MoleculeFillWithMoleculeState(fillermolecules,state->params)); } Action::state_ptr MoleculeFillWithMoleculeAction::performRedo(Action::state_ptr _state){ //MoleculeFillWithMoleculeState *state = assert_cast(_state.get()); return Action::failure; //return Action::state_ptr(_state); } bool MoleculeFillWithMoleculeAction::canUndo() { return true; } bool MoleculeFillWithMoleculeAction::shouldUndo() { return true; } /** =========== end of function ====================== */