Debian Codegraph @

avogadro/qtplugins/qtaim/qtaimcriticalpointlocator.cpp

raw
1 /******************************************************************************
2
3 This source file is part of the Avogadro project.
4
5 Copyright 2010 Eric C. Brown
6
7 This source code is released under the New BSD License, (the "License").
8
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14
15 ******************************************************************************/
16
17 #include "qtaimcriticalpointlocator.h"
18
19 #include "qtaimlsodaintegrator.h"
20 #include "qtaimmathutilities.h"
21 #include "qtaimwavefunctionevaluator.h"
22
23 #include <Eigen/Core>
24
25 #include <QList>
26
27 #include <QtConcurrent/QtConcurrentMap>
28
29 #include <QDataStream>
30 #include <QDir>
31 #include <QFile>
32 #include <QTemporaryFile>
33
34 #include <QVariant>
35
36 #include <QFuture>
37 #include <QFutureWatcher>
38 #include <QProgressDialog>
39
40 using namespace Eigen;
41
42 #define HUGE_REAL_NUMBER 1.e20
43 #define SMALL_GRADIENT_NORM 1.e-4
44
45 namespace Avogadro::QtPlugins {
46
47 namespace helper {
48 template <qint64 ExpectedSignatureV>
49 QList<QVariant> QTAIMLocateElectronDensityHelper(QList<QVariant> input)
50 {
51 qint64 counter = 0;
52 const QString fileName = input.at(counter).toString();
53 counter++;
54 // const qint64 nucleus=input.at(counter).toInt(); counter++
55 qreal x0 = input.at(counter).toReal();
56 counter++;
57 qreal y0 = input.at(counter).toReal();
58 counter++;
59 qreal z0 = input.at(counter).toReal();
60 counter++;
61
62 const QVector3D x0y0z0(x0, y0, z0);
63
64 QTAIMWavefunction wfn;
65 wfn.loadFromBinaryFile(fileName);
66
67 QTAIMWavefunctionEvaluator eval(wfn);
68
69 /**
70 The following logic chain was concatenated into a single statement to remove
71 redundant branching, for readability the following table has been included.
72 If any of the logic checks do not meet the expected result, the return value
73 will be `false`, else it will be `true`.
74
75 logic chain breakdown:
76 | Statement | Expected |
77 |-----------|----------|
78 | eval.electronDensity(x0y0z0) < 1.e-1 | false |
79 | eval.electronDensity(xyz) > 1.e-1 | true |
80 | eval.gradientOfElectronDensityLaplacian(xyz).norm() < 1.e-3 | true |
81 |
82 QTAIMMathUtilities::signatureOfASymmetricThreeByThreeMatrix(eval.hessianOfElectronDensityLaplacian(xyz))
83 == ExpectedSignatureV | true |
84 */
85
86 // Validate initial input to avoid needless costly calculations
87 if (eval.electronDensity(Matrix<qreal, 3, 1>(x0, y0, z0)) < 1.e-1) {
88 return { false };
89 }
90
91 // QTAIMODEIntegrator
92 // ode(eval,QTAIMODEIntegrator::CMBPMinusThreeGradientInElectronDensityLaplacian);
93 QTAIMLSODAIntegrator ode(
94 eval,
95 QTAIMLSODAIntegrator::CMBPMinusThreeGradientInElectronDensityLaplacian);
96 QVector3D result = ode.integrate(x0y0z0);
97
98 Matrix<qreal, 3, 1> xyz(result.x(), result.y(), result.z());
99
100 // since we are using `&&` operator, lazy evaluation will be used
101 if (eval.electronDensity(xyz) > 1.e-1 &&
102 eval.gradientOfElectronDensityLaplacian(xyz).norm() < 1.e-3 &&
103 QTAIMMathUtilities::signatureOfASymmetricThreeByThreeMatrix(
104 eval.hessianOfElectronDensityLaplacian(xyz)) == ExpectedSignatureV) {
105 return { true, result.x(), result.y(), result.z() };
106 }
107
108 return { false };
109 }
110
111 } // namespace helper
112
113 QList<QVariant> QTAIMLocateNuclearCriticalPoint(QList<QVariant> input)
114 {
115 const QString fileName = input.at(0).toString();
116 const qint64 nucleus = input.at(1).toInt();
117 const QVector3D x0y0z0(input.at(2).toReal(), input.at(3).toReal(),
118 input.at(4).toReal());
119
120 QTAIMWavefunction wfn;
121 wfn.loadFromBinaryFile(fileName);
122
123 QTAIMWavefunctionEvaluator eval(wfn);
124
125 QVector3D result;
126
127 if (wfn.nuclearCharge(nucleus) < 4) {
128 // QTAIMODEIntegrator
129 // ode(eval,QTAIMODEIntegrator::CMBPMinusThreeGradientInElectronDensity);
130 QTAIMLSODAIntegrator ode(
131 eval, QTAIMLSODAIntegrator::CMBPMinusThreeGradientInElectronDensity);
132 result = ode.integrate(x0y0z0);
133 } else {
134 result = x0y0z0;
135 }
136
137 if (QTAIMMathUtilities::signatureOfASymmetricThreeByThreeMatrix(
138 eval.hessianOfElectronDensity(
139 Matrix<qreal, 3, 1>(result.x(), result.y(), result.z()))) == -3) {
140 return { true, result.x(), result.y(), result.z() };
141 }
142
143 return { false };
144 }
145
146 QList<QVariant> QTAIMLocateBondCriticalPoint(QList<QVariant> input)
147 {
148 const QString wfnFileName = input.at(0).toString();
149 const QString nuclearCriticalPointsFileName = input.at(1).toString();
150 const qint64 nucleusA = input.at(2).toInt();
151 const qint64 nucleusB = input.at(3).toInt();
152 const QVector3D x0y0z0(input.at(4).toReal(), input.at(5).toReal(),
153 input.at(6).toReal());
154
155 QTAIMWavefunction wfn;
156 wfn.loadFromBinaryFile(wfnFileName);
157
158 QList<QVector3D> nuclearCriticalPoints;
159 QFile nuclearCriticalPointsFile(nuclearCriticalPointsFileName);
160 if (!nuclearCriticalPointsFile.open(QIODevice::ReadOnly)) {
161 QMessageBox::critical(
162 nullptr, QObject::tr("Error"),
163 QObject::tr("Cannot read file %1.").arg(nuclearCriticalPointsFileName));
164 return {};
165 }
166 QDataStream nuclearCriticalPointsFileIn(&nuclearCriticalPointsFile);
167 nuclearCriticalPointsFileIn >> nuclearCriticalPoints;
168 nuclearCriticalPointsFile.close();
169
170 QList<QPair<QVector3D, qreal>> betaSpheres;
171 for (auto nuclearCriticalPoint : nuclearCriticalPoints) {
172 QPair<QVector3D, qreal> thisBetaSphere;
173 thisBetaSphere.first = nuclearCriticalPoint;
174 thisBetaSphere.second = 0.1;
175 betaSpheres.append(thisBetaSphere);
176 }
177
178 QTAIMWavefunctionEvaluator eval(wfn);
179
180 QList<QVector3D> ncpList;
181
182 // QTAIMODEIntegrator
183 // ode(eval,QTAIMODEIntegrator::CMBPMinusOneGradientInElectronDensity);
184 QTAIMLSODAIntegrator ode(
185 eval, QTAIMLSODAIntegrator::CMBPMinusOneGradientInElectronDensity);
186 QVector3D result = ode.integrate(x0y0z0);
187 Matrix<qreal, 3, 1> xyz(result.x(), result.y(), result.z());
188
189 if (!(QTAIMMathUtilities::signatureOfASymmetricThreeByThreeMatrix(
190 eval.hessianOfElectronDensity(xyz)) == -1) ||
191 (eval.gradientOfElectronDensity(xyz)).norm() > SMALL_GRADIENT_NORM) {
192 return { false, result.x(), result.y(), result.z() };
193 }
194
195 Matrix<qreal, 3, 3> eigenvectorsOfHessian =
196 QTAIMMathUtilities::eigenvectorsOfASymmetricThreeByThreeMatrix(
197 eval.hessianOfElectronDensity(xyz));
198 Matrix<qreal, 3, 1> highestEigenvectorOfHessian(eigenvectorsOfHessian(0, 2),
199 eigenvectorsOfHessian(1, 2),
200 eigenvectorsOfHessian(2, 2));
201
202 const qreal smallStep = 0.01;
203
204 QVector3D forwardStartingPoint(
205 result.x() + smallStep * highestEigenvectorOfHessian(0),
206 result.y() + smallStep * highestEigenvectorOfHessian(1),
207 result.z() + smallStep * highestEigenvectorOfHessian(2));
208
209 QVector3D backwardStartingPoint(
210 result.x() - smallStep * highestEigenvectorOfHessian(0),
211 result.y() - smallStep * highestEigenvectorOfHessian(1),
212 result.z() - smallStep * highestEigenvectorOfHessian(2));
213
214 // QTAIMODEIntegrator
215 // forwardODE(eval,QTAIMODEIntegrator::SteepestAscentPathInElectronDensity);
216 QTAIMLSODAIntegrator forwardODE(
217 eval, QTAIMLSODAIntegrator::SteepestAscentPathInElectronDensity);
218 forwardODE.setBetaSpheres(betaSpheres);
219
220 QVector3D forwardEndpoint = forwardODE.integrate(forwardStartingPoint);
221 QList<QVector3D> forwardPath = forwardODE.path();
222
223 // QTAIMODEIntegrator
224 // backwardODE(eval,QTAIMODEIntegrator::SteepestAscentPathInElectronDensity);
225 QTAIMLSODAIntegrator backwardODE(
226 eval, QTAIMLSODAIntegrator::SteepestAscentPathInElectronDensity);
227 backwardODE.setBetaSpheres(betaSpheres);
228
229 QVector3D backwardEndpoint = backwardODE.integrate(backwardStartingPoint);
230 QList<QVector3D> backwardPath = backwardODE.path();
231
232 // Find and store the forward and backward nucleus index for pair connection
233 // check
234 qreal minForwardDistance = HUGE_REAL_NUMBER;
235 qreal minBackwardDistance = HUGE_REAL_NUMBER;
236 qint64 backwardNucleusIndex = 0;
237 qint64 forwardNucleusIndex = 0;
238
239 // cache unchanged points
240 const Matrix<qreal, 3, 1> forwardPoint(
241 forwardEndpoint.x(), forwardEndpoint.y(), forwardEndpoint.z());
242 const Matrix<qreal, 3, 1> backwardPoint(
243 backwardEndpoint.x(), backwardEndpoint.y(), backwardEndpoint.z());
244
245 for (qint64 n = 0; n < wfn.numberOfNuclei(); ++n) {
246 const Matrix<qreal, 3, 1> wavePoint(wfn.xNuclearCoordinate(n),
247 wfn.yNuclearCoordinate(n),
248 wfn.zNuclearCoordinate(n));
249
250 qreal fDistance = QTAIMMathUtilities::distance(forwardPoint, wavePoint);
251 qreal bDistance = QTAIMMathUtilities::distance(backwardPoint, wavePoint);
252
253 if (fDistance < minForwardDistance) {
254 minForwardDistance = fDistance;
255 forwardNucleusIndex = n;
256 }
257 if (bDistance < minBackwardDistance) {
258 minBackwardDistance = bDistance;
259 backwardNucleusIndex = n;
260 }
261 }
262
263 QList<QVariant> value;
264 // if statement checks if bond path connects pair
265 if ((forwardNucleusIndex == nucleusA && backwardNucleusIndex == nucleusB) ||
266 (forwardNucleusIndex == nucleusB && backwardNucleusIndex == nucleusA)) {
267 value.append(true);
268 value.append(nucleusA);
269 value.append(nucleusB);
270 value.append(result.x());
271 value.append(result.y());
272 value.append(result.z());
273
274 const Matrix<qreal, 3, 1> xyz_(result.x(), result.y(), result.z());
275 value.append(eval.laplacianOfElectronDensity(xyz_));
276 value.append(QTAIMMathUtilities::ellipticityOfASymmetricThreeByThreeMatrix(
277 eval.hessianOfElectronDensity(xyz_)));
278
279 value.append(1 + forwardPath.length() + 1 + backwardPath.length() + 1);
280 value.append(forwardEndpoint.x());
281 for (qint64 i = forwardPath.length() - 1; i >= 0; --i) {
282 value.append(forwardPath.at(i).x());
283 }
284 value.append(result.x());
285 for (auto i : backwardPath) {
286 value.append(i.x());
287 }
288 value.append(backwardEndpoint.x());
289 value.append(forwardEndpoint.y());
290 for (qint64 i = forwardPath.length() - 1; i >= 0; --i) {
291 value.append(forwardPath.at(i).y());
292 }
293 value.append(result.y());
294 for (auto i : backwardPath) {
295 value.append(i.y());
296 }
297 value.append(backwardEndpoint.y());
298 value.append(forwardEndpoint.z());
299 for (qint64 i = forwardPath.length() - 1; i >= 0; --i) {
300 value.append(forwardPath.at(i).z());
301 }
302 value.append(result.z());
303 for (auto i : backwardPath) {
304 value.append(i.z());
305 }
306 value.append(backwardEndpoint.z());
307
308 } else {
309 value.append(false);
310 // for debugging
311 value.append(result.x());
312 value.append(result.y());
313 value.append(result.z());
314 }
315
316 return value;
317 }
318
319 QList<QVariant> QTAIMLocateElectronDensitySink(QList<QVariant> input)
320 {
321 /**
322 This function acts as a wrapper to consolidate code
323 The primary functionality only deviates from other functions in its
324 expected value for
325
326 QTAIMMathUtilities::signatureOfASymmetricThreeByThreeMatrix(eval.hessianOfElectronDensityLaplacian(**Integrated
327 Point**))
328
329 which is passed in as a template parameter to the helper function.
330
331 At the time of writing this value is -3.
332 */
333 return helper::QTAIMLocateElectronDensityHelper<-3>(input);
334 }
335
336 QList<QVariant> QTAIMLocateElectronDensitySource(QList<QVariant> input)
337 {
338 /**
339 This function acts as a wrapper to consolidate code
340 The primary functionality only deviates from other functions in its
341 expected value for
342
343 QTAIMMathUtilities::signatureOfASymmetricThreeByThreeMatrix(eval.hessianOfElectronDensityLaplacian(**Integrated
344 Point**))
345
346 which is passed in as a template parameter to the helper function.
347
348 At the time of writing this value is 3.
349 */
350 return helper::QTAIMLocateElectronDensityHelper<3>(input);
351 }
352
353 QTAIMCriticalPointLocator::QTAIMCriticalPointLocator(QTAIMWavefunction& wfn)
354 {
355 m_wfn = &wfn;
356
357 m_nuclearCriticalPoints.clear();
358 m_bondCriticalPoints.clear();
359 m_ringCriticalPoints.clear();
360 m_cageCriticalPoints.clear();
361
362 m_laplacianAtBondCriticalPoints.clear();
363 m_ellipticityAtBondCriticalPoints.clear();
364
365 m_bondPaths.clear();
366 m_bondedAtoms.clear();
367
368 m_electronDensitySources.clear();
369 m_electronDensitySinks.clear();
370 }
371
372 void QTAIMCriticalPointLocator::locateNuclearCriticalPoints()
373 {
374
375 QString tempFileName = QTAIMCriticalPointLocator::temporaryFileName();
376
377 QList<QList<QVariant>> inputList;
378
379 const qint64 numberOfNuclei = m_wfn->numberOfNuclei();
380
381 for (qint64 n = 0; n < numberOfNuclei; ++n) {
382 QList<QVariant> input;
383 input.append(tempFileName);
384 input.append(n);
385 input.append(m_wfn->xNuclearCoordinate(n));
386 input.append(m_wfn->yNuclearCoordinate(n));
387 input.append(m_wfn->zNuclearCoordinate(n));
388
389 inputList.append(input);
390 }
391
392 m_wfn->saveToBinaryFile(tempFileName);
393
394 QProgressDialog dialog;
395 dialog.setWindowTitle("QTAIM");
396 dialog.setLabelText(QString("Nuclear Critical Points Search"));
397
398 QFutureWatcher<QList<QVariant>> futureWatcher;
399 QObject::connect(&futureWatcher, SIGNAL(finished()), &dialog, SLOT(reset()));
400 QObject::connect(&dialog, SIGNAL(canceled()), &futureWatcher, SLOT(cancel()));
401 QObject::connect(&futureWatcher, SIGNAL(progressRangeChanged(int, int)),
402 &dialog, SLOT(setRange(int, int)));
403 QObject::connect(&futureWatcher, SIGNAL(progressValueChanged(int)), &dialog,
404 SLOT(setValue(int)));
405
406 QFuture<QList<QVariant>> future =
407 QtConcurrent::mapped(inputList, QTAIMLocateNuclearCriticalPoint);
408 futureWatcher.setFuture(future);
409 dialog.exec();
410 futureWatcher.waitForFinished();
411
412 QList<QList<QVariant>> results;
413 if (futureWatcher.future().isCanceled()) {
414 results.clear();
415 } else {
416 results = future.results();
417 }
418
419 QFile file;
420 file.remove(tempFileName);
421
422 for (const auto& n : results) {
423 if (n.at(0).toBool()) {
424 QVector3D result(n.at(1).toReal(), n.at(2).toReal(), n.at(3).toReal());
425
426 m_nuclearCriticalPoints.append(result);
427 }
428 }
429 }
430
431 void QTAIMCriticalPointLocator::locateBondCriticalPoints()
432 {
433
434 if (m_nuclearCriticalPoints.length() < 1) {
435 return;
436 }
437
438 const qint64 numberOfNuclei = m_wfn->numberOfNuclei();
439
440 if (numberOfNuclei < 2) {
441 return;
442 }
443
444 QString tempFileName = QTAIMCriticalPointLocator::temporaryFileName();
445
446 QString nuclearCriticalPointsFileName =
447 QTAIMCriticalPointLocator::temporaryFileName();
448 QFile nuclearCriticalPointsFile(nuclearCriticalPointsFileName);
449 if (!nuclearCriticalPointsFile.open(QIODevice::WriteOnly)) {
450 QMessageBox::critical(nullptr, QObject::tr("Error"),
451 QObject::tr("Failed to create a temporary file."));
452 return;
453 }
454 QDataStream nuclearCriticalPointsOut(&nuclearCriticalPointsFile);
455 nuclearCriticalPointsOut << m_nuclearCriticalPoints;
456 nuclearCriticalPointsFile.close();
457
458 QList<QList<QVariant>> inputList;
459
460 for (qint64 M = 0; M < numberOfNuclei - 1; ++M) {
461 for (qint64 N = M + 1; N < numberOfNuclei; ++N) {
462
463 const qreal distanceCutoff = 8.0;
464
465 Matrix<qreal, 3, 1> a;
466 Matrix<qreal, 3, 1> b;
467
468 a << m_wfn->xNuclearCoordinate(M), m_wfn->yNuclearCoordinate(M),
469 m_wfn->zNuclearCoordinate(M);
470 b << m_wfn->xNuclearCoordinate(N), m_wfn->yNuclearCoordinate(N),
471 m_wfn->zNuclearCoordinate(N);
472
473 if (QTAIMMathUtilities::distance(a, b) < distanceCutoff) {
474 QVector3D x0y0z0(
475 (m_wfn->xNuclearCoordinate(M) + m_wfn->xNuclearCoordinate(N)) / 2.0,
476 (m_wfn->yNuclearCoordinate(M) + m_wfn->yNuclearCoordinate(N)) / 2.0,
477 (m_wfn->zNuclearCoordinate(M) + m_wfn->zNuclearCoordinate(N)) / 2.0);
478
479 QList<QVariant> input;
480 input.append(tempFileName);
481 input.append(nuclearCriticalPointsFileName);
482 input.append(M);
483 input.append(N);
484 input.append(x0y0z0.x());
485 input.append(x0y0z0.y());
486 input.append(x0y0z0.z());
487
488 inputList.append(input);
489 }
490 } // end N
491 } // end M
492
493 m_wfn->saveToBinaryFile(tempFileName);
494
495 QProgressDialog dialog;
496 dialog.setWindowTitle("QTAIM");
497 dialog.setLabelText(QString("Bond Critical Points Search"));
498
499 QFutureWatcher<QList<QVariant>> futureWatcher;
500 QObject::connect(&futureWatcher, SIGNAL(finished()), &dialog, SLOT(reset()));
501 QObject::connect(&dialog, SIGNAL(canceled()), &futureWatcher, SLOT(cancel()));
502 QObject::connect(&futureWatcher, SIGNAL(progressRangeChanged(int, int)),
503 &dialog, SLOT(setRange(int, int)));
504 QObject::connect(&futureWatcher, SIGNAL(progressValueChanged(int)), &dialog,
505 SLOT(setValue(int)));
506
507 QFuture<QList<QVariant>> future =
508 QtConcurrent::mapped(inputList, QTAIMLocateBondCriticalPoint);
509 ;
510 futureWatcher.setFuture(future);
511 dialog.exec();
512 futureWatcher.waitForFinished();
513
514 QList<QList<QVariant>> results;
515 if (futureWatcher.future().isCanceled()) {
516 results.clear();
517 } else {
518 results = future.results();
519 }
520
521 QFile file;
522 file.remove(tempFileName);
523 file.remove(nuclearCriticalPointsFileName);
524
525 for (const auto& thisCriticalPoint : results) {
526 bool success = thisCriticalPoint.at(0).toBool();
527
528 if (success) {
529 QPair<qint64, qint64> bondedAtoms_;
530 bondedAtoms_.first = thisCriticalPoint.at(1).toInt();
531 bondedAtoms_.second = thisCriticalPoint.at(2).toInt();
532 m_bondedAtoms.append(bondedAtoms_);
533
534 QVector3D coordinates(thisCriticalPoint.at(3).toReal(),
535 thisCriticalPoint.at(4).toReal(),
536 thisCriticalPoint.at(5).toReal());
537
538 m_bondCriticalPoints.append(coordinates);
539
540 m_laplacianAtBondCriticalPoints.append(thisCriticalPoint.at(6).toReal());
541 m_ellipticityAtBondCriticalPoints.append(
542 thisCriticalPoint.at(7).toReal());
543 qint64 pathLength = thisCriticalPoint.at(8).toInt();
544
545 QList<QVector3D> bondPath;
546 for (qint64 j = 0; j < pathLength; ++j) {
547 QVector3D pathPoint(
548 thisCriticalPoint.at(9 + j).toReal(),
549 thisCriticalPoint.at(9 + j + pathLength).toReal(),
550 thisCriticalPoint.at(9 + j + 2 * pathLength).toReal());
551
552 bondPath.append(pathPoint);
553 }
554
555 m_bondPaths.append(bondPath);
556 }
557 }
558 }
559
560 void QTAIMCriticalPointLocator::locateElectronDensitySources()
561 {
562
563 QString tempFileName = QTAIMCriticalPointLocator::temporaryFileName();
564
565 QList<QList<QVariant>> inputList;
566
567 qreal xmin, ymin, zmin;
568 qreal xmax, ymax, zmax;
569 qreal xstep, ystep, zstep;
570
571 // TODO: if only we were using Eigen data structures...
572 QList<qreal> xNuclearCoordinates;
573 QList<qreal> yNuclearCoordinates;
574 QList<qreal> zNuclearCoordinates;
575
576 for (qint64 i = 0; i < m_wfn->numberOfNuclei(); ++i) {
577 xNuclearCoordinates.append(m_wfn->xNuclearCoordinate(i));
578 yNuclearCoordinates.append(m_wfn->yNuclearCoordinate(i));
579 zNuclearCoordinates.append(m_wfn->zNuclearCoordinate(i));
580 }
581
582 xmin = xNuclearCoordinates.first();
583 xmax = xNuclearCoordinates.first();
584 for (qint64 i = 1; i < m_wfn->numberOfNuclei(); ++i) {
585 if (xNuclearCoordinates.at(i) < xmin) {
586 xmin = xNuclearCoordinates.at(i);
587 }
588 if (xNuclearCoordinates.at(i) > xmax) {
589 xmax = xNuclearCoordinates.at(i);
590 }
591 }
592
593 ymin = yNuclearCoordinates.first();
594 ymax = yNuclearCoordinates.first();
595 for (qint64 i = 1; i < yNuclearCoordinates.count(); ++i) {
596 if (yNuclearCoordinates.at(i) < ymin) {
597 ymin = yNuclearCoordinates.at(i);
598 }
599 if (yNuclearCoordinates.at(i) > ymax) {
600 ymax = yNuclearCoordinates.at(i);
601 }
602 }
603
604 zmin = zNuclearCoordinates.first();
605 zmax = zNuclearCoordinates.first();
606 for (qint64 i = 1; i < zNuclearCoordinates.count(); ++i) {
607 if (zNuclearCoordinates.at(i) < zmin) {
608 zmin = zNuclearCoordinates.at(i);
609 }
610 if (zNuclearCoordinates.at(i) > zmax) {
611 zmax = zNuclearCoordinates.at(i);
612 }
613 }
614
615 xmin = -2.0 + xmin;
616 ymin = -2.0 + ymin;
617 zmin = -2.0 + zmin;
618
619 xmax = 2.0 + xmax;
620 ymax = 2.0 + ymax;
621 zmax = 2.0 + zmax;
622
623 xstep = ystep = zstep = 0.5;
624
625 for (qreal x = xmin; x < xmax + xstep; x = x + xstep) {
626 for (qreal y = ymin; y < ymax + ystep; y = y + ystep) {
627 for (qreal z = zmin; z < zmax + zstep; z = z + zstep) {
628 QList<QVariant> input;
629 input.append(tempFileName);
630 // input.append( n );
631 input.append(x);
632 input.append(y);
633 input.append(z);
634
635 inputList.append(input);
636 }
637 }
638 }
639
640 m_wfn->saveToBinaryFile(tempFileName);
641
642 QProgressDialog dialog;
643 dialog.setWindowTitle("QTAIM");
644 dialog.setLabelText(QString("Electron Density Sources Search"));
645
646 QFutureWatcher<QList<QVariant>> futureWatcher;
647 QObject::connect(&futureWatcher, SIGNAL(finished()), &dialog, SLOT(reset()));
648 QObject::connect(&dialog, SIGNAL(canceled()), &futureWatcher, SLOT(cancel()));
649 QObject::connect(&futureWatcher, SIGNAL(progressRangeChanged(int, int)),
650 &dialog, SLOT(setRange(int, int)));
651 QObject::connect(&futureWatcher, SIGNAL(progressValueChanged(int)), &dialog,
652 SLOT(setValue(int)));
653
654 QFuture<QList<QVariant>> future =
655 QtConcurrent::mapped(inputList, QTAIMLocateElectronDensitySource);
656 futureWatcher.setFuture(future);
657 dialog.exec();
658 futureWatcher.waitForFinished();
659
660 QList<QList<QVariant>> results;
661 if (futureWatcher.future().isCanceled()) {
662 results.clear();
663 } else {
664 results = future.results();
665 }
666
667 QFile file;
668 file.remove(tempFileName);
669
670 for (const auto& n : results) {
671 if (n.at(0).toBool()) {
672 qreal x = n.at(1).toReal();
673 qreal y = n.at(2).toReal();
674 qreal z = n.at(3).toReal();
675
676 if ((xmin < x && x < xmax) && (ymin < y && y < ymax) &&
677 (zmin < z && z < zmax)) {
678 QVector3D result(x, y, z);
679
680 qreal smallestDistance = HUGE_REAL_NUMBER;
681
682 for (auto m_electronDensitySource : m_electronDensitySources) {
683
684 Matrix<qreal, 3, 1> a(x, y, z);
685 Matrix<qreal, 3, 1> b(m_electronDensitySource.x(),
686 m_electronDensitySource.y(),
687 m_electronDensitySource.z());
688
689 qreal distance = QTAIMMathUtilities::distance(a, b);
690
691 if (distance < smallestDistance) {
692 smallestDistance = distance;
693 }
694 }
695
696 if (smallestDistance > 1.e-2) {
697 m_electronDensitySources.append(result);
698 }
699 }
700 }
701 }
702 // qDebug() << "SOURCES" << m_electronDensitySources;
703 }
704
705 void QTAIMCriticalPointLocator::locateElectronDensitySinks()
706 {
707
708 QString tempFileName = QTAIMCriticalPointLocator::temporaryFileName();
709
710 QList<QList<QVariant>> inputList;
711
712 qreal xmin, ymin, zmin;
713 qreal xmax, ymax, zmax;
714 qreal xstep, ystep, zstep;
715
716 // TODO: if only we were using Eigen data structures...
717 QList<qreal> xNuclearCoordinates;
718 QList<qreal> yNuclearCoordinates;
719 QList<qreal> zNuclearCoordinates;
720
721 for (qint64 i = 0; i < m_wfn->numberOfNuclei(); ++i) {
722 xNuclearCoordinates.append(m_wfn->xNuclearCoordinate(i));
723 yNuclearCoordinates.append(m_wfn->yNuclearCoordinate(i));
724 zNuclearCoordinates.append(m_wfn->zNuclearCoordinate(i));
725 }
726
727 xmin = xNuclearCoordinates.first();
728 xmax = xNuclearCoordinates.first();
729 for (qint64 i = 1; i < m_wfn->numberOfNuclei(); ++i) {
730 if (xNuclearCoordinates.at(i) < xmin) {
731 xmin = xNuclearCoordinates.at(i);
732 }
733 if (xNuclearCoordinates.at(i) > xmax) {
734 xmax = xNuclearCoordinates.at(i);
735 }
736 }
737
738 ymin = yNuclearCoordinates.first();
739 ymax = yNuclearCoordinates.first();
740 for (qint64 i = 1; i < yNuclearCoordinates.count(); ++i) {
741 if (yNuclearCoordinates.at(i) < ymin) {
742 ymin = yNuclearCoordinates.at(i);
743 }
744 if (yNuclearCoordinates.at(i) > ymax) {
745 ymax = yNuclearCoordinates.at(i);
746 }
747 }
748
749 zmin = zNuclearCoordinates.first();
750 zmax = zNuclearCoordinates.first();
751 for (qint64 i = 1; i < zNuclearCoordinates.count(); ++i) {
752 if (zNuclearCoordinates.at(i) < zmin) {
753 zmin = zNuclearCoordinates.at(i);
754 }
755 if (zNuclearCoordinates.at(i) > zmax) {
756 zmax = zNuclearCoordinates.at(i);
757 }
758 }
759
760 xmin = -2.0 + xmin;
761 ymin = -2.0 + ymin;
762 zmin = -2.0 + zmin;
763
764 xmax = 2.0 + xmax;
765 ymax = 2.0 + ymax;
766 zmax = 2.0 + zmax;
767
768 xstep = ystep = zstep = 0.5;
769
770 for (qreal x = xmin; x < xmax + xstep; x = x + xstep) {
771 for (qreal y = ymin; y < ymax + ystep; y = y + ystep) {
772 for (qreal z = zmin; z < zmax + zstep; z = z + zstep) {
773 QList<QVariant> input;
774 input.append(tempFileName);
775 // input.append( n );
776 input.append(x);
777 input.append(y);
778 input.append(z);
779
780 inputList.append(input);
781 }
782 }
783 }
784
785 m_wfn->saveToBinaryFile(tempFileName);
786
787 QProgressDialog dialog;
788 dialog.setWindowTitle("QTAIM");
789 dialog.setLabelText(QString("Electron Density Sinks Search"));
790
791 QFutureWatcher<QList<QVariant>> futureWatcher;
792 QObject::connect(&futureWatcher, SIGNAL(finished()), &dialog, SLOT(reset()));
793 QObject::connect(&dialog, SIGNAL(canceled()), &futureWatcher, SLOT(cancel()));
794 QObject::connect(&futureWatcher, SIGNAL(progressRangeChanged(int, int)),
795 &dialog, SLOT(setRange(int, int)));
796 QObject::connect(&futureWatcher, SIGNAL(progressValueChanged(int)), &dialog,
797 SLOT(setValue(int)));
798
799 QFuture<QList<QVariant>> future =
800 QtConcurrent::mapped(inputList, QTAIMLocateElectronDensitySink);
801 futureWatcher.setFuture(future);
802 dialog.exec();
803 futureWatcher.waitForFinished();
804
805 QList<QList<QVariant>> results;
806 if (futureWatcher.future().isCanceled()) {
807 results.clear();
808 } else {
809 results = future.results();
810 }
811
812 QFile file;
813 file.remove(tempFileName);
814
815 for (const auto& n : results) {
816 if (n.at(0).toBool()) {
817 qreal x = n.at(1).toReal();
818 qreal y = n.at(2).toReal();
819 qreal z = n.at(3).toReal();
820
821 if ((xmin < x && x < xmax) && (ymin < y && y < ymax) &&
822 (zmin < z && z < zmax)) {
823 QVector3D result(x, y, z);
824
825 qreal smallestDistance = HUGE_REAL_NUMBER;
826
827 for (auto m_electronDensitySink : m_electronDensitySinks) {
828
829 Matrix<qreal, 3, 1> a(x, y, z);
830 Matrix<qreal, 3, 1> b(m_electronDensitySink.x(),
831 m_electronDensitySink.y(),
832 m_electronDensitySink.z());
833
834 qreal distance = QTAIMMathUtilities::distance(a, b);
835
836 if (distance < smallestDistance) {
837 smallestDistance = distance;
838 }
839 }
840
841 if (smallestDistance > 1.e-2) {
842 m_electronDensitySinks.append(result);
843 }
844 }
845 }
846 }
847 // qDebug() << "SINKS" << m_electronDensitySinks;
848 }
849
850 QString QTAIMCriticalPointLocator::temporaryFileName()
851 {
852 QTemporaryFile temporaryFile;
853 temporaryFile.open();
854 QString tempFileName = temporaryFile.fileName();
855 temporaryFile.close();
856 temporaryFile.remove();
857
858 // wait for temporary file to be deleted
859 QDir dir;
860 do {
861 // Nothing
862 } while (dir.exists(tempFileName));
863
864 return tempFileName;
865 }
866
867 } // namespace Avogadro::QtPlugins