189{
190 if (
pot ==
nullptr ||
pot->session ==
nullptr || positions ==
nullptr ||
191 atomicNrs == nullptr || forces == nullptr || energy == nullptr ||
192 nAtoms <= 0) {
193 return 1;
194 }
195 try {
196 std::vector<double> R(positions, positions + 3 * nAtoms);
197 std::vector<double> cell(9, 0.0);
198 const bool have_box = box != nullptr;
199 if (have_box) std::copy(box, box + 9, cell.begin());
200 if (transform != nullptr) {
201 const int rc =
202 transform(transform_user, nAtoms, R.data(), cell.data());
203 if (rc != 0) return rc;
204 }
205
206 capnp::MallocMessageBuilder message;
207 auto input = message.initRoot<::ForceInput>();
208 auto pos = input.initPos(static_cast<unsigned>(3 * nAtoms));
209 for (long i = 0; i < 3 * nAtoms; ++i)
210 pos.set(static_cast<unsigned>(i), R[static_cast<size_t>(i)]);
211 auto z = input.initAtmnrs(
static_cast<unsigned>(nAtoms));
212 for (long i = 0; i < nAtoms; ++i)
213 z.set(
static_cast<unsigned>(i), atomicNrs[i]);
214 bool diagonal = true;
215 for (int i: {1, 2, 3, 5, 6, 7}) {
216 if (cell[static_cast<size_t>(i)] != 0.0) diagonal = false;
217 }
218 if (have_box && diagonal && cell[0] > 0.0 && cell[4] > 0.0 &&
219 cell[8] > 0.0) {
220 auto capnp_box = input.initBox(9);
221 for (int i = 0; i < 9; ++i)
222 capnp_box.set(i, cell[static_cast<size_t>(i)]);
223 }
224 input.setLengthUnit("angstrom");
225 input.setEnergyUnit("eV");
226
227 const kj::Array<capnp::word> words = capnp::messageToFlatArray(message);
228 const kj::ArrayPtr<const kj::byte> bytes = words.asBytes();
229 const size_t need =
pot->api.result_size(bytes.begin(), bytes.size());
230 if (need == 0) {
231 std::fprintf(stderr,
232 "rgpot_engine_force(lammps): ForceInput rejected\n");
233 return 2;
234 }
235
236
237 std::vector<capnp::word>
out((need +
sizeof(capnp::word) - 1) /
238 sizeof(capnp::word));
239 size_t wrote = 0;
240 const LammpcResult result =
pot->api.calculate(
241 pot->session, bytes.begin(), bytes.size(),
out.data(),
242 out.size() *
sizeof(capnp::word), &wrote);
243 if (result.ok == 0 || wrote == 0 || wrote % sizeof(capnp::word) != 0) {
244 std::fprintf(stderr, "rgpot_engine_force(lammps): %s\n",
245 result.message[0] != '\0'
246 ? result.message
247 : api_error(
pot->api,
"lammpc force failed"));
248 return 2;
249 }
250 const kj::ArrayPtr<const capnp::word> out_words(
251 out.data(), wrote /
sizeof(capnp::word));
252 capnp::FlatArrayMessageReader reader(out_words);
253 const auto decoded = reader.getRoot<::PotentialResult>();
254 const auto f = decoded.getForces();
255 if (f.size() != static_cast<unsigned>(3 * nAtoms)) {
256 std::fprintf(
257 stderr,
258 "rgpot_engine_force(lammps): force length %u, expected "
259 "%ld\n",
260 f.size(), 3 * nAtoms);
261 return 2;
262 }
263 *energy = decoded.getEnergy();
264 for (unsigned i = 0; i < f.size(); ++i)
265 forces[i] = f[i];
266 if (variance !=
nullptr) *
variance = 0.0;
267 return 0;
268 } catch (const std::exception& ex) {
269 std::fprintf(stderr, "rgpot_engine_force(lammps): %s\n", ex.what());
270 return 2;
271 } catch (...) {
272 std::fprintf(stderr, "rgpot_engine_force(lammps): unknown exception\n");
273 return 2;
274 }
275}
eon_relax_band_t eon_relax_surface_fn void eon_relax_outcome_t * out
double variance(const Weights &w, const Matrix &covariance)