| 1 | 
 | 
|---|
| 2 | /* simple.cc -- implementation of the simple internal coordinate classes
 | 
|---|
| 3 |  *
 | 
|---|
| 4 |  *      THIS SOFTWARE FITS THE DESCRIPTION IN THE U.S. COPYRIGHT ACT OF A
 | 
|---|
| 5 |  *      "UNITED STATES GOVERNMENT WORK".  IT WAS WRITTEN AS A PART OF THE
 | 
|---|
| 6 |  *      AUTHOR'S OFFICIAL DUTIES AS A GOVERNMENT EMPLOYEE.  THIS MEANS IT
 | 
|---|
| 7 |  *      CANNOT BE COPYRIGHTED.  THIS SOFTWARE IS FREELY AVAILABLE TO THE
 | 
|---|
| 8 |  *      PUBLIC FOR USE WITHOUT A COPYRIGHT NOTICE, AND THERE ARE NO
 | 
|---|
| 9 |  *      RESTRICTIONS ON ITS USE, NOW OR SUBSEQUENTLY.
 | 
|---|
| 10 |  *
 | 
|---|
| 11 |  *  Author:
 | 
|---|
| 12 |  *      E. T. Seidl
 | 
|---|
| 13 |  *      Bldg. 12A, Rm. 2033
 | 
|---|
| 14 |  *      Computer Systems Laboratory
 | 
|---|
| 15 |  *      Division of Computer Research and Technology
 | 
|---|
| 16 |  *      National Institutes of Health
 | 
|---|
| 17 |  *      Bethesda, Maryland 20892
 | 
|---|
| 18 |  *      Internet: seidl@alw.nih.gov
 | 
|---|
| 19 |  *      February, 1993
 | 
|---|
| 20 |  */
 | 
|---|
| 21 | 
 | 
|---|
| 22 | #ifdef __GNUC__
 | 
|---|
| 23 | #pragma implementation
 | 
|---|
| 24 | #endif
 | 
|---|
| 25 | 
 | 
|---|
| 26 | #include <string.h>
 | 
|---|
| 27 | #include <math.h>
 | 
|---|
| 28 | 
 | 
|---|
| 29 | #if defined(SGI) && !defined(__GNUC__)
 | 
|---|
| 30 | #include <bstring.h>
 | 
|---|
| 31 | #endif
 | 
|---|
| 32 | 
 | 
|---|
| 33 | #include <util/class/scexception.h>
 | 
|---|
| 34 | #include <util/misc/formio.h>
 | 
|---|
| 35 | #include <util/state/stateio.h>
 | 
|---|
| 36 | #include <chemistry/molecule/simple.h>
 | 
|---|
| 37 | #include <chemistry/molecule/localdef.h>
 | 
|---|
| 38 | 
 | 
|---|
| 39 | #include <util/container/bitarray.h>
 | 
|---|
| 40 | 
 | 
|---|
| 41 | using namespace std;
 | 
|---|
| 42 | using namespace sc;
 | 
|---|
| 43 | 
 | 
|---|
| 44 | //////////////////////////////////////////////////////////////////////
 | 
|---|
| 45 | 
 | 
|---|
| 46 | static ClassDesc SimpleCo_cd(
 | 
|---|
| 47 |   typeid(SimpleCo),"SimpleCo",1,"public IntCoor",
 | 
|---|
| 48 |   0, 0, 0);
 | 
|---|
| 49 | 
 | 
|---|
| 50 | SimpleCo::SimpleCo():
 | 
|---|
| 51 |   natoms_(0),
 | 
|---|
| 52 |   atoms(0)
 | 
|---|
| 53 | {
 | 
|---|
| 54 | }
 | 
|---|
| 55 | 
 | 
|---|
| 56 | SimpleCo::SimpleCo(int na, const char *re) :
 | 
|---|
| 57 |   IntCoor(re),
 | 
|---|
| 58 |   natoms_(na), atoms(0)
 | 
