| 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"
 | 
|---|
| 35 | #include "Box.hpp"
 | 
|---|
| 36 | #include "ClusterInterface.hpp"
 | 
|---|
| 37 | #include "Descriptors/AtomIdDescriptor.hpp"
 | 
|---|
| 38 | #include "Inserter/Inserter.hpp"
 | 
|---|
| 39 | #include "LinearAlgebra/RealSpaceMatrix.hpp"
 | 
|---|
| 40 | #include "LinearAlgebra/Vector.hpp"
 | 
|---|
| 41 | #include "NodeTypes.hpp"
 | 
|---|
| 42 | #include "Predicates/FillPredicate.hpp"
 | 
|---|
| 43 | #include "Predicates/Ops_FillPredicate.hpp"
 | 
|---|
| 44 | #include "World.hpp"
 | 
|---|
| 45 | 
 | 
|---|
| 46 | 
 | 
|---|
| 47 | /** Constructor for class Filler.
 | 
|---|
| 48 |  *
 | 
|---|
| 49 |  * \note We store inverted \a _predicate because we need it only for
 | 
|---|
| 50 |  * remove_copy_if which works in this inverted way as desired.
 | 
|---|
| 51 |  *
 | 
|---|
| 52 |  * @param _mesh Mesh with a NodeSet that fills its Shape
 | 
|---|
| 53 |  * @param _predicate predicate construct to check at each Node
 | 
|---|
| 54 |  * @param _inserter inserter which places the cloned cluster
 | 
|---|
| 55 |  */
 | 
|---|
| 56 | Filler::Filler(const Mesh &_mesh, const FillPredicate &_predicate, const Inserter &_inserter) :
 | 
|---|
| 57 |   mesh(_mesh),
 | 
|---|
| 58 |   predicate(!_predicate),
 | 
|---|
| 59 |   inserter(_inserter)
 | 
|---|
| 60 | {}
 | 
|---|
| 61 | 
 | 
|---|
| 62 | /** Destructor for class Filler.
 | 
|---|
| 63 |  *
 | 
|---|
| 64 |  */
 | 
|---|
| 65 | Filler::~Filler()
 | 
|---|
| 66 | {}
 | 
|---|
| 67 | 
 | 
|---|
| 68 | /** Fill in the desired Cluster at each remaining node.
 | 
|---|
| 69 |  *
 | 
|---|
| 70 |  * \note The cluster is non-const because it is moved to the first vacant node.
 | 
|---|
| 71 |  *
 | 
|---|
| 72 |  * @param copyMethod functor that knows how to copy atoms.
 | 
|---|
| 73 |  * @param cluster set of atomic ids contained in a specific Shape to fill each Node with
 | 
|---|
| 74 |  * @return true - at least one node has been filled, false - no node filled
 | 
|---|
| 75 |  */
 | 
|---|
| 76 | bool Filler::operator()(
 | 
|---|
| 77 |     CopyAtomsInterface ©Method,
 | 
|---|
| 78 |     ClusterInterface::Cluster_impl cluster) const
 | 
