/** \file molecule.hpp * * Class definitions of atom and molecule, element and periodentafel */ #ifndef MOLECULES_HPP_ #define MOLECULES_HPP_ /*********************************************** includes ***********************************/ #ifdef HAVE_CONFIG_H #include #endif //// STL headers #include #include #include #include #include #include #include "types.hpp" #include "graph.hpp" #include "tesselation.hpp" #include "Patterns/Observer.hpp" #include "Patterns/ObservedIterator.hpp" #include "Patterns/Cacheable.hpp" #include "Descriptors/MoleculeDescriptor_impl.hpp" /****************************************** forward declarations *****************************/ class atom; class bond; class BondedParticle; class BondGraph; class element; class ForceMatrix; class LinkedCell; class molecule; class MoleculeLeafClass; class MoleculeListClass; class periodentafel; class Vector; class Shape; template class StackClass; /******************************** Some definitions for easier reading **********************************/ #define MoleculeList list #define MoleculeListTest pair #define DistancePair pair < double, atom* > #define DistanceMap multimap < double, atom* > #define DistanceTestPair pair < DistanceMap::iterator, bool> /************************************* Class definitions ****************************************/ /** Structure to contain parameters needed for evaluation of constraint potential. */ struct EvaluatePotential { int startstep; //!< start configuration (MDStep in atom::trajectory) int endstep; //!< end configuration (MDStep in atom::trajectory) atom **PermutationMap; //!< gives target ptr for each atom, array of size molecule::AtomCount (this is "x" in \f$ V^{con}(x) \f$ ) DistanceMap **DistanceList; //!< distance list of each atom to each atom DistanceMap::iterator *StepList; //!< iterator to ascend through NearestNeighbours \a **DistanceList int *DoubleList; //!< count of which sources want to move to this target, basically the injective measure (>1 -> not injective) DistanceMap::iterator *DistanceIterators; //!< marks which was the last picked target as injective candidate with smallest distance bool IsAngstroem; //!< whether coordinates are in angstroem (true) or bohrradius (false) double *PenaltyConstants; //!< penalty constant in front of each term }; /** The complete molecule. * Class incorporates number of types */ class molecule : public PointCloud , public Observable { friend molecule *NewMolecule(); friend void DeleteMolecule(molecule *); public: typedef std::list atomSet; typedef std::set atomIdSet; typedef ObservedIterator iterator; typedef atomSet::const_iterator const_iterator; const periodentafel * const elemente; //!< periodic table with each element // old deprecated atom handling //atom *start; //!< start of atom list //atom *end; //!< end of atom list //bond *first; //!< start of bond list //bond *last; //!< end of bond list int MDSteps; //!< The number of MD steps in Trajectories //int AtomCount; //!< number of atoms, brought up-to-date by CountAtoms() int BondCount; //!< number of atoms, brought up-to-date by CountBonds() int ElementCount; //!< how many unique elements are therein int ElementsInMolecule[MAX_ELEMENTS]; //!< list whether element (sorted by atomic number) is alread present or not mutable int NoNonHydrogen; //!< number of non-hydrogen atoms in molecule mutable int NoNonBonds; //!< number of non-hydrogen bonds in molecule mutable int NoCyclicBonds; //!< number of cyclic bonds in molecule, by DepthFirstSearchAnalysis() double BondDistance; //!< typical bond distance used in CreateAdjacencyList() and furtheron bool ActiveFlag; //!< in a MoleculeListClass used to discern active from inactive molecules Vector Center; //!< Center of molecule in a global box int IndexNr; //!< index of molecule in a MoleculeListClass char name[MAXSTRINGSIZE]; //!< arbitrary name private: Cacheable formula; Cacheable AtomCount; moleculeId_t id; atomSet atoms; // insert ( atom * const key ); bool containsAtom(atom* key); // re-definition of virtual functions from PointCloud const char * const GetName() const; Vector *GetCenter() const ; TesselPoint *GetPoint() const ; int GetMaxId() const; void GoToNext() const ; void GoToFirst() const ; bool IsEmpty() const ; bool IsEnd() const ; // templates for allowing global manipulation of all vectors template void ActOnAllVectors( res (Vector::*f)() ) const; template void ActOnAllVectors( res (Vector::*f)() const) const; template void ActOnAllVectors( res (Vector::*f)(T), T t ) const; template void ActOnAllVectors( res (Vector::*f)(T) const, T t ) const; template void ActOnAllVectors( res (Vector::*f)(T&), T &t ) const; template void ActOnAllVectors( res (Vector::*f)(T&) const, T &t ) const; template void ActOnAllVectors( res (Vector::*f)(T, U), T t, U u ) const; template void ActOnAllVectors( res (Vector::*f)(T, U) const, T t, U u ) const; template void ActOnAllVectors( res (Vector::*f)(T, U, V), T t, U u, V v) const; template void ActOnAllVectors( res (Vector::*f)(T, U, V) const, T t, U u, V v) const; // templates for allowing global manipulation of molecule with each atom as single argument template void ActWithEachAtom( res (molecule::*f)(atom *) ) const; template void ActWithEachAtom( res (molecule::*f)(atom *) const) const; // templates for allowing global copying of molecule with each atom as single argument template void ActOnCopyWithEachAtom( res (molecule::*f)(atom *) , molecule *copy) const; template void ActOnCopyWithEachAtom( res (molecule::*f)(atom *) const, molecule *copy) const; // templates for allowing global manipulation of all atoms template void ActOnAllAtoms( res (typ::*f)() ) const; template void ActOnAllAtoms( res (typ::*f)() const) const; template void ActOnAllAtoms( res (typ::*f)(T), T t ) const; template void ActOnAllAtoms( res (typ::*f)(T) const, T t ) const; template void ActOnAllAtoms( res (typ::*f)(T, U), T t, U u ) const; template void ActOnAllAtoms( res (typ::*f)(T, U) const, T t, U u ) const; template void ActOnAllAtoms( res (typ::*f)(T, U, V), T t, U u, V v) const; template void ActOnAllAtoms( res (typ::*f)(T, U, V) const, T t, U u, V v) const; template void ActOnAllAtoms( res (typ::*f)(T, U, V, W), T t, U u, V v, W w) const; template void ActOnAllAtoms( res (typ::*f)(T, U, V, W) const, T t, U u, V v, W w) const; // templates for allowing conditional global copying of molecule with each atom as single argument template void ActOnCopyWithEachAtomIfTrue( res (molecule::*f)(atom *) , molecule *copy, bool (atom::*condition) () ) const; template void ActOnCopyWithEachAtomIfTrue( res (molecule::*f)(atom *) , molecule *copy, bool (atom::*condition) () const ) const; template void ActOnCopyWithEachAtomIfTrue( res (molecule::*f)(atom *) const , molecule *copy, bool (atom::*condition) () ) const; template void ActOnCopyWithEachAtomIfTrue( res (molecule::*f)(atom *) const , molecule *copy, bool (atom::*condition) () const ) const; template void ActOnCopyWithEachAtomIfTrue( res (molecule::*f)(atom *) , molecule *copy, bool (atom::*condition) (T), T t ) const; template void ActOnCopyWithEachAtomIfTrue( res (molecule::*f)(atom *) , molecule *copy, bool (atom::*condition) (T) const, T t ) const; template void ActOnCopyWithEachAtomIfTrue( res (molecule::*f)(atom *) const , molecule *copy, bool (atom::*condition) (T), T t ) const; template void ActOnCopyWithEachAtomIfTrue( res (molecule::*f)(atom *) const , molecule *copy, bool (atom::*condition) (T) const, T t ) const; template void ActOnCopyWithEachAtomIfTrue( res (molecule::*f)(atom *) , molecule *copy, bool (atom::*condition) (T, U), T t, U u ) const; template void ActOnCopyWithEachAtomIfTrue( res (molecule::*f)(atom *) , molecule *copy, bool (atom::*condition) (T, U) const, T t, U u ) const; template void ActOnCopyWithEachAtomIfTrue( res (molecule::*f)(atom *) const , molecule *copy, bool (atom::*condition) (T, U), T t, U u ) const; template void ActOnCopyWithEachAtomIfTrue( res (molecule::*f)(atom *) const , molecule *copy, bool (atom::*condition) (T, U) const, T t, U u ) const; template void ActOnCopyWithEachAtomIfTrue( res (molecule::*f)(atom *) , molecule *copy, bool (atom::*condition) (T, U, V), T t, U u, V v ) const; template void ActOnCopyWithEachAtomIfTrue( res (molecule::*f)(atom *) , molecule *copy, bool (atom::*condition) (T, U, V) const, T t, U u, V v ) const; template void ActOnCopyWithEachAtomIfTrue( res (molecule::*f)(atom *) const , molecule *copy, bool (atom::*condition) (T, U, V), T t, U u, V v ) const; template void ActOnCopyWithEachAtomIfTrue( res (molecule::*f)(atom *) const , molecule *copy, bool (atom::*condition) (T, U, V) const, T t, U u, V v ) const; // templates for allowing global manipulation of an array with one entry per atom void SetIndexedArrayForEachAtomTo ( atom **array, int ParticleInfo::* index) const; template void SetIndexedArrayForEachAtomTo ( T *array, int ParticleInfo::* index, void (*Setor)(T *, T *)) const; template void SetIndexedArrayForEachAtomTo ( T *array, int ParticleInfo::* index, void (*Setor)(T *, T *), T t) const; template void SetIndexedArrayForEachAtomTo ( T *array, int ParticleInfo::* index, void (*Setor)(T *, T *), T *t) const; template void SetIndexedArrayForEachAtomTo ( T *array, int element::* index, void (*Setor)(T *, T *)) const; template void SetIndexedArrayForEachAtomTo ( T *array, int element::* index, void (*Setor)(T *, T *), T t) const; template void SetIndexedArrayForEachAtomTo ( T *array, int element::* index, void (*Setor)(T *, T *), T *t) const; template void SetIndexedArrayForEachAtomTo ( T *array, int ParticleInfo::*index, T (atom::*Setor)(typ &), typ atom::*value) const; template void SetIndexedArrayForEachAtomTo ( T *array, int ParticleInfo::*index, T (atom::*Setor)(typ &) const, typ atom::*value) const; template void SetIndexedArrayForEachAtomTo ( T *array, int ParticleInfo::*index, T (atom::*Setor)(typ &), typ &vect ) const; template void SetIndexedArrayForEachAtomTo ( T *array, int ParticleInfo::*index, T (atom::*Setor)(typ &) const, typ &vect ) const; // templates for allowing global manipulation of each atom by entries in an array template void SetAtomValueToIndexedArray ( T *array, int typ::*index, T typ2::*value ) const; template void SetAtomValueToValue ( T value, T typ::*ptr ) const; template res SumPerAtom(res (typ::*f)() ) const; template res SumPerAtom(res (typ::*f)() const ) const; template res SumPerAtom(res (typ::*f)(T) , T t ) const; template res SumPerAtom(res (typ::*f)(T) const, T t ) const; /// remove atoms from molecule. bool AddAtom(atom *pointer); bool RemoveAtom(atom *pointer); bool UnlinkAtom(atom *pointer); bool CleanupMolecule(); /// Add/remove atoms to/from molecule. atom * AddCopyAtom(atom *pointer); bool AddXYZFile(string filename); bool AddHydrogenReplacementAtom(bond *Bond, atom *BottomOrigin, atom *TopOrigin, atom *TopReplacement, bool IsAngstroem); bond * AddBond(atom *first, atom *second, int degree = 1); bool RemoveBond(bond *pointer); bool RemoveBonds(atom *BondPartner); bool hasBondStructure(); unsigned int CountBonds() const; /// Find atoms. atom * FindAtom(int Nr) const; atom * AskAtom(string text); /// Count and change present atoms' coordination. void CountElements(); bool CenterInBox(); bool BoundInBox(); void CenterEdge(Vector *max); void CenterOrigin(); void CenterPeriodic(); void CenterAtVector(Vector *newcenter); void Translate(const Vector *x); void TranslatePeriodically(const Vector *trans); void Mirror(const Vector *x); void Align(Vector *n); void Scale(const double ** const factor); void DeterminePeriodicCenter(Vector ¢er); Vector * DetermineCenterOfGravity(); Vector * DetermineCenterOfAll() const; Vector * DetermineCenterOfBox() const; void SetNameFromFilename(const char *filename); void SetBoxDimension(Vector *dim); void ScanForPeriodicCorrection(); bool VerletForceIntegration(char *file, config &configuration, const size_t offset); void Thermostats(config &configuration, double ActualTemp, int Thermostat); void PrincipalAxisSystem(bool DoRotate); double VolumeOfConvexEnvelope(bool IsAngstroem); double ConstrainedPotential(struct EvaluatePotential &Params); double MinimiseConstrainedPotential(atom **&permutation, int startstep, int endstep, bool IsAngstroem); void EvaluateConstrainedForces(int startstep, int endstep, atom **PermutationMap, ForceMatrix *Force); bool LinearInterpolationBetweenConfiguration(int startstep, int endstep, std::string &prefix, config &configuration, bool MapByIdentity); bool CheckBounds(const Vector *x) const; void GetAlignvector(struct lsq_params * par) const; /// Initialising routines in fragmentation void CreateAdjacencyListFromDbondFile(ifstream *output); void CreateAdjacencyList(double bonddistance, bool IsAngstroem, void (BondGraph::*f)(BondedParticle * const , BondedParticle * const , double &, double &, bool), BondGraph *BG = NULL); int CorrectBondDegree() const; void OutputBondsList() const; void CyclicBondAnalysis() const; void OutputGraphInfoPerAtom() const; void OutputGraphInfoPerBond() const; // Graph analysis MoleculeLeafClass * DepthFirstSearchAnalysis(class StackClass *&BackEdgeStack) const; void CyclicStructureAnalysis(class StackClass *BackEdgeStack, int *&MinimumRingSize) const; bool PickLocalBackEdges(atom **ListOfLocalAtoms, class StackClass *&ReferenceStack, class StackClass *&LocalStack) const; bond * FindNextUnused(atom *vertex) const; void SetNextComponentNumber(atom *vertex, int nr) const; void ResetAllBondsToUnused() const; int CountCyclicBonds(); bool CheckForConnectedSubgraph(KeySet *Fragment); string GetColor(enum Shading color) const; bond * CopyBond(atom *left, atom *right, bond *CopyBond); molecule *CopyMolecule(); molecule* CopyMoleculeFromSubRegion(const Shape&) const; /// Fragment molecule by two different approaches: int FragmentMolecule(int Order, std::string &prefix); bool CheckOrderAtSite(bool *AtomMask, Graph *GlobalKeySetList, int Order, int *MinimumRingSize, std::string path = ""); bool StoreBondsToFile(std::string &filename, std::string path = ""); bool StoreAdjacencyToFile(std::string &filename, std::string path = ""); bool CheckAdjacencyFileAgainstMolecule(std::string &path, atom **ListOfAtoms); bool ParseOrderAtSiteFromFile(std::string &path); bool StoreOrderAtSiteFile(std::string &path); bool StoreForcesFile(MoleculeListClass *BondFragments, std::string &path, int *SortIndex); bool CreateMappingLabelsToConfigSequence(int *&SortIndex); bool CreateFatherLookupTable(atom **&LookupTable, int count = 0); void BreadthFirstSearchAdd(molecule *Mol, atom **&AddedAtomList, bond **&AddedBondList, atom *Root, bond *Bond, int BondOrder, bool IsAngstroem); /// -# BOSSANOVA void FragmentBOSSANOVA(Graph *&FragmentList, KeyStack &RootStack, int *MinimumRingSize); int PowerSetGenerator(int Order, struct UniqueFragments &FragmentSearch, KeySet RestrictedKeySet); bool BuildInducedSubgraph(const molecule *Father); molecule * StoreFragmentFromKeySet(KeySet &Leaflet, bool IsAngstroem); void SPFragmentGenerator(struct UniqueFragments *FragmentSearch, int RootDistance, bond **BondsSet, int SetDimension, int SubOrder); int LookForRemovalCandidate(KeySet *&Leaf, int *&ShortestPathList); int GuesstimateFragmentCount(int order); // Recognize doubly appearing molecules in a list of them int * IsEqualToWithinThreshold(molecule *OtherMolecule, double threshold); int * GetFatherSonAtomicMap(molecule *OtherMolecule); // Output routines. bool Output(ofstream * const output); bool OutputTrajectories(ofstream * const output); void OutputListOfBonds() const; bool OutputXYZ(ofstream * const output) const; bool OutputTrajectoriesXYZ(ofstream * const output); bool Checkout(ofstream * const output) const; bool OutputTemperatureFromTrajectories(ofstream * const output, int startstep, int endstep); // Manipulation routines void flipActiveFlag(); private: int last_atom; //!< number given to last atom mutable internal_iterator InternalPointer; //!< internal pointer for PointCloud }; molecule *NewMolecule(); void DeleteMolecule(molecule* mol); #include "molecule_template.hpp" /** A list of \a molecule classes. */ class MoleculeListClass : public Observable { public: MoleculeList ListOfMolecules; //!< List of the contained molecules int MaxIndex; MoleculeListClass(World *world); ~MoleculeListClass(); bool AddHydrogenCorrection(std::string &path); bool StoreForcesFile(std::string &path, int *SortIndex); void insert(molecule *mol); void erase(molecule *mol); molecule * ReturnIndex(int index); bool OutputConfigForListOfFragments(std::string &prefix, int *SortIndex); int NumberOfActiveMolecules(); void Enumerate(ostream *out); void Output(ofstream *out); void DissectMoleculeIntoConnectedSubgraphs(const periodentafel * const periode, config * const configuration); int CountAllAtoms() const; // Methods moved here from the menus // TODO: more refactoring needed on these methods void flipChosen(); void createNewMolecule(periodentafel *periode); void loadFromXYZ(periodentafel *periode); void setMoleculeFilename(); void parseXYZIntoMolecule(); void eraseMolecule(); // merging of molecules bool SimpleMerge(molecule *mol, molecule *srcmol); bool SimpleAdd(molecule *mol, molecule *srcmol); bool SimpleMultiMerge(molecule *mol, int *src, int N); bool SimpleMultiAdd(molecule *mol, int *src, int N); bool ScatterMerge(molecule *mol, int *src, int N); bool EmbedMerge(molecule *mol, molecule *srcmol); private: World *world; //!< The world this List belongs to. Needed to avoid deadlocks in the destructor }; /** A leaf for a tree of \a molecule class * Wraps molecules in a tree structure */ class MoleculeLeafClass { public: molecule *Leaf; //!< molecule of this leaf //MoleculeLeafClass *UpLeaf; //!< Leaf one level up //MoleculeLeafClass *DownLeaf; //!< First leaf one level down MoleculeLeafClass *previous; //!< Previous leaf on this level MoleculeLeafClass *next; //!< Next leaf on this level //MoleculeLeafClass(MoleculeLeafClass *Up, MoleculeLeafClass *Previous); MoleculeLeafClass(MoleculeLeafClass *PreviousLeaf); ~MoleculeLeafClass(); bool AddLeaf(molecule *ptr, MoleculeLeafClass *Previous); bool FillBondStructureFromReference(const molecule * const reference, atom **&ListOfLocalAtoms, bool FreeList = false); bool FillRootStackForSubgraphs(KeyStack *&RootStack, bool *AtomMask, int &FragmentCounter); bool AssignKeySetsToFragment(molecule *reference, Graph *KeySetList, atom ***&ListOfLocalAtoms, Graph **&FragmentList, int &FragmentCounter, bool FreeList = false); bool FillListOfLocalAtoms(atom **&ListOfLocalAtoms, const int GlobalAtomCount, bool &FreeList); void TranslateIndicesToGlobalIDs(Graph **FragmentList, int &FragmentCounter, int &TotalNumberOfKeySets, Graph &TotalGraph); int Count() const; }; #endif /*MOLECULES_HPP_*/