|---|
| 59 | {
 | 
|---|
| 60 |   atoms=new int[na]; memset(atoms,'\0',sizeof(int)*na);
 | 
|---|
| 61 | }
 | 
|---|
| 62 | 
 | 
|---|
| 63 | SimpleCo::SimpleCo(const Ref<KeyVal>&kv,int na) :
 | 
|---|
| 64 |   IntCoor(kv),
 | 
|---|
| 65 |   natoms_(na), atoms(0)
 | 
|---|
| 66 | {
 | 
|---|
| 67 |   atoms=new int[na];
 | 
|---|
| 68 |   memset(atoms,'\0',sizeof(int)*na);
 | 
|---|
| 69 | 
 | 
|---|
| 70 |   if (kv->count() == 0) {
 | 
|---|
| 71 |       int i;
 | 
|---|
| 72 |       for (i=0; i<na; i++) {
 | 
|---|
| 73 |           atoms[i] = kv->intvalue("atoms",i);
 | 
|---|
| 74 |           if (kv->error() != KeyVal::OK) break;
 | 
|---|
| 75 |         }
 | 
|---|
| 76 |       if (i == 0) {
 | 
|---|
| 77 |           // couldn't find any atoms so look for a molecule and atom labels
 | 
|---|
| 78 |           Ref<Molecule> mol; mol << kv->describedclassvalue("molecule");
 | 
|---|
| 79 |           if (mol.nonnull()) {
 | 
|---|
| 80 |               for (i=0; i<na; i++) {
 | 
|---|
| 81 |                   char *label = kv->pcharvalue("atom_labels", i);
 | 
|---|
| 82 |                   if (kv->error() != KeyVal::OK) break;
 | 
|---|
| 83 |                   atoms[i] = mol->atom_label_to_index(label) + 1;
 | 
|---|
| 84 |                   delete[] label;
 | 
|---|
| 85 |                   if (atoms[i] == 0) break;
 | 
|---|
| 86 |                 }
 | 
|---|
| 87 |             }
 | 
|---|
| 88 |         }
 | 
|---|
| 89 |       if (i != na) {
 | 
|---|
| 90 |           InputError ex("KeyVal CTOR: missing one of the atoms "
 | 
|---|
| 91 |                         "or atom_labels (requires a molecule too) "
 | 
|---|
| 92 |                         "or an atom label was invalid",
 | 
|---|
| 93 |                         __FILE__, __LINE__, 0, 0, class_desc());
 | 
|---|
| 94 |           try {
 | 
|---|
| 95 |               kv->errortrace(ex.elaborate());
 | 
|---|
| 96 |             }
 | 
|---|
| 97 |           catch (...) {}
 | 
|---|
| 98 |           throw ex;
 | 
|---|
| 99 |         }
 | 
|---|
| 100 |     }
 | 
|---|
| 101 |   else {
 | 
|---|
| 102 |       // This is a shorthand form for the input that doesn't allow
 | 
|---|
| 103 |       // the specification of a value.
 | 
|---|
| 104 |       if (label_) delete[] label_;
 | 
|---|
| 105 |       label_=kv->pcharvalue(0);
 | 
|---|
| 106 |       for (int i=0; i<na; i++) {
 | 
|---|
| 107 |           atoms[i]=kv->intvalue(i+1);
 | 
|---|
| 108 |           if (kv->error() != KeyVal::OK) {
 | 
|---|
| 109 |               InputError ex("KeyVal CTOR: missing an atom",
 | 
|---|
| 110 |                             __FILE__, __LINE__, 0, 0, class_desc());
 | 
|---|
| 111 |               try {
 | 
|---|
| 112 |                   kv->errortrace(ex.elaborate());
 | 
|---|
| 113 |                 }
 | 
|---|
| 114 |               catch (...) {}
 | 
|---|
| 115 |               throw ex;
 | 
|---|
| 116 |             }
 | 
|---|
| 117 |         }
 | 
|---|
| 118 |     }
 | 
|---|
| 119 | }
 | 
