| [d505a3] | 1 | /*
 | 
|---|
 | 2 |  * Project: MoleCuilder
 | 
|---|
 | 3 |  * Description: creates and alters molecular systems
 | 
|---|
 | 4 |  * Copyright (C)  2012 University of Bonn. All rights reserved.
 | 
|---|
 | 5 |  * Please see the LICENSE file or "Copyright notice" in builder.cpp for details.
 | 
|---|
 | 6 |  */
 | 
|---|
 | 7 | 
 | 
|---|
 | 8 | /*
 | 
|---|
 | 9 |  * Filler.cpp
 | 
|---|
 | 10 |  *
 | 
|---|
 | 11 |  *  Created on: Jan 16, 2012
 | 
|---|
 | 12 |  *      Author: heber
 | 
|---|
 | 13 |  */
 | 
|---|
 | 14 | 
 | 
|---|
 | 15 | 
 | 
|---|
 | 16 | // include config.h
 | 
|---|
 | 17 | #ifdef HAVE_CONFIG_H
 | 
|---|
 | 18 | #include <config.h>
 | 
|---|
 | 19 | #endif
 | 
|---|
 | 20 | 
 | 
|---|
 | 21 | #include "CodePatterns/MemDebug.hpp"
 | 
|---|
 | 22 | 
 | 
|---|
 | 23 | #include <algorithm>
 | 
|---|
 | 24 | #include <boost/bind.hpp>
 | 
|---|
 | 25 | #include <boost/lambda/lambda.hpp>
 | 
|---|
 | 26 | #include <sstream>
 | 
|---|
 | 27 | #include <vector>
 | 
|---|
 | 28 | 
 | 
|---|
 | 29 | #include "Filler.hpp"
 | 
|---|
 | 30 | 
 | 
|---|
 | 31 | #include "CodePatterns/Assert.hpp"
 | 
|---|
 | 32 | #include "CodePatterns/Log.hpp"
 | 
|---|
 | 33 | 
 | 
|---|
 | 34 | #include "Atom/atom.hpp"
 | 
|---|
| [4fbc4eb] | 35 | #include "Box.hpp"
 | 
|---|
| [d505a3] | 36 | #include "ClusterInterface.hpp"
 | 
|---|
 | 37 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
| [901d87] | 38 | #include "Inserter/Inserter.hpp"
 | 
|---|
| [4fbc4eb] | 39 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
 | 
|---|
| [d505a3] | 40 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| [b3d687] | 41 | #include "molecule.hpp"
 | 
|---|
| [d505a3] | 42 | #include "NodeTypes.hpp"
 | 
|---|
 | 43 | #include "Predicates/FillPredicate.hpp"
 | 
|---|
 | 44 | #include "Predicates/Ops_FillPredicate.hpp"
 | 
|---|
 | 45 | #include "World.hpp"
 | 
|---|
 | 46 | 
 | 
|---|
 | 47 | 
 | 
|---|
| [901d87] | 48 | Filler::Filler(const Mesh &_mesh, const FillPredicate &_predicate, const Inserter &_inserter) :
 | 
|---|
| [d505a3] | 49 |   mesh(_mesh),
 | 
|---|
| [901d87] | 50 |   predicate(!_predicate),
 | 
|---|
 | 51 |   inserter(_inserter)
 | 
|---|
| [d505a3] | 52 | {}
 | 
|---|
 | 53 | 
 | 
|---|
 | 54 | Filler::~Filler()
 | 
|---|
 | 55 | {}
 | 
|---|
 | 56 | 
 | 
|---|
| [68abe5] | 57 | bool Filler::operator()(
 | 
|---|
| [d505a3] | 58 |     CopyAtomsInterface ©Method,
 | 
|---|
| [42b6de] | 59 |     ClusterInterface::Cluster_impl cluster,
 | 
|---|
 | 60 |     ClusterVector_t &ClonedClusters) const
 | 
