-
Notifications
You must be signed in to change notification settings - Fork 0
/
enhancement.h
2572 lines (2164 loc) · 85.5 KB
/
enhancement.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright 2018 Benjamin Santos <[email protected]>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/
#ifndef ENHANCEMENT_H
#define ENHANCEMENT_H
#include <algorithm>
#include <chrono>
#include <cstdlib>
#include <ctime>
#include <fstream>
#include <functional>
#include <iostream>
#include <memory>
#include <parallel/algorithm>
#include <vector>
#include <sstream>
#include <string>
#include <utility>
#include <omp.h>
#include <boost/algorithm/string.hpp>
#include <boost/lexical_cast.hpp>
#include "array.h"
#include "constants.h"
#include "eint.h"
#include "log.h"
#include "utils.h"
#define MPC_ERROR 1.0 // 0.4
#define ETA_TOL 1.0e-16
using namespace utils;
typedef std::pair<short, short> pp;
typedef std::pair<pp, pp> pairs;
typedef std::vector<short> vs;
typedef std::vector<pp> vp;
typedef std::vector<pairs> vpairs;
namespace enhancement {
const boost::uintmax_t ROOT_MAXITER = 1000;
const tools::eps_tolerance<double> ROOT_TOL(30);
/*!
* class PairElement
*
* Represents a value for a particle pair combination (l,q)+(m,p)
*
*/
class PairElement {
public:
//
PairElement() {
l_ = 0;
q_ = 0;
m_ = 0;
p_ = 0;
value_ = 0.0;
}
PairElement(const short &l, const short &q, const short &m, const short &p,
const double &value)
: l_(l), q_(q), m_(m), p_(p), value_(value) {}
PairElement(const PairElement &pe) { assign(pe); }
short l_; //!< Index l of coalescing particle 1
short q_; //!< Index q of coalescing particle 1
short m_; //!< Index m of coalescing particle 2
short p_; //!< Index p of coalescing particle 2
double value_; //!< Value
void assign(const PairElement &pe) {
l_ = pe.l_;
q_ = pe.q_;
m_ = pe.m_;
p_ = pe.p_;
value_ = pe.value_;
}
friend void fill_pe(PairElement &pe, const short &l, const short &q,
const short &m, const short &p, const double &value);
friend void pairelement_tostream(const PairElement &pe, std::ostream &stream);
friend void pairelement_tostream(PairElement &pe, std::istringstream &stream);
void print(std::ostream &stream = std::cout) {
pairelement_tostream(*this, stream);
}
};
inline void get_pe(const PairElement &pe, short &l, short &q, short &m,
short &p, double &value) {
l = pe.l_;
q = pe.q_;
m = pe.m_;
p = pe.p_;
value = pe.value_;
}
inline void fill_pe(PairElement &pe, const short &l, const short &q,
const short &m, const short &p, const double &value) {
pe.l_ = l;
pe.q_ = q;
pe.m_ = m;
pe.p_ = p;
pe.value_ = value;
}
inline void pairelement_tostream(const PairElement &pe, std::ostream &stream) {
stream << pe.l_ << '\t' << pe.q_ << '\t' << pe.m_ << '\t' << pe.p_ << '\t'
<< pe.value_;
}
inline void pairelement_fromstream(PairElement &pe,
std::istringstream &stream) {
std::string sl_; //!< Index l of current volume section
std::string sq_; //!< Index q of current charge section
std::string sm_; //!< Index m of coalescing volume
std::string sp_; //!< Index p of coalescing charge
std::string svalue_;
short l_; //!< Index l of coalescing particle 1
short q_; //!< Index q of coalescing particle 1
short m_; //!< Index m of coalescing particle 2
short p_; //!< Index p of coalescing particle 2
double value_; //!< Value
stream >> sl_ >> sq_ >> sm_ >> sp_ >> svalue_;
l_ = boost::lexical_cast<short>(sl_);
q_ = boost::lexical_cast<short>(sq_);
m_ = boost::lexical_cast<short>(sm_);
p_ = boost::lexical_cast<short>(sp_);
value_ = std::stod(svalue_);
fill_pe(pe, l_, q_, m_, p_, value_);
}
//
class ParticlePair {
public:
//
ParticlePair() {
// call fill to set 0 all the fields
// fill_ppair(*this, 0, 0, 0, 0, 0.0, 0.0, true);
id_ = 0;
l_ = 0;
q_ = 0;
m_ = 0;
p_ = 0;
r21_ = 0.0;
q21_ = 0.0;
notswapd_ = true;
}
ParticlePair(const unsigned long &id, const short &l, const short &q,
const short &m, const short &p, const double &r21,
const double &q21, const bool ¬swapd)
: id_(id),
l_(l),
q_(q),
m_(m),
p_(p),
r21_(r21),
q21_(q21),
notswapd_(notswapd) {
// call fill to set 0 all the fields
// fill_ppair(*this, 0, 0, 0, 0, 0.0, 0.0, true);
}
ParticlePair(const ParticlePair &pp) { assign(pp); }
unsigned long id_; //!< Index
short l_; //!< Index l of coalescing particle 1
short q_; //!< Index q of coalescing particle 1
short m_; //!< Index m of coalescing particle 2
short p_; //!< Index p of coalescing particle 2
double r21_; //!< Value for radius fraction r2/r1
double q21_; //!< Value for radius fraction r2/r1
bool notswapd_; //!< false if indices were swapped
void assign(const ParticlePair &pp) {
id_ = pp.id_;
l_ = pp.l_;
q_ = pp.q_;
m_ = pp.m_;
p_ = pp.p_;
r21_ = pp.r21_;
q21_ = pp.q21_;
notswapd_ = pp.notswapd_;
}
friend void fill_ppair(ParticlePair &pp, const unsigned long &id,
const short &l, const short &q, const short &m,
const short &p, const double &r21, const double &q21,
const bool ¬swapd);
friend void particlepair_tostream(const ParticlePair &pp,
std::ostream &stream);
friend void particlepair_fromstream(ParticlePair &pp,
std::istringstream &stream);
void print(std::ostream &stream = std::cout) {
particlepair_tostream(*this, stream);
}
};
inline void fill_ppair(ParticlePair &pp, const unsigned long &id,
const short &l, const short &q, const short &m,
const short &p, const double &r21, const double &q21,
const bool ¬swapd) {
pp.id_ = id;
pp.l_ = l;
pp.q_ = q;
pp.m_ = m;
pp.p_ = p;
pp.r21_ = r21;
pp.q21_ = q21;
pp.notswapd_ = notswapd;
}
inline void particlepair_tostream(const ParticlePair &pp,
std::ostream &stream) {
stream << pp.id_ << '\t' << pp.l_ << '\t' << pp.q_ << '\t' << pp.m_ << '\t'
<< pp.p_ << '\t' << pp.r21_ << '\t' << pp.q21_ << '\t' << pp.notswapd_;
}
inline void particlepair_fromstream(ParticlePair &pp,
std::istringstream &stream) {
std::string sid_; //!< Index l of current volume section
std::string sl_; //!< Index l of current volume section
std::string sq_; //!< Index q of current charge section
std::string sm_; //!< Index m of coalescing volume
std::string sp_; //!< Index p of coalescing charge
std::string sr21_;
std::string sq21_;
std::string snotswapd_;
unsigned long id_;
short l_; //!< Index l of coalescing particle 1
short q_; //!< Index q of coalescing particle 1
short m_; //!< Index m of coalescing particle 2
short p_; //!< Index p of coalescing particle 2
double r21_; //!< Value for radius fraction r2/r1
double q21_; //!< Value for radius fraction r2/r1
bool notswapd_; //!< false if indices were swapped
stream >> sid_ >> sl_ >> sq_ >> sm_ >> sp_ >> sr21_ >> sq21_ >> snotswapd_;
id_ = boost::lexical_cast<unsigned long>(sid_);
l_ = boost::lexical_cast<short>(sl_);
q_ = boost::lexical_cast<short>(sq_);
m_ = boost::lexical_cast<short>(sm_);
p_ = boost::lexical_cast<short>(sp_);
r21_ = std::stod(sr21_);
q21_ = std::stod(sq21_);
notswapd_ = boost::lexical_cast<bool>(snotswapd_);
fill_ppair(pp, id_, l_, q_, m_, p_, r21_, q21_, notswapd_);
}
class ReducedParticlePair {
public:
//
ReducedParticlePair() {
// call fill to set 0 all the fields
fill_rppair(*this, 0, 0.0, 0.0);
}
bool operator<(const ReducedParticlePair &rpp) const {
if (r21_ != rpp.r21_) {
return (r21_ < rpp.r21_);
} else {
return (q21_ < rpp.q21_);
}
}
inline bool operator==(const ReducedParticlePair &rpp) const {
if ((r21_ == rpp.r21_) && (q21_ == rpp.q21_)) {
return true;
} else {
return false;
}
}
// friend
// bool operator == (const ReducedParticlePair& lx,
// const ReducedParticlePair& rx);
unsigned long id_;
double r21_; //!< Value for radius fraction r2/r1
double q21_; //!< Value for radius fraction r2/r1
std::vector<long> repetitions_;
ReducedParticlePair(const ReducedParticlePair &rpp) { assign(rpp); }
void assign(const ReducedParticlePair &rpp) {
id_ = rpp.id_;
r21_ = rpp.r21_;
q21_ = rpp.q21_;
repetitions_ = rpp.repetitions_;
}
friend void reducedparticlepair_tostream(const ReducedParticlePair &rpp,
std::ostream &stream);
friend void reducedparticlepair_fromstream(ReducedParticlePair &rpp,
std::istream &stream);
void print(std::ostream &stream = std::cout) {
reducedparticlepair_tostream(*this, stream);
}
friend void fill_rppair(ReducedParticlePair &rpp, unsigned long id_,
double r21, double q21);
friend void fill_rppair(ReducedParticlePair &rpp, unsigned long id_,
double r21, double q21,
std::vector<long> repetitions);
friend void fill_rppair(ReducedParticlePair &rpp, unsigned long id_,
double r21, double q21, long irepeated);
void fill_repeated(long const &irepeated) {
repetitions_.push_back(irepeated);
}
};
// bool operator == (const ReducedParticlePair& lx,
// const ReducedParticlePair& rx) {
// // return ((lx.r21_==rx.r21_) && (lx.q21_==rx.q21_));
// if ((lx.r21_==rx.r21_) && (lx.q21_==rx.q21_)) {
// return true;
// }
// else {
// return false;
// }
// }
inline bool operator==(const ReducedParticlePair &lr, const ParticlePair &rp) {
// return ((lx.r21_==rx.r21_) && (lx.q21_==rx.q21_));
if ((lr.r21_ == rp.r21_) && (lr.q21_ == rp.q21_)) {
return true;
} else {
return false;
}
}
inline void fill_rppair(ReducedParticlePair &rpp, unsigned long id, double r21,
double q21) {
rpp.id_ = id;
rpp.r21_ = r21;
rpp.q21_ = q21;
}
inline void fill_rppair(ReducedParticlePair &rpp, unsigned long id, double r21,
double q21, std::vector<long> repetitions) {
rpp.id_ = id;
rpp.r21_ = r21;
rpp.q21_ = q21;
rpp.repetitions_ = repetitions;
}
inline void fill_rppair(ReducedParticlePair &rpp, unsigned long id, double r21,
double q21, long irepeated) {
rpp.id_ = id;
rpp.r21_ = r21;
rpp.q21_ = q21;
rpp.repetitions_.push_back(irepeated);
}
struct reducedPairsComparison {
bool operator()(const ReducedParticlePair &lx,
const ReducedParticlePair &rx) const {
if (lx.r21_ != rx.r21_) {
return (lx.r21_ < rx.r21_);
}
return (lx.q21_ < rx.q21_);
}
};
inline void reducedparticlepair_tostream(const ReducedParticlePair &rpp,
std::ostream &stream) {
stream << rpp.id_ << '\t' << rpp.r21_ << '\t' << rpp.q21_ << '\t';
// << "repeated = \t";
for (auto ir : rpp.repetitions_) {
stream << ir << ",";
}
}
inline void reducedparticlepair_fromstream(ReducedParticlePair &rpp,
std::istream &stream) {
std::string sid_;
std::string sr21_;
std::string sq21_;
std::string srep_;
unsigned long id_;
double r21_; //!< Value for radius fraction r2/r1
double q21_; //!< Value for radius fraction r2/r1
std::vector<long> repetitions_;
stream >> sid_ >> sr21_ >> sq21_ >> srep_;
id_ = boost::lexical_cast<unsigned long>(sid_);
r21_ = std::stod(sr21_);
q21_ = std::stod(sq21_);
// split line
std::vector<std::string> vssrep_;
boost::split(vssrep_, srep_, boost::is_any_of(","));
for (std::vector<std::string>::iterator it = vssrep_.begin();
it < vssrep_.end(); ++it) {
try {
long trep = (boost::lexical_cast<long>(*it));
repetitions_.push_back(trep);
} catch (const boost::bad_lexical_cast &) {
// std::cerr << "\n[ee] Error from lexical cast : " << *it;
break;
}
// std::cout << "\t" << *it;
}
fill_rppair(rpp, id_, r21_, q21_, repetitions_);
}
class ContactPotential {
public:
ContactPotential(const unsigned long &id = 0, const double &potential = 0,
const short &n = 0)
: id_(id), potential_(potential), n_(n) {}
friend void fill_contact_potential(ContactPotential &cpot,
const unsigned long &id,
const double &potential, const short &n);
void print(std::ostream &stream = std::cout) {
contactpotential_tostream(*this, stream);
}
friend void contactpotential_tostream(const ContactPotential &cpot,
std::ostream &stream);
unsigned long id_;
double potential_;
short n_;
};
inline void fill_contact_potential(ContactPotential &cpot,
const unsigned long &id,
const double &potential, const short &n) {
cpot.id_ = id;
cpot.potential_ = potential;
cpot.n_ = n;
}
inline void contactpotential_tostream(const ContactPotential &cpot,
std::ostream &stream) {
stream << '\n' << cpot.id_ << '\t' << cpot.potential_ << '\t' << cpot.n_;
}
struct find_id {
unsigned long id;
find_id(const unsigned long &id_) : id(id_) {}
bool operator()(const ContactPotential &cpot) const {
// return rpp.id_ = id
// std::vector<long> rptd = rpp.repetitions_;
// std::vector<long>::iterator it = std::find(rptd.begin(), rptd.end(), id);
// return std::find(rptd.begin(), rptd.end(), id) != rptd.end();
return (cpot.id_ == id);
}
};
typedef std::vector<ContactPotential>::iterator ContactPotentialIterator;
class Enhancement {
public:
// Enhancement() {
//}
Enhancement(const darray &rarray_, const darray &qarray_,
boost_array4d_ref efactor_, boost_array4d_ref cpotentials_,
boost_array4d_ref bpotentials_, boost_array4d_ref rbarriers_,
double eps_, src::severity_logger<severity_level> lg_)
: rarray(rarray_),
qarray(qarray_),
efactor(efactor_),
cpotentials(cpotentials_),
bpotentials(bpotentials_),
rbarriers(rbarriers_),
eps(eps_),
lg(lg_) {
rarray_size = static_cast<unsigned short>(rarray.size());
qarray_size = static_cast<unsigned short>(qarray.size());
ncombs =
((rarray_size * qarray_size) * (rarray_size * qarray_size + 1)) / 2;
particle_pairs.reserve(ncombs);
potential_threshold = 0.0;
nmin = 25;
nmax = 2000;
nstep = 5;
AH = 20e-20;
}
Enhancement(const darray &rarray_, const darray &qarray_, double eps_,
src::severity_logger<severity_level> lg_)
: rarray(rarray_),
qarray(qarray_),
efactor(dummy), // Hack, references must belong to an actual object
cpotentials(dummy),
bpotentials(dummy),
rbarriers(dummy),
eps(eps_),
lg(lg_) {
rarray_size = static_cast<unsigned short>(rarray.size());
qarray_size = static_cast<unsigned short>(qarray.size());
ncombs =
((rarray_size * qarray_size) * (rarray_size * qarray_size + 1)) / 2;
particle_pairs.reserve(ncombs);
potential_threshold = 0.0;
nmin = 25;
nmax = 2000;
nstep = 5;
}
Enhancement(const darray &rarray_, const darray &qarray_,
boost_array4d_ref efactor_, double eps_,
src::severity_logger<severity_level> lg_)
: rarray(rarray_),
qarray(qarray_),
efactor(efactor_), // Hack, references must belong to an actual object
cpotentials(dummy),
bpotentials(dummy),
rbarriers(dummy),
eps(eps_),
lg(lg_) {
rarray_size = static_cast<unsigned short>(rarray.size());
qarray_size = static_cast<unsigned short>(qarray.size());
ncombs =
((rarray_size * qarray_size) * (rarray_size * qarray_size + 1)) / 2;
particle_pairs.reserve(ncombs);
potential_threshold = 0.0;
nmin = 25;
nmax = 2000;
nstep = 5;
}
inline int write_particlepairs(std::string ppfilename) {
std::ofstream ppstream(std::string(ppfilename + "_pp.dat"));
ppstream << "#id\tl\tq\tm\tp\tr21\tq21\tnotswapd";
// #pragma omp parallel for ordered//schedule(nonmonotonic:dynamic)
for (unsigned int i = 0; i < particle_pairs.size(); ++i) {
// #pragma omp ordered
// {
ppstream << '\n';
particlepair_tostream(particle_pairs[i], ppstream);
// }
}
ppstream.close();
// Neutral particles
std::ofstream npstream(std::string(ppfilename + "_np.dat"));
npstream << "#id\tl\tq\tm\tp\tr21\tq21\tnotswapd";
// #pragma omp parallel for ordered//schedule(nonmonotonic:dynamic)
for (unsigned int i = 0; i < neutral_pairs.size(); ++i) {
// #pragma omp ordered
// {
npstream << '\n';
particlepair_tostream(neutral_pairs[i], npstream);
// }
}
npstream.close();
// Reduced pairs
std::ofstream rpstream(std::string(ppfilename + "_rp.dat"));
rpstream << "#id\tr21\tq21\trepetitions";
// #pragma omp parallel for ordered//schedule(nonmonotonic:dynamic)
for (unsigned int i = 0; i < reduced_pairs.size(); ++i) {
// #pragma omp ordered
// {
rpstream << '\n';
reducedparticlepair_tostream(reduced_pairs[i], rpstream);
// }
}
rpstream.close();
return 0;
}
inline int read_particlepairs(std::string ppfilename) {
// Read particle pairs
{
// Clear vector
particle_pairs.clear();
std::ifstream ppstream(std::string(ppfilename + "_pp.dat"));
std::string line;
unsigned int nlines = 0;
std::getline(ppstream, line);
// if a comment first line isnt found, rewind the file
if (line.find("#") == std::string::npos) {
ppstream.clear();
ppstream.seekg(0);
}
// read line by line
while (std::getline(ppstream, line)) {
ParticlePair pp;
std::istringstream iss(line);
particlepair_fromstream(pp, iss);
particle_pairs.push_back(pp);
++nlines;
}
ppstream.close();
BOOST_LOG_SEV(lg, info)
<< "Particle pair size = " << particle_pairs.size();
}
// Read particle pairs
{
// Clear vector
neutral_pairs.clear();
std::ifstream npstream(std::string(ppfilename + "_np.dat"));
std::string line;
unsigned int nlines = 0;
std::getline(npstream, line);
// if a comment first line isnt found, rewind the file
if (line.find("#") == std::string::npos) {
npstream.clear();
npstream.seekg(0);
}
// read line by line
while (std::getline(npstream, line)) {
ParticlePair np;
std::istringstream iss(line);
particlepair_fromstream(np, iss);
neutral_pairs.push_back(np);
++nlines;
}
npstream.close();
BOOST_LOG_SEV(lg, info) << "Neutral pair size = " << neutral_pairs.size();
}
// Read reduced pairs
{
// Clear vector
reduced_pairs.clear();
std::ifstream rpstream(std::string(ppfilename + "_rp.dat"));
std::string line;
unsigned int nlines = 0;
std::getline(rpstream, line);
// if a comment first line isnt found, rewind the file
if (line.find("#") == std::string::npos) {
rpstream.clear();
rpstream.seekg(0);
}
// read line by line
while (std::getline(rpstream, line)) {
ReducedParticlePair rp;
std::istringstream iss(line);
reducedparticlepair_fromstream(rp, iss);
reduced_pairs.push_back(rp);
++nlines;
}
rpstream.close();
BOOST_LOG_SEV(lg, info) << "Reduced pair size = " << reduced_pairs.size();
}
// for (auto rp: reduced_pairs){
// std::cout << std::endl;
// rp.print();
// }
if (reduced_pairs.size() > 0) {
contact_potentials.resize(reduced_pairs.size());
}
return 0;
}
inline void compute_reducedpairs() {
BOOST_LOG_SEV(lg, info) << "Computing particle pairs...";
auto start = std::chrono::system_clock::now();
//
unsigned long idpp = 0; // index for particle pairs
unsigned long idnp = 0; // index for neutral pairs
// #pragma omp parallel for collapse(4) schedule(auto)
for (unsigned int l = 0; l < rarray_size; ++l) {
// iterate in charges particle 1
for (unsigned int q = 0; q < qarray_size; ++q) {
// iterate in radii particle 2
for (unsigned int m = 0; m < rarray_size; ++m) {
// iterate in charges particle 2
for (unsigned int p = 0; p < qarray_size; ++p) {
unsigned int ip1 = q * rarray_size + l;
unsigned int ip2 = p * rarray_size + m;
// avoid repetitions
if ((p >= q) && (ip2 >= ip1)) {
double r1 = rarray[l];
double q1 = qarray[q];
double r2 = rarray[m];
double q2 = qarray[p];
unsigned int lp1 = l;
unsigned int qp1 = q;
unsigned int mp2 = m;
unsigned int pp2 = p;
bool notswapd = true;
// swap particles. We want to keep r21 < 1
if (r2 > r1) {
std::swap(r1, r2);
std::swap(q1, q2);
std::swap(lp1, mp2);
std::swap(qp1, pp2);
notswapd = false;
}
//double r21 = r2 / r1;
double q21 = q2 / q1;
// WARNING float comparison
if (q1 == 0.0) {
// q1=0, permute particle 1 with 2
std::swap(r1, r2);
std::swap(q1, q2);
std::swap(lp1, mp2);
std::swap(qp1, pp2);
q21 = 0.0;
notswapd = notswapd | false;
}
double r21 = r2 / r1;
// if(q2==0.0){
// q21 = 0.0;
// }
// all neutrals case
if ((q1 == 0.0) && (q2 == 0.0)) {
// lp1 = l; qp1 = q;
// mp2 = m; pp2 = p;
// q21 = 0.0;
// r21 = rarray[m]/rarray[l];
ParticlePair neutralpair(idnp, lp1, qp1, mp2, pp2, r21, q21,
notswapd);
neutral_pairs.push_back(neutralpair);
++idnp;
continue; // break;
}
ParticlePair ppair(idpp, lp1, qp1, mp2, pp2, r21, q21, notswapd);
particle_pairs.push_back(ppair);
++idpp;
// write symmetric combination
}
}
}
}
}
auto end = std::chrono::system_clock::now();
std::chrono::duration<double> elapsed_seconds = end - start;
BOOST_LOG_SEV(lg, info) << "Elapsed time : " << elapsed_seconds.count();
BOOST_LOG_SEV(lg, info) << "Pairs size : " << particle_pairs.size();
BOOST_LOG_SEV(lg, info) << "Neutral pairs size : " << neutral_pairs.size();
BOOST_LOG_SEV(lg, info) << "Total pairs size : "
<< particle_pairs.size() + neutral_pairs.size();
BOOST_LOG_SEV(lg, info) << "Total combinations : " << ncombs;
BOOST_LOG_SEV(lg, info) << "Computing reduced pairs...";
reduced_pairs.resize(particle_pairs.size());
start = std::chrono::system_clock::now();
for (unsigned int i = 0; i < particle_pairs.size(); ++i) {
reduced_pairs[i].id_ = particle_pairs[i].id_;
reduced_pairs[i].r21_ = particle_pairs[i].r21_;
reduced_pairs[i].q21_ = particle_pairs[i].q21_;
}
end = std::chrono::system_clock::now();
elapsed_seconds = end - start;
BOOST_LOG_SEV(lg, info) << "Elapsed time : " << elapsed_seconds.count();
start = std::chrono::system_clock::now();
BOOST_LOG_SEV(lg, info) << "Sorting...";
//__gnu_parallel::sort(reduced_pairs.begin(), reduced_pairs.end(),
//reducedPairsComparison());
std::sort(reduced_pairs.begin(), reduced_pairs.end(),
reducedPairsComparison());
end = std::chrono::system_clock::now();
elapsed_seconds = end - start;
BOOST_LOG_SEV(lg, info) << "Elapsed time : " << elapsed_seconds.count();
// ******** new stuff
// vector sorted with repeated
std::vector<ReducedParticlePair> sorted_reducedpairs = reduced_pairs;
// ******** end new stuff
BOOST_LOG_SEV(lg, info) << "Erasing...";
reduced_pairs.erase(std::unique(reduced_pairs.begin(), reduced_pairs.end()),
reduced_pairs.end());
//__gnu_parallel::unique_copy(reduced_pairs.begin(), reduced_pairs.end(),
//reduced_pairs);
// resize in memory
std::vector<ReducedParticlePair> tmp = reduced_pairs;
reduced_pairs.swap(tmp);
// clear memory for tmp
std::vector<ReducedParticlePair>().swap(tmp);
//****************************************************************************
// Add indices of repeated to vector repetitions
//****************************************************************************
BOOST_LOG_SEV(lg, info) << "Reduced pairs size : " << reduced_pairs.size();
start = std::chrono::system_clock::now();
/// new stuff
// #pragma omp parallel for schedule(nonmonotonic:dynamic)// collapse(2)
long jstart = 0;
for (unsigned int irp = 0; irp < reduced_pairs.size(); ++irp) {
for (long jpp = jstart; jpp < sorted_reducedpairs.size(); ++jpp) {
if (reduced_pairs[irp] == sorted_reducedpairs[jpp]) {
// #pragma omp critical
// {
// one thread a at time
reduced_pairs[irp].fill_repeated(sorted_reducedpairs[jpp].id_);
// }
} else {
jstart = jpp;
break;
}
}
}
/// end new stuff
end = std::chrono::system_clock::now();
elapsed_seconds = end - start;
BOOST_LOG_SEV(lg, info) << "Elapsed time : " << elapsed_seconds.count();
start = std::chrono::system_clock::now();
// resize contact potentials
if (reduced_pairs.size() > 0) {
contact_potentials.resize(reduced_pairs.size());
}
end = std::chrono::system_clock::now();
elapsed_seconds = end - start;
BOOST_LOG_SEV(lg, info) << "Elapsed time : " << elapsed_seconds.count();
BOOST_LOG_SEV(lg, info) << "Done computing pairs.";
}
inline
void compute_ipavdwpotential_contact(double cutoff, double vdw=1.0) {
BOOST_LOG_SEV(lg, info) << "Computing IPA+vdW at contact...";
auto start = std::chrono::system_clock::now();
//
unsigned long idpp = 0; // index for particle pairs
unsigned long idnp = 0; // index for neutral pairs
// #pragma omp parallel for collapse(4) schedule(auto)
for (unsigned int l = 0; l < rarray_size; ++l) {
// iterate in charges particle 1
for (unsigned int q = 0; q < qarray_size; ++q) {
// iterate in radii particle 2
for (unsigned int m = 0; m < rarray_size; ++m) {
// iterate in charges particle 2
for (unsigned int p = 0; p < qarray_size; ++p) {
double r1 = rarray[l];
double q1 = qarray[q];
double r2 = rarray[m];
double q2 = qarray[p];
double rt = r1+r2;
eint::potential_ipavdw_funct pipavdw(r1, q1, r2, q2, eps, AH, cutoff, 1.0, vdw);
cpotentials[l][q][m][p] = pipavdw(rt);
}
}
}
}
BOOST_LOG_SEV(lg, info) << "Done computing IPA+vdW potential contact.";
}
inline
short particle_number(short l, short q, short L) {
return l+L*q;
}
inline
void mpcvdwpotential_barrier(short l, short q,
short m, short p,
double cutoff) {
double r1 = rarray[l];
double q1 = qarray[q];
double r2 = rarray[m];
double q2 = qarray[p];
double rmin = r1+r2;
double rmax = 100.0*rmin;
double min = rmin;
double max = rmax;
short n = nterms4d[l][q][m][p];
eint::force_mpcvdw_funct fmpcvdwfunct(r1, q1, r2, q2, eps, n, AH, cutoff, 1.0/*mpc*/, 1.0/*vdw*/);
// force mpc at contact
double fmpcvdw_rmin = fmpcvdwfunct(rmin);
// Force at r max
double fmpcvdw_rmax = fmpcvdwfunct(rmax);
// checks if minimum exists
if (fmpcvdw_rmin * fmpcvdw_rmax < 0.0) {
// std::cerr << "\n[ii] Mixed phi_rt = " << fmpc_rmin << '\t' <<
// fmpc_rmax;
boost::uintmax_t bmax_iter = ROOT_MAXITER;
tools::eps_tolerance<double> tol = ROOT_TOL;
std::pair<double, double> pair_fmpcvdw;
// try {
pair_fmpcvdw = tools::toms748_solve(fmpcvdwfunct, min, max, fmpcvdw_rmin,
fmpcvdw_rmax, tol, bmax_iter);
if (bmax_iter > 990) {
std::cerr << "\n ERROR max iter " << bmax_iter << "\n\n";
std::terminate();
}
double rbarrier = 0.5 * (pair_fmpcvdw.first + pair_fmpcvdw.second);
if (rbarrier >= min) {
//*********************** USE SAME COEFFICIENTS OF FORCE
//#pragma omp critical
//{
// mpc functor
eint::potential_mpcvdw_funct pmpcvdwfunct(r1, q1, r2, q2, eps, n, AH, cutoff, 1.0/*mpc*/, 1.0/*vdw*/);
// mpc at contact
double pmpcvdw_rb = pmpcvdwfunct(rbarrier);
bpotentials[l][q][m][p] = pmpcvdw_rb;