|---|
| 120 | 
 | 
|---|
| 121 | SimpleCo::~SimpleCo()
 | 
|---|
| 122 | {
 | 
|---|
| 123 |   if(atoms) delete[] atoms; atoms=0;
 | 
|---|
| 124 |   natoms_=0;
 | 
|---|
| 125 | }
 | 
|---|
| 126 | 
 | 
|---|
| 127 | void
 | 
|---|
| 128 | SimpleCo::save_data_state(StateOut& s)
 | 
|---|
| 129 | {
 | 
|---|
| 130 |   IntCoor::save_data_state(s);
 | 
|---|
| 131 |   s.put(natoms_);
 | 
|---|
| 132 |   s.put(atoms,natoms_);
 | 
|---|
| 133 | }
 | 
|---|
| 134 | 
 | 
|---|
| 135 | SimpleCo::SimpleCo(StateIn& si):
 | 
|---|
| 136 |   IntCoor(si)
 | 
|---|
| 137 | {
 | 
|---|
| 138 |   si.get(natoms_);
 | 
|---|
| 139 |   si.get(atoms);
 | 
|---|
| 140 | }
 | 
|---|
| 141 | 
 | 
|---|
| 142 | int
 | 
|---|
| 143 | SimpleCo::natoms() const
 | 
|---|
| 144 | {
 | 
|---|
| 145 |   return natoms_;
 | 
|---|
| 146 | }
 | 
|---|
| 147 | 
 | 
|---|
| 148 | int
 | 
|---|
| 149 | SimpleCo::operator[](int i) const
 | 
|---|
| 150 | {
 | 
|---|
| 151 |   return atoms[i];
 | 
|---|
| 152 | }
 | 
|---|
| 153 | 
 | 
|---|
| 154 | int
 | 
|---|
| 155 | SimpleCo::operator!=(SimpleCo&u)
 | 
|---|
| 156 | {
 | 
|---|
| 157 |   return !(*this==u);
 | 
|---|
| 158 | }
 | 
|---|
| 159 | 
 | 
|---|
| 160 | int
 | 
|---|
| 161 | SimpleCo::operator==(SimpleCo& sc)
 | 
|---|
| 162 | {
 | 
|---|
| 163 |   if(label_ && !sc.label_ || !label_ && sc.label_) return 0;
 | 
|---|
| 164 |   if(label_ && strcmp(label_,sc.label_)) return 0;
 | 
|---|
| 165 | 
 | 
|---|
| 166 |   if(atoms && !sc.atoms || !atoms && sc.atoms) return 0;
 | 
|---|
| 167 |   if(atoms)
 | 
|---|
| 168 |     for(int i=0; i < natoms_; i++) if (atoms[i]!=sc.atoms[i]) return 0;
 | 
|---|
| 169 | 
 | 
|---|
| 170 |   return 1;
 | 
|---|
| 171 | }
 | 
|---|
| 172 | 
 | 
|---|
| 173 | double
 | 
|---|
| 174 | SimpleCo::force_constant(Ref<Molecule>&mol)
 | 
|---|
| 175 | {
 | 
|---|
| 176 |   return calc_force_con(*mol);
 | 
|---|
| 177 | }
 | 
|---|
| 178 | 
 | 
|---|
| 179 | // this updates the values before it computes the bmatrix,
 | 
|---|
| 180 | // which is not quite what I wanted--but close enough
 | 
|---|
| 181 | void
 | 
|---|
| 182 | SimpleCo::bmat(const Ref<Molecule>&mol,RefSCVector&bmat,double coef)
 | 
