1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38 package ffx.potential.constraint;
39
40 import ffx.numerics.Constraint;
41 import ffx.potential.bonded.Angle;
42 import ffx.potential.bonded.Atom;
43 import ffx.potential.bonded.Bond;
44 import org.apache.commons.math3.linear.OpenMapRealMatrix;
45 import org.apache.commons.math3.linear.QRDecomposition;
46 import org.apache.commons.math3.linear.RealMatrix;
47 import org.apache.commons.math3.linear.RealVectorPreservingVisitor;
48 import org.apache.commons.math3.util.FastMath;
49
50 import java.util.ArrayList;
51 import java.util.Arrays;
52 import java.util.HashSet;
53 import java.util.List;
54 import java.util.Set;
55 import java.util.logging.Logger;
56 import java.util.stream.IntStream;
57
58 public class CcmaConstraint implements Constraint {
59
60 public static final double DEFAULT_CCMA_NONZERO_CUTOFF = 0.01;
61 private static final Logger logger = Logger.getLogger(CcmaConstraint.class.getName());
62 private static final int DEFAULT_MAX_ITERS = 150;
63
64
65 private final int[] atoms1;
66 private final int[] atoms2;
67 private final double[] lengths;
68 private final int nConstraints;
69 private final int[] uniqueIndices;
70 private final int maxIters = DEFAULT_MAX_ITERS;
71 private final double elementCutoff = DEFAULT_CCMA_NONZERO_CUTOFF;
72 private final double[] reducedMasses;
73 private final RealMatrix kInvSparse;
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95 private CcmaConstraint(
96 List<Bond> constrainedBonds,
97 List<Angle> constrainedAngles,
98 final Atom[] allAtoms,
99 final double[] masses,
100 double nonzeroCutoff) {
101
102 long time = -System.nanoTime();
103 int nBonds = constrainedBonds.size();
104 int nAngles = constrainedAngles.size();
105 assert constrainedAngles.stream()
106 .flatMap((Angle a) -> a.getBondList().stream())
107 .noneMatch(constrainedBonds::contains);
108 nConstraints = nBonds + (3 * nAngles);
109 atoms1 = new int[nConstraints];
110 atoms2 = new int[nConstraints];
111 lengths = new double[nConstraints];
112
113 for (int i = 0; i < nBonds; i++) {
114 Bond bi = constrainedBonds.get(i);
115 atoms1[i] = bi.getAtom(0).getXyzIndex() - 1;
116 atoms2[i] = bi.getAtom(1).getXyzIndex() - 1;
117 lengths[i] = bi.bondType.distance;
118 }
119
120
121
122
123
124 for (int i = 0; i < nAngles; i++) {
125 int iAng = nBonds + (3 * i);
126 Angle ai = constrainedAngles.get(i);
127 Atom center = ai.getCentralAtom();
128 Bond b1 = ai.getBond(0);
129 Bond b2 = ai.getBond(1);
130 Atom at0 = b1.get1_2(center);
131 Atom at2 = b2.get1_2(center);
132
133 double angVal = ai.angleType.angle[ai.nh];
134 double dist1 = b1.bondType.distance;
135 double dist2 = b2.bondType.distance;
136 double dist3 = SettleConstraint.lawOfCosines(dist1, dist2, angVal);
137
138 int index0 = at0.getXyzIndex() - 1;
139 int index1 = center.getXyzIndex() - 1;
140 int index2 = at2.getXyzIndex() - 1;
141
142 atoms1[iAng] = index0;
143 atoms2[iAng] = index1;
144 lengths[iAng] = dist1;
145
146 ++iAng;
147 atoms1[iAng] = index1;
148 atoms2[iAng] = index2;
149 lengths[iAng] = dist2;
150
151 ++iAng;
152 atoms1[iAng] = index0;
153 atoms2[iAng] = index2;
154 lengths[iAng] = dist3;
155 }
156
157 uniqueIndices =
158 IntStream.concat(Arrays.stream(atoms1), Arrays.stream(atoms2))
159 .sorted()
160 .distinct()
161 .toArray();
162
163 OpenMapRealMatrix kSparse = new OpenMapRealMatrix(nConstraints, nConstraints);
164
165 int nAtoms = allAtoms.length;
166
167
168 List<Set<Integer>> atomsToConstraints = new ArrayList<>(nAtoms);
169 for (int i = 0; i < nAtoms; i++) {
170
171 atomsToConstraints.add(new HashSet<>(4));
172 }
173
174 for (int i = 0; i < nConstraints; i++) {
175 atomsToConstraints.get(atoms1[i]).add(i);
176 atomsToConstraints.get(atoms2[i]).add(i);
177 }
178
179 logger.info(
180 String.format(" Initial CCMA setup: %10.6g sec", 1.0E-9 * (time + System.nanoTime())));
181 long subTime = -System.nanoTime();
182
183 for (int i = 0; i < nConstraints; i++) {
184 int atomi0 = atoms1[i];
185 int atomi1 = atoms2[i];
186
187 double invMassI0 = 1.0 / masses[atomi0 * 3];
188 double invMassI1 = 1.0 / masses[atomi1 * 3];
189 double sumInv = invMassI0 + invMassI1;
190
191
192 Set<Integer> coupledConstraints = new HashSet<>(atomsToConstraints.get(atomi0));
193 coupledConstraints.addAll(atomsToConstraints.get(atomi1));
194
195
196 for (int j : coupledConstraints) {
197
198 if (i == j) {
199 kSparse.setEntry(i, j, 1.0);
200 continue;
201 }
202
203 double scale;
204 int atomj0 = atoms1[j];
205 int atomj1 = atoms2[j];
206 int atoma;
207 int atomb;
208 int atomc;
209
210 if (atomi0 == atomj0) {
211 assert atomi1 != atomj1;
212 atoma = atomi1;
213 atomb = atomi0;
214 atomc = atomj1;
215 scale = invMassI0 / sumInv;
216 } else if (atomi0 == atomj1) {
217 assert atomi1 != atomj0;
218 atoma = atomi1;
219 atomb = atomi0;
220 atomc = atomj0;
221 scale = invMassI0 / sumInv;
222 } else if (atomi1 == atomj0) {
223
224 assert atomi0 != atomj1;
225 atoma = atomi0;
226 atomb = atomi1;
227 atomc = atomj1;
228 scale = invMassI1 / sumInv;
229 } else if (atomi1 == atomj1) {
230 assert atomi0 != atomj0;
231 atoma = atomi1;
232 atomb = atomi0;
233 atomc = atomj1;
234 scale = invMassI1 / sumInv;
235 } else {
236 throw new IllegalArgumentException(
237 " Despite by necessity sharing an atom, these constraints don't share an atom.");
238 }
239
240
241
242
243 boolean foundAngle = false;
244 for (int constraintK : atomsToConstraints.get(atoma)) {
245 if (atoms1[constraintK] == atomc || atoms2[constraintK] == atomc) {
246 double dab = lengths[i];
247 double dbc = lengths[j];
248 double dac = lengths[constraintK];
249
250
251 double angle = (dab * dab) + (dbc * dbc) - (dac * dac);
252 angle /= (2 * dab * dbc);
253
254
255 double coupling = scale * angle;
256 kSparse.setEntry(i, j, coupling);
257 foundAngle = true;
258 break;
259 }
260 }
261
262
263
264 if (!foundAngle) {
265 Atom atA = allAtoms[atoma];
266 Atom atB = allAtoms[atomb];
267 Atom atC = allAtoms[atomc];
268 Angle angleB = atA.getAngle(atB, atC);
269 double angVal = angleB.angleType.angle[angleB.nh];
270 double coupling = scale * FastMath.cos(FastMath.toRadians(angVal));
271 kSparse.setEntry(i, j, coupling);
272 foundAngle = true;
273 }
274
275 if (!foundAngle) {
276 logger.severe(
277 String.format(" Could not find the angle between coupled constraints %d, %d", i, j));
278 }
279 }
280 }
281
282 subTime += System.nanoTime();
283 logger.info(
284 String.format(" Time to construct K as a sparse matrix: %10.6g sec", 1.0E-9 * subTime));
285
286
287
288 subTime = -System.nanoTime();
289 QRDecomposition qrd = new QRDecomposition(kSparse);
290 RealMatrix kInvDense = qrd.getSolver().getInverse();
291 subTime += System.nanoTime();
292 logger.info(String.format(" Time to invert K: %10.6g sec", 1.0E-9 * subTime));
293 subTime = -System.nanoTime();
294
295 kInvSparse = new OpenMapRealMatrix(nConstraints, nConstraints);
296 IntStream.range(0, nConstraints)
297 .
298
299 forEach(
300 (int i) -> {
301 double[] rowI = kInvDense.getRow(i);
302 for (int j = 0; j < nConstraints; j++) {
303 double val = rowI[j];
304 if (Math.abs(val) > elementCutoff) {
305 kInvSparse.setEntry(i, j, val);
306 }
307 }
308 });
309
310
311 double[][] dataDense = kInvDense.getData();
312 double[][] dataSparse = kInvSparse.getData();
313 logger.fine(" Dense array:");
314 for (int i = 0; i < nConstraints; i++) {
315 logger.fine(Arrays.toString(dataDense[i]));
316 }
317 logger.fine(" Sparse array:");
318 for (int i = 0; i < nConstraints; i++) {
319 logger.fine(Arrays.toString(dataSparse[i]));
320 }
321
322
323 reducedMasses = new double[nConstraints];
324 for (int i = 0; i < nConstraints; i++) {
325 int atI = atoms1[i];
326 atI *= 3;
327 int atJ = atoms2[i];
328 atJ *= 3;
329 double invMassI = 1.0 / masses[atI];
330 double invMassJ = 1.0 / masses[atJ];
331 reducedMasses[i] = 0.5 / (invMassI + invMassJ);
332 }
333 }
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354 public static CcmaConstraint ccmaFactory(
355 List<Bond> constrainedBonds,
356 List<Angle> constrainedAngles,
357 final Atom[] allAtoms,
358 final double[] masses,
359 double nonzeroCutoff) {
360 CcmaConstraint newC =
361 new CcmaConstraint(constrainedBonds, constrainedAngles, allAtoms, masses, nonzeroCutoff);
362 constrainedBonds.forEach((Bond b) -> b.setConstraint(newC));
363 constrainedAngles.forEach((Angle a) -> a.setConstraint(newC));
364 return newC;
365 }
366
367
368 @Override
369 public void applyConstraintToStep(double[] xPrior, double[] xNew, double[] masses, double tol) {
370 applyConstraints(xPrior, xNew, masses, false, tol);
371 }
372
373
374 @Override
375 public void applyConstraintToVelocities(double[] x, double[] v, double[] masses, double tol) {
376 applyConstraints(x, v, masses, true, tol);
377 }
378
379
380 @Override
381 public int[] constrainedAtomIndices() {
382 return Arrays.copyOf(uniqueIndices, uniqueIndices.length);
383 }
384
385
386 @Override
387 public boolean constraintSatisfied(double[] x, double tol) {
388 return false;
389 }
390
391
392 @Override
393 public boolean constraintSatisfied(double[] x, double[] v, double xTol, double vTol) {
394 return false;
395 }
396
397
398 @Override
399 public int getNumDegreesFrozen() {
400 return nConstraints;
401 }
402
403
404
405
406
407
408
409
410
411
412
413 private void applyConstraints(
414 double[] xPrior, double[] output, double[] masses, boolean constrainV, double tol) {
415 if (xPrior == output) {
416 throw new IllegalArgumentException(" xPrior and output must be different arrays!");
417 }
418 long time = -System.nanoTime();
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509 }
510
511 private static class MatrixWalker implements RealVectorPreservingVisitor {
512
513 private final double[] constraintDelta;
514 private double sum = 0.0;
515
516 MatrixWalker(final double[] cDelta) {
517 constraintDelta = cDelta;
518 }
519
520
521 @Override
522 public double end() {
523 return sum;
524 }
525
526
527 @Override
528 public void start(int dimension, int start, int end) {
529 }
530
531
532 @Override
533 public void visit(int index, double value) {
534 sum += (value * constraintDelta[index]);
535 }
536 }
537 }