Debian Codegraph @

avogadro/qtplugins/openbabel/obcharges.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 "obcharges.h"
7 #include "obprocess.h"
8
9 #include <avogadro/core/array.h>
10 #include <avogadro/core/molecule.h>
11
12 #include <nlohmann/json.hpp>
13
14 #include <QtCore/QCoreApplication>
15 #include <QtCore/QDebug>
16 #include <QtCore/QTimer>
17
18 namespace Avogadro {
19
20 using Core::Array;
21 using Core::Molecule;
22
23 namespace QtPlugins {
24
25 class OBCharges::ProcessListener : public QObject
26 {
27 Q_OBJECT
28 public:
29 ProcessListener() : QObject(), m_finished(false) {}
30
31 bool waitForOutput(Array<double>& output, int msTimeout = 120000)
32 {
33 if (!wait(msTimeout))
34 return false;
35
36 // success!
37 output = m_output;
38 return true;
39 }
40
41 public slots:
42 void responseReceived(const Array<double> output)
43 {
44 m_finished = true;
45 m_output = output;
46 }
47
48 private:
49 bool wait(int msTimeout)
50 {
51 QTimer timer;
52 timer.start(msTimeout);
53
54 while (timer.isActive() && !m_finished)
55 qApp->processEvents(QEventLoop::AllEvents, 500);
56
57 return m_finished;
58 }
59
60 // OBProcess* m_process;
61 bool m_finished;
62 Array<double> m_output;
63 };
64
65 OBCharges::OBCharges(const std::string& id) : ChargeModel(), m_identifier(id)
66 {
67 // set the element mask based on our type / identifier
68 m_elements.reset();
69 if (id == "eqeq") {
70 // defined for 1-84
71 for (unsigned int i = 1; i <= 84; ++i) {
72 m_elements.set(i);
73 }
74 } else if (id == "eem") {
75 // H, Li, B, C, N, O, F, Na, Mg, Si, P, S, Cl
76 m_elements.set(1);
77 m_elements.set(3);
78 m_elements.set(5);
79 m_elements.set(6);
80 m_elements.set(7);
81 m_elements.set(8);
82 m_elements.set(9);
83 m_elements.set(11);
84 m_elements.set(12);
85 m_elements.set(14);
86 m_elements.set(15);
87 m_elements.set(16);
88 m_elements.set(17);
89 } else if (id == "eem2015ba") {
90 // H, C, N, O, F, P, S, Cl, Br, I
91 m_elements.set(1);
92 m_elements.set(6);
93 m_elements.set(7);
94 m_elements.set(8);
95 m_elements.set(9);
96 m_elements.set(15);
97 m_elements.set(16);
98 m_elements.set(17);
99 m_elements.set(35);
100 m_elements.set(53);
101 } else if (id == "gasteiger") {
102 // H, C, N, O, F, P, S, Cl, Br, I, Al
103 m_elements.set(1);
104 m_elements.set(6);
105 m_elements.set(7);
106 m_elements.set(8);
107 m_elements.set(9);
108 m_elements.set(13);
109 m_elements.set(15);
110 m_elements.set(16);
111 m_elements.set(17);
112 m_elements.set(35);
113 m_elements.set(53);
114 } else if (id == "mmff94") {
115 // H, C, N, O, F, Si, P, S, Cl, Br, and I
116 // ions - Fe, F, Cl, Br, Li, Na, K, Zn, Ca, Cu, Mg
117 m_elements.set(1);
118 m_elements.set(6);
119 m_elements.set(7);
120 m_elements.set(8);
121 m_elements.set(9);
122 m_elements.set(14);
123 m_elements.set(15);
124 m_elements.set(16);
125 m_elements.set(17);
126 m_elements.set(35);
127 m_elements.set(53);
128
129 // ions
130 m_elements.set(3);
131 m_elements.set(11);
132 m_elements.set(12);
133 m_elements.set(19);
134 m_elements.set(20);
135 m_elements.set(26);
136 m_elements.set(29);
137 m_elements.set(30);
138 }
139 }
140
141 OBCharges::~OBCharges() {}
142
143 std::string OBCharges::name() const
144 {
145 if (m_identifier == "eqeq")
146 return "EQEq";
147 else if (m_identifier == "eem")
148 return "EEM";
149 else if (m_identifier == "eem2015ba")
150 return "EEM 2015";
151 else if (m_identifier == "gasteiger")
152 return "Gasteiger";
153 else if (m_identifier == "mmff94")
154 return "MMFF94";
155 else
156 return "";
157 }
158
159 MatrixX OBCharges::partialCharges(const Core::Molecule& molecule) const
160 {
161 MatrixX charges(molecule.atomCount(), 1);
162
163 if (m_identifier.empty()) {
164 // no identifier, so we can't get the charges
165 return charges;
166 }
167
168 // check to see if we already have them in the molecule
169 charges = molecule.partialCharges(m_identifier);
170 // if the number of charges matches the number of atoms
171 // and there's a non-zero charge, then we're done
172 if (charges.rows() == static_cast<Eigen::Index>(molecule.atomCount())) {
173 for (unsigned int i = 0; i < charges.rows(); ++i) {
174 if (abs(charges(i, 0)) > 0.00001)
175 return charges;
176 }
177 }
178
179 // otherwise, we're going to run obprocess to get the charges
180 OBProcess process;
181 ProcessListener listener;
182 QObject::connect(&process, &OBProcess::chargesFinished, &listener,
183 &ProcessListener::responseReceived);
184
185 std::string outputString;
186 // todo - check for failure, append errors, etc.
187 m_cmlFormat.writeString(outputString, molecule);
188
189 process.calculateCharges(QByteArray(outputString.c_str()), "cml",
190 m_identifier);
191
192 Core::Array<double> output;
193 if (!listener.waitForOutput(output)) {
194 qDebug() << "Charges timed out.";
195 return charges;
196 }
197
198 // push the output into our charges array
199 for (unsigned int i = 0; i < output.size(); ++i) {
200 charges(i, 0) = output[i];
201 }
202
203 // workaround failed runs causing the code to freeze
204 if (abs(charges(0, 0)) < 0.00001)
205 charges(0, 0) = 0.0001;
206
207 // check the size
208 if (output.size() != molecule.atomCount()) {
209 qDebug() << "Charges size mismatch.";
210 return charges;
211 }
212 return charges;
213 }
214
215 MatrixX OBCharges::partialCharges(Core::Molecule& molecule) const
216 {
217 MatrixX charges =
218 partialCharges(static_cast<const Core::Molecule&>(molecule));
219 // cache the charges and allow them to show up in output
220 molecule.setPartialCharges(m_identifier, charges);
221 return charges;
222 }
223
224 } // namespace QtPlugins
225 } // namespace Avogadro
226
227 #include "obcharges.moc"