|---|
| 183 | {
 | 
|---|
| 184 |   int i;
 | 
|---|
| 185 |   int n = bmat.dim().n();
 | 
|---|
| 186 | 
 | 
|---|
| 187 |   double* v = new double[n];
 | 
|---|
| 188 |   for (i=0; i<n; i++) v[i] = bmat(i);
 | 
|---|
| 189 | 
 | 
|---|
| 190 |   calc_intco(*mol,v,coef);
 | 
|---|
| 191 | 
 | 
|---|
| 192 |   for (i=0; i<n; i++) {
 | 
|---|
| 193 |       bmat(i) = v[i];
 | 
|---|
| 194 |     }
 | 
|---|
| 195 |   
 | 
|---|
| 196 |   delete[] v;
 | 
|---|
| 197 | }
 | 
|---|
| 198 | 
 | 
|---|
| 199 | void
 | 
|---|
| 200 | SimpleCo::update_value(const Ref<Molecule>&mol)
 | 
|---|
| 201 | {
 | 
|---|
| 202 |   calc_intco(*mol);
 | 
|---|
| 203 | }
 | 
|---|
| 204 | 
 | 
|---|
| 205 | void
 | 
|---|
| 206 | SimpleCo::print_details(const Ref<Molecule> &mol, ostream& os) const
 | 
|---|
| 207 | {
 | 
|---|
| 208 |   os << indent
 | 
|---|
| 209 |      << scprintf("%-5s %7s %11.5f", ctype(), (label()?label():""),
 | 
|---|
| 210 |                  preferred_value());
 | 
|---|
| 211 |   
 | 
|---|
| 212 |   int i;
 | 
|---|
| 213 |   for (i=0; i<natoms(); i++)
 | 
|---|
| 214 |       os << scprintf(" %4d", atoms[i]);
 | 
|---|
| 215 | 
 | 
|---|
| 216 |   if (mol.nonnull()) {
 | 
|---|
| 217 |       char *separator = " ";
 | 
|---|
| 218 |       os << "  ";
 | 
|---|
| 219 |       for (i=0; i<(4-natoms()); i++) {
 | 
|---|
| 220 |           os << "   ";
 | 
|---|
| 221 |         }
 | 
|---|
| 222 |       for (i=0; i<natoms(); i++) {
 | 
|---|
| 223 |           os << separator << mol->atom_symbol(atoms[i]-1);
 | 
|---|
| 224 |           separator = "-";
 | 
|---|
| 225 |         }
 | 
|---|
| 226 |     }
 | 
|---|
| 227 | 
 | 
|---|
| 228 |   os << endl;
 | 
|---|
| 229 |   
 | 
|---|
| 230 | }
 | 
|---|
| 231 | 
 | 
|---|
| 232 | // this doesn't catch all cases, it would be best for each subclass
 | 
|---|
| 233 | // to override this
 | 
|---|
| 234 | int
 | 
|---|
| 235 | SimpleCo::equivalent(Ref<IntCoor>&c)
 | 
|---|
| 236 | {
 | 
|---|
| 237 |   if (class_desc() != c->class_desc()) {
 | 
|---|
| 238 |       return 0;
 | 
|---|
| 239 |     }
 | 
|---|
| 240 |   SimpleCo* sc = dynamic_cast<SimpleCo*>(c.pointer());
 | 
|---|
| 241 |   if (natoms_ != sc->natoms_) return 0; // this should never be the case
 | 
|---|
| 242 |   for (int i=0; i<natoms_; i++) {
 | 
|---|
| 243 |       if (atoms[i] != sc->atoms[i]) return 0;
 | 
|---|
| 244 |     }
 | 
|---|
| 245 |   return 1;
 | 
|---|
| 246 | }
 | 
|---|
| 247 | 
 | 
|---|
| 248 | /////////////////////////////////////////////////////////////////////////////
 | 
|---|
| 249 | 
 | 
|---|
| 250 | // Local Variables:
 | 
|---|
| 251 | // mode: c++
 | 
|---|
| 252 | // c-file-style: "CLJ"
 | 
|---|
| 253 | // End:
 | 
|---|