| 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 "slatersetconcurrent.h" |
| 7 |
|
| 8 |
#include <avogadro/core/molecule.h> |
| 9 |
#include <avogadro/core/slaterset.h> |
| 10 |
#include <avogadro/core/slatersettools.h> |
| 11 |
|
| 12 |
#include <avogadro/core/cube.h> |
| 13 |
#include <avogadro/core/mutex.h> |
| 14 |
|
| 15 |
#include <QtConcurrent/QtConcurrentMap> |
| 16 |
|
| 17 |
namespace Avogadro::QtGui { |
| 18 |
|
| 19 |
using Core::Cube; |
| 20 |
using Core::SlaterSet; |
| 21 |
using Core::SlaterSetTools; |
| 22 |
|
| 23 |
struct SlaterShell |
| 24 |
{ |
| 25 |
SlaterSetTools* tools; // A pointer to the tools, cannot write to member vars |
| 26 |
Cube* tCube; // The target cube, used to initialise temp cubes too |
| 27 |
unsigned int pos; // The index of the point to calculate the MO for |
| 28 |
unsigned int state; // The MO number to calculate |
| 29 |
}; |
| 30 |
|
| 31 |
SlaterSetConcurrent::SlaterSetConcurrent(QObject* p) |
| 32 |
: QObject(p), m_shells(nullptr), m_set(nullptr), m_tools(nullptr) |
| 33 |
{ |
| 34 |
// Watch for the future |
| 35 |
connect(&m_watcher, SIGNAL(finished()), this, SLOT(calculationComplete())); |
| 36 |
} |
| 37 |
|
| 38 |
SlaterSetConcurrent::~SlaterSetConcurrent() |
| 39 |
{ |
| 40 |
delete m_shells; |
| 41 |
} |
| 42 |
|
| 43 |
void SlaterSetConcurrent::setMolecule(Core::Molecule* mol) |
| 44 |
{ |
| 45 |
if (!mol) |
| 46 |
return; |
| 47 |
m_set = dynamic_cast<SlaterSet*>(mol->basisSet()); |
| 48 |
|
| 49 |
delete m_tools; |
| 50 |
m_tools = new SlaterSetTools(mol); |
| 51 |
} |
| 52 |
|
| 53 |
bool SlaterSetConcurrent::calculateMolecularOrbital(Core::Cube* cube, |
| 54 |
unsigned int state) |
| 55 |
{ |
| 56 |
return setUpCalculation(cube, state, SlaterSetConcurrent::processOrbital); |
| 57 |
} |
| 58 |
|
| 59 |
bool SlaterSetConcurrent::calculateElectronDensity(Core::Cube* cube) |
| 60 |
{ |
| 61 |
return setUpCalculation(cube, 0, SlaterSetConcurrent::processDensity); |
| 62 |
} |
| 63 |
|
| 64 |
bool SlaterSetConcurrent::calculateSpinDensity(Core::Cube* cube) |
| 65 |
{ |
| 66 |
return setUpCalculation(cube, 0, SlaterSetConcurrent::processSpinDensity); |
| 67 |
} |
| 68 |
|
| 69 |
void SlaterSetConcurrent::calculationComplete() |
| 70 |
{ |
| 71 |
// (*m_shells)[0].tCube->lock()->unlock(); |
| 72 |
delete m_shells; |
| 73 |
m_shells = nullptr; |
| 74 |
emit finished(); |
| 75 |
} |
| 76 |
|
| 77 |
bool SlaterSetConcurrent::setUpCalculation(Core::Cube* cube, unsigned int state, |
| 78 |
void (*func)(SlaterShell&)) |
| 79 |
{ |
| 80 |
if (!m_set || !m_tools) |
| 81 |
return false; |
| 82 |
|
| 83 |
m_set->initCalculation(); |
| 84 |
|
| 85 |
// Set up the points we want to calculate the density at. |
| 86 |
m_shells = new QVector<SlaterShell>(static_cast<int>(cube->data()->size())); |
| 87 |
|
| 88 |
for (int i = 0; i < m_shells->size(); ++i) { |
| 89 |
(*m_shells)[i].tools = m_tools; |
| 90 |
(*m_shells)[i].tCube = cube; |
| 91 |
(*m_shells)[i].pos = i; |
| 92 |
(*m_shells)[i].state = state; |
| 93 |
} |
| 94 |
|
| 95 |
// Lock the cube until we are done. |
| 96 |
// cube->lock()->lock(); |
| 97 |
|
| 98 |
// The main part of the mapped reduced function... |
| 99 |
m_future = QtConcurrent::map(*m_shells, func); |
| 100 |
// Connect our watcher to our future |
| 101 |
m_watcher.setFuture(m_future); |
| 102 |
|
| 103 |
return true; |
| 104 |
} |
| 105 |
|
| 106 |
void SlaterSetConcurrent::processOrbital(SlaterShell& shell) |
| 107 |
{ |
| 108 |
Vector3 pos = shell.tCube->position(shell.pos); |
| 109 |
shell.tCube->setValue( |
| 110 |
shell.pos, shell.tools->calculateMolecularOrbital(pos, shell.state)); |
| 111 |
} |
| 112 |
|
| 113 |
void SlaterSetConcurrent::processDensity(SlaterShell& shell) |
| 114 |
{ |
| 115 |
Vector3 pos = shell.tCube->position(shell.pos); |
| 116 |
shell.tCube->setValue(shell.pos, shell.tools->calculateElectronDensity(pos)); |
| 117 |
} |
| 118 |
|
| 119 |
void SlaterSetConcurrent::processSpinDensity(SlaterShell& shell) |
| 120 |
{ |
| 121 |
Vector3 pos = shell.tCube->position(shell.pos); |
| 122 |
shell.tCube->setValue(shell.pos, shell.tools->calculateSpinDensity(pos)); |
| 123 |
} |
| 124 |
} // namespace Avogadro::QtGui |