Debian Codegraph @

avogadro/qtgui/gaussiansetconcurrent.cpp

raw
1 /******************************************************************************
2 This source file is part of the Avogadro project.
3 This source code is released under the 3-Clause BSD License, (see "LICENSE").
4 ******************************************************************************/
5
6 #include "gaussiansetconcurrent.h"
7
8 #include <avogadro/core/gaussianset.h>
9 #include <avogadro/core/gaussiansettools.h>
10 #include <avogadro/core/molecule.h>
11 #include <avogadro/core/mutex.h>
12
13 #include <avogadro/core/cube.h>
14
15 #include <QtConcurrent/QtConcurrentMap>
16
17 namespace Avogadro::QtGui {
18
19 using Core::BasisSet;
20 using Core::Cube;
21 using Core::GaussianSet;
22 using Core::GaussianSetTools;
23 using Core::Molecule;
24
25 template <typename Derived>
26 class BasisSetConcurrent
27 {
28 void setMolecule(Molecule* mol)
29 {
30 static_cast<Derived*>(this)->setMolecule(mol);
31 }
32 };
33
34 struct GaussianShell
35 {
36 GaussianSetTools* tools; // A pointer to the tools, can't write to member vars
37 Cube* tCube; // The target cube, used to initialise temp cubes too
38 unsigned int pos; // The index of the point to calculate the MO for
39 unsigned int state; // The MO number to calculate
40 };
41
42 GaussianSetConcurrent::GaussianSetConcurrent(QObject* p)
43 : QObject(p), m_gaussianShells(nullptr), m_set(nullptr), m_tools(nullptr)
44 {
45 // Watch for the future
46 connect(&m_watcher, SIGNAL(finished()), this, SLOT(calculationComplete()));
47 }
48
49 GaussianSetConcurrent::~GaussianSetConcurrent()
50 {
51 delete m_gaussianShells;
52 }
53
54 void GaussianSetConcurrent::setMolecule(Core::Molecule* mol)
55 {
56 if (!mol)
57 return;
58 m_set = dynamic_cast<GaussianSet*>(mol->basisSet());
59
60 delete m_tools;
61 m_tools = new GaussianSetTools(mol);
62 }
63
64 bool GaussianSetConcurrent::calculateMolecularOrbital(Core::Cube* cube,
65 unsigned int state,
66 bool beta)
67 {
68 // We can do some initial set up of the tools here to set electron type.
69 if (!beta)
70 m_tools->setElectronType(BasisSet::Alpha);
71 else
72 m_tools->setElectronType(BasisSet::Beta);
73
74 return setUpCalculation(cube, state, GaussianSetConcurrent::processOrbital);
75 }
76
77 bool GaussianSetConcurrent::calculateElectronDensity(Core::Cube* cube)
78 {
79 const MatrixX& matrix = m_set->densityMatrix();
80 if (matrix.rows() == 0 || matrix.cols() == 0) {
81 // we don't have a density matrix, so calculate one
82 m_set->generateDensityMatrix();
83 }
84
85 return setUpCalculation(cube, 0, GaussianSetConcurrent::processDensity);
86 }
87
88 bool GaussianSetConcurrent::calculateSpinDensity(Core::Cube* cube)
89 {
90 return setUpCalculation(cube, 0, GaussianSetConcurrent::processSpinDensity);
91 }
92
93 void GaussianSetConcurrent::calculationComplete()
94 {
95 // (*m_gaussianShells)[0].tCube->lock()->unlock();
96 delete m_gaussianShells;
97 m_gaussianShells = nullptr;
98 emit finished();
99 }
100
101 bool GaussianSetConcurrent::setUpCalculation(Core::Cube* cube,
102 unsigned int state,
103 void (*func)(GaussianShell&))
104 {
105 if (!m_set || !m_tools)
106 return false;
107
108 m_set->initCalculation();
109
110 // Set up the points we want to calculate the density at.
111 m_gaussianShells =
112 new QVector<GaussianShell>(static_cast<int>(cube->data()->size()));
113
114 for (int i = 0; i < m_gaussianShells->size(); ++i) {
115 (*m_gaussianShells)[i].tools = m_tools;
116 (*m_gaussianShells)[i].tCube = cube;
117 (*m_gaussianShells)[i].pos = i;
118 (*m_gaussianShells)[i].state = state;
119 }
120
121 // Lock the cube until we are done.
122 // cube->lock()->lock();
123
124 // The main part of the mapped reduced function...
125 m_future = QtConcurrent::map(*m_gaussianShells, func);
126 // Connect our watcher to our future
127 m_watcher.setFuture(m_future);
128
129 return true;
130 }
131
132 void GaussianSetConcurrent::processOrbital(GaussianShell& shell)
133 {
134 Vector3 pos = shell.tCube->position(shell.pos);
135 shell.tCube->setValue(
136 shell.pos, shell.tools->calculateMolecularOrbital(pos, shell.state));
137 }
138
139 void GaussianSetConcurrent::processDensity(GaussianShell& shell)
140 {
141 Vector3 pos = shell.tCube->position(shell.pos);
142 shell.tCube->setValue(shell.pos, shell.tools->calculateElectronDensity(pos));
143 }
144
145 void GaussianSetConcurrent::processSpinDensity(GaussianShell& shell)
146 {
147 Vector3 pos = shell.tCube->position(shell.pos);
148 shell.tCube->setValue(shell.pos, shell.tools->calculateSpinDensity(pos));
149 }
150 } // namespace Avogadro::QtGui