|---|
| [d505a3] | 61 | {
 | 
|---|
 | 62 |   const NodeSet &nodes = mesh.getNodes();
 | 
|---|
 | 63 |   std::stringstream output;
 | 
|---|
 | 64 |   std::for_each( nodes.begin(), nodes.end(), output << boost::lambda::_1 << " ");
 | 
|---|
| [5a4cbc] | 65 |   LOG(3, "DEBUG: Listing nodes to check: " << output.str());
 | 
|---|
| [68abe5] | 66 |   if (nodes.size() == 0)
 | 
|---|
 | 67 |     return false;
 | 
|---|
| [d505a3] | 68 |   NodeSet FillNodes(nodes.size(), zeroVec);
 | 
|---|
 | 69 | 
 | 
|---|
| [b3d687] | 70 |   // evaluate predicates at each FillNode
 | 
|---|
| [4fbc4eb] | 71 |   {
 | 
|---|
| [b3d687] | 72 |     // move filler cluster's atoms out of domain such that it does not disturb the predicate.
 | 
|---|
 | 73 |     // we only move the atoms as otherwise two translate ShapeOps will be on top of the Shape
 | 
|---|
 | 74 |     // which is subsequently copied to all other cloned Clusters ...
 | 
|---|
 | 75 |     Vector BoxDiagonal;
 | 
|---|
 | 76 |     {
 | 
|---|
 | 77 |       const RealSpaceMatrix &M = World::getInstance().getDomain().getM();
 | 
|---|
 | 78 |       BoxDiagonal = (M * Vector(1.,1.,1.));
 | 
|---|
 | 79 |       BoxDiagonal -= cluster->getShape().getCenter();
 | 
|---|
 | 80 |       BoxDiagonal *= 1. + 2.*cluster->getShape().getRadius()/BoxDiagonal.Norm(); // extend it a little further
 | 
|---|
 | 81 |       AtomIdSet atoms = cluster->getAtoms();
 | 
|---|
 | 82 |       for (AtomIdSet::iterator iter = atoms.begin(); iter != atoms.end(); ++iter)
 | 
|---|
 | 83 |         (*iter)->setPosition( (*iter)->getPosition() + BoxDiagonal );
 | 
|---|
 | 84 |       LOG(1, "INFO: Translating original cluster's atoms by " << BoxDiagonal << ".");
 | 
|---|
 | 85 |     }
 | 
|---|
 | 86 | 
 | 
|---|
 | 87 |     // evaluate predicate and gather into new set
 | 
|---|
 | 88 |     NodeSet::iterator transform_end  =
 | 
|---|
 | 89 |         std::remove_copy_if(nodes.begin(), nodes.end(), FillNodes.begin(), predicate );
 | 
|---|
 | 90 |     FillNodes.erase(transform_end, FillNodes.end());
 | 
|---|
 | 91 | 
 | 
|---|
 | 92 |     // shift cluster back to original place
 | 
|---|
 | 93 |     {
 | 
|---|
 | 94 |       AtomIdSet atoms = cluster->getAtoms();
 | 
|---|
 | 95 |       for (AtomIdSet::iterator iter = atoms.begin(); iter != atoms.end(); ++iter)
 | 
|---|
 | 96 |         (*iter)->setPosition( (*iter)->getPosition() - BoxDiagonal );
 | 
|---|
 | 97 |       LOG(1, "INFO: Translating original cluster's atoms back.");
 | 
|---|
 | 98 |     }
 | 
|---|
| [4fbc4eb] | 99 |   }
 | 
|---|
 | 100 | 
 | 
|---|
| [d505a3] | 101 |   if (FillNodes.size() == 0) {
 | 
|---|
| [5a4cbc] | 102 |     ELOG(2, "For none of the nodes did the predicate return true.");
 | 
|---|
| [68abe5] | 103 |     return false;
 | 
|---|
| [5a4cbc] | 104 |   } else {
 | 
|---|
 | 105 |     LOG(1, "INFO: " << FillNodes.size() << " out of " << nodes.size() << " returned true from predicate.");
 | 
|---|
| [d505a3] | 106 |   }
 | 
|---|
 | 107 | 
 | 
|---|
| [e32fa6] | 108 |   // clone clusters
 | 
|---|
| [42b6de] | 109 |   ClonedClusters.resize(FillNodes.size());
 | 
|---|
| [b3d687] | 110 |   {
 | 
|---|
| [42b6de] | 111 |     std::vector<ClusterInterface::Cluster_impl>::iterator clusteriter = ClonedClusters.begin();
 | 
|---|
| [b3d687] | 112 |     *clusteriter = cluster;
 | 
|---|
 | 113 |     clusteriter++;
 | 
|---|
 | 114 |     std::generate_n(clusteriter, FillNodes.size()-1,
 | 
|---|
 | 115 |         boost::bind(&ClusterInterface::clone, boost::cref(cluster), boost::ref(copyMethod), zeroVec) );
 | 
|---|
 | 116 |   }
 | 
|---|
| [e32fa6] | 117 | 
 | 
|---|
 | 118 |   // insert each cluster by abusing std::search a bit:
 | 
|---|
| [b3d687] | 119 |   {
 | 
|---|
 | 120 |     // we look for the subsequence of FillNodes inside clusters. If Inserter always
 | 
|---|
 | 121 |     // returns true, we'll have the iterator pointing at first cluster
 | 
|---|
 | 122 |     std::vector<ClusterInterface::Cluster_impl>::const_iterator inserteriter =
 | 
|---|
| [42b6de] | 123 |       std::search(ClonedClusters.begin(), ClonedClusters.end(), FillNodes.begin(), FillNodes.end(),
 | 
|---|
| [b3d687] | 124 |           boost::bind(&Inserter::operator(), boost::cref(inserter), _1, _2));
 | 
|---|
| [42b6de] | 125 |     if( inserteriter != ClonedClusters.begin()) {
 | 
|---|
| [b3d687] | 126 |       ELOG(1, "Not all cloned clusters could be successfully inserted.");
 | 
|---|
 | 127 |       return false;
 | 
|---|
 | 128 |     }
 | 
|---|
 | 129 |   }
 | 
|---|
 | 130 | 
 | 
|---|
 | 131 |   // create molecules for each cluster and fill in atoms
 | 
|---|
 | 132 |   {
 | 
|---|
| [42b6de] | 133 |     std::vector<molecule *> molecules(ClonedClusters.size()-1, NULL);
 | 
|---|
| [b3d687] | 134 |     std::generate_n(molecules.begin(), FillNodes.size()-1,
 | 
|---|
 | 135 |         boost::bind(&World::createMolecule, World::getPointer()) );
 | 
|---|
| [42b6de] | 136 |     std::vector<ClusterInterface::Cluster_impl>::const_iterator clusteriter = ClonedClusters.begin();
 | 
|---|
| [b3d687] | 137 |     ++clusteriter;
 | 
|---|
 | 138 |     std::vector<molecule *>::iterator moliter = molecules.begin();
 | 
|---|
 | 139 |     for (;moliter != molecules.end(); ++clusteriter, ++moliter) {
 | 
|---|
 | 140 |       AtomIdSet atoms = (*clusteriter)->getAtoms();
 | 
|---|
 | 141 |       for (AtomIdSet::iterator iter = atoms.begin(); iter != atoms.end(); ++iter)
 | 
|---|
 | 142 |         (*moliter)->AddAtom(*iter);
 | 
|---|
 | 143 |     }
 | 
|---|
| [e32fa6] | 144 |   }
 | 
|---|
| [d505a3] | 145 | 
 | 
|---|
| [e32fa6] | 146 |   // give final statment on whether at least \a single cluster has been placed
 | 
|---|
| [42b6de] | 147 |   return ( FillNodes.size() != 0);
 | 
|---|
| [d505a3] | 148 | }
 | 
|---|
| [e32fa6] | 149 | 
 | 
|---|