Simpatico  v1.10
mcMd/chemistry/Molecule.h
1 #ifndef MCMD_MOLECULE_H
2 #define MCMD_MOLECULE_H
3 
4 /*
5 * Simpatico - Simulation Package for Polymeric and Molecular Liquids
6 *
7 * Copyright 2010 - 2017, The Regents of the University of Minnesota
8 * Distributed under the terms of the GNU General Public License.
9 */
10 
11 #include "Atom.h"
12 #ifdef SIMP_BOND
13 #include "Bond.h"
14 #endif
15 #ifdef SIMP_ANGLE
16 #include "Angle.h"
17 #endif
18 #ifdef SIMP_DIHEDRAL
19 #include "Dihedral.h"
20 #endif
21 #include <util/containers/ArrayIterator.h>
22 #include <util/containers/ConstArrayIterator.h>
23 #include <util/global.h>
24 
25 namespace Simp {
26  class Species;
27 }
28 
29 namespace McMd
30 {
31 
32  using namespace Util;
33  using namespace Simp;
34 
35  class System;
36 
42  class Molecule
43  {
44 
45  public:
46 
47  // Typedefs
48 
51 
54 
55  #ifdef SIMP_BOND
58 
61  #endif
62 
63  #ifdef SIMP_ANGLE
66 
69  #endif
70 
71  #ifdef SIMP_DIHEDRAL
74 
77  #endif
78 
80  static const int NullIndex = -1;
81 
82  // Member functions
83 
87  Molecule();
88 
93 
99  void setSpecies(Species &species);
100 
106  void setSystem(System &system);
107 
111  void unsetSystem();
112 
118  void setId(int id);
119 
125  void setFirstAtom(Atom &atom);
126 
132  void setNAtom(int nAtom);
133 
134  #ifdef SIMP_BOND
135 
140  void setFirstBond(Bond &bond);
141 
147  void setNBond(int nBond);
148  #endif
149 
150  #ifdef SIMP_ANGLE
151 
156  void setFirstAngle(Angle &angle);
157 
163  void setNAngle(int nAngle);
164  #endif
165 
166  #ifdef SIMP_DIHEDRAL
167 
172  void setFirstDihedral(Dihedral &dihedral);
173 
179  void setNDihedral(int nDihedral);
180  #endif
181 
183 
185 
187  Species& species() const;
188 
190  System& system() const;
191 
193  int id() const;
194 
196  int nAtom() const;
197 
198  #ifdef SIMP_BOND
199  int nBond() const;
201  #endif
202 
203  #ifdef SIMP_ANGLE
204  int nAngle() const;
206  #endif
207 
208  #ifdef SIMP_DIHEDRAL
209  int nDihedral() const;
211  #endif
212 
220  const Atom& atom(int localId) const;
221 
229  Atom& atom(int localId);
230 
231  #ifdef SIMP_BOND
232 
239  Bond& bond(int localId);
240 
248  const Bond& bond(int localId) const;
249  #endif
250 
251  #ifdef SIMP_ANGLE
252 
259  Angle& angle(int localId);
260 
268  const Angle& angle(int localId) const;
269  #endif
270 
271  #ifdef SIMP_DIHEDRAL
272 
279  Dihedral& dihedral(int localId);
280 
288  const Dihedral& dihedral(int localId) const;
289  #endif
290 
292 
294 
300  void begin(AtomIterator &iterator);
301 
307  void begin(ConstAtomIterator &iterator) const;
308 
309  #ifdef SIMP_BOND
310 
315  void begin(BondIterator &iterator);
316 
322  void begin(ConstBondIterator &iterator) const;
323  #endif
324 
325  #ifdef SIMP_ANGLE
326 
331  void begin(AngleIterator &iterator);
332 
338  void begin(ConstAngleIterator &iterator) const;
339  #endif
340 
341  #ifdef SIMP_DIHEDRAL
342 
347  void begin(DihedralIterator &iterator);
348 
354  void begin(ConstDihedralIterator &iterator) const;
355  #endif
356 
358 
359  private:
360 
362  Species* speciesPtr_;
363 
366  System* systemPtr_;
367 
369  Atom* firstAtomPtr_;
370 
371  #ifdef SIMP_BOND
372  Bond* firstBondPtr_;
374  #endif
375 
376  #ifdef SIMP_ANGLE
377  Angle* firstAnglePtr_;
379  #endif
380 
381  #ifdef SIMP_DIHEDRAL
382  Dihedral* firstDihedralPtr_;
384  #endif
385 
387  int nAtom_;
388 
389  #ifdef SIMP_BOND
390  int nBond_;
392  #endif
393 
394  #ifdef SIMP_ANGLE
395  int nAngle_;
397  #endif
398 
399  #ifdef SIMP_DIHEDRAL
400  int nDihedral_;
402  #endif
403 
405  int id_;
406 
407  // friends:
408 
409  class DeActivator;
410 
411  };
412 
413  // Inline member functions
414 
415  /*
416  * Get the Species by reference.
417  */
418  inline Species& Molecule::species() const
419  {
420  assert(speciesPtr_);
421  return *speciesPtr_;
422  }
423 
424  /*
425  * Get the parent System by reference.
426  */
427  inline System& Molecule::system() const
428  {
429  assert(systemPtr_);
430  return *systemPtr_;
431  }
432 
433  /*
434  * Get number of atoms in this molecule.
435  */
436  inline int Molecule::nAtom() const
437  { return nAtom_; }
438 
439  #ifdef SIMP_BOND
440  /*
441  * Get number of bonds in this molecule.
442  */
443  inline int Molecule::nBond() const
444  { return nBond_; }
445  #endif
446 
447  #ifdef SIMP_ANGLE
448  /*
449  * Get number of angles in this molecule.
450  */
451  inline int Molecule::nAngle() const
452  { return nAngle_; }
453  #endif
454 
455  #ifdef SIMP_DIHEDRAL
456  /*
457  * Get number of dihedrals in this molecule.
458  */
459  inline int Molecule::nDihedral() const
460  { return nDihedral_; }
461  #endif
462 
463  /*
464  * Get a specific Atom, referenced by an index.
465  */
466  inline Atom& Molecule::atom(int localIndex)
467  {
468  assert(firstAtomPtr_);
469  assert(localIndex >= 0);
470  assert(localIndex < nAtom_);
471  return *(firstAtomPtr_ + localIndex);
472  }
473 
474  /*
475  * Get a specific Atom, referenced by an index.
476  */
477  inline const Atom& Molecule::atom(int localIndex) const
478  {
479  assert(firstAtomPtr_);
480  assert(localIndex >= 0);
481  assert(localIndex < nAtom_);
482  return *(firstAtomPtr_ + localIndex);
483  }
484 
485  #ifdef SIMP_BOND
486  /*
487  * Get a specific Bond by reference.
488  */
489  inline Bond& Molecule::bond(int localIndex)
490  {
491  assert(firstBondPtr_);
492  assert(localIndex >= 0);
493  assert(localIndex < nBond_);
494  return *(firstBondPtr_ + localIndex);
495  }
496 
497  /*
498  * Get a specific Bond by const reference.
499  */
500  inline const Bond& Molecule::bond(int localIndex) const
501  {
502  assert(firstBondPtr_);
503  assert(localIndex >= 0);
504  assert(localIndex < nBond_);
505  return *(firstBondPtr_ + localIndex);
506  }
507  #endif
508 
509  #ifdef SIMP_ANGLE
510  /*
511  * Get a specific Angle, referenced by an index.
512  */
513  inline Angle& Molecule::angle(int localIndex)
514  {
515  assert(firstAnglePtr_);
516  assert(localIndex >= 0);
517  assert(localIndex < nAngle_);
518  return *(firstAnglePtr_ + localIndex);
519  }
520 
521  /*
522  * Get a specific Angle, referenced by an index.
523  */
524  inline const Angle& Molecule::angle(int localIndex) const
525  {
526  assert(firstAnglePtr_);
527  assert(localIndex >= 0);
528  assert(localIndex < nAngle_);
529  return *(firstAnglePtr_ + localIndex);
530  }
531  #endif
532 
533  #ifdef SIMP_DIHEDRAL
534  /*
535  * Get a specific Dihedral, referenced by an index.
536  */
537  inline Dihedral& Molecule::dihedral(int localIndex)
538  {
539  assert(firstDihedralPtr_);
540  assert(localIndex >= 0);
541  assert(localIndex < nDihedral_);
542  return *(firstDihedralPtr_ + localIndex);
543  }
544 
545  /*
546  * Get a specific Dihedral, referenced by an index.
547  */
548  inline const Dihedral& Molecule::dihedral(int localIndex) const
549  {
550  assert(firstDihedralPtr_);
551  assert(localIndex >= 0);
552  assert(localIndex < nAngle_);
553  return *(firstDihedralPtr_ + localIndex);
554  }
555  #endif
556 
557  /*
558  * Get global index for this Molecule.
559  */
560  inline int Molecule::id() const
561  { return id_; }
562 
563  /*
564  * Set AtomIterator to first Atom in this molecule.
565  */
566  inline void Molecule::begin(AtomIterator &iterator)
567  {
568  assert(firstAtomPtr_);
569  assert(nAtom_ > 0);
570  iterator.setCurrent(firstAtomPtr_);
571  iterator.setEnd(firstAtomPtr_ + nAtom_);
572  }
573 
574  /*
575  * Set AtomIterator to first Atom in this molecule.
576  */
577  inline void Molecule::begin(ConstAtomIterator &iterator) const
578  {
579  assert(firstAtomPtr_);
580  assert(nAtom_ > 0);
581  iterator.setCurrent(firstAtomPtr_);
582  iterator.setEnd(firstAtomPtr_ + nAtom_);
583  }
584 
585  #ifdef SIMP_BOND
586  /*
587  * Set BondIterator to first Bond in this molecule.
588  */
589  inline void Molecule::begin(BondIterator &iterator)
590  {
591  assert(firstBondPtr_);
592  assert(nBond_ > 0);
593  iterator.setCurrent(firstBondPtr_);
594  iterator.setEnd(firstBondPtr_ + nBond_);
595  }
596 
597  /*
598  * Set ConstBondIterator to first Bond in this molecule.
599  */
600  inline void Molecule::begin(ConstBondIterator &iterator) const
601  {
602  assert(firstBondPtr_);
603  assert(nBond_ > 0);
604  iterator.setCurrent(firstBondPtr_);
605  iterator.setEnd(firstBondPtr_ + nBond_);
606  }
607  #endif
608 
609  #ifdef SIMP_ANGLE
610  /*
611  * Set AngleIterator to first Angle in this molecule.
612  */
613  inline void Molecule::begin(AngleIterator &iterator)
614  {
615  assert(firstAnglePtr_);
616  assert(nAngle_ > 0);
617  iterator.setCurrent(firstAnglePtr_);
618  iterator.setEnd(firstAnglePtr_ + nAngle_);
619  }
620 
621  /*
622  * Set ConstAngleIterator to first Angle in this molecule.
623  */
624  inline void Molecule::begin(ConstAngleIterator &iterator) const
625  {
626  assert(firstAnglePtr_);
627  assert(nAngle_ > 0);
628  iterator.setCurrent(firstAnglePtr_);
629  iterator.setEnd(firstAnglePtr_ + nAngle_);
630  }
631  #endif
632 
633  #ifdef SIMP_DIHEDRAL
634  /*
635  * Set DihedralIterator to first Dihedral in this molecule.
636  */
637  inline void Molecule::begin(DihedralIterator &iterator)
638  {
639  assert(firstDihedralPtr_);
640  assert(nDihedral_ > 0);
641  iterator.setCurrent(firstDihedralPtr_);
642  iterator.setEnd(firstDihedralPtr_ + nDihedral_);
643  }
644 
645  /*
646  * Set ConstDihedralIterator to first Dihedral in this molecule.
647  */
648  inline void Molecule::begin(ConstDihedralIterator &iterator) const
649  {
650  assert(firstDihedralPtr_);
651  assert(nDihedral_ > 0);
652  iterator.setCurrent(firstDihedralPtr_);
653  iterator.setEnd(firstDihedralPtr_ + nDihedral_);
654  }
655  #endif
656 
657  /*
658  * Set pointer to molecular Species.
659  */
660  inline void Molecule::setSpecies(Species &species)
661  { speciesPtr_ = &species; }
662 
663  /*
664  * Set pointer to parent System.
665  */
666  inline void Molecule::setSystem(System &system)
667  { systemPtr_ = &system; }
668 
669  /*
670  * Set the pointer to the parent System to null.
671  */
672  inline void Molecule::unsetSystem()
673  { systemPtr_ = 0; }
674 
675  /*
676  * Set pointer to first Atom in molecule.
677  */
678  inline void Molecule::setFirstAtom(Atom &atom)
679  { firstAtomPtr_ = &atom; }
680 
681  /*
682  * Set number of atoms per molecule.
683  */
684  inline void Molecule::setNAtom(int nAtom)
685  { nAtom_ = nAtom; }
686 
687  #ifdef SIMP_BOND
688  /*
689  * Set pointer to first Bond in molecule.
690  */
691  inline void Molecule::setFirstBond(Bond &bond)
692  { firstBondPtr_ = &bond; }
693 
694  /*
695  * Set number of bonds per molecule.
696  */
697  inline void Molecule::setNBond(int nBond)
698  { nBond_ = nBond; }
699  #endif
700 
701  #ifdef SIMP_ANGLE
702  /*
703  * Set pointer to first Angle in molecule.
704  */
705  inline void Molecule::setFirstAngle(Angle &angle)
706  { firstAnglePtr_ = &angle; }
707 
708  /*
709  * Set number of angles per molecule.
710  */
711  inline void Molecule::setNAngle(int nAngle)
712  { nAngle_ = nAngle; }
713  #endif
714 
715  #ifdef SIMP_DIHEDRAL
716  /*
717  * Set pointer to first Dihedral in molecule.
718  */
719  inline void Molecule::setFirstDihedral(Dihedral &dihedral)
720  { firstDihedralPtr_ = &dihedral; }
721 
722  /*
723  * Set number of dihedrals per molecule.
724  */
725  inline void Molecule::setNDihedral(int nDihedral)
726  { nDihedral_ = nDihedral; }
727  #endif
728 
729  /*
730  * Set global index.
731  */
732  inline void Molecule::setId(int id)
733  { id_ = id; }
734 
735  // Inline member function of Atom class.
736 
737  /*
738  * Return local id of an Atom within its parent molecule.
739  *
740  * This function is defined here, rather than in Atom.h, because its
741  * implementation requires the inline method Molecule::atom(int).
742  */
743  inline int Atom::indexInMolecule() const
744  { return int(this - &molecule().atom(0));}
745 
746 }
747 #endif
ConstArrayIterator< Atom > ConstAtomIterator
Iterator for const Atoms within a Molecule.
ConstArrayIterator< Angle > ConstAngleIterator
Iterator for const Angles within a Molecule.
A set of interacting Molecules enclosed by a Boundary.
Definition: System.h:115
void setCurrent(Data *ptr)
Set the current pointer value.
Definition: ArrayIterator.h:59
File containing preprocessor macros for error handling.
ArrayIterator< Atom > AtomIterator
Iterator for Atoms within a Molecule.
Classes used by all simpatico molecular simulations.
Forward const iterator for an Array or a C array.
ConstArrayIterator< Bond > ConstBondIterator
Iterator for const Bonds within a Molecule.
void setEnd(Data *ptr)
Set the value of the end pointer.
ConstArrayIterator< Dihedral > ConstDihedralIterator
Iterator for const Dihedrals within a Molecule.
A point particle within a Molecule.
Utility classes for scientific computation.
Definition: accumulators.mod:1
Forward iterator for an Array or a C array.
Definition: ArrayIterator.h:39
void setEnd(Data *ptr)
Set the value of the end pointer.
Definition: ArrayIterator.h:67
Single-processor Monte Carlo (MC) and molecular dynamics (MD).
A sequence of NAtom covalently interacting atoms.
void setCurrent(Data *ptr)
Set the current pointer value.
A physical molecule (a set of covalently bonded Atoms).
A Species represents a set of chemically similar molecules.