| 1 |
/****************************************************************************** |
| 2 |
|
| 3 |
This source file is part of the Avogadro project. |
| 4 |
|
| 5 |
Copyright (C) 2010 Eric C. Brown |
| 6 |
|
| 7 |
This source code is released under the GPL v3 or later (the "License"). |
| 8 |
|
| 9 |
You should have received a copy of the GNU General Public License along with |
| 10 |
this program; if not, write to the Free Software Foundation, Inc., 51 Franklin |
| 11 |
Street, Fifth Floor, Boston, MA 02110-1301, USA. |
| 12 |
|
| 13 |
Unless required by applicable law or agreed to in writing, software |
| 14 |
distributed under the License is distributed on an "AS IS" BASIS, |
| 15 |
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 16 |
See the License for the specific language governing permissions and |
| 17 |
limitations under the License. |
| 18 |
|
| 19 |
******************************************************************************/ |
| 20 |
|
| 21 |
/* Based on */ |
| 22 |
|
| 23 |
/* http://ab-initio.mit.edu/cubature/cubature-20101018.tgz */ |
| 24 |
/* Adaptive multidimensional integration of a vector of integrands. |
| 25 |
* |
| 26 |
* Copyright (c) 2005-2010 Steven G. Johnson |
| 27 |
* |
| 28 |
* Portions (see comments) based on HIntLib (also distributed under |
| 29 |
* the GNU GPL, v2 or later), copyright (c) 2002-2005 Rudolf Schuerer. |
| 30 |
* (http://www.cosy.sbg.ac.at/~rschuer/hintlib/) |
| 31 |
* |
| 32 |
* Portions (see comments) based on GNU GSL (also distributed under |
| 33 |
* the GNU GPL, v2 or later), copyright (c) 1996-2000 Brian Gough. |
| 34 |
* (http://www.gnu.org/software/gsl/) |
| 35 |
* |
| 36 |
* This program is free software; you can redistribute it and/or modify |
| 37 |
* it under the terms of the GNU General Public License as published by |
| 38 |
* the Free Software Foundation; either version 2 of the License, or |
| 39 |
* (at your option) any later version. |
| 40 |
* |
| 41 |
* This program is distributed in the hope that it will be useful, |
| 42 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 43 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 44 |
* GNU General Public License for more details. |
| 45 |
* |
| 46 |
* You should have received a copy of the GNU General Public License |
| 47 |
* along with this program; if not, write to the Free Software |
| 48 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA |
| 49 |
* |
| 50 |
*/ |
| 51 |
|
| 52 |
#include "qtaimcubature.h" |
| 53 |
|
| 54 |
#include "qtaimcriticalpointlocator.h" |
| 55 |
#include "qtaimlsodaintegrator.h" |
| 56 |
#include "qtaimmathutilities.h" |
| 57 |
|
| 58 |
#include <QDataStream> |
| 59 |
#include <QDebug> |
| 60 |
#include <QDir> |
| 61 |
#include <QFile> |
| 62 |
#include <QTemporaryFile> |
| 63 |
#include <QTextStream> |
| 64 |
|
| 65 |
#include <QPair> |
| 66 |
#include <QVariantList> |
| 67 |
#include <QVector3D> |
| 68 |
|
| 69 |
#include <QFuture> |
| 70 |
#include <QFutureWatcher> |
| 71 |
#include <QList> |
| 72 |
#include <QProgressDialog> |
| 73 |
#include <QVariant> |
| 74 |
#include <QtConcurrent/QtConcurrentMap> |
| 75 |
|
| 76 |
#include <cfloat> |
| 77 |
#include <climits> |
| 78 |
#include <cmath> |
| 79 |
#include <cstdio> |
| 80 |
#include <cstdlib> |
| 81 |
#include <cstring> |
| 82 |
|
| 83 |
/* Adaptive multidimensional integration on hypercubes (or, really, |
| 84 |
hyper-rectangles) using cubature rules. |
| 85 |
|
| 86 |
A cubature rule takes a function and a hypercube and evaluates |
| 87 |
the function at a small number of points, returning an estimate |
| 88 |
of the integral as well as an estimate of the error, and also |
| 89 |
a suggested dimension of the hypercube to subdivide. |
| 90 |
|
| 91 |
Given such a rule, the adaptive integration is simple: |
| 92 |
|
| 93 |
1) Evaluate the cubature rule on the hypercube(s). |
| 94 |
Stop if converged. |
| 95 |
|
| 96 |
2) Pick the hypercube with the largest estimated error, |
| 97 |
and divide it in two along the suggested dimension. |
| 98 |
|
| 99 |
3) Goto (1). |
| 100 |
|
| 101 |
The basic algorithm is based on the adaptive cubature described in |
| 102 |
|
| 103 |
A. C. Genz and A. A. Malik, "An adaptive algorithm for numeric |
| 104 |
integration over an N-dimensional rectangular region," |
| 105 |
J. Comput. Appl. Math. 6 (4), 295-302 (1980). |
| 106 |
|
| 107 |
and subsequently extended to integrating a vector of integrands in |
| 108 |
|
| 109 |
J. Berntsen, T. O. Espelid, and A. Genz, "An adaptive algorithm |
| 110 |
for the approximate calculation of multiple integrals," |
| 111 |
ACM Trans. Math. Soft. 17 (4), 437-451 (1991). |
| 112 |
|
| 113 |
Note, however, that we do not use any of code from the above authors |
| 114 |
(in part because their code is Fortran 77, but mostly because it is |
| 115 |
under the restrictive ACM copyright license). I did make use of some |
| 116 |
GPL code from Rudolf Schuerer's HIntLib and from the GNU Scientific |
| 117 |
Library as listed in the copyright notice above, on the other hand. |
| 118 |
|
| 119 |
I am also grateful to Dmitry Turbiner <[email protected]>, who |
| 120 |
implemented an initial prototype of the "vectorized" functionality |
| 121 |
for evaluating multiple points in a single call (as opposed to |
| 122 |
multiple functions in a single call). (Although Dmitry implemented |
| 123 |
a working version, I ended up re-implementing this feature from |
| 124 |
scratch as part of a larger code-cleanup, and in order to have |
| 125 |
a single code path for the vectorized and non-vectorized APIs. I |
| 126 |
subsequently implemented the algorithm by Gladwell to extract |
| 127 |
even more parallelism by evaluating many hypercubes at once.) |
| 128 |
|
| 129 |
TODO: |
| 130 |
|
| 131 |
* Putting these routines into the GNU GSL library would be nice. |
| 132 |
|
| 133 |
* A Python interface would be nice. (Also a Matlab interface, |
| 134 |
a GNU Octave interface, ...) |
| 135 |
|
| 136 |
* For high-dimensional integrals, it would be nice to implement |
| 137 |
a sparse-grid cubature scheme using Clenshaw-Curtis quadrature. |
| 138 |
Currently, for dimensions > 7 or so, quasi Monte Carlo methods win. |
| 139 |
|
| 140 |
* Berntsen et. al also describe a "two-level" error estimation scheme |
| 141 |
that they claim makes the algorithm more robust. It might be |
| 142 |
nice to implement this, at least as an option (although I seem |
| 143 |
to remember trying it once and it made the number of evaluations |
| 144 |
substantially worse for my test integrands). |
| 145 |
|
| 146 |
*/ |
| 147 |
|
| 148 |
/* USAGE: Call adapt_integrate with your function as described in cubature.h. |
| 149 |
|
| 150 |
To compile a test program, compile cubature.c with |
| 151 |
-DTEST_INTEGRATOR as described at the end. */ |
| 152 |
|
| 153 |
using namespace Avogadro::QtPlugins; |
| 154 |
|
| 155 |
/* error return codes */ |
| 156 |
#define SUCCESS 0 |
| 157 |
#define FAILURE 1 |
| 158 |
|
| 159 |
/***************************************************************************/ |
| 160 |
/* Basic datatypes */ |
| 161 |
|
| 162 |
using esterr = struct |
| 163 |
{ |
| 164 |
double val, err; |
| 165 |
}; |
| 166 |
|
| 167 |
static double relError(esterr ee) |
| 168 |
{ |
| 169 |
return (ee.val == 0.0 ? HUGE_VAL : fabs(ee.err / ee.val)); |
| 170 |
} |
| 171 |
|
| 172 |
static double errMax(unsigned int fdim, const esterr* ee) |
| 173 |
{ |
| 174 |
double errmax = 0; |
| 175 |
unsigned int k; |
| 176 |
for (k = 0; k < fdim; ++k) |
| 177 |
if (ee[k].err > errmax) |
| 178 |
errmax = ee[k].err; |
| 179 |
return errmax; |
| 180 |
} |
| 181 |
|
| 182 |
using hypercube = struct |
| 183 |
{ |
| 184 |
unsigned int dim; |
| 185 |
double* data; /* length 2*dim = center followed by half-widths */ |
| 186 |
double vol; /* cache volume = product of widths */ |
| 187 |
}; |
| 188 |
|
| 189 |
static double compute_vol(const hypercube* h) |
| 190 |
{ |
| 191 |
unsigned int i; |
| 192 |
double vol = 1; |
| 193 |
for (i = 0; i < h->dim; ++i) |
| 194 |
vol *= 2 * h->data[i + h->dim]; |
| 195 |
return vol; |
| 196 |
} |
| 197 |
|
| 198 |
static hypercube make_hypercube(unsigned int dim, const double* center, |
| 199 |
const double* halfwidth) |
| 200 |
{ |
| 201 |
unsigned int i; |
| 202 |
hypercube h; |
| 203 |
h.dim = dim; |
| 204 |
h.data = (double*)malloc(sizeof(double) * dim * 2); |
| 205 |
h.vol = 0; |
| 206 |
if (h.data) { |
| 207 |
for (i = 0; i < dim; ++i) { |
| 208 |
h.data[i] = center[i]; |
| 209 |
h.data[i + dim] = halfwidth[i]; |
| 210 |
} |
| 211 |
h.vol = compute_vol(&h); |
| 212 |
} |
| 213 |
return h; |
| 214 |
} |
| 215 |
|
| 216 |
static hypercube make_hypercube_range(unsigned int dim, const double* xmin, |
| 217 |
const double* xmax) |
| 218 |
{ |
| 219 |
hypercube h = make_hypercube(dim, xmin, xmax); |
| 220 |
unsigned int i; |
| 221 |
if (h.data) { |
| 222 |
for (i = 0; i < dim; ++i) { |
| 223 |
h.data[i] = 0.5 * (xmin[i] + xmax[i]); |
| 224 |
h.data[i + dim] = 0.5 * (xmax[i] - xmin[i]); |
| 225 |
} |
| 226 |
h.vol = compute_vol(&h); |
| 227 |
} |
| 228 |
return h; |
| 229 |
} |
| 230 |
|
| 231 |
static void destroy_hypercube(hypercube* h) |
| 232 |
{ |
| 233 |
free(h->data); |
| 234 |
h->dim = 0; |
| 235 |
} |
| 236 |
|
| 237 |
using region = struct |
| 238 |
{ |
| 239 |
hypercube h; |
| 240 |
unsigned int splitDim; |
| 241 |
unsigned int fdim; /* dimensionality of vector integrand */ |
| 242 |
esterr* ee; /* array of length fdim */ |
| 243 |
double errmax; /* max ee[k].err */ |
| 244 |
}; |
| 245 |
|
| 246 |
static region make_region(const hypercube* h, unsigned int fdim) |
| 247 |
{ |
| 248 |
region R; |
| 249 |
R.h = make_hypercube(h->dim, h->data, h->data + h->dim); |
| 250 |
R.splitDim = 0; |
| 251 |
R.fdim = fdim; |
| 252 |
R.ee = R.h.data ? (esterr*)malloc(sizeof(esterr) * fdim) : nullptr; |
| 253 |
return R; |
| 254 |
} |
| 255 |
|
| 256 |
static void destroy_region(region* R) |
| 257 |
{ |
| 258 |
destroy_hypercube(&R->h); |
| 259 |
free(R->ee); |
| 260 |
R->ee = nullptr; |
| 261 |
} |
| 262 |
|
| 263 |
static int cut_region(region* R, region* R2) |
| 264 |
{ |
| 265 |
unsigned int d = R->splitDim, dim = R->h.dim; |
| 266 |
*R2 = *R; |
| 267 |
R->h.data[d + dim] *= 0.5; |
| 268 |
R->h.vol *= 0.5; |
| 269 |
R2->h = make_hypercube(dim, R->h.data, R->h.data + dim); |
| 270 |
if (!R2->h.data) |
| 271 |
return FAILURE; |
| 272 |
R->h.data[d] -= R->h.data[d + dim]; |
| 273 |
R2->h.data[d] += R->h.data[d + dim]; |
| 274 |
R2->ee = (esterr*)malloc(sizeof(esterr) * R2->fdim); |
| 275 |
return R2->ee == nullptr; |
| 276 |
} |
| 277 |
|
| 278 |
struct rule_s; /* forward declaration */ |
| 279 |
|
| 280 |
using evalError_func = int (*)(struct rule_s*, unsigned int, integrand_v, void*, |
| 281 |
unsigned int, region*); |
| 282 |
using destroy_func = void (*)(struct rule_s*); |
| 283 |
|
| 284 |
using rule = struct rule_s |
| 285 |
{ |
| 286 |
unsigned int dim, fdim; /* the dimensionality & number of functions */ |
| 287 |
unsigned int num_points; /* number of evaluation points */ |
| 288 |
unsigned int num_regions; /* max number of regions evaluated at once */ |
| 289 |
double* pts; /* points to eval: num_regions * num_points * dim */ |
| 290 |
double* vals; /* num_regions * num_points * fdim */ |
| 291 |
evalError_func evalError; |
| 292 |
destroy_func destroy; |
| 293 |
}; |
| 294 |
|
| 295 |
static void destroy_rule(rule* r) |
| 296 |
{ |
| 297 |
if (r) { |
| 298 |
if (r->destroy) |
| 299 |
r->destroy(r); |
| 300 |
free(r->pts); |
| 301 |
free(r); |
| 302 |
} |
| 303 |
} |
| 304 |
|
| 305 |
static int alloc_rule_pts(rule* r, unsigned int num_regions) |
| 306 |
{ |
| 307 |
if (num_regions > r->num_regions) { |
| 308 |
free(r->pts); |
| 309 |
r->pts = r->vals = nullptr; |
| 310 |
r->num_regions = 0; |
| 311 |
num_regions *= 2; /* allocate extra so that |
| 312 |
repeatedly calling alloc_rule_pts with |
| 313 |
growing num_regions only needs |
| 314 |
a logarithmic number of allocations */ |
| 315 |
r->pts = (double*)malloc( |
| 316 |
sizeof(double) * (num_regions * r->num_points * (r->dim + r->fdim))); |
| 317 |
if (r->fdim + r->dim > 0 && !r->pts) |
| 318 |
return FAILURE; |
| 319 |
r->vals = r->pts + num_regions * r->num_points * r->dim; |
| 320 |
r->num_regions = num_regions; |
| 321 |
} |
| 322 |
return SUCCESS; |
| 323 |
} |
| 324 |
|
| 325 |
static rule* make_rule(size_t sz, /* >= sizeof(rule) */ |
| 326 |
unsigned int dim, unsigned int fdim, |
| 327 |
unsigned int num_points, evalError_func evalError, |
| 328 |
destroy_func destroy) |
| 329 |
{ |
| 330 |
rule* r; |
| 331 |
|
| 332 |
if (sz < sizeof(rule)) |
| 333 |
return nullptr; |
| 334 |
r = (rule*)malloc(sz); |
| 335 |
if (!r) |
| 336 |
return nullptr; |
| 337 |
r->pts = r->vals = nullptr; |
| 338 |
r->num_regions = 0; |
| 339 |
r->dim = dim; |
| 340 |
r->fdim = fdim; |
| 341 |
r->num_points = num_points; |
| 342 |
r->evalError = evalError; |
| 343 |
r->destroy = destroy; |
| 344 |
return r; |
| 345 |
} |
| 346 |
|
| 347 |
/* note: all regions must have same fdim */ |
| 348 |
static int eval_regions(unsigned int nR, region* R, integrand_v f, void* fdata, |
| 349 |
rule* r) |
| 350 |
{ |
| 351 |
unsigned int iR; |
| 352 |
if (nR == 0) |
| 353 |
return SUCCESS; /* nothing to evaluate */ |
| 354 |
if (r->evalError(r, R->fdim, f, fdata, nR, R)) |
| 355 |
return FAILURE; |
| 356 |
for (iR = 0; iR < nR; ++iR) |
| 357 |
R[iR].errmax = errMax(R->fdim, R[iR].ee); |
| 358 |
return SUCCESS; |
| 359 |
} |
| 360 |
|
| 361 |
/***************************************************************************/ |
| 362 |
/* Functions to loop over points in a hypercube. */ |
| 363 |
|
| 364 |
/* Based on orbitrule.cpp in HIntLib-0.0.10 */ |
| 365 |
|
| 366 |
/* ls0 returns the least-significant 0 bit of n (e.g. it returns |
| 367 |
0 if the LSB is 0, it returns 1 if the 2 LSBs are 01, etcetera). */ |
| 368 |
|
| 369 |
static unsigned int ls0(unsigned int n) |
| 370 |
{ |
| 371 |
#if defined(__GNUC__) && \ |
| 372 |
((__GNUC__ == 3 && __GNUC_MINOR__ >= 4) || __GNUC__ > 3) |
| 373 |
return __builtin_ctz(~n); /* gcc builtin for version >= 3.4 */ |
| 374 |
#else |
| 375 |
const unsigned int bits[256] = { |
| 376 |
0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, |
| 377 |
0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, |
| 378 |
0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6, 0, 1, 0, 2, 0, 1, 0, 3, |
| 379 |
0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, |
| 380 |
0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, |
| 381 |
0, 1, 0, 2, 0, 1, 0, 7, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, |
| 382 |
0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, |
| 383 |
0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 6, |
| 384 |
0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, 0, 1, 0, 2, 0, 1, 0, 3, |
| 385 |
0, 1, 0, 2, 0, 1, 0, 5, 0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 4, |
| 386 |
0, 1, 0, 2, 0, 1, 0, 3, 0, 1, 0, 2, 0, 1, 0, 8, |
| 387 |
}; |
| 388 |
unsigned int bit = 0; |
| 389 |
while ((n & 0xff) == 0xff) { |
| 390 |
n >>= 8; |
| 391 |
bit += 8; |
| 392 |
} |
| 393 |
return bit + bits[n & 0xff]; |
| 394 |
#endif |
| 395 |
} |
| 396 |
|
| 397 |
/** |
| 398 |
* Evaluate the integration points for all 2^n points (+/-r,...+/-r) |
| 399 |
* |
| 400 |
* A Gray-code ordering is used to minimize the number of coordinate updates |
| 401 |
* in p, although this doesn't matter as much now that we are saving all pts. |
| 402 |
*/ |
| 403 |
static void evalR_Rfs(double* pts, unsigned int dim, double* p, const double* c, |
| 404 |
const double* r) |
| 405 |
{ |
| 406 |
unsigned int i; |
| 407 |
unsigned int signs = 0; /* 0/1 bit = +/- for corresponding element of r[] */ |
| 408 |
|
| 409 |
/* We start with the point where r is ADDed in every coordinate |
| 410 |
(this implies signs=0). */ |
| 411 |
for (i = 0; i < dim; ++i) |
| 412 |
p[i] = c[i] + r[i]; |
| 413 |
|
| 414 |
/* Loop through the points in Gray-code ordering */ |
| 415 |
for (i = 0;; ++i) { |
| 416 |
unsigned int mask, d; |
| 417 |
|
| 418 |
memcpy(pts, p, sizeof(double) * dim); |
| 419 |
pts += dim; |
| 420 |
|
| 421 |
d = ls0(i); /* which coordinate to flip */ |
| 422 |
if (d >= dim) |
| 423 |
break; |
| 424 |
|
| 425 |
/* flip the d-th bit and add/subtract r[d] */ |
| 426 |
mask = 1U << d; |
| 427 |
signs ^= mask; |
| 428 |
p[d] = (signs & mask) ? c[d] - r[d] : c[d] + r[d]; |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
static void evalRR0_0fs(double* pts, unsigned int dim, double* p, |
| 433 |
const double* c, const double* r) |
| 434 |
{ |
| 435 |
unsigned int i, j; |
| 436 |
|
| 437 |
for (i = 0; i < dim - 1; ++i) { |
| 438 |
p[i] = c[i] - r[i]; |
| 439 |
for (j = i + 1; j < dim; ++j) { |
| 440 |
p[j] = c[j] - r[j]; |
| 441 |
memcpy(pts, p, sizeof(double) * dim); |
| 442 |
pts += dim; |
| 443 |
p[i] = c[i] + r[i]; |
| 444 |
memcpy(pts, p, sizeof(double) * dim); |
| 445 |
pts += dim; |
| 446 |
p[j] = c[j] + r[j]; |
| 447 |
memcpy(pts, p, sizeof(double) * dim); |
| 448 |
pts += dim; |
| 449 |
p[i] = c[i] - r[i]; |
| 450 |
memcpy(pts, p, sizeof(double) * dim); |
| 451 |
pts += dim; |
| 452 |
|
| 453 |
p[j] = c[j]; /* Done with j -> Restore p[j] */ |
| 454 |
} |
| 455 |
p[i] = c[i]; /* Done with i -> Restore p[i] */ |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
static void evalR0_0fs4d(double* pts, unsigned int dim, double* p, |
| 460 |
const double* c, const double* r1, const double* r2) |
| 461 |
{ |
| 462 |
unsigned int i; |
| 463 |
|
| 464 |
memcpy(pts, p, sizeof(double) * dim); |
| 465 |
pts += dim; |
| 466 |
|
| 467 |
for (i = 0; i < dim; i++) { |
| 468 |
p[i] = c[i] - r1[i]; |
| 469 |
memcpy(pts, p, sizeof(double) * dim); |
| 470 |
pts += dim; |
| 471 |
|
| 472 |
p[i] = c[i] + r1[i]; |
| 473 |
memcpy(pts, p, sizeof(double) * dim); |
| 474 |
pts += dim; |
| 475 |
|
| 476 |
p[i] = c[i] - r2[i]; |
| 477 |
memcpy(pts, p, sizeof(double) * dim); |
| 478 |
pts += dim; |
| 479 |
|
| 480 |
p[i] = c[i] + r2[i]; |
| 481 |
memcpy(pts, p, sizeof(double) * dim); |
| 482 |
pts += dim; |
| 483 |
|
| 484 |
p[i] = c[i]; |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
#define num0_0(dim) (1U) |
| 489 |
#define numR0_0fs(dim) (2 * (dim)) |
| 490 |
#define numRR0_0fs(dim) (2 * (dim) * (dim - 1)) |
| 491 |
#define numR_Rfs(dim) (1U << (dim)) |
| 492 |
|
| 493 |
/***************************************************************************/ |
| 494 |
/* Based on rule75genzmalik.cpp in HIntLib-0.0.10: An embedded |
| 495 |
cubature rule of degree 7 (embedded rule degree 5) due to A. C. Genz |
| 496 |
and A. A. Malik. See: |
| 497 |
|
| 498 |
A. C. Genz and A. A. Malik, "An imbedded [sic] family of fully |
| 499 |
symmetric numerical integration rules," SIAM |
| 500 |
J. Numer. Anal. 20 (3), 580-588 (1983). |
| 501 |
*/ |
| 502 |
|
| 503 |
using rule75genzmalik = struct |
| 504 |
{ |
| 505 |
rule parent; |
| 506 |
|
| 507 |
/* temporary arrays of length dim */ |
| 508 |
double *widthLambda, *widthLambda2, *p; |
| 509 |
|
| 510 |
/* dimension-dependent constants */ |
| 511 |
double weight1, weight3, weight5; |
| 512 |
double weightE1, weightE3; |
| 513 |
}; |
| 514 |
|
| 515 |
#define real(x) ((double)(x)) |
| 516 |
#define to_int(n) ((int)(n)) |
| 517 |
|
| 518 |
static int isqr(int x) |
| 519 |
{ |
| 520 |
return x * x; |
| 521 |
} |
| 522 |
|
| 523 |
static void destroy_rule75genzmalik(rule* r_) |
| 524 |
{ |
| 525 |
auto* r = (rule75genzmalik*)r_; |
| 526 |
free(r->p); |
| 527 |
} |
| 528 |
|
| 529 |
static int rule75genzmalik_evalError(rule* r_, unsigned int fdim, integrand_v f, |
| 530 |
void* fdata, unsigned int nR, region* R) |
| 531 |
{ |
| 532 |
/* lambda2 = sqrt(9/70), lambda4 = sqrt(9/10), lambda5 = sqrt(9/19) */ |
| 533 |
const double lambda2 = 0.3585685828003180919906451539079374954541; |
| 534 |
const double lambda4 = 0.9486832980505137995996680633298155601160; |
| 535 |
const double lambda5 = 0.6882472016116852977216287342936235251269; |
| 536 |
const double weight2 = 980. / 6561.; |
| 537 |
const double weight4 = 200. / 19683.; |
| 538 |
const double weightE2 = 245. / 486.; |
| 539 |
const double weightE4 = 25. / 729.; |
| 540 |
const double ratio = (lambda2 * lambda2) / (lambda4 * lambda4); |
| 541 |
|
| 542 |
auto* r = (rule75genzmalik*)r_; |
| 543 |
unsigned int i, j, iR, dim = r_->dim, npts = 0; |
| 544 |
double *diff, *pts, *vals; |
| 545 |
|
| 546 |
if (alloc_rule_pts(r_, nR)) |
| 547 |
return FAILURE; |
| 548 |
pts = r_->pts; |
| 549 |
vals = r_->vals; |
| 550 |
|
| 551 |
for (iR = 0; iR < nR; ++iR) { |
| 552 |
const double* center = R[iR].h.data; |
| 553 |
const double* halfwidth = R[iR].h.data + dim; |
| 554 |
|
| 555 |
for (i = 0; i < dim; ++i) |
| 556 |
r->p[i] = center[i]; |
| 557 |
|
| 558 |
for (i = 0; i < dim; ++i) |
| 559 |
r->widthLambda2[i] = halfwidth[i] * lambda2; |
| 560 |
for (i = 0; i < dim; ++i) |
| 561 |
r->widthLambda[i] = halfwidth[i] * lambda4; |
| 562 |
|
| 563 |
/* Evaluate points in the center, in (lambda2,0,...,0) and |
| 564 |
(lambda3=lambda4, 0,...,0). */ |
| 565 |
evalR0_0fs4d(pts + npts * dim, dim, r->p, center, r->widthLambda2, |
| 566 |
r->widthLambda); |
| 567 |
npts += num0_0(dim) + 2 * numR0_0fs(dim); |
| 568 |
|
| 569 |
/* Calculate points for (lambda4, lambda4, 0, ...,0) */ |
| 570 |
evalRR0_0fs(pts + npts * dim, dim, r->p, center, r->widthLambda); |
| 571 |
npts += numRR0_0fs(dim); |
| 572 |
|
| 573 |
/* Calculate points for (lambda5, lambda5, ..., lambda5) */ |
| 574 |
for (i = 0; i < dim; ++i) |
| 575 |
r->widthLambda[i] = halfwidth[i] * lambda5; |
| 576 |
evalR_Rfs(pts + npts * dim, dim, r->p, center, r->widthLambda); |
| 577 |
npts += numR_Rfs(dim); |
| 578 |
} |
| 579 |
|
| 580 |
/* Evaluate the integrand function(s) at all the points */ |
| 581 |
f(dim, npts, pts, fdata, fdim, vals); |
| 582 |
|
| 583 |
/* we are done with the points, and so we can re-use the pts |
| 584 |
array to store the maximum difference diff[i] in each dimension |
| 585 |
for each hypercube */ |
| 586 |
diff = pts; |
| 587 |
for (i = 0; i < dim * nR; ++i) |
| 588 |
diff[i] = 0; |
| 589 |
|
| 590 |
for (j = 0; j < fdim; ++j) { |
| 591 |
for (iR = 0; iR < nR; ++iR) { |
| 592 |
double result, res5th; |
| 593 |
double val0, sum2 = 0, sum3 = 0, sum4 = 0, sum5 = 0; |
| 594 |
unsigned int k, k0 = 0; |
| 595 |
|
| 596 |
/* accumulate j-th function values into j-th integrals |
| 597 |
NOTE: this relies on the ordering of the eval functions |
| 598 |
above, as well as on the internal structure of |
| 599 |
the evalR0_0fs4d function */ |
| 600 |
|
| 601 |
val0 = vals[0]; /* central point */ |
| 602 |
k0 += 1; |
| 603 |
|
| 604 |
for (k = 0; k < dim; ++k) { |
| 605 |
double v0 = vals[k0 + 4 * k]; |
| 606 |
double v1 = vals[(k0 + 4 * k) + 1]; |
| 607 |
double v2 = vals[(k0 + 4 * k) + 2]; |
| 608 |
double v3 = vals[(k0 + 4 * k) + 3]; |
| 609 |
|
| 610 |
sum2 += v0 + v1; |
| 611 |
sum3 += v2 + v3; |
| 612 |
|
| 613 |
diff[iR * dim + k] += |
| 614 |
fabs(v0 + v1 - 2 * val0 - ratio * (v2 + v3 - 2 * val0)); |
| 615 |
} |
| 616 |
k0 += 4 * k; |
| 617 |
|
| 618 |
for (k = 0; k < numRR0_0fs(dim); ++k) |
| 619 |
sum4 += vals[k0 + k]; |
| 620 |
k0 += k; |
| 621 |
|
| 622 |
for (k = 0; k < numR_Rfs(dim); ++k) |
| 623 |
sum5 += vals[k0 + k]; |
| 624 |
|
| 625 |
/* Calculate fifth and seventh order results */ |
| 626 |
result = |
| 627 |
R[iR].h.vol * (r->weight1 * val0 + weight2 * sum2 + r->weight3 * sum3 + |
| 628 |
weight4 * sum4 + r->weight5 * sum5); |
| 629 |
res5th = R[iR].h.vol * (r->weightE1 * val0 + weightE2 * sum2 + |
| 630 |
r->weightE3 * sum3 + weightE4 * sum4); |
| 631 |
|
| 632 |
R[iR].ee[j].val = result; |
| 633 |
R[iR].ee[j].err = fabs(res5th - result); |
| 634 |
|
| 635 |
vals += r_->num_points; |
| 636 |
} |
| 637 |
} |
| 638 |
|
| 639 |
/* figure out dimension to split: */ |
| 640 |
for (iR = 0; iR < nR; ++iR) { |
| 641 |
double maxdiff = 0; |
| 642 |
unsigned int dimDiffMax = 0; |
| 643 |
|
| 644 |
for (i = 0; i < dim; ++i) |
| 645 |
if (diff[iR * dim + i] > maxdiff) { |
| 646 |
maxdiff = diff[iR * dim + i]; |
| 647 |
dimDiffMax = i; |
| 648 |
} |
| 649 |
R[iR].splitDim = dimDiffMax; |
| 650 |
} |
| 651 |
return SUCCESS; |
| 652 |
} |
| 653 |
|
| 654 |
static rule* make_rule75genzmalik(unsigned int dim, unsigned int fdim) |
| 655 |
{ |
| 656 |
rule75genzmalik* r; |
| 657 |
|
| 658 |
if (dim < 2) |
| 659 |
return nullptr; /* this rule does not support 1d integrals */ |
| 660 |
|
| 661 |
/* Because of the use of a bit-field in evalR_Rfs, we are limited |
| 662 |
to be < 32 dimensions (or however many bits are in unsigned int). |
| 663 |
This is not a practical limitation...long before you reach |
| 664 |
32 dimensions, the Genz-Malik cubature becomes excruciatingly |
| 665 |
slow and is superseded by other methods (e.g. Monte-Carlo). */ |
| 666 |
if (dim >= sizeof(unsigned int) * 8) |
| 667 |
return nullptr; |
| 668 |
|
| 669 |
r = (rule75genzmalik*)make_rule( |
| 670 |
sizeof(rule75genzmalik), dim, fdim, |
| 671 |
num0_0(dim) + 2 * numR0_0fs(dim) + numRR0_0fs(dim) + numR_Rfs(dim), |
| 672 |
rule75genzmalik_evalError, destroy_rule75genzmalik); |
| 673 |
if (!r) |
| 674 |
return nullptr; |
| 675 |
|
| 676 |
r->weight1 = |
| 677 |
(real(12824 - 9120 * to_int(dim) + 400 * isqr(to_int(dim))) / real(19683)); |
| 678 |
r->weight3 = real(1820 - 400 * to_int(dim)) / real(19683); |
| 679 |
r->weight5 = real(6859) / real(19683) / real(1U << dim); |
| 680 |
r->weightE1 = |
| 681 |
(real(729 - 950 * to_int(dim) + 50 * isqr(to_int(dim))) / real(729)); |
| 682 |
r->weightE3 = real(265 - 100 * to_int(dim)) / real(1458); |
| 683 |
|
| 684 |
r->p = (double*)malloc(sizeof(double) * dim * 3); |
| 685 |
if (!r->p) { |
| 686 |
destroy_rule((rule*)r); |
| 687 |
return nullptr; |
| 688 |
} |
| 689 |
r->widthLambda = r->p + dim; |
| 690 |
r->widthLambda2 = r->p + 2 * dim; |
| 691 |
|
| 692 |
return (rule*)r; |
| 693 |
} |
| 694 |
|
| 695 |
/***************************************************************************/ |
| 696 |
/* 1d 15-point Gaussian quadrature rule, based on qk15.c and qk.c in |
| 697 |
GNU GSL (which in turn is based on QUADPACK). */ |
| 698 |
|
| 699 |
static int rule15gauss_evalError(rule* r, unsigned int fdim, integrand_v f, |
| 700 |
void* fdata, unsigned int nR, region* R) |
| 701 |
{ |
| 702 |
/* Gauss quadrature weights and kronrod quadrature abscissae and |
| 703 |
weights as evaluated with 80 decimal digit arithmetic by |
| 704 |
L. W. Fullerton, Bell Labs, Nov. 1981. */ |
| 705 |
const unsigned int n = 8; |
| 706 |
const double xgk[8] = { |
| 707 |
/* abscissae of the 15-point kronrod rule */ |
| 708 |
0.991455371120812639206854697526329, |
| 709 |
0.949107912342758524526189684047851, |
| 710 |
0.864864423359769072789712788640926, |
| 711 |
0.741531185599394439863864773280788, |
| 712 |
0.586087235467691130294144838258730, |
| 713 |
0.405845151377397166906606412076961, |
| 714 |
0.207784955007898467600689403773245, |
| 715 |
0.000000000000000000000000000000000 |
| 716 |
/* xgk[1], xgk[3], ... abscissae of the 7-point gauss rule. |
| 717 |
xgk[0], xgk[2], ... to optimally extend the 7-point gauss rule */ |
| 718 |
}; |
| 719 |
static const double wg[4] = { |
| 720 |
/* weights of the 7-point gauss rule */ |
| 721 |
0.129484966168869693270611432679082, 0.279705391489276667901467771423780, |
| 722 |
0.381830050505118944950369775488975, 0.417959183673469387755102040816327 |
| 723 |
}; |
| 724 |
static const double wgk[8] = { |
| 725 |
/* weights of the 15-point kronrod rule */ |
| 726 |
0.022935322010529224963732008058970, 0.063092092629978553290700663189204, |
| 727 |
0.104790010322250183839876322541518, 0.140653259715525918745189590510238, |
| 728 |
0.169004726639267902826583426598550, 0.190350578064785409913256402421014, |
| 729 |
0.204432940075298892414161999234649, 0.209482141084727828012999174891714 |
| 730 |
}; |
| 731 |
unsigned int j, k, iR, npts = 0; |
| 732 |
double *pts, *vals; |
| 733 |
|
| 734 |
if (alloc_rule_pts(r, nR)) |
| 735 |
return FAILURE; |
| 736 |
pts = r->pts; |
| 737 |
vals = r->vals; |
| 738 |
|
| 739 |
for (iR = 0; iR < nR; ++iR) { |
| 740 |
const double center = R[iR].h.data[0]; |
| 741 |
const double halfwidth = R[iR].h.data[1]; |
| 742 |
|
| 743 |
pts[npts++] = center; |
| 744 |
|
| 745 |
for (j = 0; j < (n - 1) / 2; ++j) { |
| 746 |
int j2 = 2 * j + 1; |
| 747 |
double w = halfwidth * xgk[j2]; |
| 748 |
pts[npts++] = center - w; |
| 749 |
pts[npts++] = center + w; |
| 750 |
} |
| 751 |
for (j = 0; j < n / 2; ++j) { |
| 752 |
int j2 = 2 * j; |
| 753 |
double w = halfwidth * xgk[j2]; |
| 754 |
pts[npts++] = center - w; |
| 755 |
pts[npts++] = center + w; |
| 756 |
} |
| 757 |
|
| 758 |
R[iR].splitDim = 0; /* no choice but to divide 0th dimension */ |
| 759 |
} |
| 760 |
|
| 761 |
f(1, npts, pts, fdata, fdim, vals); |
| 762 |
|
| 763 |
for (k = 0; k < fdim; ++k) { |
| 764 |
for (iR = 0; iR < nR; ++iR) { |
| 765 |
const double halfwidth = R[iR].h.data[1]; |
| 766 |
double result_gauss = vals[0] * wg[n / 2 - 1]; |
| 767 |
double result_kronrod = vals[0] * wgk[n - 1]; |
| 768 |
double result_abs = fabs(result_kronrod); |
| 769 |
double result_asc, mean, err; |
| 770 |
|
| 771 |
/* accumulate integrals */ |
| 772 |
npts = 1; |
| 773 |
for (j = 0; j < (n - 1) / 2; ++j) { |
| 774 |
int j2 = 2 * j + 1; |
| 775 |
double v = vals[npts] + vals[npts + 1]; |
| 776 |
result_gauss += wg[j] * v; |
| 777 |
result_kronrod += wgk[j2] * v; |
| 778 |
result_abs += wgk[j2] * (fabs(vals[npts]) + fabs(vals[npts + 1])); |
| 779 |
npts += 2; |
| 780 |
} |
| 781 |
for (j = 0; j < n / 2; ++j) { |
| 782 |
int j2 = 2 * j; |
| 783 |
result_kronrod += wgk[j2] * (vals[npts] + vals[npts + 1]); |
| 784 |
result_abs += wgk[j2] * (fabs(vals[npts]) + fabs(vals[npts + 1])); |
| 785 |
npts += 2; |
| 786 |
} |
| 787 |
|
| 788 |
/* integration result */ |
| 789 |
R[iR].ee[k].val = result_kronrod * halfwidth; |
| 790 |
|
| 791 |
/* error estimate |
| 792 |
(from GSL, probably dates back to QUADPACK |
| 793 |
... not completely clear to me why we don't just use |
| 794 |
fabs(result_kronrod - result_gauss) * halfwidth */ |
| 795 |
mean = result_kronrod * 0.5; |
| 796 |
result_asc = wgk[n - 1] * fabs(vals[0] - mean); |
| 797 |
npts = 1; |
| 798 |
for (j = 0; j < (n - 1) / 2; ++j) { |
| 799 |
int j2 = 2 * j + 1; |
| 800 |
result_asc += |
| 801 |
wgk[j2] * (fabs(vals[npts] - mean) + fabs(vals[npts + 1] - mean)); |
| 802 |
npts += 2; |
| 803 |
} |
| 804 |
for (j = 0; j < n / 2; ++j) { |
| 805 |
int j2 = 2 * j; |
| 806 |
result_asc += |
| 807 |
wgk[j2] * (fabs(vals[npts] - mean) + fabs(vals[npts + 1] - mean)); |
| 808 |
npts += 2; |
| 809 |
} |
| 810 |
err = fabs(result_kronrod - result_gauss) * halfwidth; |
| 811 |
result_abs *= halfwidth; |
| 812 |
result_asc *= halfwidth; |
| 813 |
if (result_asc != 0 && err != 0) { |
| 814 |
double scale = pow((200 * err / result_asc), 1.5); |
| 815 |
err = (scale < 1) ? result_asc * scale : result_asc; |
| 816 |
} |
| 817 |
if (result_abs > DBL_MIN / (50 * DBL_EPSILON)) { |
| 818 |
double min_err = 50 * DBL_EPSILON * result_abs; |
| 819 |
if (min_err > err) |
| 820 |
err = min_err; |
| 821 |
} |
| 822 |
R[iR].ee[k].err = err; |
| 823 |
|
| 824 |
/* increment vals to point to next batch of results */ |
| 825 |
vals += 15; |
| 826 |
} |
| 827 |
} |
| 828 |
return SUCCESS; |
| 829 |
} |
| 830 |
|
| 831 |
static rule* make_rule15gauss(unsigned int dim, unsigned int fdim) |
| 832 |
{ |
| 833 |
if (dim != 1) |
| 834 |
return nullptr; /* this rule is only for 1d integrals */ |
| 835 |
|
| 836 |
return make_rule(sizeof(rule), dim, fdim, 15, rule15gauss_evalError, nullptr); |
| 837 |
} |
| 838 |
|
| 839 |
/***************************************************************************/ |
| 840 |
/* binary heap implementation (ala _Introduction to Algorithms_ by |
| 841 |
Cormen, Leiserson, and Rivest), for use as a priority queue of |
| 842 |
regions to integrate. */ |
| 843 |
|
| 844 |
using heap_item = region; |
| 845 |
#define KEY(hi) ((hi).errmax) |
| 846 |
|
| 847 |
using heap = struct |
| 848 |
{ |
| 849 |
unsigned int n, nalloc; |
| 850 |
heap_item* items; |
| 851 |
unsigned int fdim; |
| 852 |
esterr* ee; /* array of length fdim of the total integrand & error */ |
| 853 |
}; |
| 854 |
|
| 855 |
static void heap_resize(heap* h, unsigned int nalloc) |
| 856 |
{ |
| 857 |
h->nalloc = nalloc; |
| 858 |
h->items = (heap_item*)realloc(h->items, sizeof(heap_item) * nalloc); |
| 859 |
} |
| 860 |
|
| 861 |
static heap heap_alloc(unsigned int nalloc, unsigned int fdim) |
| 862 |
{ |
| 863 |
heap h; |
| 864 |
unsigned int i; |
| 865 |
h.n = 0; |
| 866 |
h.nalloc = 0; |
| 867 |
h.items = nullptr; |
| 868 |
h.fdim = fdim; |
| 869 |
h.ee = (esterr*)malloc(sizeof(esterr) * fdim); |
| 870 |
if (h.ee) { |
| 871 |
for (i = 0; i < fdim; ++i) |
| 872 |
h.ee[i].val = h.ee[i].err = 0; |
| 873 |
heap_resize(&h, nalloc); |
| 874 |
} |
| 875 |
return h; |
| 876 |
} |
| 877 |
|
| 878 |
/* note that heap_free does not deallocate anything referenced by the items */ |
| 879 |
static void heap_free(heap* h) |
| 880 |
{ |
| 881 |
h->n = 0; |
| 882 |
heap_resize(h, 0); |
| 883 |
h->fdim = 0; |
| 884 |
free(h->ee); |
| 885 |
} |
| 886 |
|
| 887 |
static int heap_push(heap* h, heap_item hi) |
| 888 |
{ |
| 889 |
int insert; |
| 890 |
unsigned int i, fdim = h->fdim; |
| 891 |
|
| 892 |
for (i = 0; i < fdim; ++i) { |
| 893 |
h->ee[i].val += hi.ee[i].val; |
| 894 |
h->ee[i].err += hi.ee[i].err; |
| 895 |
} |
| 896 |
insert = h->n; |
| 897 |
if (++(h->n) > h->nalloc) { |
| 898 |
heap_resize(h, h->n * 2); |
| 899 |
if (!h->items) |
| 900 |
return FAILURE; |
| 901 |
} |
| 902 |
|
| 903 |
while (insert) { |
| 904 |
int parent = (insert - 1) / 2; |
| 905 |
if (KEY(hi) <= KEY(h->items[parent])) |
| 906 |
break; |
| 907 |
h->items[insert] = h->items[parent]; |
| 908 |
insert = parent; |
| 909 |
} |
| 910 |
h->items[insert] = hi; |
| 911 |
return SUCCESS; |
| 912 |
} |
| 913 |
|
| 914 |
static int heap_push_many(heap* h, unsigned int ni, heap_item* hi) |
| 915 |
{ |
| 916 |
unsigned int i; |
| 917 |
for (i = 0; i < ni; ++i) |
| 918 |
if (heap_push(h, hi[i])) |
| 919 |
return FAILURE; |
| 920 |
return SUCCESS; |
| 921 |
} |
| 922 |
|
| 923 |
static heap_item heap_pop(heap* h) |
| 924 |
{ |
| 925 |
heap_item ret; |
| 926 |
int i, n, child; |
| 927 |
|
| 928 |
if (!(h->n)) { |
| 929 |
fprintf(stderr, "attempted to pop an empty heap\n"); |
| 930 |
return ret; // error |
| 931 |
} |
| 932 |
|
| 933 |
ret = h->items[0]; |
| 934 |
h->items[i = 0] = h->items[n = --(h->n)]; |
| 935 |
while ((child = i * 2 + 1) < n) { |
| 936 |
int largest; |
| 937 |
heap_item swap; |
| 938 |
|
| 939 |
if (KEY(h->items[child]) <= KEY(h->items[i])) |
| 940 |
largest = i; |
| 941 |
else |
| 942 |
largest = child; |
| 943 |
if (++child < n && KEY(h->items[largest]) < KEY(h->items[child])) |
| 944 |
largest = child; |
| 945 |
if (largest == i) |
| 946 |
break; |
| 947 |
swap = h->items[i]; |
| 948 |
h->items[i] = h->items[largest]; |
| 949 |
h->items[i = largest] = swap; |
| 950 |
} |
| 951 |
|
| 952 |
{ |
| 953 |
unsigned int i_, fdim = h->fdim; |
| 954 |
for (i_ = 0; i_ < fdim; ++i_) { |
| 955 |
h->ee[i_].val -= ret.ee[i_].val; |
| 956 |
h->ee[i_].err -= ret.ee[i_].err; |
| 957 |
} |
| 958 |
} |
| 959 |
return ret; |
| 960 |
} |
| 961 |
|
| 962 |
/***************************************************************************/ |
| 963 |
|
| 964 |
/* adaptive integration, analogous to adaptintegrator.cpp in HIntLib */ |
| 965 |
|
| 966 |
static int ruleadapt_integrate(rule* r, unsigned int fdim, integrand_v f, |
| 967 |
void* fdata, const hypercube* h, |
| 968 |
unsigned int maxEval, double reqAbsError, |
| 969 |
double reqRelError, double* val, double* err, |
| 970 |
int parallel) |
| 971 |
{ |
| 972 |
unsigned int numEval = 0; |
| 973 |
heap regions; |
| 974 |
unsigned int i, j; |
| 975 |
region* R = nullptr; /* array of regions to evaluate */ |
| 976 |
unsigned int nR_alloc = 0; |
| 977 |
esterr* ee = nullptr; |
| 978 |
|
| 979 |
regions = heap_alloc(1, fdim); |
| 980 |
if (!regions.ee || !regions.items) |
| 981 |
goto bad; |
| 982 |
|
| 983 |
ee = (esterr*)malloc(sizeof(esterr) * fdim); |
| 984 |
if (!ee) |
| 985 |
goto bad; |
| 986 |
|
| 987 |
nR_alloc = 2; |
| 988 |
R = (region*)malloc(sizeof(region) * nR_alloc); |
| 989 |
if (!R) |
| 990 |
goto bad; |
| 991 |
R[0] = make_region(h, fdim); |
| 992 |
if (!R[0].ee || eval_regions(1, R, f, fdata, r) || heap_push(®ions, R[0])) |
| 993 |
goto bad; |
| 994 |
numEval += r->num_points; |
| 995 |
|
| 996 |
while (numEval < maxEval || !maxEval) { |
| 997 |
for (j = 0; j < fdim && (regions.ee[j].err <= reqAbsError || |
| 998 |
relError(regions.ee[j]) <= reqRelError); |
| 999 |
++j) |
| 1000 |
; |
| 1001 |
if (j == fdim) |
| 1002 |
break; /* convergence */ |
| 1003 |
|
| 1004 |
if (parallel) { /* maximize potential parallelism */ |
| 1005 |
/* adapted from I. Gladwell, "Vectorization of one |
| 1006 |
dimensional quadrature codes," pp. 230--238 in |
| 1007 |
_Numerical Integration. Recent Developments, |
| 1008 |
Software and Applications_, G. Fairweather and |
| 1009 |
P. M. Keast, eds., NATO ASI Series C203, Dordrecht |
| 1010 |
(1987), as described in J. M. Bull and |
| 1011 |
T. L. Freeman, "Parallel Globally Adaptive |
| 1012 |
Algorithms for Multi-dimensional Integration," |
| 1013 |
http://citeseerx.ist.psu.edu/viewdoc/summary?doi=10.1.1.42.6638 |
| 1014 |
(1994). |
| 1015 |
|
| 1016 |
Basically, this evaluates in one shot all regions |
| 1017 |
that *must* be evaluated in order to reduce the |
| 1018 |
error to the requested bound: the minimum set of |
| 1019 |
largest-error regions whose errors push the total |
| 1020 |
error over the bound. |
| 1021 |
|
| 1022 |
[Note: Bull and Freeman claim that the Gladwell |
| 1023 |
approach is intrinsically inefficient because it |
| 1024 |
"requires sorting", and propose an alternative |
| 1025 |
algorithm that "only" requires three passes over the |
| 1026 |
entire set of regions. Apparently, they didn't |
| 1027 |
realize that one could use a heap data structure, in |
| 1028 |
which case the time to pop K biggest-error regions |
| 1029 |
out of N is only O(K log N), much better than the |
| 1030 |
O(N) cost of the Bull and Freeman algorithm if K << |
| 1031 |
N, and it is also much simpler.] */ |
| 1032 |
unsigned int nR = 0; |
| 1033 |
for (j = 0; j < fdim; ++j) |
| 1034 |
ee[j] = regions.ee[j]; |
| 1035 |
do { |
| 1036 |
if (nR + 2 > nR_alloc) { |
| 1037 |
nR_alloc = (nR + 2) * 2; |
| 1038 |
R = (region*)realloc(R, nR_alloc * sizeof(region)); |
| 1039 |
if (!R) |
| 1040 |
goto bad; |
| 1041 |
} |
| 1042 |
R[nR] = heap_pop(®ions); |
| 1043 |
for (j = 0; j < fdim; ++j) |
| 1044 |
ee[j].err -= R[nR].ee[j].err; |
| 1045 |
if (cut_region(R + nR, R + nR + 1)) |
| 1046 |
goto bad; |
| 1047 |
numEval += r->num_points * 2; |
| 1048 |
nR += 2; |
| 1049 |
for (j = 0; j < fdim && (ee[j].err <= reqAbsError || |
| 1050 |
relError(ee[j]) <= reqRelError); |
| 1051 |
++j) |
| 1052 |
; |
| 1053 |
if (j == fdim) |
| 1054 |
break; /* other regions have small errs */ |
| 1055 |
} while (regions.n > 0 && (numEval < maxEval || !maxEval)); |
| 1056 |
if (eval_regions(nR, R, f, fdata, r) || heap_push_many(®ions, nR, R)) |
| 1057 |
goto bad; |
| 1058 |
} else { /* minimize number of function evaluations */ |
| 1059 |
R[0] = heap_pop(®ions); /* get worst region */ |
| 1060 |
if (cut_region(R, R + 1) || eval_regions(2, R, f, fdata, r) || |
| 1061 |
heap_push_many(®ions, 2, R)) |
| 1062 |
goto bad; |
| 1063 |
numEval += r->num_points * 2; |
| 1064 |
} |
| 1065 |
} |
| 1066 |
|
| 1067 |
/* re-sum integral and errors */ |
| 1068 |
for (j = 0; j < fdim; ++j) |
| 1069 |
val[j] = err[j] = 0; |
| 1070 |
for (i = 0; i < regions.n; ++i) { |
| 1071 |
for (j = 0; j < fdim; ++j) { |
| 1072 |
val[j] += regions.items[i].ee[j].val; |
| 1073 |
err[j] += regions.items[i].ee[j].err; |
| 1074 |
} |
| 1075 |
destroy_region(®ions.items[i]); |
| 1076 |
} |
| 1077 |
|
| 1078 |
/* printf("regions.nalloc = %d\n", regions.nalloc); */ |
| 1079 |
free(ee); |
| 1080 |
heap_free(®ions); |
| 1081 |
free(R); |
| 1082 |
return SUCCESS; |
| 1083 |
|
| 1084 |
bad: |
| 1085 |
free(ee); |
| 1086 |
heap_free(®ions); |
| 1087 |
free(R); |
| 1088 |
return FAILURE; |
| 1089 |
} |
| 1090 |
|
| 1091 |
static int integrate(unsigned int fdim, integrand_v f, void* fdata, |
| 1092 |
unsigned int dim, const double* xmin, const double* xmax, |
| 1093 |
unsigned int maxEval, double reqAbsError, |
| 1094 |
double reqRelError, double* val, double* err, int parallel) |
| 1095 |
{ |
| 1096 |
rule* r; |
| 1097 |
hypercube h; |
| 1098 |
int status; |
| 1099 |
unsigned int i; |
| 1100 |
|
| 1101 |
if (fdim == 0) /* nothing to do */ |
| 1102 |
return SUCCESS; |
| 1103 |
if (dim == 0) { /* trivial integration */ |
| 1104 |
f(0, 1, xmin, fdata, fdim, val); |
| 1105 |
for (i = 0; i < fdim; ++i) |
| 1106 |
err[i] = 0; |
| 1107 |
return SUCCESS; |
| 1108 |
} |
| 1109 |
r = dim == 1 ? make_rule15gauss(dim, fdim) : make_rule75genzmalik(dim, fdim); |
| 1110 |
if (!r) { |
| 1111 |
for (i = 0; i < fdim; ++i) { |
| 1112 |
val[i] = 0; |
| 1113 |
err[i] = HUGE_VAL; |
| 1114 |
} |
| 1115 |
return FAILURE; |
| 1116 |
} |
| 1117 |
h = make_hypercube_range(dim, xmin, xmax); |
| 1118 |
status = !h.data |
| 1119 |
? FAILURE |
| 1120 |
: ruleadapt_integrate(r, fdim, f, fdata, &h, maxEval, reqAbsError, |
| 1121 |
reqRelError, val, err, parallel); |
| 1122 |
destroy_hypercube(&h); |
| 1123 |
destroy_rule(r); |
| 1124 |
return status; |
| 1125 |
} |
| 1126 |
|
| 1127 |
int adapt_integrate_v(unsigned int fdim, integrand_v f, void* fdata, |
| 1128 |
unsigned int dim, const double* xmin, const double* xmax, |
| 1129 |
unsigned int maxEval, double reqAbsError, |
| 1130 |
double reqRelError, double* val, double* err) |
| 1131 |
{ |
| 1132 |
return integrate(fdim, f, fdata, dim, xmin, xmax, maxEval, reqAbsError, |
| 1133 |
reqRelError, val, err, 1); |
| 1134 |
} |
| 1135 |
|
| 1136 |
/* wrapper around non-vectorized integrand */ |
| 1137 |
using fv_data = struct fv_data_s |
| 1138 |
{ |
| 1139 |
integrand f; |
| 1140 |
void* fdata; |
| 1141 |
double* fval1; |
| 1142 |
}; |
| 1143 |
static void fv(unsigned int ndim, unsigned int npt, const double* x, void* d_, |
| 1144 |
unsigned int fdim, double* fval) |
| 1145 |
{ |
| 1146 |
auto* d = (fv_data*)d_; |
| 1147 |
double* fval1 = d->fval1; |
| 1148 |
unsigned int i, k; |
| 1149 |
/* printf("npt = %u\n", npt); */ |
| 1150 |
for (i = 0; i < npt; ++i) { |
| 1151 |
d->f(ndim, x + i * ndim, d->fdata, fdim, fval1); |
| 1152 |
for (k = 0; k < fdim; ++k) |
| 1153 |
fval[k * npt + i] = fval1[k]; |
| 1154 |
} |
| 1155 |
} |
| 1156 |
|
| 1157 |
int adapt_integrate(unsigned int fdim, integrand f, void* fdata, |
| 1158 |
unsigned int dim, const double* xmin, const double* xmax, |
| 1159 |
unsigned int maxEval, double reqAbsError, |
| 1160 |
double reqRelError, double* val, double* err) |
| 1161 |
{ |
| 1162 |
int ret; |
| 1163 |
fv_data d; |
| 1164 |
|
| 1165 |
if (fdim == 0) |
| 1166 |
return SUCCESS; /* nothing to do */ |
| 1167 |
|
| 1168 |
d.f = f; |
| 1169 |
d.fdata = fdata; |
| 1170 |
d.fval1 = (double*)malloc(sizeof(double) * fdim); |
| 1171 |
if (!d.fval1) { |
| 1172 |
unsigned int i; |
| 1173 |
for (i = 0; i < fdim; ++i) { |
| 1174 |
val[i] = 0; |
| 1175 |
err[i] = HUGE_VAL; |
| 1176 |
} |
| 1177 |
return -2; /* ERROR */ |
| 1178 |
} |
| 1179 |
ret = integrate(fdim, fv, &d, dim, xmin, xmax, maxEval, reqAbsError, |
| 1180 |
reqRelError, val, err, 0); |
| 1181 |
free(d.fval1); |
| 1182 |
return ret; |
| 1183 |
} |
| 1184 |
|
| 1185 |
// TODO: Consider QVariantList. For now, mimic what is known to work. |
| 1186 |
QList<QVariant> QTAIMEvaluateProperty(QList<QVariant> variantList) |
| 1187 |
{ |
| 1188 |
/* |
| 1189 |
Order of variantList: |
| 1190 |
QString wfnFileName |
| 1191 |
qreal x0 |
| 1192 |
qreal y0 |
| 1193 |
qreal z0 |
| 1194 |
qint64 nncp |
| 1195 |
qint64 xncp1 |
| 1196 |
qint64 yncp1 |
| 1197 |
qint64 zncp1 |
| 1198 |
qint64 xncp2 |
| 1199 |
qint64 yncp2 |
| 1200 |
qint64 zncp2 |
| 1201 |
... |
| 1202 |
qint64 nmode |
| 1203 |
qint64 mode1 |
| 1204 |
qint64 mode2 |
| 1205 |
... |
| 1206 |
qint64 nbasin |
| 1207 |
qint64 basin1 |
| 1208 |
qint64 basin2 |
| 1209 |
... |
| 1210 |
*/ |
| 1211 |
qint64 counter = 0; |
| 1212 |
QString wfnFileName = variantList.at(counter).toString(); |
| 1213 |
counter++; |
| 1214 |
qreal x0 = variantList.at(counter).toDouble(); |
| 1215 |
counter++; |
| 1216 |
qreal y0 = variantList.at(counter).toDouble(); |
| 1217 |
counter++; |
| 1218 |
qreal z0 = variantList.at(counter).toDouble(); |
| 1219 |
counter++; |
| 1220 |
|
| 1221 |
qint64 nncp = variantList.at(counter).toLongLong(); |
| 1222 |
counter++; |
| 1223 |
QList<QVector3D> ncpList; |
| 1224 |
for (qint64 n = 0; n < nncp; ++n) { |
| 1225 |
qreal x = variantList.at(counter).toDouble(); |
| 1226 |
counter++; |
| 1227 |
qreal y = variantList.at(counter).toDouble(); |
| 1228 |
counter++; |
| 1229 |
qreal z = variantList.at(counter).toDouble(); |
| 1230 |
counter++; |
| 1231 |
|
| 1232 |
ncpList.append(QVector3D(x, y, z)); |
| 1233 |
} |
| 1234 |
|
| 1235 |
qint64 nmode = variantList.at(counter).toLongLong(); |
| 1236 |
counter++; |
| 1237 |
QList<qint64> modeList; |
| 1238 |
for (qint64 m = 0; m < nmode; ++m) { |
| 1239 |
qint64 mode = variantList.at(counter).toLongLong(); |
| 1240 |
counter++; |
| 1241 |
modeList.append(mode); |
| 1242 |
} |
| 1243 |
|
| 1244 |
qint64 nbasin = variantList.at(counter).toLongLong(); |
| 1245 |
counter++; |
| 1246 |
QList<qint64> basinList; |
| 1247 |
for (qint64 b = 0; b < nbasin; ++b) { |
| 1248 |
qint64 basin = variantList.at(counter).toLongLong(); |
| 1249 |
counter++; |
| 1250 |
basinList.append(basin); |
| 1251 |
} |
| 1252 |
QSet<qint64> basinSet(basinList.begin(), basinList.end()); |
| 1253 |
|
| 1254 |
QTAIMWavefunction wfn; |
| 1255 |
wfn.loadFromBinaryFile(wfnFileName); |
| 1256 |
|
| 1257 |
QTAIMWavefunctionEvaluator eval(wfn); |
| 1258 |
|
| 1259 |
QList<QVariant> valueList; |
| 1260 |
|
| 1261 |
double initialElectronDensity = |
| 1262 |
eval.electronDensity(Eigen::Vector3d(x0, y0, z0)); |
| 1263 |
|
| 1264 |
// if less than some small value, then return zero for all integrands. |
| 1265 |
if (initialElectronDensity < 1.e-5) { |
| 1266 |
for (qint64 m = 0; m < nmode; ++m) { |
| 1267 |
qreal zero = 0.0; |
| 1268 |
valueList.append(zero); |
| 1269 |
} |
| 1270 |
} else { |
| 1271 |
QList<QPair<QVector3D, qreal>> betaSpheres; |
| 1272 |
for (qint64 i = 0; i < nncp; ++i) { |
| 1273 |
QPair<QVector3D, qreal> thisBetaSphere; |
| 1274 |
thisBetaSphere.first = |
| 1275 |
QVector3D(ncpList.at(i).x(), ncpList.at(i).y(), ncpList.at(i).z()); |
| 1276 |
thisBetaSphere.second = 0.10; |
| 1277 |
betaSpheres.append(thisBetaSphere); |
| 1278 |
} |
| 1279 |
|
| 1280 |
QTAIMLSODAIntegrator ode(eval, 0); |
| 1281 |
// Avogadro::QTAIMODEIntegrator ode(eval,0); |
| 1282 |
|
| 1283 |
ode.setBetaSpheres(betaSpheres); |
| 1284 |
|
| 1285 |
QVector3D endpoint = ode.integrate(QVector3D(x0, y0, z0)); |
| 1286 |
// QList<QVector3D> path=ode.path(); |
| 1287 |
|
| 1288 |
#define HUGE_REAL_NUMBER 1.e20 |
| 1289 |
qreal smallestDistance = HUGE_REAL_NUMBER; |
| 1290 |
qint64 smallestDistanceIndex = -1; |
| 1291 |
|
| 1292 |
for (qint64 n = 0; n < betaSpheres.length(); ++n) { |
| 1293 |
Matrix<qreal, 3, 1> a(endpoint.x(), endpoint.y(), endpoint.z()); |
| 1294 |
Matrix<qreal, 3, 1> b(betaSpheres.at(n).first.x(), |
| 1295 |
betaSpheres.at(n).first.y(), |
| 1296 |
betaSpheres.at(n).first.z()); |
| 1297 |
|
| 1298 |
qreal distance = QTAIMMathUtilities::distance(a, b); |
| 1299 |
|
| 1300 |
if (distance < smallestDistance) { |
| 1301 |
smallestDistance = distance; |
| 1302 |
smallestDistanceIndex = n; |
| 1303 |
} |
| 1304 |
} |
| 1305 |
qint64 nucleusIndex = smallestDistanceIndex; |
| 1306 |
|
| 1307 |
if (basinSet.contains(nucleusIndex)) { |
| 1308 |
// if(nucleusIndex==0) |
| 1309 |
// { |
| 1310 |
// QFile file("/scratch/brown/0.txt"); |
| 1311 |
// file.open(QIODevice::WriteOnly | QIODevice::Append); |
| 1312 |
// QTextStream out(&file); |
| 1313 |
// out << x0 << " " << y0 << " " << z0 << "\n"; |
| 1314 |
// file.close(); |
| 1315 |
// } |
| 1316 |
for (qint64 m = 0; m < nmode; ++m) { |
| 1317 |
if (modeList.at(m) == 0) { |
| 1318 |
valueList.append(eval.electronDensity(Eigen::Vector3d(x0, y0, z0))); |
| 1319 |
} else { |
| 1320 |
qDebug() << "mode not defined"; |
| 1321 |
qreal zero = 0.0; |
| 1322 |
valueList.append(zero); |
| 1323 |
} |
| 1324 |
} |
| 1325 |
} else { |
| 1326 |
for (qint64 m = 0; m < nmode; ++m) { |
| 1327 |
qreal zero = 0.0; |
| 1328 |
valueList.append(zero); |
| 1329 |
} |
| 1330 |
} |
| 1331 |
} |
| 1332 |
|
| 1333 |
return valueList; |
| 1334 |
} |
| 1335 |
|
| 1336 |
void property_v(unsigned int /* ndim */, unsigned int npts, const double* xyz, |
| 1337 |
void* param, unsigned int /* dim */, double* fval) |
| 1338 |
{ |
| 1339 |
|
| 1340 |
auto* paramVariantListPtr = (QVariantList*)param; |
| 1341 |
QVariantList paramVariantList = *paramVariantListPtr; |
| 1342 |
|
| 1343 |
qint64 counter = 0; |
| 1344 |
QString wfnFileName = paramVariantList.at(counter).toString(); |
| 1345 |
counter++; |
| 1346 |
|
| 1347 |
qint64 nncp = paramVariantList.at(counter).toLongLong(); |
| 1348 |
counter++; |
| 1349 |
QList<QVector3D> ncpList; |
| 1350 |
for (qint64 i = 0; i < nncp; ++i) { |
| 1351 |
qreal x = paramVariantList.at(counter).toDouble(); |
| 1352 |
counter++; |
| 1353 |
qreal y = paramVariantList.at(counter).toDouble(); |
| 1354 |
counter++; |
| 1355 |
qreal z = paramVariantList.at(counter).toDouble(); |
| 1356 |
counter++; |
| 1357 |
|
| 1358 |
ncpList.append(QVector3D(x, y, z)); |
| 1359 |
} |
| 1360 |
qint64 nmode = 1; |
| 1361 |
qint64 mode = paramVariantList.at(counter).toLongLong(); |
| 1362 |
counter++; |
| 1363 |
QList<qint64> basinList; |
| 1364 |
for (qint64 i = counter; i < paramVariantList.length(); ++i) { |
| 1365 |
basinList.append(paramVariantList.at(i).toLongLong()); |
| 1366 |
counter++; |
| 1367 |
} |
| 1368 |
|
| 1369 |
// prepare input |
| 1370 |
|
| 1371 |
QList<QList<QVariant>> inputList; |
| 1372 |
|
| 1373 |
for (unsigned int i = 0; i < npts; ++i) { |
| 1374 |
|
| 1375 |
double x0 = xyz[i * 3 + 0]; |
| 1376 |
double y0 = xyz[i * 3 + 1]; |
| 1377 |
double z0 = xyz[i * 3 + 2]; |
| 1378 |
|
| 1379 |
QList<QVariant> variantList; |
| 1380 |
|
| 1381 |
variantList.append(wfnFileName); |
| 1382 |
|
| 1383 |
variantList.append(x0); |
| 1384 |
variantList.append(y0); |
| 1385 |
variantList.append(z0); |
| 1386 |
|
| 1387 |
variantList.append(nncp); |
| 1388 |
for (qint64 n = 0; n < nncp; ++n) { |
| 1389 |
variantList.append(ncpList.at(n).x()); |
| 1390 |
variantList.append(ncpList.at(n).y()); |
| 1391 |
variantList.append(ncpList.at(n).z()); |
| 1392 |
} |
| 1393 |
|
| 1394 |
variantList.append(nmode); // for now, one mode |
| 1395 |
for (qint64 m = 0; m < nmode; ++m) { |
| 1396 |
variantList.append(mode); |
| 1397 |
} |
| 1398 |
|
| 1399 |
qint64 nbasin = basinList.length(); |
| 1400 |
variantList.append(nbasin); |
| 1401 |
for (long long b : basinList) { |
| 1402 |
variantList.append(b); |
| 1403 |
} |
| 1404 |
|
| 1405 |
inputList.append(variantList); |
| 1406 |
} |
| 1407 |
|
| 1408 |
// calculate |
| 1409 |
|
| 1410 |
QProgressDialog dialog; |
| 1411 |
dialog.setWindowTitle("QTAIM"); |
| 1412 |
dialog.setLabelText(QString("Atomic Basin Integration")); |
| 1413 |
|
| 1414 |
QFutureWatcher<QList<QVariant>> futureWatcher; |
| 1415 |
QObject::connect(&futureWatcher, SIGNAL(finished()), &dialog, SLOT(reset())); |
| 1416 |
QObject::connect(&dialog, SIGNAL(canceled()), &futureWatcher, SLOT(cancel())); |
| 1417 |
QObject::connect(&futureWatcher, SIGNAL(progressRangeChanged(int, int)), |
| 1418 |
&dialog, SLOT(setRange(int, int))); |
| 1419 |
QObject::connect(&futureWatcher, SIGNAL(progressValueChanged(int)), &dialog, |
| 1420 |
SLOT(setValue(int))); |
| 1421 |
|
| 1422 |
QFuture<QList<QVariant>> future = |
| 1423 |
QtConcurrent::mapped(inputList, QTAIMEvaluateProperty); |
| 1424 |
futureWatcher.setFuture(future); |
| 1425 |
dialog.exec(); |
| 1426 |
futureWatcher.waitForFinished(); |
| 1427 |
|
| 1428 |
QList<QList<QVariant>> results; |
| 1429 |
if (futureWatcher.future().isCanceled()) { |
| 1430 |
results.clear(); |
| 1431 |
} else { |
| 1432 |
results = future.results(); |
| 1433 |
} |
| 1434 |
|
| 1435 |
// harvest results |
| 1436 |
for (qint64 i = 0; i < npts; ++i) { |
| 1437 |
for (qint64 m = 0; m < nmode; ++m) { |
| 1438 |
fval[m * nmode + i] = results.at(i).at(m).toDouble(); |
| 1439 |
} |
| 1440 |
} |
| 1441 |
} |
| 1442 |
|
| 1443 |
// TODO: Consider QVariantList. For now, mimic what is known to work. |
| 1444 |
// This version performs integration in Spherical Polar Coordinates. |
| 1445 |
// Note that the basin limits are not explicitly determined. |
| 1446 |
QList<QVariant> QTAIMEvaluatePropertyRTP(QList<QVariant> variantList) |
| 1447 |
{ |
| 1448 |
/* |
| 1449 |
Order of variantList: |
| 1450 |
QString wfnFileName |
| 1451 |
qreal r0 |
| 1452 |
qreal t0 |
| 1453 |
qreal p0 |
| 1454 |
qint64 nncp |
| 1455 |
qint64 xncp1 |
| 1456 |
qint64 yncp1 |
| 1457 |
qint64 zncp1 |
| 1458 |
qint64 xncp2 |
| 1459 |
qint64 yncp2 |
| 1460 |
qint64 zncp2 |
| 1461 |
... |
| 1462 |
qint64 nmode |
| 1463 |
qint64 mode1 |
| 1464 |
qint64 mode2 |
| 1465 |
... |
| 1466 |
qint64 nbasin |
| 1467 |
qint64 basin1 |
| 1468 |
qint64 basin2 |
| 1469 |
... |
| 1470 |
*/ |
| 1471 |
qint64 counter = 0; |
| 1472 |
QString wfnFileName = variantList.at(counter).toString(); |
| 1473 |
counter++; |
| 1474 |
qreal r0 = variantList.at(counter).toDouble(); |
| 1475 |
counter++; |
| 1476 |
qreal t0 = variantList.at(counter).toDouble(); |
| 1477 |
counter++; |
| 1478 |
qreal p0 = variantList.at(counter).toDouble(); |
| 1479 |
counter++; |
| 1480 |
|
| 1481 |
qint64 nncp = variantList.at(counter).toLongLong(); |
| 1482 |
counter++; |
| 1483 |
QList<QVector3D> ncpList; |
| 1484 |
for (qint64 n = 0; n < nncp; ++n) { |
| 1485 |
qreal x = variantList.at(counter).toDouble(); |
| 1486 |
counter++; |
| 1487 |
qreal y = variantList.at(counter).toDouble(); |
| 1488 |
counter++; |
| 1489 |
qreal z = variantList.at(counter).toDouble(); |
| 1490 |
counter++; |
| 1491 |
|
| 1492 |
ncpList.append(QVector3D(x, y, z)); |
| 1493 |
} |
| 1494 |
|
| 1495 |
qint64 nmode = variantList.at(counter).toLongLong(); |
| 1496 |
counter++; |
| 1497 |
QList<qint64> modeList; |
| 1498 |
for (qint64 m = 0; m < nmode; ++m) { |
| 1499 |
qint64 mode = variantList.at(counter).toLongLong(); |
| 1500 |
counter++; |
| 1501 |
modeList.append(mode); |
| 1502 |
} |
| 1503 |
|
| 1504 |
qint64 nbasin = variantList.at(counter).toLongLong(); |
| 1505 |
counter++; |
| 1506 |
QList<qint64> basinList; |
| 1507 |
for (qint64 b = 0; b < nbasin; ++b) { |
| 1508 |
qint64 basin = variantList.at(counter).toLongLong(); |
| 1509 |
counter++; |
| 1510 |
basinList.append(basin); |
| 1511 |
} |
| 1512 |
QSet<qint64> basinSet(basinList.begin(), basinList.end()); |
| 1513 |
|
| 1514 |
Matrix<qreal, 3, 1> r0t0p0; |
| 1515 |
r0t0p0 << r0, t0, p0; |
| 1516 |
Matrix<qreal, 3, 1> origin; |
| 1517 |
origin << ncpList.at(basinList.at(0)).x(), ncpList.at(basinList.at(0)).y(), |
| 1518 |
ncpList.at(basinList.at(0)).z(); |
| 1519 |
|
| 1520 |
Matrix<qreal, 3, 1> x0y0z0 = |
| 1521 |
QTAIMMathUtilities::sphericalToCartesian(r0t0p0, origin); |
| 1522 |
|
| 1523 |
qreal x0 = x0y0z0(0); |
| 1524 |
qreal y0 = x0y0z0(1); |
| 1525 |
qreal z0 = x0y0z0(2); |
| 1526 |
|
| 1527 |
QTAIMWavefunction wfn; |
| 1528 |
wfn.loadFromBinaryFile(wfnFileName); |
| 1529 |
|
| 1530 |
QTAIMWavefunctionEvaluator eval(wfn); |
| 1531 |
|
| 1532 |
QList<QVariant> valueList; |
| 1533 |
|
| 1534 |
double initialElectronDensity = |
| 1535 |
eval.electronDensity(Eigen::Vector3d(x0, y0, z0)); |
| 1536 |
|
| 1537 |
// if less than some small value, then return zero for all integrands. |
| 1538 |
if (initialElectronDensity < 1.e-5) { |
| 1539 |
for (qint64 m = 0; m < nmode; ++m) { |
| 1540 |
qreal zero = 0.0; |
| 1541 |
valueList.append(zero); |
| 1542 |
} |
| 1543 |
} else { |
| 1544 |
QList<QPair<QVector3D, qreal>> betaSpheres; |
| 1545 |
for (qint64 i = 0; i < nncp; ++i) { |
| 1546 |
QPair<QVector3D, qreal> thisBetaSphere; |
| 1547 |
thisBetaSphere.first = |
| 1548 |
QVector3D(ncpList.at(i).x(), ncpList.at(i).y(), ncpList.at(i).z()); |
| 1549 |
thisBetaSphere.second = 0.10; |
| 1550 |
betaSpheres.append(thisBetaSphere); |
| 1551 |
} |
| 1552 |
|
| 1553 |
QTAIMLSODAIntegrator ode(eval, 0); |
| 1554 |
// Avogadro::QTAIMODEIntegrator ode(eval,0); |
| 1555 |
|
| 1556 |
ode.setBetaSpheres(betaSpheres); |
| 1557 |
|
| 1558 |
QVector3D endpoint = ode.integrate(QVector3D(x0, y0, z0)); |
| 1559 |
// QList<QVector3D> path=ode.path(); |
| 1560 |
|
| 1561 |
#define HUGE_REAL_NUMBER 1.e20 |
| 1562 |
qreal smallestDistance = HUGE_REAL_NUMBER; |
| 1563 |
qint64 smallestDistanceIndex = -1; |
| 1564 |
|
| 1565 |
for (qint64 n = 0; n < betaSpheres.length(); ++n) { |
| 1566 |
Matrix<qreal, 3, 1> a(endpoint.x(), endpoint.y(), endpoint.z()); |
| 1567 |
Matrix<qreal, 3, 1> b(betaSpheres.at(n).first.x(), |
| 1568 |
betaSpheres.at(n).first.y(), |
| 1569 |
betaSpheres.at(n).first.z()); |
| 1570 |
|
| 1571 |
qreal distance = QTAIMMathUtilities::distance(a, b); |
| 1572 |
|
| 1573 |
if (distance < smallestDistance) { |
| 1574 |
smallestDistance = distance; |
| 1575 |
smallestDistanceIndex = n; |
| 1576 |
} |
| 1577 |
} |
| 1578 |
qint64 nucleusIndex = smallestDistanceIndex; |
| 1579 |
|
| 1580 |
if (basinSet.contains(nucleusIndex)) { |
| 1581 |
// if(nucleusIndex==0) |
| 1582 |
// { |
| 1583 |
// QFile file("/scratch/brown/0.txt"); |
| 1584 |
// file.open(QIODevice::WriteOnly | QIODevice::Append); |
| 1585 |
// QTextStream out(&file); |
| 1586 |
// out << x0 << " " << y0 << " " << z0 << "\n"; |
| 1587 |
//// out << r0 << " " << t0 << " " << p0 << "\n"; |
| 1588 |
// file.close(); |
| 1589 |
// } |
| 1590 |
for (qint64 m = 0; m < nmode; ++m) { |
| 1591 |
if (modeList.at(m) == 0) { |
| 1592 |
valueList.append( |
| 1593 |
|
| 1594 |
r0 * r0 * sin(t0) * |
| 1595 |
eval.electronDensity(Eigen::Vector3d(x0, y0, z0)) |
| 1596 |
|
| 1597 |
); |
| 1598 |
} else { |
| 1599 |
qDebug() << "mode not defined"; |
| 1600 |
qreal zero = 0.0; |
| 1601 |
valueList.append(zero); |
| 1602 |
} |
| 1603 |
} |
| 1604 |
} else { |
| 1605 |
for (qint64 m = 0; m < nmode; ++m) { |
| 1606 |
qreal zero = 0.0; |
| 1607 |
valueList.append(zero); |
| 1608 |
} |
| 1609 |
} |
| 1610 |
} |
| 1611 |
|
| 1612 |
return valueList; |
| 1613 |
} |
| 1614 |
|
| 1615 |
void property_v_rtp(unsigned int /* ndim */, unsigned int npts, |
| 1616 |
const double* xyz, void* param, unsigned int /* fdim */, |
| 1617 |
double* fval) |
| 1618 |
{ |
| 1619 |
|
| 1620 |
auto* paramVariantListPtr = (QVariantList*)param; |
| 1621 |
QVariantList paramVariantList = *paramVariantListPtr; |
| 1622 |
|
| 1623 |
qint64 counter = 0; |
| 1624 |
QString wfnFileName = paramVariantList.at(counter).toString(); |
| 1625 |
counter++; |
| 1626 |
|
| 1627 |
qint64 nncp = paramVariantList.at(counter).toLongLong(); |
| 1628 |
counter++; |
| 1629 |
QList<QVector3D> ncpList; |
| 1630 |
for (qint64 i = 0; i < nncp; ++i) { |
| 1631 |
qreal x = paramVariantList.at(counter).toDouble(); |
| 1632 |
counter++; |
| 1633 |
qreal y = paramVariantList.at(counter).toDouble(); |
| 1634 |
counter++; |
| 1635 |
qreal z = paramVariantList.at(counter).toDouble(); |
| 1636 |
counter++; |
| 1637 |
|
| 1638 |
ncpList.append(QVector3D(x, y, z)); |
| 1639 |
} |
| 1640 |
qint64 nmode = 1; |
| 1641 |
qint64 mode = paramVariantList.at(counter).toLongLong(); |
| 1642 |
counter++; |
| 1643 |
QList<qint64> basinList; |
| 1644 |
for (qint64 i = counter; i < paramVariantList.length(); ++i) { |
| 1645 |
basinList.append(paramVariantList.at(i).toLongLong()); |
| 1646 |
counter++; |
| 1647 |
} |
| 1648 |
|
| 1649 |
// prepare input |
| 1650 |
|
| 1651 |
QList<QList<QVariant>> inputList; |
| 1652 |
|
| 1653 |
for (unsigned int i = 0; i < npts; ++i) { |
| 1654 |
|
| 1655 |
double x0 = xyz[i * 3 + 0]; |
| 1656 |
double y0 = xyz[i * 3 + 1]; |
| 1657 |
double z0 = xyz[i * 3 + 2]; |
| 1658 |
|
| 1659 |
QList<QVariant> variantList; |
| 1660 |
|
| 1661 |
variantList.append(wfnFileName); |
| 1662 |
|
| 1663 |
variantList.append(x0); |
| 1664 |
variantList.append(y0); |
| 1665 |
variantList.append(z0); |
| 1666 |
|
| 1667 |
variantList.append(nncp); |
| 1668 |
for (qint64 n = 0; n < nncp; ++n) { |
| 1669 |
variantList.append(ncpList.at(n).x()); |
| 1670 |
variantList.append(ncpList.at(n).y()); |
| 1671 |
variantList.append(ncpList.at(n).z()); |
| 1672 |
} |
| 1673 |
|
| 1674 |
variantList.append(nmode); // for now, one mode |
| 1675 |
for (qint64 m = 0; m < nmode; ++m) { |
| 1676 |
variantList.append(mode); |
| 1677 |
} |
| 1678 |
|
| 1679 |
qint64 nbasin = basinList.length(); |
| 1680 |
variantList.append(nbasin); |
| 1681 |
for (long long b : basinList) { |
| 1682 |
variantList.append(b); |
| 1683 |
} |
| 1684 |
|
| 1685 |
inputList.append(variantList); |
| 1686 |
} |
| 1687 |
|
| 1688 |
// calculate |
| 1689 |
|
| 1690 |
QProgressDialog dialog; |
| 1691 |
dialog.setWindowTitle("QTAIM"); |
| 1692 |
dialog.setLabelText(QString("Atomic Basin Integration")); |
| 1693 |
|
| 1694 |
QFutureWatcher<QList<QVariant>> futureWatcher; |
| 1695 |
QObject::connect(&futureWatcher, SIGNAL(finished()), &dialog, SLOT(reset())); |
| 1696 |
QObject::connect(&dialog, SIGNAL(canceled()), &futureWatcher, SLOT(cancel())); |
| 1697 |
QObject::connect(&futureWatcher, SIGNAL(progressRangeChanged(int, int)), |
| 1698 |
&dialog, SLOT(setRange(int, int))); |
| 1699 |
QObject::connect(&futureWatcher, SIGNAL(progressValueChanged(int)), &dialog, |
| 1700 |
SLOT(setValue(int))); |
| 1701 |
|
| 1702 |
QFuture<QList<QVariant>> future = |
| 1703 |
QtConcurrent::mapped(inputList, QTAIMEvaluatePropertyRTP); |
| 1704 |
futureWatcher.setFuture(future); |
| 1705 |
dialog.exec(); |
| 1706 |
futureWatcher.waitForFinished(); |
| 1707 |
|
| 1708 |
QList<QList<QVariant>> results; |
| 1709 |
if (futureWatcher.future().isCanceled()) { |
| 1710 |
results.clear(); |
| 1711 |
} else { |
| 1712 |
results = future.results(); |
| 1713 |
} |
| 1714 |
|
| 1715 |
// harvest results |
| 1716 |
for (qint64 i = 0; i < npts; ++i) { |
| 1717 |
for (qint64 m = 0; m < nmode; ++m) { |
| 1718 |
fval[m * nmode + i] = results.at(i).at(m).toDouble(); |
| 1719 |
} |
| 1720 |
} |
| 1721 |
} |
| 1722 |
|
| 1723 |
void property_r(unsigned int ndim, const double* xyz, void* param, |
| 1724 |
unsigned int fdim, double* fval) |
| 1725 |
{ |
| 1726 |
|
| 1727 |
ndim = ndim; |
| 1728 |
fdim = fdim; |
| 1729 |
|
| 1730 |
auto* paramVariantListPtr = (QVariantList*)param; |
| 1731 |
QVariantList paramVariantList = *paramVariantListPtr; |
| 1732 |
|
| 1733 |
qint64 counter = 0; |
| 1734 |
QString wfnFileName = paramVariantList.at(counter).toString(); |
| 1735 |
counter++; |
| 1736 |
|
| 1737 |
qreal r = xyz[0]; |
| 1738 |
qreal t = paramVariantList.at(counter).toDouble(); |
| 1739 |
counter++; |
| 1740 |
qreal p = paramVariantList.at(counter).toDouble(); |
| 1741 |
counter++; |
| 1742 |
|
| 1743 |
qint64 nncp = paramVariantList.at(counter).toLongLong(); |
| 1744 |
counter++; |
| 1745 |
QList<QVector3D> ncpList; |
| 1746 |
for (qint64 i = 0; i < nncp; ++i) { |
| 1747 |
qreal x = paramVariantList.at(counter).toDouble(); |
| 1748 |
counter++; |
| 1749 |
qreal y = paramVariantList.at(counter).toDouble(); |
| 1750 |
counter++; |
| 1751 |
qreal z = paramVariantList.at(counter).toDouble(); |
| 1752 |
counter++; |
| 1753 |
|
| 1754 |
ncpList.append(QVector3D(x, y, z)); |
| 1755 |
} |
| 1756 |
qint64 nmode = 1; |
| 1757 |
qint64 mode = paramVariantList.at(counter).toLongLong(); |
| 1758 |
counter++; |
| 1759 |
QList<qint64> basinList; |
| 1760 |
for (qint64 i = counter; i < paramVariantList.length(); ++i) { |
| 1761 |
basinList.append(paramVariantList.at(i).toLongLong()); |
| 1762 |
counter++; |
| 1763 |
} |
| 1764 |
|
| 1765 |
Matrix<qreal, 3, 1> rtp; |
| 1766 |
rtp << r, t, p; |
| 1767 |
Matrix<qreal, 3, 1> origin; |
| 1768 |
origin << ncpList.at(basinList.at(0)).x(), ncpList.at(basinList.at(0)).y(), |
| 1769 |
ncpList.at(basinList.at(0)).z(); |
| 1770 |
|
| 1771 |
Matrix<qreal, 3, 1> XYZ = |
| 1772 |
QTAIMMathUtilities::sphericalToCartesian(rtp, origin); |
| 1773 |
|
| 1774 |
qreal x = XYZ(0); |
| 1775 |
qreal y = XYZ(1); |
| 1776 |
qreal z = XYZ(2); |
| 1777 |
|
| 1778 |
// This routine reads the wavefunction file repeatedly. |
| 1779 |
// Let's hope that this time is dwarfed by the time |
| 1780 |
// taken to delineate the atomic basins in the calling routine. |
| 1781 |
QTAIMWavefunction wfn; |
| 1782 |
wfn.loadFromBinaryFile(wfnFileName); |
| 1783 |
QTAIMWavefunctionEvaluator eval(wfn); |
| 1784 |
|
| 1785 |
for (qint64 m = 0; m < nmode; ++m) { |
| 1786 |
if (mode == 0) { |
| 1787 |
fval[m] = r * r * eval.electronDensity(Eigen::Vector3d(x, y, z)); |
| 1788 |
} |
| 1789 |
} |
| 1790 |
} |
| 1791 |
|
| 1792 |
QList<QVariant> QTAIMEvaluatePropertyTP(QList<QVariant> variantList) |
| 1793 |
{ |
| 1794 |
|
| 1795 |
/* |
| 1796 |
Order of variantList: |
| 1797 |
QString wfnFileName |
| 1798 |
qreal t |
| 1799 |
qreal p |
| 1800 |
qint64 nncp |
| 1801 |
qint64 xncp1 |
| 1802 |
qint64 yncp1 |
| 1803 |
qint64 zncp1 |
| 1804 |
qint64 xncp2 |
| 1805 |
qint64 yncp2 |
| 1806 |
qint64 zncp2 |
| 1807 |
... |
| 1808 |
qint64 nmode |
| 1809 |
qint64 mode1 |
| 1810 |
qint64 mode2 |
| 1811 |
... |
| 1812 |
qint64 nbasin |
| 1813 |
qint64 basin1 |
| 1814 |
qint64 basin2 |
| 1815 |
... |
| 1816 |
*/ |
| 1817 |
qint64 counter = 0; |
| 1818 |
QString wfnFileName = variantList.at(counter).toString(); |
| 1819 |
counter++; |
| 1820 |
qreal t = variantList.at(counter).toDouble(); |
| 1821 |
counter++; |
| 1822 |
qreal p = variantList.at(counter).toDouble(); |
| 1823 |
counter++; |
| 1824 |
|
| 1825 |
qint64 nncp = variantList.at(counter).toLongLong(); |
| 1826 |
counter++; |
| 1827 |
QList<QVector3D> ncpList; |
| 1828 |
for (qint64 n = 0; n < nncp; ++n) { |
| 1829 |
qreal x = variantList.at(counter).toDouble(); |
| 1830 |
counter++; |
| 1831 |
qreal y = variantList.at(counter).toDouble(); |
| 1832 |
counter++; |
| 1833 |
qreal z = variantList.at(counter).toDouble(); |
| 1834 |
counter++; |
| 1835 |
|
| 1836 |
ncpList.append(QVector3D(x, y, z)); |
| 1837 |
} |
| 1838 |
|
| 1839 |
qint64 nmode = variantList.at(counter).toLongLong(); |
| 1840 |
counter++; |
| 1841 |
QList<qint64> modeList; |
| 1842 |
for (qint64 m = 0; m < nmode; ++m) { |
| 1843 |
qint64 mode = variantList.at(counter).toLongLong(); |
| 1844 |
counter++; |
| 1845 |
modeList.append(mode); |
| 1846 |
} |
| 1847 |
|
| 1848 |
qint64 nbasin = variantList.at(counter).toLongLong(); |
| 1849 |
counter++; |
| 1850 |
QList<qint64> basinList; |
| 1851 |
for (qint64 b = 0; b < nbasin; ++b) { |
| 1852 |
qint64 basin = variantList.at(counter).toLongLong(); |
| 1853 |
counter++; |
| 1854 |
basinList.append(basin); |
| 1855 |
} |
| 1856 |
QSet<qint64> basinSet(basinList.begin(), basinList.end()); |
| 1857 |
|
| 1858 |
QTAIMWavefunction wfn; |
| 1859 |
wfn.loadFromBinaryFile(wfnFileName); |
| 1860 |
QTAIMWavefunctionEvaluator eval(wfn); |
| 1861 |
|
| 1862 |
// Set up steepest ascent integrator and beta spheres |
| 1863 |
QList<QPair<QVector3D, qreal>> betaSpheres; |
| 1864 |
for (qint64 i = 0; i < nncp; ++i) { |
| 1865 |
QPair<QVector3D, qreal> thisBetaSphere; |
| 1866 |
thisBetaSphere.first = |
| 1867 |
QVector3D(ncpList.at(i).x(), ncpList.at(i).y(), ncpList.at(i).z()); |
| 1868 |
thisBetaSphere.second = 0.10; |
| 1869 |
betaSpheres.append(thisBetaSphere); |
| 1870 |
} |
| 1871 |
|
| 1872 |
QTAIMLSODAIntegrator ode(eval, 0); |
| 1873 |
// Avogadro::QTAIMODEIntegrator ode(eval,0); |
| 1874 |
|
| 1875 |
ode.setBetaSpheres(betaSpheres); |
| 1876 |
|
| 1877 |
// Determine radial basin limit via bisection |
| 1878 |
// Bisection Algorithm courtesy of Wikipedia |
| 1879 |
|
| 1880 |
qint64 thisBasin = basinList.at(0); |
| 1881 |
Matrix<qreal, 3, 1> origin; |
| 1882 |
origin << ncpList.at(thisBasin).x(), ncpList.at(thisBasin).y(), |
| 1883 |
ncpList.at(thisBasin).z(); |
| 1884 |
|
| 1885 |
const qreal rmin = betaSpheres.at(thisBasin).second; |
| 1886 |
const qreal rmax = 8.0; |
| 1887 |
const qreal epsilon = 1.e-3; |
| 1888 |
|
| 1889 |
qreal left = rmin; |
| 1890 |
qreal right = rmax; |
| 1891 |
|
| 1892 |
Matrix<qreal, 3, 1> rtpl; |
| 1893 |
rtpl << left, t, p; |
| 1894 |
Matrix<qreal, 3, 1> xyzl = |
| 1895 |
QTAIMMathUtilities::sphericalToCartesian(rtpl, origin); |
| 1896 |
|
| 1897 |
qreal fleft; |
| 1898 |
qreal x = xyzl(0); |
| 1899 |
qreal y = xyzl(1); |
| 1900 |
qreal z = xyzl(2); |
| 1901 |
qreal leftElectronDensity = eval.electronDensity(Eigen::Vector3d(x, y, z)); |
| 1902 |
|
| 1903 |
if (leftElectronDensity < 1.e-5) { |
| 1904 |
fleft = -1.0; |
| 1905 |
} else { |
| 1906 |
QVector3D endpoint = ode.integrate(QVector3D(x, y, z)); |
| 1907 |
|
| 1908 |
#define HUGE_REAL_NUMBER 1.e20 |
| 1909 |
qreal smallestDistance = HUGE_REAL_NUMBER; |
| 1910 |
qint64 smallestDistanceIndex = -1; |
| 1911 |
|
| 1912 |
for (qint64 n = 0; n < betaSpheres.length(); ++n) { |
| 1913 |
Matrix<qreal, 3, 1> a(endpoint.x(), endpoint.y(), endpoint.z()); |
| 1914 |
Matrix<qreal, 3, 1> b(betaSpheres.at(n).first.x(), |
| 1915 |
betaSpheres.at(n).first.y(), |
| 1916 |
betaSpheres.at(n).first.z()); |
| 1917 |
|
| 1918 |
qreal distance = QTAIMMathUtilities::distance(a, b); |
| 1919 |
|
| 1920 |
if (distance < smallestDistance) { |
| 1921 |
smallestDistance = distance; |
| 1922 |
smallestDistanceIndex = n; |
| 1923 |
} |
| 1924 |
} |
| 1925 |
qint64 nucleusIndex = smallestDistanceIndex; |
| 1926 |
|
| 1927 |
if (thisBasin == nucleusIndex) { |
| 1928 |
fleft = leftElectronDensity; |
| 1929 |
} else { |
| 1930 |
fleft = -1.0; |
| 1931 |
} |
| 1932 |
} |
| 1933 |
|
| 1934 |
Matrix<qreal, 3, 1> rtpr; |
| 1935 |
rtpr << right, t, p; |
| 1936 |
Matrix<qreal, 3, 1> xyzr = |
| 1937 |
QTAIMMathUtilities::sphericalToCartesian(rtpr, origin); |
| 1938 |
|
| 1939 |
qreal fright; |
| 1940 |
x = xyzr(0); |
| 1941 |
y = xyzr(1); |
| 1942 |
z = xyzr(2); |
| 1943 |
qreal rightElectronDensity = eval.electronDensity(Eigen::Vector3d(x, y, z)); |
| 1944 |
|
| 1945 |
if (rightElectronDensity < 1.e-5) { |
| 1946 |
fright = -1.0; |
| 1947 |
} else { |
| 1948 |
QVector3D endpoint = ode.integrate(QVector3D(x, y, z)); |
| 1949 |
|
| 1950 |
#define HUGE_REAL_NUMBER 1.e20 |
| 1951 |
qreal smallestDistance = HUGE_REAL_NUMBER; |
| 1952 |
qint64 smallestDistanceIndex = -1; |
| 1953 |
|
| 1954 |
for (qint64 n = 0; n < betaSpheres.length(); ++n) { |
| 1955 |
Matrix<qreal, 3, 1> a(endpoint.x(), endpoint.y(), endpoint.z()); |
| 1956 |
Matrix<qreal, 3, 1> b(betaSpheres.at(n).first.x(), |
| 1957 |
betaSpheres.at(n).first.y(), |
| 1958 |
betaSpheres.at(n).first.z()); |
| 1959 |
|
| 1960 |
qreal distance = QTAIMMathUtilities::distance(a, b); |
| 1961 |
|
| 1962 |
if (distance < smallestDistance) { |
| 1963 |
smallestDistance = distance; |
| 1964 |
smallestDistanceIndex = n; |
| 1965 |
} |
| 1966 |
} |
| 1967 |
qint64 nucleusIndex = smallestDistanceIndex; |
| 1968 |
|
| 1969 |
if (thisBasin == nucleusIndex) { |
| 1970 |
fright = rightElectronDensity; |
| 1971 |
} else { |
| 1972 |
fright = -1.0; |
| 1973 |
} |
| 1974 |
} |
| 1975 |
|
| 1976 |
if (fleft > 0.0 && fright > 0.0) { |
| 1977 |
qDebug() << "error in bisection: both values positive."; |
| 1978 |
} |
| 1979 |
|
| 1980 |
qreal rf(0.0); |
| 1981 |
while (fabs(right - left) > 2.0 * epsilon) { |
| 1982 |
|
| 1983 |
qreal midpoint = (right + left) / 2.0; |
| 1984 |
rf = midpoint; |
| 1985 |
|
| 1986 |
// qDebug() << left << midpoint << right ; |
| 1987 |
|
| 1988 |
Matrix<qreal, 3, 1> rtpm; |
| 1989 |
rtpm << midpoint, t, p; |
| 1990 |
Matrix<qreal, 3, 1> xyzm = |
| 1991 |
QTAIMMathUtilities::sphericalToCartesian(rtpm, origin); |
| 1992 |
|
| 1993 |
qreal fmidpoint; |
| 1994 |
x = xyzm(0); |
| 1995 |
y = xyzm(1); |
| 1996 |
z = xyzm(2); |
| 1997 |
qreal midpointElectronDensity = |
| 1998 |
eval.electronDensity(Eigen::Vector3d(x, y, z)); |
| 1999 |
|
| 2000 |
if (midpointElectronDensity < 1.e-5) { |
| 2001 |
fmidpoint = -1.0; |
| 2002 |
} else { |
| 2003 |
QVector3D endpoint = ode.integrate(QVector3D(x, y, z)); |
| 2004 |
|
| 2005 |
#define HUGE_REAL_NUMBER 1.e20 |
| 2006 |
qreal smallestDistance = HUGE_REAL_NUMBER; |
| 2007 |
qint64 smallestDistanceIndex = -1; |
| 2008 |
|
| 2009 |
for (qint64 n = 0; n < betaSpheres.length(); ++n) { |
| 2010 |
Matrix<qreal, 3, 1> a(endpoint.x(), endpoint.y(), endpoint.z()); |
| 2011 |
Matrix<qreal, 3, 1> b(betaSpheres.at(n).first.x(), |
| 2012 |
betaSpheres.at(n).first.y(), |
| 2013 |
betaSpheres.at(n).first.z()); |
| 2014 |
|
| 2015 |
qreal distance = QTAIMMathUtilities::distance(a, b); |
| 2016 |
|
| 2017 |
if (distance < smallestDistance) { |
| 2018 |
smallestDistance = distance; |
| 2019 |
smallestDistanceIndex = n; |
| 2020 |
} |
| 2021 |
} |
| 2022 |
qint64 nucleusIndex = smallestDistanceIndex; |
| 2023 |
|
| 2024 |
if (thisBasin == nucleusIndex) { |
| 2025 |
fmidpoint = midpointElectronDensity; |
| 2026 |
} else { |
| 2027 |
fmidpoint = -1.0; |
| 2028 |
} |
| 2029 |
} |
| 2030 |
|
| 2031 |
if ((fleft * fmidpoint) < 0) { |
| 2032 |
right = midpoint; |
| 2033 |
fright = fmidpoint; |
| 2034 |
} else if ((fright * fmidpoint) < 0) { |
| 2035 |
left = midpoint; |
| 2036 |
fleft = fmidpoint; |
| 2037 |
} else { |
| 2038 |
goto endOfBisection; |
| 2039 |
} |
| 2040 |
} |
| 2041 |
endOfBisection: |
| 2042 |
|
| 2043 |
// Integration over r |
| 2044 |
unsigned int fdim = 1; |
| 2045 |
double* val; |
| 2046 |
double* err; |
| 2047 |
val = (double*)malloc(sizeof(double) * fdim); |
| 2048 |
err = (double*)malloc(sizeof(double) * fdim); |
| 2049 |
|
| 2050 |
double tol = 1.e-6; |
| 2051 |
unsigned int maxEval = 0; |
| 2052 |
|
| 2053 |
unsigned int dim = 1; |
| 2054 |
|
| 2055 |
double* xmin; |
| 2056 |
double* xmax; |
| 2057 |
xmin = (double*)malloc(dim * sizeof(double)); |
| 2058 |
xmax = (double*)malloc(dim * sizeof(double)); |
| 2059 |
|
| 2060 |
xmin[0] = 0.0; |
| 2061 |
xmax[0] = rf; |
| 2062 |
|
| 2063 |
QVariantList paramVariantList; |
| 2064 |
paramVariantList.append(wfnFileName); |
| 2065 |
paramVariantList.append(t); |
| 2066 |
paramVariantList.append(p); |
| 2067 |
paramVariantList.append( |
| 2068 |
ncpList.length()); // number of nuclear critical points |
| 2069 |
for (auto j : ncpList) { |
| 2070 |
paramVariantList.append(j.x()); |
| 2071 |
paramVariantList.append(j.y()); |
| 2072 |
paramVariantList.append(j.z()); |
| 2073 |
} |
| 2074 |
paramVariantList.append(0); // mode |
| 2075 |
paramVariantList.append(basinList.at(0)); // basin |
| 2076 |
|
| 2077 |
// qDebug() << "Into R with rf=" << rf; |
| 2078 |
adapt_integrate(fdim, property_r, ¶mVariantList, dim, xmin, xmax, maxEval, |
| 2079 |
tol, 0, val, err); |
| 2080 |
// qDebug() << "Out of R with val=" << val[0] << "err=" << err[0]; |
| 2081 |
qreal Rval = val[0]; |
| 2082 |
|
| 2083 |
free(xmin); |
| 2084 |
free(xmax); |
| 2085 |
free(val); |
| 2086 |
free(err); |
| 2087 |
|
| 2088 |
// QList<QVariant> variantList; |
| 2089 |
|
| 2090 |
variantList.append(sin(t) * Rval); |
| 2091 |
|
| 2092 |
// qDebug() << rf << t << p << sin(t) * Rval; |
| 2093 |
|
| 2094 |
return variantList; |
| 2095 |
} |
| 2096 |
|
| 2097 |
void property_v_tp(unsigned int /* ndim */, unsigned int npts, |
| 2098 |
const double* xyz, void* param, unsigned int /* fdim */, |
| 2099 |
double* fval) |
| 2100 |
{ |
| 2101 |
|
| 2102 |
auto* paramVariantListPtr = (QVariantList*)param; |
| 2103 |
QVariantList paramVariantList = *paramVariantListPtr; |
| 2104 |
|
| 2105 |
qint64 counter = 0; |
| 2106 |
QString wfnFileName = paramVariantList.at(counter).toString(); |
| 2107 |
counter++; |
| 2108 |
|
| 2109 |
qint64 nncp = paramVariantList.at(counter).toLongLong(); |
| 2110 |
counter++; |
| 2111 |
QList<QVector3D> ncpList; |
| 2112 |
for (qint64 i = 0; i < nncp; ++i) { |
| 2113 |
qreal x = paramVariantList.at(counter).toDouble(); |
| 2114 |
counter++; |
| 2115 |
qreal y = paramVariantList.at(counter).toDouble(); |
| 2116 |
counter++; |
| 2117 |
qreal z = paramVariantList.at(counter).toDouble(); |
| 2118 |
counter++; |
| 2119 |
|
| 2120 |
ncpList.append(QVector3D(x, y, z)); |
| 2121 |
} |
| 2122 |
qint64 nmode = 1; |
| 2123 |
qint64 mode = paramVariantList.at(counter).toLongLong(); |
| 2124 |
counter++; |
| 2125 |
QList<qint64> basinList; |
| 2126 |
for (qint64 i = counter; i < paramVariantList.length(); ++i) { |
| 2127 |
basinList.append(paramVariantList.at(i).toLongLong()); |
| 2128 |
counter++; |
| 2129 |
} |
| 2130 |
|
| 2131 |
// prepare input |
| 2132 |
|
| 2133 |
QList<QList<QVariant>> inputList; |
| 2134 |
|
| 2135 |
for (unsigned int i = 0; i < npts; ++i) { |
| 2136 |
|
| 2137 |
double t = xyz[i * 2 + 0]; |
| 2138 |
double p = xyz[i * 2 + 1]; |
| 2139 |
|
| 2140 |
QList<QVariant> variantList; |
| 2141 |
|
| 2142 |
variantList.append(wfnFileName); |
| 2143 |
|
| 2144 |
variantList.append(t); |
| 2145 |
variantList.append(p); |
| 2146 |
|
| 2147 |
variantList.append(nncp); |
| 2148 |
for (qint64 n = 0; n < nncp; ++n) { |
| 2149 |
variantList.append(ncpList.at(n).x()); |
| 2150 |
variantList.append(ncpList.at(n).y()); |
| 2151 |
variantList.append(ncpList.at(n).z()); |
| 2152 |
} |
| 2153 |
|
| 2154 |
variantList.append(nmode); // for now, one mode |
| 2155 |
for (qint64 m = 0; m < nmode; ++m) { |
| 2156 |
variantList.append(mode); |
| 2157 |
} |
| 2158 |
|
| 2159 |
qint64 nbasin = basinList.length(); |
| 2160 |
variantList.append(nbasin); |
| 2161 |
for (long long b : basinList) { |
| 2162 |
variantList.append(b); |
| 2163 |
} |
| 2164 |
|
| 2165 |
inputList.append(variantList); |
| 2166 |
} |
| 2167 |
|
| 2168 |
// calculate |
| 2169 |
|
| 2170 |
QProgressDialog dialog; |
| 2171 |
dialog.setWindowTitle("QTAIM"); |
| 2172 |
dialog.setLabelText(QString("Atomic Basin Integration")); |
| 2173 |
|
| 2174 |
QFutureWatcher<QList<QVariant>> futureWatcher; |
| 2175 |
QObject::connect(&futureWatcher, SIGNAL(finished()), &dialog, SLOT(reset())); |
| 2176 |
QObject::connect(&dialog, SIGNAL(canceled()), &futureWatcher, SLOT(cancel())); |
| 2177 |
QObject::connect(&futureWatcher, SIGNAL(progressRangeChanged(int, int)), |
| 2178 |
&dialog, SLOT(setRange(int, int))); |
| 2179 |
QObject::connect(&futureWatcher, SIGNAL(progressValueChanged(int)), &dialog, |
| 2180 |
SLOT(setValue(int))); |
| 2181 |
|
| 2182 |
QFuture<QList<QVariant>> future = |
| 2183 |
QtConcurrent::mapped(inputList, QTAIMEvaluatePropertyTP); |
| 2184 |
futureWatcher.setFuture(future); |
| 2185 |
dialog.exec(); |
| 2186 |
futureWatcher.waitForFinished(); |
| 2187 |
|
| 2188 |
QList<QList<QVariant>> results; |
| 2189 |
if (futureWatcher.future().isCanceled()) { |
| 2190 |
results.clear(); |
| 2191 |
} else { |
| 2192 |
results = future.results(); |
| 2193 |
} |
| 2194 |
|
| 2195 |
// harvest results |
| 2196 |
// qDebug() << "results=" << results; |
| 2197 |
for (qint64 i = 0; i < npts; ++i) { |
| 2198 |
for (qint64 m = 0; m < nmode; ++m) { |
| 2199 |
fval[m * nmode + i] = results.at(i).at(m).toDouble(); |
| 2200 |
} |
| 2201 |
} |
| 2202 |
} |
| 2203 |
|
| 2204 |
namespace Avogadro::QtPlugins { |
| 2205 |
|
| 2206 |
QTAIMCubature::QTAIMCubature(QTAIMWavefunction& wfn) |
| 2207 |
{ |
| 2208 |
|
| 2209 |
m_wfn = &wfn; |
| 2210 |
|
| 2211 |
m_temporaryFileName = QTAIMCubature::temporaryFileName(); |
| 2212 |
m_wfn->saveToBinaryFile(m_temporaryFileName); |
| 2213 |
|
| 2214 |
// Instantiate a Critical Point Locator |
| 2215 |
QTAIMCriticalPointLocator cpl(wfn); |
| 2216 |
|
| 2217 |
// Locate the Nuclear Critical Points |
| 2218 |
cpl.locateNuclearCriticalPoints(); |
| 2219 |
|
| 2220 |
// QLists of results |
| 2221 |
m_ncpList = cpl.nuclearCriticalPoints(); |
| 2222 |
} |
| 2223 |
|
| 2224 |
QList<QPair<qreal, qreal>> QTAIMCubature::integrate(qint64 mode, |
| 2225 |
QList<qint64> basins) |
| 2226 |
{ |
| 2227 |
|
| 2228 |
QList<QPair<qreal, qreal>> value; |
| 2229 |
|
| 2230 |
m_mode = mode; |
| 2231 |
m_basins = basins; |
| 2232 |
|
| 2233 |
double tol = 1.e-2; |
| 2234 |
unsigned int maxEval = 0; |
| 2235 |
|
| 2236 |
bool threeDimensionalIntegration = false; |
| 2237 |
bool cartesianIntegrationLimits = false; |
| 2238 |
|
| 2239 |
unsigned int fdim = 1; |
| 2240 |
double* val; |
| 2241 |
double* err; |
| 2242 |
val = (double*)malloc(sizeof(double) * fdim); |
| 2243 |
err = (double*)malloc(sizeof(double) * fdim); |
| 2244 |
|
| 2245 |
for (qint64 i = 0; i < m_basins.length(); ++i) { |
| 2246 |
if (threeDimensionalIntegration) { |
| 2247 |
|
| 2248 |
unsigned int dim = 3; |
| 2249 |
|
| 2250 |
double* xmin; |
| 2251 |
double* xmax; |
| 2252 |
xmin = (double*)malloc(dim * sizeof(double)); |
| 2253 |
xmax = (double*)malloc(dim * sizeof(double)); |
| 2254 |
|
| 2255 |
if (cartesianIntegrationLimits) { |
| 2256 |
|
| 2257 |
// shift origin of the integration to the nuclear coordinates of the ith |
| 2258 |
// nucleus. |
| 2259 |
|
| 2260 |
xmin[0] = -8. + m_ncpList.at(i).x(); |
| 2261 |
xmax[0] = 8. + m_ncpList.at(i).x(); |
| 2262 |
xmin[1] = -8. + m_ncpList.at(i).y(); |
| 2263 |
xmax[1] = 8. + m_ncpList.at(i).y(); |
| 2264 |
xmin[2] = -8. + m_ncpList.at(i).z(); |
| 2265 |
xmax[2] = 8. + m_ncpList.at(i).z(); |
| 2266 |
|
| 2267 |
QVariantList paramVariantList; |
| 2268 |
paramVariantList.append(m_temporaryFileName); |
| 2269 |
|
| 2270 |
paramVariantList.append( |
| 2271 |
m_ncpList.length()); // number of nuclear critical points |
| 2272 |
for (auto j : m_ncpList) { |
| 2273 |
paramVariantList.append(j.x()); |
| 2274 |
paramVariantList.append(j.y()); |
| 2275 |
paramVariantList.append(j.z()); |
| 2276 |
} |
| 2277 |
paramVariantList.append(0); // mode |
| 2278 |
paramVariantList.append(basins.at(i)); // basin |
| 2279 |
|
| 2280 |
adapt_integrate_v(fdim, property_v, ¶mVariantList, dim, xmin, xmax, |
| 2281 |
maxEval, tol, 0, val, err); |
| 2282 |
|
| 2283 |
} else { |
| 2284 |
const qreal pi = 4.0 * atan(1.0); |
| 2285 |
|
| 2286 |
xmin[0] = 0.; |
| 2287 |
xmax[0] = 8.; |
| 2288 |
xmin[1] = 0.; |
| 2289 |
xmax[1] = pi; |
| 2290 |
xmin[2] = 0.; |
| 2291 |
xmax[2] = 2.0 * pi; |
| 2292 |
|
| 2293 |
QVariantList paramVariantList; |
| 2294 |
paramVariantList.append(m_temporaryFileName); |
| 2295 |
|
| 2296 |
paramVariantList.append( |
| 2297 |
m_ncpList.length()); // number of nuclear critical points |
| 2298 |
for (auto j : m_ncpList) { |
| 2299 |
paramVariantList.append(j.x()); |
| 2300 |
paramVariantList.append(j.y()); |
| 2301 |
paramVariantList.append(j.z()); |
| 2302 |
} |
| 2303 |
paramVariantList.append(0); // mode |
| 2304 |
paramVariantList.append(basins.at(i)); // basin |
| 2305 |
|
| 2306 |
adapt_integrate_v(fdim, property_v_rtp, ¶mVariantList, dim, xmin, |
| 2307 |
xmax, maxEval, tol, 0, val, err); |
| 2308 |
} |
| 2309 |
|
| 2310 |
free(xmin); |
| 2311 |
free(xmax); |
| 2312 |
|
| 2313 |
} else { |
| 2314 |
unsigned int dim = 2; |
| 2315 |
|
| 2316 |
double* xmin; |
| 2317 |
double* xmax; |
| 2318 |
xmin = (double*)malloc(dim * sizeof(double)); |
| 2319 |
xmax = (double*)malloc(dim * sizeof(double)); |
| 2320 |
|
| 2321 |
const qreal pi = 4.0 * atan(1.0); |
| 2322 |
|
| 2323 |
xmin[0] = 0.; |
| 2324 |
xmax[0] = pi; |
| 2325 |
xmin[1] = 0.; |
| 2326 |
xmax[1] = 2.0 * pi; |
| 2327 |
|
| 2328 |
QVariantList paramVariantList; |
| 2329 |
paramVariantList.append(m_temporaryFileName); |
| 2330 |
|
| 2331 |
paramVariantList.append( |
| 2332 |
m_ncpList.length()); // number of nuclear critical points |
| 2333 |
for (auto j : m_ncpList) { |
| 2334 |
paramVariantList.append(j.x()); |
| 2335 |
paramVariantList.append(j.y()); |
| 2336 |
paramVariantList.append(j.z()); |
| 2337 |
} |
| 2338 |
paramVariantList.append(0); // mode |
| 2339 |
paramVariantList.append(basins.at(i)); // basin |
| 2340 |
|
| 2341 |
adapt_integrate_v(fdim, property_v_tp, ¶mVariantList, dim, xmin, xmax, |
| 2342 |
maxEval, tol, 0, val, err); |
| 2343 |
|
| 2344 |
free(xmin); |
| 2345 |
free(xmax); |
| 2346 |
} |
| 2347 |
|
| 2348 |
qDebug() << "basin=" << basins.at(i) + 1 << "value= " << val[0] |
| 2349 |
<< "err=" << err[0]; |
| 2350 |
|
| 2351 |
QPair<qreal, qreal> thisPair; |
| 2352 |
thisPair.first = val[0]; |
| 2353 |
thisPair.second = err[0]; |
| 2354 |
|
| 2355 |
value.append(thisPair); |
| 2356 |
} |
| 2357 |
|
| 2358 |
free(val); |
| 2359 |
free(err); |
| 2360 |
|
| 2361 |
return value; |
| 2362 |
} |
| 2363 |
|
| 2364 |
QTAIMCubature::~QTAIMCubature() |
| 2365 |
{ |
| 2366 |
if (QFile::exists(m_temporaryFileName)) { |
| 2367 |
QFile::remove(m_temporaryFileName); |
| 2368 |
} |
| 2369 |
} |
| 2370 |
|
| 2371 |
void QTAIMCubature::setMode(qint64 mode) |
| 2372 |
{ |
| 2373 |
m_mode = mode; |
| 2374 |
} |
| 2375 |
|
| 2376 |
QString QTAIMCubature::temporaryFileName() |
| 2377 |
{ |
| 2378 |
QTemporaryFile temporaryFile; |
| 2379 |
temporaryFile.open(); |
| 2380 |
QString tempFileName = temporaryFile.fileName(); |
| 2381 |
temporaryFile.close(); |
| 2382 |
temporaryFile.remove(); |
| 2383 |
|
| 2384 |
// wait for temporary file to be deleted |
| 2385 |
QDir dir; |
| 2386 |
do { |
| 2387 |
// Nothing |
| 2388 |
} while (dir.exists(tempFileName)); |
| 2389 |
|
| 2390 |
return tempFileName; |
| 2391 |
} |
| 2392 |
|
| 2393 |
} // namespace Avogadro::QtPlugins |