overlap_algorithm.c 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931
  1. #include <math.h>
  2. #include <stdlib.h>
  3. #include <stdio.h>
  4. #include <string.h>
  5. #define R (sqrt(5)-1.)*0.5
  6. #define C (1.0-R)
  7. #define SHFT2(a,b,c) (a)=(b);(b)=(c);
  8. #define SHFT3(a,b,c,d) (a)=(b);(b)=(c);(c)=(d);
  9. //.........................COMPILE TO SHARED LIBRARY FOR USE WITH PYTHON.........................................
  10. // gcc -shared overlap_algorithm.c -o overlap_algorithm.so -lm -fPIC -O3
  11. //..................................LINE MINIMIZATION ALGORITHMS.................................................
  12. double gss_eigenvalue_minim(double a, double b, double eps, double t, double *matA, double *matB, int early_stop,
  13. double f ( double x, double *mat1, double *mat2 ), double *xmin, int *feval) {
  14. double f1,f2,x0,x1,x2,x3,m,tol,t2;
  15. int num_eval;
  16. x0=a;
  17. x3=b;
  18. x1 = a + C * (b - a);
  19. x2 = a + R * (b - a);
  20. f1=f(x1, matA, matB);
  21. f2=f(x2, matA, matB);
  22. num_eval = 2;
  23. for ( ; ; )
  24. {
  25. m = 0.5 * ( x1 + x2 ) ;
  26. tol = eps * fabs ( m ) + t;
  27. t2 = 2.0 * tol;
  28. /*
  29. Check the stopping criterion.
  30. */
  31. if (fabs(x3-x0) < t2)
  32. {
  33. break;
  34. }
  35. if ( early_stop == 1 && (f1 < -1. || f2 < -1.)) {
  36. break;
  37. }
  38. if (f2 < f1) {
  39. SHFT3(x0,x1,x2,R*x1+C*x3);
  40. SHFT2(f1,f2,f(x2, matA, matB));
  41. num_eval++;
  42. }
  43. else {
  44. SHFT3(x3,x2,x1,R*x2+C*x0);
  45. SHFT2(f2,f1,f(x1, matA, matB));
  46. num_eval++;
  47. }
  48. }
  49. if (f1 < f2) {
  50. *xmin=x1;
  51. *feval = num_eval;
  52. return f1;
  53. }
  54. else {
  55. *xmin=x2;
  56. *feval = num_eval;
  57. return f2;
  58. }
  59. }
  60. double brent_eigenvalue_minim ( double a, double b, double eps, double t,
  61. double *matA, double *matB, int early_stop,
  62. double f ( double x, double *mat1, double *mat2 ), double *x, int *feval)
  63. /*
  64. Adapted version of LOCAL_MIN function for finding eigenvalue extrema.
  65. Authors of original minimization code:
  66. Orignal FORTRAN77 version by Richard Brent.
  67. C version by John Burkardt.
  68. Licensing:
  69. This code is distributed under the GNU LGPL license.
  70. Parameters:
  71. Input, double A, B, the endpoints of the interval.
  72. Input, double EPS, a positive relative error tolerance.
  73. EPS should be no smaller than twice the relative machine precision,
  74. and preferably not much less than the square root of the relative
  75. machine precision.
  76. Input, double T, a positive absolute error tolerance.
  77. Input, double F ( double x ,double *mat1, double *mat2 ), a user-supplied
  78. function whose local minimum along x is being sought, with matrices mat1
  79. and mat2 as parameters.
  80. Output, double *X, the estimated value of an abscissa
  81. for which F attains a local minimum value in [A,B].
  82. Output, double *feval, the number of function evaluations.
  83. Output, double LOCAL_MIN, the value F(X).
  84. */
  85. {
  86. double c;
  87. double d;
  88. double e;
  89. double fu;
  90. double fv;
  91. double fw;
  92. double fx;
  93. double m;
  94. double p;
  95. double q;
  96. double r;
  97. double sa;
  98. double sb;
  99. double t2;
  100. double tol;
  101. double u;
  102. double v;
  103. double w;
  104. int num_eval;
  105. /*
  106. C is the square of the inverse of the golden ratio.
  107. */
  108. c = 0.5 * ( 3.0 - sqrt ( 5.0 ) );
  109. sa = a;
  110. sb = b;
  111. *x = sa + c * ( b - a );
  112. w = *x;
  113. v = w;
  114. e = 0.0;
  115. fx = f ( *x, matA, matB );
  116. fw = fx;
  117. fv = fw;
  118. num_eval = 1;
  119. for ( ; ; )
  120. {
  121. m = 0.5 * ( sa + sb ) ;
  122. tol = eps * fabs ( *x ) + t;
  123. t2 = 2.0 * tol;
  124. /*
  125. Check the stopping criterion.
  126. */
  127. if ( fabs ( *x - m ) <= t2 - 0.5 * ( sb - sa ) )
  128. {
  129. break;
  130. }
  131. /*
  132. If we are looking for the maximum of smallest eigenvalue and the
  133. calculation goes over the value of 1 (under -1 because algorithm finds min),
  134. we can stop as that means the ellipses certainly do not overlap
  135. */
  136. if ( early_stop == 1 && fx < -1.) {
  137. break;
  138. }
  139. /*
  140. Fit a parabola.
  141. */
  142. r = 0.0;
  143. q = r;
  144. p = q;
  145. if ( tol < fabs ( e ) )
  146. {
  147. r = ( *x - w ) * ( fx - fv );
  148. q = ( *x - v ) * ( fx - fw );
  149. p = ( *x - v ) * q - ( *x - w ) * r;
  150. q = 2.0 * ( q - r );
  151. if ( 0.0 < q )
  152. {
  153. p = - p;
  154. }
  155. q = fabs ( q );
  156. r = e;
  157. e = d;
  158. }
  159. if ( fabs ( p ) < fabs ( 0.5 * q * r ) &&
  160. q * ( sa - *x ) < p &&
  161. p < q * ( sb - *x ) )
  162. {
  163. /*
  164. Take the parabolic interpolation step.
  165. */
  166. d = p / q;
  167. u = *x + d;
  168. /*
  169. F must not be evaluated too close to A or B.
  170. */
  171. if ( ( u - sa ) < t2 || ( sb - u ) < t2 )
  172. {
  173. if ( *x < m )
  174. {
  175. d = tol;
  176. }
  177. else
  178. {
  179. d = - tol;
  180. }
  181. }
  182. }
  183. /*
  184. A golden-section step.
  185. */
  186. else
  187. {
  188. if ( *x < m )
  189. {
  190. e = sb - *x;
  191. }
  192. else
  193. {
  194. e = sa - *x;
  195. }
  196. d = c * e;
  197. }
  198. /*
  199. F must not be evaluated too close to X.
  200. */
  201. if ( tol <= fabs ( d ) )
  202. {
  203. u = *x + d;
  204. }
  205. else if ( 0.0 < d )
  206. {
  207. u = *x + tol;
  208. }
  209. else
  210. {
  211. u = *x - tol;
  212. }
  213. fu = f ( u, matA, matB );
  214. num_eval++;
  215. /*
  216. Update A, B, V, W, and X.
  217. */
  218. if ( fu <= fx )
  219. {
  220. if ( u < *x )
  221. {
  222. sb = *x;
  223. }
  224. else
  225. {
  226. sa = *x;
  227. }
  228. v = w;
  229. fv = fw;
  230. w = *x;
  231. fw = fx;
  232. *x = u;
  233. fx = fu;
  234. }
  235. else
  236. {
  237. if ( u < *x )
  238. {
  239. sa = u;
  240. }
  241. else
  242. {
  243. sb = u;
  244. }
  245. if ( fu <= fw || w == *x )
  246. {
  247. v = w;
  248. fv = fw;
  249. w = u;
  250. fw = fu;
  251. }
  252. else if ( fu <= fv || v == *x || v == w )
  253. {
  254. v = u;
  255. fv = fu;
  256. }
  257. }
  258. }
  259. *feval = num_eval;
  260. return fx;
  261. }
  262. double r8_epsilon ( )
  263. /*
  264. Purpose:
  265. R8_EPSILON returns the R8 round off unit.
  266. Discussion:
  267. R8_EPSILON is a number R which is a power of 2 with the property that,
  268. to the precision of the computer's arithmetic,
  269. 1 < 1 + R
  270. but
  271. 1 = ( 1 + R / 2 )
  272. Parameters:
  273. Output, double R8_EPSILON, the R8 round-off unit.
  274. */
  275. {
  276. const double value = 2.220446049250313E-016;
  277. return value;
  278. }
  279. //.................................EIGENVALUE AND EIGENVECTOR CALCULATION.............................................
  280. double determinant(double* a){
  281. double det = a[0] * (a[4]*a[8] - a[7]*a[5]) - a[3] * (a[1]*a[8] - a[7]*a[2]) + a[6] * (a[1]*a[5] - a[4]*a[2]);
  282. return det;
  283. }
  284. void eigenvalues(double* matrix, double* eigs){
  285. double p1 = matrix[1]*matrix[1] + matrix[2]*matrix[2] + matrix[5]*matrix[5];
  286. double q, p2, p;
  287. double aux[9];
  288. double r, phi;
  289. double pi = M_PI;
  290. if (p1 == 0) {
  291. eigs[0] = matrix[0];
  292. eigs[1] = matrix[4];
  293. eigs[2] = matrix[8];
  294. }
  295. else {
  296. q = (matrix[0]+matrix[4]+matrix[8])/3;
  297. p2 = (matrix[0]-q)*(matrix[0]-q) + (matrix[4]-q)*(matrix[4]-q) + (matrix[8]-q)*(matrix[8]-q) + 2*p1;
  298. p = sqrt(p2 / 6);
  299. for (int i=0; i<9; i++) {
  300. aux[i] = matrix[i];
  301. if (i % 4 == 0) {
  302. aux[i] -= q;
  303. }
  304. aux[i] = (1 / p) * aux[i];
  305. }
  306. r = determinant(aux) / 2;
  307. if (r <= -1){
  308. phi = pi / 3;
  309. }
  310. else if (r >= 1){
  311. phi = 0;
  312. }
  313. else {
  314. phi = acos(r) / 3;
  315. }
  316. eigs[2] = q + 2 * p * cos(phi);
  317. eigs[0] = q + 2 * p * cos(phi + (2*pi/3));
  318. eigs[1] = 3 * q - eigs[0] - eigs[2];
  319. }
  320. }
  321. void eigenvector(double* matrix, int which, double* vector){
  322. double eigs[3];
  323. double mat_lbd1[9];
  324. double mat_lbd2[9];
  325. double product[9];
  326. double norm1, norm2, norm3, norm;
  327. int idx;
  328. eigenvalues(matrix, eigs);
  329. for (int i=0; i<9; i++) {
  330. mat_lbd1[i] = matrix[i];
  331. mat_lbd2[i] = matrix[i];
  332. if (i % 4 == 0) {
  333. if (which == 1) {
  334. mat_lbd1[i] -= eigs[1];
  335. mat_lbd2[i] -= eigs[2];
  336. }
  337. else if (which == 2) {
  338. mat_lbd1[i] -= eigs[0];
  339. mat_lbd2[i] -= eigs[2];
  340. }
  341. else {
  342. mat_lbd1[i] -= eigs[0];
  343. mat_lbd2[i] -= eigs[1];
  344. }
  345. }
  346. }
  347. for (int i=0; i<3; i++) {
  348. for (int k=0; k<3; k++) {
  349. product[3*i+k] = mat_lbd1[3*i] * mat_lbd2[k] + mat_lbd1[3*i+1] * mat_lbd2[3+k] + mat_lbd1[3*i+2] * mat_lbd2[6+k];
  350. }
  351. }
  352. norm1 = product[0]*product[0] + product[3]*product[3] + product[6]*product[6];
  353. norm2 = product[1]*product[1] + product[4]*product[4] + product[7]*product[7];
  354. norm3 = product[2]*product[2] + product[5]*product[5] + product[8]*product[8];
  355. if (norm1 > norm2 && norm1 > norm3) {
  356. idx = 0;
  357. norm = norm1;
  358. }
  359. else if (norm2 > norm1 && norm2 > norm3) {
  360. idx = 1;
  361. norm = norm2;
  362. }
  363. else {
  364. idx = 2;
  365. norm = norm3;
  366. }
  367. norm = 1/sqrt(norm);
  368. vector[0] = product[idx]*norm;
  369. vector[1] = product[3+idx]*norm;
  370. vector[2] = product[6+idx]*norm;
  371. }
  372. //.....................................EIGENVALUE EXTREMIZATION.................................................
  373. double neg_min_eig(double lbd, double* matA, double* matB){
  374. double lcb[9];
  375. double eigs[3];
  376. double result;
  377. for (int i=0; i<9; i++) {
  378. lcb[i] = lbd * matA[i] + (1 - lbd) * matB[i];
  379. }
  380. eigenvalues(lcb, eigs);
  381. result = -eigs[0];
  382. return result;
  383. }
  384. double med_eig(double lbd, double* matA, double* matB){
  385. double lcb[9];
  386. double eigs[3];
  387. double result;
  388. for (int i=0; i<9; i++) {
  389. lcb[i] = lbd * matA[i] + (1 - lbd) * matB[i];
  390. }
  391. eigenvalues(lcb, eigs);
  392. result = eigs[1];
  393. return result;
  394. }
  395. double max_min_eig_brent(double* matA, double* matB, int early_stop, double* t_point, int* feval){
  396. double a = r8_epsilon();
  397. double b = 1. - r8_epsilon();
  398. double e = r8_epsilon(); // relative error
  399. double t = 1.0E-7; // absolute error
  400. double solution;
  401. double initial = 0.;
  402. double *y = &initial;
  403. solution = -brent_eigenvalue_minim(a, b, e, t, matA, matB, early_stop, neg_min_eig, t_point, feval);
  404. return solution;
  405. }
  406. double min_med_eig_brent(double* matA, double* matB, double* t_point, int* feval){
  407. double a = r8_epsilon();
  408. double b = 1. - r8_epsilon();
  409. double e = r8_epsilon(); // relative error
  410. double t = 1.0E-7; // absolute error
  411. int early_stop = 0;
  412. double solution;
  413. double initial = 0.;
  414. double *y = &initial;
  415. solution = brent_eigenvalue_minim(a, b, e, t, matA, matB, early_stop, med_eig, t_point, feval);
  416. return solution;
  417. }
  418. double max_min_eig_gss(double* matA, double* matB, int early_stop, double* t_point, int* feval){
  419. double a = r8_epsilon();
  420. double b = 1. - r8_epsilon();
  421. double c = 0.5;
  422. double m = 100.;
  423. double mechep = r8_epsilon();
  424. double e = r8_epsilon(); // relative error
  425. double t = 1.0E-7; // absolute error
  426. double solution;
  427. solution = -gss_eigenvalue_minim(a, b, e, t, matA, matB, early_stop, neg_min_eig, t_point, feval);
  428. return solution;
  429. }
  430. double min_med_eig_gss(double* matA, double* matB, double* t_point, int* feval){
  431. double a = r8_epsilon();
  432. double b = 1. - r8_epsilon();
  433. double c = 0.5;
  434. double m = 100.;
  435. double mechep = r8_epsilon();
  436. double e = r8_epsilon(); // relative error
  437. double t = 1.0E-7; // absolute error
  438. int early_stop = 0;
  439. double solution;
  440. solution = gss_eigenvalue_minim(a, b, e, t, matA, matB, early_stop, med_eig, t_point, feval);
  441. return solution;
  442. }
  443. //.......................................CONTACT FUNCTION...................................................
  444. void cross_product(double* coord, double* orient, double* cross) {
  445. cross[0] = coord[1]*orient[2] - coord[2]*orient[1];
  446. cross[1] = coord[2]*orient[0] - coord[0]*orient[2];
  447. cross[2] = coord[0]*orient[1] - coord[1]*orient[0];
  448. }
  449. double contact_function(double a0, double a1, double b0, double b1,
  450. double* coord0, double* orient0, double* coord1, double* orient1, int minimizer,
  451. double* other_results, int* fevals, int* branch) {
  452. /*
  453. Other results array: [min_eig_T, med_eig_T, min_vec_scalar, med_vec_scalar]
  454. fevals array: [feval_min, feval_med]
  455. */
  456. double a_inter;
  457. if (a0 < a1) {
  458. a_inter = a0;
  459. a0 = a1;
  460. a1 = a_inter;
  461. }
  462. double cross0[3];
  463. double cross1[3];
  464. double lin_interpolation[9];
  465. double qf0[9];
  466. double qf1[9];
  467. double min_eig;
  468. double med_eig;
  469. double eigvec_mineig[3];
  470. double mineig_coord0=0.;
  471. double mineig_coord1=0.;
  472. double inv2a0=1/(a0 * a0);
  473. double inv2a1=1/(a1 * a1);
  474. double inv2b0=1/(b0 * b0);
  475. double inv2b1=1/(b1 * b1);
  476. double limit=inv2a0;
  477. cross_product(coord0, orient0, cross0);
  478. cross_product(coord1, orient1, cross1);
  479. /* Trasformation of quadratic forms */
  480. for (int i=0; i<3; i++){
  481. for (int l=0; l<3; l++){
  482. qf0[3*i+l] = orient0[i]*inv2a0*orient0[l] + cross0[i]*inv2b0*cross0[l];
  483. qf1[3*i+l] = orient1[i]*inv2a1*orient1[l] + cross1[i]*inv2b1*cross1[l];
  484. }
  485. }
  486. if (minimizer == 0) {
  487. min_eig = max_min_eig_brent(qf0, qf1, 0, &other_results[0], &fevals[0]);
  488. }
  489. else if (minimizer == 1) {
  490. min_eig = max_min_eig_brent(qf0, qf1, 1, &other_results[0], &fevals[0]);
  491. }
  492. else if (minimizer == 2) {
  493. min_eig = max_min_eig_gss(qf0, qf1, 0, &other_results[0], &fevals[0]);
  494. }
  495. else if (minimizer == 3) {
  496. min_eig = max_min_eig_gss(qf0, qf1, 1, &other_results[0], &fevals[0]);
  497. }
  498. else {
  499. return -1.;
  500. }
  501. /* eigenvector corresponding to maximal 1st eigenvalue (1st intersection coordinate)*/
  502. for (int i=0; i<9; i++) {
  503. lin_interpolation[i] = other_results[0] * qf0[i] + (1 - other_results[0]) * qf1[i];
  504. }
  505. eigenvector(lin_interpolation, 1, eigvec_mineig);
  506. /* scalar product between eigenvectors and position vectors for both ellipses in the pair */
  507. for (int i=0; i<3; i++) {
  508. mineig_coord0 += eigvec_mineig[i]*coord0[i];
  509. mineig_coord1 += eigvec_mineig[i]*coord1[i];
  510. }
  511. other_results[2] = mineig_coord0 * mineig_coord1;
  512. other_results[3] = 0;
  513. if (mineig_coord0 * mineig_coord1 > 0) {
  514. other_results[1] = -1; // meaningless result for med_eig_T
  515. fevals[1] = 0;
  516. if (min_eig < limit) {
  517. *branch = 0;
  518. return min_eig;
  519. }
  520. else {
  521. *branch = 2;
  522. return limit;
  523. }
  524. }
  525. else {
  526. if (minimizer == 0 || minimizer == 1) {
  527. med_eig = min_med_eig_brent(qf0, qf1, &other_results[1], &fevals[1]);
  528. }
  529. else if (minimizer == 2 || minimizer == 3) {
  530. med_eig = min_med_eig_gss(qf0, qf1, &other_results[1], &fevals[1]);
  531. }
  532. else {
  533. return -1.;
  534. }
  535. if (1.0E-4 < other_results[1] && other_results[1] < 1 - 1.0E-4 && med_eig < limit) {
  536. *branch = 1;
  537. return med_eig;
  538. }
  539. else {
  540. *branch = 2;
  541. other_results[1] = 1;
  542. return limit;
  543. }
  544. }
  545. }
  546. void contact_function_multi(int n, double a0, double a1, double b0, double b1,
  547. double* coords0, double* orients0, double* coords1, double* orients1, int minimizer,
  548. int* all_evals, double* results) {
  549. double other_results[4];
  550. int branch=0;
  551. int* branch_point = &branch;
  552. int fevals[2];
  553. fevals[0] = 0;
  554. fevals[1] = 0;
  555. for (int i=0; i<n; i++){
  556. results[i] = contact_function(a0, a1, b0, b1, &coords0[3*i], &orients0[3*i], &coords1[3*i], &orients1[3*i],
  557. minimizer, &other_results[0], fevals, &branch);
  558. all_evals[0] += fevals[0];
  559. all_evals[1] += fevals[1];
  560. fevals[0] = 0;
  561. fevals[1] = 0;
  562. }
  563. }
  564. double contact_mono_packing(double inv2a, double inv2b,
  565. double* coord0, double* orient0,
  566. double* coord1, double* orient1,
  567. int try_second, int early_stop) {
  568. /*
  569. Simplified contact function calculation for monodispersed ellipse packing.
  570. */
  571. double cross0[3];
  572. double cross1[3];
  573. double lin_interpolation[9];
  574. double qf0[9];
  575. double qf1[9];
  576. double min_eig;
  577. double min_eig_T = 0.;
  578. double* meT_point = &min_eig_T;
  579. int fevals = 0;
  580. int* fevals_point = &fevals;
  581. cross_product(coord0, orient0, cross0);
  582. cross_product(coord1, orient1, cross1);
  583. /* Trasformation of quadratic forms */
  584. for (int i=0; i<3; i++){
  585. for (int l=0; l<3; l++){
  586. qf0[3*i+l] = orient0[i]*inv2a*orient0[l] + cross0[i]*inv2b*cross0[l];
  587. qf1[3*i+l] = orient1[i]*inv2a*orient1[l] + cross1[i]*inv2b*cross1[l];
  588. }
  589. }
  590. min_eig = max_min_eig_brent(qf0, qf1, early_stop, meT_point, fevals_point);
  591. if (try_second == 0 ){
  592. return min_eig;
  593. }
  594. else { // The case where the 2nd eigenvalue must also be calculated
  595. double lin_interpolation[9];
  596. double eigvec_mineig[3];
  597. double mineig_coord0 = 0;
  598. double mineig_coord1 = 0;
  599. /* eigenvector corresponding to maximal 1st eigenvalue (1st intersection coordinate)*/
  600. for (int i=0; i<9; i++) {
  601. lin_interpolation[i] = min_eig_T * qf0[i] + (1 - min_eig_T) * qf1[i];
  602. }
  603. eigenvector(lin_interpolation, 1, eigvec_mineig);
  604. /* scalar product between eigenvectors and position vectors for both ellipses in the pair */
  605. for (int i=0; i<3; i++) {
  606. mineig_coord0 += eigvec_mineig[i]*coord0[i];
  607. mineig_coord1 += eigvec_mineig[i]*coord1[i];
  608. }
  609. if (mineig_coord0 * mineig_coord1 > 0) {
  610. return min_eig;
  611. }
  612. else {
  613. min_eig_T = 0.;
  614. fevals = 0;
  615. return min_med_eig_brent(qf0, qf1, meT_point, fevals_point);
  616. }
  617. }
  618. }
  619. double contact_mono_pack_multi(int n, double inv2a, double inv2b, double* coords, double* orients, double dist_lim) {
  620. double energy=0.;
  621. double cos_dist;
  622. double contact_f;
  623. int try_second=0;
  624. if (inv2a < 2) {
  625. try_second = 1; // for large ellipses, 2nd eigenvalue might be the correct one
  626. }
  627. for (int i=0; i<n; i++) {
  628. for (int j=i+1; j<n; j++) {
  629. cos_dist = coords[3*i]*coords[3*j] + coords[3*i+1]*coords[3*j+1] + coords[3*i+2]*coords[3*j+2];
  630. if (cos_dist > dist_lim) {
  631. contact_f = contact_mono_packing(inv2a, inv2b, &coords[3*i], &orients[3*i], &coords[3*j], &orients[3*j],
  632. try_second, 1);
  633. if (contact_f < 1) {
  634. energy += contact_f + 1 / contact_f - 2;
  635. }
  636. }
  637. }
  638. }
  639. return energy;
  640. }
  641. double contact_bidisp_packing(double inv2a_0, double inv2b_0,
  642. double inv2a_1, double inv2b_1,
  643. double* coord0, double* orient0,
  644. double* coord1, double* orient1,
  645. int try_second, double omega) {
  646. /*
  647. Simplified contact function calculation for bidispersed ellipse packing.
  648. */
  649. double cross0[3];
  650. double cross1[3];
  651. double lin_interpolation[9];
  652. double qf0[9];
  653. double qf1[9];
  654. double min_eig;
  655. double med_eig;
  656. double min_eig_T = 0.;
  657. double* meT_point = &min_eig_T;
  658. int fevals = 0;
  659. int* fevals_point = &fevals;
  660. cross_product(coord0, orient0, cross0);
  661. cross_product(coord1, orient1, cross1);
  662. /* Trasformation of quadratic forms */
  663. for (int i=0; i<3; i++){
  664. for (int l=0; l<3; l++){
  665. qf0[3*i+l] = orient0[i]*inv2a_0*orient0[l] + cross0[i]*inv2b_0*cross0[l];
  666. qf1[3*i+l] = orient1[i]*inv2a_1*orient1[l] + cross1[i]*inv2b_1*cross1[l];
  667. }
  668. }
  669. min_eig = max_min_eig_brent(qf0, qf1, 1, meT_point, fevals_point);
  670. if (try_second == 0 ){
  671. if (min_eig < omega) {
  672. return min_eig;
  673. }
  674. else {
  675. return omega;
  676. }
  677. }
  678. else { // The case where the 2nd eigenvalue must also be calculated
  679. double lin_interpolation[9];
  680. double eigvec_mineig[3];
  681. double mineig_coord0 = 0;
  682. double mineig_coord1 = 0;
  683. /* eigenvector corresponding to maximal 1st eigenvalue (1st intersection coordinate)*/
  684. for (int i=0; i<9; i++) {
  685. lin_interpolation[i] = min_eig_T * qf0[i] + (1 - min_eig_T) * qf1[i];
  686. }
  687. eigenvector(lin_interpolation, 1, eigvec_mineig);
  688. /* scalar product between eigenvectors and position vectors for both ellipses in the pair */
  689. for (int i=0; i<3; i++) {
  690. mineig_coord0 += eigvec_mineig[i]*coord0[i];
  691. mineig_coord1 += eigvec_mineig[i]*coord1[i];
  692. }
  693. if (mineig_coord0 * mineig_coord1 > 0) {
  694. if (min_eig < omega) {
  695. return min_eig;
  696. }
  697. else {
  698. return omega;
  699. }
  700. }
  701. else {
  702. min_eig_T = 0.;
  703. fevals = 0;
  704. med_eig = min_med_eig_brent(qf0, qf1, meT_point, fevals_point);
  705. if (med_eig < omega) {
  706. return med_eig;
  707. }
  708. else {
  709. return omega;
  710. }
  711. }
  712. }
  713. }
  714. double contact_bidisp_pack_multi(int n, double* inv2a, double* inv2b,
  715. double* coords, double* orients, double dist_lim, double omega) {
  716. double energy=0.;
  717. double cos_dist;
  718. double contact_f;
  719. int try_second;
  720. for (int i=0; i<n; i++) {
  721. for (int j=i+1; j<n; j++) {
  722. cos_dist = coords[3*i]*coords[3*j] + coords[3*i+1]*coords[3*j+1] + coords[3*i+2]*coords[3*j+2];
  723. if (cos_dist > dist_lim) {
  724. if (inv2a[i] * inv2a[j] < inv2a[i] + inv2a[j]) { // derived from the condition alpha0 + alpha1 > pi / 2
  725. try_second = 1; // for large ellipses, 2nd eigenvalue might be the correct one
  726. }
  727. else {
  728. try_second = 0;
  729. }
  730. contact_f = contact_bidisp_packing(inv2a[i], inv2b[i], inv2a[j], inv2b[j],
  731. &coords[3*i], &orients[3*i], &coords[3*j], &orients[3*j], try_second, omega);
  732. if (contact_f < 1) {
  733. energy += contact_f + 1 / contact_f - 2;
  734. }
  735. }
  736. }
  737. }
  738. return energy;
  739. }