|---|
| 79 | {
 | 
|---|
| 80 |   const NodeSet &nodes = mesh.getNodes();
 | 
|---|
| 81 |   std::stringstream output;
 | 
|---|
| 82 |   std::for_each( nodes.begin(), nodes.end(), output << boost::lambda::_1 << " ");
 | 
|---|
| 83 |   LOG(3, "DEBUG: Listing nodes to check: " << output.str());
 | 
|---|
| 84 |   if (nodes.size() == 0)
 | 
|---|
| 85 |     return false;
 | 
|---|
| 86 |   NodeSet FillNodes(nodes.size(), zeroVec);
 | 
|---|
| 87 | 
 | 
|---|
| 88 |   // move filler cluster's atoms out of domain such that it does not disturb the predicate.
 | 
|---|
| 89 |   // we only move the atoms as otherwise two translate ShapeOps will be on top of the Shape
 | 
|---|
| 90 |   // which is subsequently copied to all other cloned Clusters ...
 | 
|---|
| 91 |   Vector BoxDiagonal;
 | 
|---|
| 92 |   {
 | 
|---|
| 93 |     const RealSpaceMatrix &M = World::getInstance().getDomain().getM();
 | 
|---|
| 94 |     BoxDiagonal = (M * Vector(1.,1.,1.));
 | 
|---|
| 95 |     BoxDiagonal -= cluster->getShape().getCenter();
 | 
|---|
| 96 |     BoxDiagonal *= 1. + 2.*cluster->getShape().getRadius()/BoxDiagonal.Norm(); // extend it a little further
 | 
|---|
| 97 |     AtomIdSet atoms = cluster->getAtoms();
 | 
|---|
| 98 |     for (AtomIdSet::iterator iter = atoms.begin(); iter != atoms.end(); ++iter)
 | 
|---|
| 99 |       (*iter)->setPosition( (*iter)->getPosition() + BoxDiagonal );
 | 
|---|
| 100 |     LOG(1, "INFO: Translating original cluster's atoms by " << BoxDiagonal << ".");
 | 
|---|
| 101 |   }
 | 
|---|
| 102 | 
 | 
|---|
| 103 |   // evaluate predicate and gather into new set
 | 
|---|
| 104 |   NodeSet::iterator transform_end  =
 | 
|---|
| 105 |       std::remove_copy_if(nodes.begin(), nodes.end(), FillNodes.begin(), predicate );
 | 
|---|
| 106 |   FillNodes.erase(transform_end, FillNodes.end());
 | 
|---|
| 107 | 
 | 
|---|
| 108 |   // shift cluster back to original place
 | 
|---|
| 109 |   {
 | 
|---|
| 110 |     AtomIdSet atoms = cluster->getAtoms();
 | 
|---|
| 111 |     for (AtomIdSet::iterator iter = atoms.begin(); iter != atoms.end(); ++iter)
 | 
|---|
| 112 |       (*iter)->setPosition( (*iter)->getPosition() - BoxDiagonal );
 | 
|---|
| 113 |     LOG(1, "INFO: Translating original cluster's atoms back.");
 | 
|---|
| 114 |   }
 | 
|---|
| 115 | 
 | 
|---|
| 116 |   if (FillNodes.size() == 0) {
 | 
|---|
| 117 |     ELOG(2, "For none of the nodes did the predicate return true.");
 | 
|---|
| 118 |     return false;
 | 
|---|
| 119 |   } else {
 | 
|---|
| 120 |     LOG(1, "INFO: " << FillNodes.size() << " out of " << nodes.size() << " returned true from predicate.");
 | 
|---|
| 121 |   }
 | 
|---|
| 122 | 
 | 
|---|
| 123 |   // clone clusters
 | 
|---|
| 124 |   std::vector<ClusterInterface::Cluster_impl> clusters(FillNodes.size());
 | 
|---|
| 125 |   std::vector<ClusterInterface::Cluster_impl>::iterator clusteriter = clusters.begin();
 | 
|---|
| 126 |   *clusteriter = cluster;
 | 
|---|
| 127 |   clusteriter++;
 | 
|---|
| 128 |   std::generate_n(clusteriter, FillNodes.size()-1,
 | 
|---|
| 129 |       boost::bind(&ClusterInterface::clone, boost::cref(cluster), boost::ref(copyMethod), zeroVec) );
 | 
|---|
| 130 | 
 | 
|---|
| 131 |   // insert each cluster by abusing std::search a bit:
 | 
|---|
| 132 |   // we look for the subsequence of FillNodes inside clusters. If Inserter always
 | 
|---|
| 133 |   // returns true, we'll have the iterator pointing at first cluster
 | 
|---|
| 134 |   std::vector<ClusterInterface::Cluster_impl>::const_iterator inserteriter =
 | 
|---|
| 135 |     std::search(clusters.begin(), clusters.end(), FillNodes.begin(), FillNodes.end(),
 | 
|---|
| 136 |         boost::bind(&Inserter::operator(), boost::cref(inserter), _1, _2));
 | 
|---|
| 137 |   if( inserteriter != clusters.begin()) {
 | 
|---|
| 138 |     ELOG(1, "Not all cloned clusters could be successfully inserted.");
 | 
|---|
| 139 |     return false;
 | 
|---|
| 140 |   }
 | 
|---|
| 141 | 
 | 
|---|
| 142 |   // give final statment on whether at least \a single cluster has been placed
 | 
|---|
| 143 |   if ( FillNodes.size() != 0)
 | 
|---|
| 144 |     return true;
 | 
|---|
| 145 |   else
 | 
|---|
| 146 |     return false;
 | 
|---|
| 147 | }
 | 
|---|
| 148 | 
 | 
|---|