-
Notifications
You must be signed in to change notification settings - Fork 0
/
logic.hpp
777 lines (694 loc) · 37.7 KB
/
logic.hpp
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
#ifndef __MDN_LOGIC__
#define __MDN_LOGIC__
#include <span>
#include <version>
#include <filesystem>
#include <unordered_set>
#include <chrono>
#include <numbers>
#include "adios2.h"
#include "mpi.h"
// #include "enums.hpp"
#include "ct_map.hpp"
#include "constants.hpp"
#include "config.hpp"
#ifdef __cpp_lib_ranges_zip
#include <ranges>
#else
#include "zip.hpp"
#endif // !__cpp_lib_ranges_zip
#ifdef __MDN_TRACE_OUT__
#define __MDN_TRACE__ \
trace(logger, __FILE__, __LINE__, "");
#define __MDN_TRACE_MESS__(mess) \
trace(logger, __FILE__, __LINE__, mess);
#else
#define __MDN_TRACE__
#define __MDN_TRACE_MESS__(mess)
#endif
namespace mdn{
extern spdlog::logger logger;
template <typename T, uint64_t N>
constexpr std::array<T, N> _linspace(T start, T stop){
std::array<T, N> a;
T dx = (stop - start) / N;
for (auto& el : a){
el = start;
start += dx;
}
return a;
}
template <typename T, uint64_t N>
constexpr std::array<T, N-1> _centers(const std::array<T, N>& arr){
std::array<T, N-1> a;
for (size_t i = 0; i < N-1; ++i) a[i] = (arr[i] + arr[i+1])/2;
return a;
}
template <typename T, uint64_t N>
constexpr std::array<T, N-1> _sl_volumes(const std::array<T, N>& arr, const uint64_t Np_bins, const uint64_t Nt_bins){
std::array<T, N-1> lv;
for (size_t i = 0; i < N-1; ++i) lv[i] = 4 * std::numbers::pi * (std::pow(arr[i+1], 3) - std::pow(arr[i], 3)) / 3 / Np_bins / Nt_bins;
return lv;
}
namespace fs = std::filesystem;
void MDN::run(){
int storages_to_skip = -1;
uint64_t steps_to_skip = -1;
logger.debug("Open file");
MPI_File_open(wcomm, wfile.string().c_str(), amode, MPI_INFO_NULL, &fh);
logger.debug("File open");
if (cont){
logger.debug("Continuation enabled");
MPI_File_read_at(fh, off, &storages_to_skip, 1, MPI_INT, &status);
MPI_File_read_at(fh, off+sizeof(int), &steps_to_skip, 1, MPI_UINT64_T, &status);
}
logger.debug("Initializing ADIOS2");
adios2::ADIOS adios;
// if (args.adios_conf.string().length() > 0)
adios = adios2::ADIOS(args.adios_conf, MPI_COMM_SELF);
// else adios = adios2::ADIOS(MPI_COMM_SELF);
logger.debug("ADIOS2 initialized");
adios2::IO dataio = adios.DeclareIO("DataWriter");
adios2::IO lmpsio = adios.DeclareIO("LAMMPSReader");
// if (!(args.adios_conf.string().length() > 0)){
dataio.SetEngine("BP4");
dataio.AddTransport("File", {{"Library", "posix"}});
lmpsio.SetEngine("BP4");
lmpsio.AddTransport("File", {{"Library", "posix"}});
lmpsio.SetParameter("substreams", "1");
// }
logger.debug("ADIOS2 IO initialized");
__MDN_TRACE_MESS__("Defining variables")
adios2::Variable<uint64_t> WvarNstep = dataio.DefineVariable<uint64_t>("ntimestep");
adios2::Variable<uint64_t> WvarN = dataio.DefineVariable<uint64_t>("natoms");
adios2::Variable<uint64_t> WvarDist = dataio.DefineVariable<uint64_t>("dist", {_Natoms + 1}, {0}, {_Natoms + 1}, adios2::ConstantDims);
adios2::Variable<double> WvarTemps = dataio.DefineVariable<double> ("temps", {_Natoms + 1}, {0}, {_Natoms + 1}, adios2::ConstantDims);
adios2::Variable<double> WvarVol = dataio.DefineVariable<double> ("volume");
adios2::Variable<double> WvarTTemp = dataio.DefineVariable<double> ("total_temp");
#ifdef __CALC_ENTHROPY__
adios2::Variable<double> WvarEnthr = dataio.DefineVariable<double> ("enthr", {_Natoms + 1}, {0}, {_Natoms + 1}, adios2::ConstantDims);
adios2::Variable<double> WvarEnerg = dataio.DefineVariable<double> ("energ", {_Natoms + 1}, {0}, {_Natoms + 1}, adios2::ConstantDims);
#endif // __CALC_ENTHROPY__
__MDN_TRACE_MESS__("Defined variables")
adios2::Engine writer = dataio.Open(args.outfile, adios2::Mode::Write, MPI_COMM_SELF);
logger.debug("ADIOS2 IO DataWriter initialized");
writer.LockWriterDefinitions();
adios2::Variable<uint64_t> varNstep;
adios2::Variable<uint64_t> varNatoms;
adios2::Variable<double> varBoxxhi;
adios2::Variable<double> varBoxyhi;
adios2::Variable<double> varBoxzhi;
adios2::Variable<double> varBoxxlo;
adios2::Variable<double> varBoxylo;
adios2::Variable<double> varBoxzlo;
adios2::Variable<double> varAtoms;
constexpr int ndim = 3;
// #ifdef __CALC_ENTHROPY__
// #ifdef __KE_PE_PRESENT__
// constexpr int nprops = 11;
// #else
// constexpr int nprops = 9;
// #endif // __KE_PE_PRESENT__
// #else
// constexpr int nprops = 9;
// #endif // __CALC_ENTHROPY__
constexpr double rcut = 2.5;
logger.debug("Number of dimensions: {}", ndim);
logger.debug("Number of properties: {}", memory.nprops());
logger.debug("rcut: {}", rcut);
#ifdef __CALC_ENTHROPY__
__MDN_TRACE_MESS__("Initializing variables for enthropy calculations")
constexpr size_t Nr_bins = 10;
constexpr size_t Np_bins = 36;
constexpr size_t Nt_bins = 18;
constexpr double rho_normalize = 4 * std::numbers::pi * std::pow(rcut, 3) / 3;
constexpr double bin_r_w = (rcut - 0) / Nr_bins;
constexpr double bin_p_w = (2 * std::numbers::pi) / Np_bins;
constexpr double bin_t_w = (std::numbers::pi - 0) / Nt_bins;
constexpr std::array<double, Nr_bins + 1> r_mesh = _linspace<double, Nr_bins + 1>(0.0, rcut);
constexpr std::array<double, Nr_bins> rbs = _centers(r_mesh);
constexpr std::array<double, Nr_bins> lv = _sl_volumes(r_mesh, Np_bins, Nt_bins);
#endif // __CALC_ENTHROPY__
__MDN_TRACE_MESS__("Allocating memory for data")
memory.allocate(_Natoms);
double* memptr = memory.data();
// auto particle_ids = memory.prop(PROPS::PID);
auto cluster_ids = memory.prop(CT::CID);
auto masses = memory.prop(CT::MASS);
auto velX = memory.prop(CT::VELX);
auto velY = memory.prop(CT::VELY);
auto velZ = memory.prop(CT::VELZ);
// std::unique_ptr<double[]> Atoms_buf(new double[_Natoms * nprops]);
// std::span<const double> particle_ids(Atoms_buf.get() + 0 * _Natoms, 1 * _Natoms);
// std::span<const double> cluster_ids (Atoms_buf.get() + 1 * _Natoms, 1 * _Natoms);
// std::span<const double> masses (Atoms_buf.get() + 2 * _Natoms, 1 * _Natoms);
// std::span<const double> velocities (Atoms_buf.get() + 3 * _Natoms, 3 * _Natoms);
// #ifdef __CALC_ENTHROPY__
// std::span<const double> xs (Atoms_buf.get() + 6 * _Natoms, 1 * _Natoms);
// std::span<const double> ys (Atoms_buf.get() + 7 * _Natoms, 1 * _Natoms);
// std::span<const double> zs (Atoms_buf.get() + 8 * _Natoms, 1 * _Natoms);
// #endif // __CALC_ENTHROPY__
// #ifdef __KE_PE_PRESENT__
// std::span<const double> kes (Atoms_buf.get() + 9 * _Natoms, 1 * _Natoms);
// std::span<const double> pes (Atoms_buf.get() + 10 * _Natoms, 1 * _Natoms);
// #endif // __KE_PE_PRESENT__
__MDN_TRACE_MESS__("Initializing auxillary variables")
// Main algorithm containers
uint64_t timestep = 0, Natoms = 0, Nclusters = 0, c_size = 0;
double boxxhi = 0, boxyhi = 0, boxzhi = 0, boxxlo = 0, boxylo = 0, boxzlo = 0, Volume = 0;
double total_temp = 0, ke = 0, pe = 0;
std::unordered_set<uint64_t> unique_cluster_ids;
unique_cluster_ids.reserve(_Natoms);
std::map<uint64_t, std::vector<uint64_t>> particles_by_cluster_id, cluster_ids_by_size, particles_by_size;
std::vector<uint64_t> sizes_counts(_Natoms + 1, 0UL);
std::vector<double> temps_by_size(_Natoms + 1, 0.0);
std::vector<double> sqvels(_Natoms, 0.0);
// std::map<uint64_t, double> particle_count_by_size, ndofs_by_size;
#ifndef __KE_PE_PRESENT__
__MDN_TRACE__
std::vector<double> kes(_Natoms, 0);
// kes.reserve(_Natoms);
#ifdef __CALC_PE__
__MDN_TRACE__
std::vector<double> pes(_Natoms, 0.0);
double dist = 0, en = 0;
#endif // __CALC_PE__
#endif // !__KE_PE_PRESENT__
__MDN_TRACE__
// Enthropy containers
#ifdef __CALC_ENTHROPY__
__MDN_TRACE__
std::vector<std::array<double, ndim>> pts; // cleared
pts.reserve(_Natoms);
std::vector<std::array<double, ndim>> _pts; // cleared
_pts.reserve(_Natoms);
std::vector<double> rho_a; // not required
rho_a.resize(_Natoms, 0.0);
__MDN_TRACE__
std::vector<double> rvi(_Natoms, 0.0); // cleaning not required
size_t r_bin_n{}, p_bin_n{}, t_bin_n{};
double rho{}, s{}, phi{}, theta{};
std::array<std::vector<int>, Nr_bins+1> r_bin; // cleared
std::array<std::array<std::array<std::vector<int>, Np_bins+1>, Nt_bins+1>, Nr_bins+1> ang_bin; // cleared
for (size_t i = 0; i < Nr_bins+1; ++i){
r_bin[i].reserve(_Natoms);
for (size_t j = 0; j < Nt_bins+1; ++j)
for (size_t k = 0; k < Np_bins+1; ++k)
ang_bin[i][j][k].reserve(_Natoms);
}
__MDN_TRACE__
std::array<double, Nr_bins> rs; // cleaning not required
std::array<std::array<std::array<double, Np_bins>, Nt_bins>, Nr_bins> ang_s; // cleaning not required
std::array<double, Nr_bins> enth_ang({0.0}); // cleaning not required
double r_sum{}, t_sum{}, enth{};
std::vector<double> enth_by_size(_Natoms + 1, 0.0); // cleared
// std::map<uint64_t, double> kes_by_size; // cleared
#ifdef __KE_PE_PRESENT__
// std::vector<double> f_energy(_Natoms + 1, 0.0);
// std::vector<double> kes_by_size(_Natoms + 1, 0.0); // cleared
// std::vector<double> pes_by_size(_Natoms + 1, 0.0); // cleared
std::vector<double> eng_by_size(_Natoms + 1, 0.0); // cleared
#endif // __KE_PE_PRESENT__
__MDN_TRACE__
// std::map<size_t, std::vector<std::pair<std::array<double, 3>, std::array<double, Nr_bins>>>> stats;
// std::array<double, Nr_bins> ang_int;
// double rr_integral=0;
#endif // __CALC_ENTHROPY__
// id c_clusters mass vx vy vz [x y z [ke pe]]
logger.debug("Variables and buffers initialized");
timer Tmisc;
Tmisc.b();
__MDN_TRACE_MESS__("Initializing timers")
#ifdef __MDN_PROFILING__
__MDN_TRACE__
timer Tglobal;
timer TPstorage;
timer TPstep;
timer TADIOS_get_data;
timer TADIOS_write_data;
timer Tcalc_PE;
timer Tcalc_enthropy;
timer Tcleaning;
Tglobal.start();
TPstorage.start();
#endif // __MDN_PROFILING__
total_steps = 0;
__MDN_TRACE_MESS__("Counting total steps")
for (const auto &[storage, steps] : storages) total_steps += steps.second - steps.first;
__MDN_TRACE__
logger.debug("Starting main loop");
uint64_t done_steps = 0;
int storage_index = -1;
for (const auto &[storage, steps] : storages){
__MDN_TRACE__
++storage_index;
if (cont && storage_index < storages_to_skip) continue;
if (cont && steps_to_skip >= steps.second) continue;
uint64_t currentStep = 0;
__MDN_TRACE__
try{
__MDN_TRACE__
adios2::Engine reader = lmpsio.Open(storage, adios2::Mode::Read, MPI_COMM_SELF);
#ifdef __MDN_TRACE_OUT__
logger.trace("Open storage ({}-{}):{}", steps.first, steps.second, storage.string().c_str());
#endif // __MDN_TRACE_OUT__
__MDN_TRACE__
while (currentStep != steps.first) {
if (reader.BeginStep() == adios2::StepStatus::EndOfStream) {
logger.error("End of stream happened while scrolling to begin step");
logger.error("Storage: {}, begin: {}, end: {}", storage.string(), steps.first, steps.second);
throw std::logic_error("End of stream happened while scrolling to begin step");
}
__MDN_TRACE__
currentStep = reader.CurrentStep();
reader.EndStep();
}
__MDN_TRACE__
if (cont && steps.first < steps_to_skip){
__MDN_TRACE__
while (currentStep <= steps_to_skip) {
__MDN_TRACE__
if (reader.BeginStep() == adios2::StepStatus::EndOfStream) {
__MDN_TRACE__
logger.error("End of stream happened while scrolling to last processed step");
logger.error("Storage: {}, begin: {}, end: {}", storage.string(), steps.first, steps.second);
throw std::logic_error("End of stream happened while scrolling to last processed step");
}
currentStep = reader.CurrentStep();
reader.EndStep();
__MDN_TRACE__
}
}
#ifdef __MDN_TRACE_OUT__
logger.trace("Skipped {} steps", currentStep);
#endif // __MDN_TRACE_OUT__
#ifdef __MDN_PROFILING__
TPstep.start();
#endif // __MDN_PROFILING__
#ifdef __MDN_TRACE_OUT__
logger.trace("Writing current state to start-stop file: {}, {}", storage_index, currentStep);
#endif // __MDN_TRACE_OUT__
MPI_File_write_at(fh, off, &storage_index, 1, MPI_INT, &status);
MPI_File_write_at(fh, off + sizeof(int), ¤tStep, 1, MPI_UINT64_T, &status);
#ifdef __MDN_TRACE_OUT__
logger.trace("Written current state to start-stop file");
#endif // __MDN_TRACE_OUT__
__MDN_TRACE__
while (currentStep < steps.second) {
if (reader.BeginStep() == adios2::StepStatus::EndOfStream){
#ifdef __MDN_TRACE_OUT__
logger.trace("EOS reached");
#endif // __MDN_TRACE_OUT__
break;
}
__MDN_TRACE__
currentStep = reader.CurrentStep();
varNstep = lmpsio.InquireVariable<uint64_t>(std::string(lcf::timestep));
varNatoms = lmpsio.InquireVariable<uint64_t>(std::string(lcf::natoms ));
varBoxxhi = lmpsio.InquireVariable<double> (std::string(lcf::boxxhi ));
varBoxyhi = lmpsio.InquireVariable<double> (std::string(lcf::boxyhi ));
varBoxzhi = lmpsio.InquireVariable<double> (std::string(lcf::boxzhi ));
varBoxxlo = lmpsio.InquireVariable<double> (std::string(lcf::boxxlo ));
varBoxylo = lmpsio.InquireVariable<double> (std::string(lcf::boxylo ));
varBoxzlo = lmpsio.InquireVariable<double> (std::string(lcf::boxzlo ));
varAtoms = lmpsio.InquireVariable<double> (std::string(lcf::atoms ));
__MDN_TRACE__
if (!(varNstep && varNatoms && varBoxxhi && varBoxyhi && varBoxzhi && varBoxxlo && varBoxylo && varBoxzlo && varAtoms)){
// if (!(varNstep && varNatoms && varAtoms)){
__MDN_TRACE__
reader.EndStep();
logger.error("Error on read variables at step {}, continuing to next step", currentStep);
continue;
}
__MDN_TRACE__
#ifdef __MDN_TRACE_OUT__
adios2::Dims vAtomsShape = varAtoms.Shape();
logger.trace("_Natoms: {}", _Natoms);
std::string au = "varAtoms::Shape: ";
for (const auto& elem : vAtomsShape){
au += std::to_string(elem) + ", ";
}
logger.trace(au);
logger.trace("Inquired variables");
#endif // __MDN_TRACE_OUT__
__MDN_TRACE__
#ifdef __MDN_PROFILING__
TADIOS_get_data.b();
#endif // __MDN_PROFILING__
reader.Get(varNstep, ×tep, adios2::Mode::Deferred);
__MDN_TRACE__
reader.Get(varNatoms, &Natoms, adios2::Mode::Deferred);
__MDN_TRACE__
reader.Get(varBoxxhi, &boxxhi, adios2::Mode::Deferred);
__MDN_TRACE__
reader.Get(varBoxyhi, &boxyhi, adios2::Mode::Deferred);
__MDN_TRACE__
reader.Get(varBoxzhi, &boxzhi, adios2::Mode::Deferred);
__MDN_TRACE__
reader.Get(varBoxxlo, &boxxlo, adios2::Mode::Deferred);
__MDN_TRACE__
reader.Get(varBoxylo, &boxylo, adios2::Mode::Deferred);
__MDN_TRACE__
reader.Get(varBoxzlo, &boxzlo, adios2::Mode::Deferred);
__MDN_TRACE__
reader.Get(varAtoms, memptr, adios2::Mode::Deferred);
__MDN_TRACE__
// reader.PerformGets();
reader.EndStep();
#ifdef __MDN_PROFILING__
TADIOS_get_data.e();
#endif // __MDN_PROFILING__
#ifdef __MDN_TRACE_OUT__
logger.trace("Got data");
#endif // __MDN_TRACE_OUT__
__MDN_TRACE__
// logger.debug("First line:");
// logger.debug("{} {} {} {} {} {} {} {} {}", memptr[0], memptr[1], memptr[2], memptr[3], memptr[4], memptr[5], memptr[6], memptr[7], memptr[8]);
// logger.debug("CIDS: {}-{}", cluster_ids[0], cluster_ids[Natoms-1]);
// logger.debug("MASS: {}-{}", masses[0], masses[Natoms-1]);
// logger.debug("velx: {}-{}", velX[0], velX[Natoms-1]);
// logger.debug("vely: {}-{}", velY[0], velY[Natoms-1]);
// logger.debug("velz: {}-{}", velZ[0], velZ[Natoms-1]);
Volume = std::abs((boxxhi - boxxlo) * (boxyhi - boxylo) * (boxzhi - boxzlo));
particles_by_cluster_id.clear();
for (uint64_t i = 0; i < Natoms; ++i) particles_by_cluster_id[cluster_ids[i]].emplace_back(i);
unique_cluster_ids.clear();
for (size_t i = 0; i < Natoms; ++i) unique_cluster_ids.emplace(cluster_ids[i]);
// for (const uint64_t& i : cluster_ids) unique_cluster_ids.emplace(i);
Nclusters = unique_cluster_ids.size();
__MDN_TRACE__
cluster_ids_by_size.clear();
particles_by_size.clear();
c_size = 0;
for (const uint64_t &i : unique_cluster_ids)
{
c_size = particles_by_cluster_id[i].size();
cluster_ids_by_size[c_size].push_back(i);
particles_by_size[c_size].insert(particles_by_size[c_size].end(), particles_by_cluster_id[i].begin(), particles_by_cluster_id[i].end());
}
__MDN_TRACE__
std::fill(sizes_counts.begin(), sizes_counts.end(), 0);
for (const auto &[k, v] : cluster_ids_by_size)
{
if (v.size()){
sizes_counts[k] = v.size();
if (k > max_cluster_size) max_cluster_size = k;
}
}
__MDN_TRACE__
#ifdef __CALC_PE__
#ifdef __MDN_PROFILING__
Tcalc_PE.b();
#endif // !__MDN_PROFILING__
__MDN_TRACE__
for (size_t i = 0; i < Natoms; ++i)
for (size_t j = i; j < Natoms; ++j)
{
__MDN_TRACE__
dist = std::sqrt(std::pow(xs[i] - xs[j], 2) + std::pow(ys[i] - ys[j], 2) + std::pow(zs[i] - zs[j], 2));
if (dist < rcut){
en = 4*(std::pow(dist, -12) - std::pow(dist, -6));
pes[i] += en;
pes[j] += en;
}
}
__MDN_TRACE__
#ifdef __MDN_PROFILING__
Tcalc_PE.e();
#endif // !__MDN_PROFILING__
#endif // __CALC_PE__
__MDN_TRACE__
total_temp = 0;
#ifndef __KE_PE_PRESENT__
std::fill(sqvels.begin(), sqvels.end(), 0);
for (size_t i = 0; i < _Natoms; ++i) {
sqvels[i] += std::pow(velX[i], 2) + std::pow(velY[i], 2) + std::pow(velZ[i], 2);
}
std::fill(kes.begin(), kes.end(), 0);
for (size_t i = 0; i < _Natoms; ++i)
{
kes[i] = masses[i]*sqvels[i] / 2;
total_temp += kes[i];
}
// #ifndef __cpp_lib_ranges_zip
// for (const auto &[vel, mass] : zip<const std::span<const double> &, const std::span<const double> &>(velocities, masses))
// #else
// for (const auto &[vel, mass] : std::ranges::views::zip(velocities, masses))
// #endif // !__cpp_lib_ranges_zip
// {
// kes.emplace_back(mass * vel*vel / 2);
// total_temp += kes.back();
// }
#else
__MDN_TRACE__
for (const double& ke: kes) total_temp += ke;
#endif // !__KE_PE_PRESENT__
total_temp = 2 * total_temp / ((Natoms - 1) * ndim);
// logger.debug(total_temp);
__MDN_TRACE__
std::fill(temps_by_size.begin(), temps_by_size.end(), 0.0);
for (const auto &[c_size, v] : particles_by_size)
{
__MDN_TRACE__
ke = 0;
#ifdef __KE_PE_PRESENT__
pe = 0;
#endif // __KE_PE_PRESENT__
for (const uint64_t& i : v)
{
ke += kes[i];
#ifdef __CALC_ENTHROPY__
pe += pes[i];
#endif // __CALC_ENTHROPY__
}
#ifdef __CALC_ENTHROPY__
eng_by_size[size] = (ke + pe) / v.size();
// kes_by_size[size] = ke / v.size();
// pes_by_size[size] = pe / v.size();
#endif // __CALC_ENTHROPY__
// particle_count_by_size = v.size();
// ndofs_by_size[k] = (v.size() - 1) * ndim;
temps_by_size[c_size] = 2 * ke / ((v.size() - 1) * ndim);
}
__MDN_TRACE__
#ifdef __CALC_ENTHROPY__
#ifdef __MDN_PROFILING__
Tcalc_enthropy.b();
#endif // !__MDN_PROFILING__
for (const auto&[size, cl_ids] : cluster_ids_by_size){
for (const uint64_t& cl_id: cl_ids){
pts.clear();
for (const uint64_t& i : particles_by_cluster_id[cl_id]){
pts[i] = {xs[i], ys[i], zs[i]};
}
for (size_t i = 0; i < Nr_bins+1; ++i){
r_bin[i].clear();
r_bin[i].resize(size, 0);
for (size_t j = 0; j < Nt_bins+1; ++j)
for (size_t k = 0; k < Np_bins+1; ++k){
ang_bin[i][j][k].clear();
ang_bin[i][j][k].resize(size, 0);
}
}
size_t i = 0;
for (size_t id = 0; id < size; ++id){
_pts.clear();
i = 0;
for (size_t _id = 0; _id < size; ++_id){
if (_id == id) continue;
rvi[i] = std::sqrt(std::pow(pts[_id][0] - pts[id][0], 2) + std::pow(pts[_id][1] - pts[id][1], 2) + std::pow(pts[_id][2] - pts[id][2], 2));
if (rvi[i] < rcut){
_pts.push_back(pts[_id]);
++i;
}
}
rho_a[id] = static_cast<double>(_pts.size());
for (size_t _id = 0; _id < _pts.size(); ++_id){
r_bin_n = static_cast<size_t>(rvi[_id] / bin_r_w);
phi = std::atan2(_pts[_id][1] - pts[id][1], _pts[_id][0] - pts[id][0]) + std::numbers::pi;
// if (phi < 0) phi += 2 * std::numbers::pi;
p_bin_n = static_cast<size_t>(phi / bin_p_w);
theta = std::atan((_pts[_id][2] - pts[id][2])/(_pts[_id][1] - pts[id][1]));
// if (theta < - std::numbers::pi / 2) theta = - (std::numbers::pi + theta);
// if (theta > std::numbers::pi / 2) theta = std::numbers::pi - theta;
t_bin_n = static_cast<size_t>((theta + std::numbers::pi / 2) / bin_t_w);
++ang_bin[r_bin_n][t_bin_n][p_bin_n][id];
++r_bin[r_bin_n][id];
}
}
for (size_t i = 0; i < size; ++i)
{
r_bin[Nr_bins-1][i] += r_bin[Nr_bins][i];
}
for (size_t i = 0; i < Nr_bins; ++i)
for (size_t j = 0; j < Nt_bins; ++j)
for (size_t k = 0; k < size; ++k)
ang_bin[i][j][Np_bins - 1][k] += ang_bin[i][j][Np_bins][k];
for (size_t i = 0; i < Nr_bins; ++i)
for (size_t j = 0; j < Np_bins; ++j)
for (size_t k = 0; k < size; ++k)
ang_bin[i][Nt_bins - 1][j][k] += ang_bin[i][Nt_bins][j][k];
for (size_t i = 0; i < Nt_bins; ++i)
for (size_t j = 0; j < Np_bins; ++j)
for (size_t k = 0; k < size; ++k)
ang_bin[Nr_bins - 1][i][j][k] += ang_bin[Nr_bins][i][j][k];
rho = 0;
for (size_t i = 0; i < size; ++i)
rho += rho_a[i];
rho /= size * rho_normalize;
s = 0;
for (size_t i = 0; i < Nr_bins; ++i)
{
s = 0;
for (size_t j = 0; j < size; ++j) s += r_bin[i][j];
rs[i] = s / (size * lv[i] * rho);
// rs[i] = s / (size * 4 * std::numbers::pi * std::pow(rbs[i], 2) * bin_r_w * rho);
}
for (size_t i = 0; i < Nr_bins; ++i){
for (size_t j = 0; j < Nt_bins; ++j)
for (size_t k = 0; k < Np_bins; ++k){
s = 0;
for (size_t l = 0; l < size; ++l) s += ang_bin[i][j][k][l];
ang_s[i][j][k] = s / (size * lv[i] * rho);
// ang_s[i][j][k] = s / (size * 4 * std::numbers::pi * std::pow(rbs[i], 2) * bin_r_w * rho);
}
}
for (size_t i = 0; i < Nr_bins; ++i){
r_sum = 0;
for (size_t j = 0; j < Nt_bins; ++j){
t_sum = 0;
// ternary avoid nan in 0*ln(0)
for (size_t k = 0; k < Np_bins; ++k) t_sum += (ang_s[i][j][k]>0) ? ang_s[i][j][k] * std::log(ang_s[i][j][k]) * bin_p_w : 0;
r_sum += t_sum * bin_t_w;
}
enth_ang[i] = -r_sum / (4 * std::numbers::pi);
}
enth = 0;
for (size_t i = 0; i < Nr_bins; ++i)
enth += rs[i] * enth_ang[i] * std::pow(rbs[i], 2);
enth *= 4 * std::numbers::pi * bin_r_w;
enth_by_size[size] += enth;
}
enth_by_size[size] /= cl_ids.size();
// f_energy[size] = kes_by_size[size] + pes_by_size[size] - enth_by_size[size]*temps_by_size[size];
// kes_by_size[size] + pes_by_size[size] - enth_by_size[size]*temps_by_size[size];
}
#ifdef __MDN_PROFILING__
Tcalc_enthropy.e();
#endif // !__MDN_PROFILING__
#endif // __CALC_ENTHROPY__
__MDN_TRACE__
#ifdef __MDN_PROFILING__
TADIOS_write_data.b();
#endif // !__MDN_PROFILING__
writer.BeginStep();
writer.Put(WvarNstep, ×tep);
writer.Put(WvarDist, sizes_counts.data());
writer.Put(WvarVol, &_Volume);
writer.Put(WvarN, &Natoms);
writer.Put(WvarTTemp, &total_temp);
writer.Put(WvarTemps, temps_by_size.data());
#ifdef __CALC_ENTHROPY__
writer.Put(WvarEnthr, enth_by_size.data());
writer.Put(WvarEnerg, eng_by_size.data());
#endif // __CALC_ENTHROPY__
// writer.PerformPuts();
// writer.PerformDataWrite();
writer.EndStep();
#ifdef __MDN_PROFILING__
TADIOS_write_data.e();
#endif // !__MDN_PROFILING__
__MDN_TRACE__
#ifdef __CALC_ENTHROPY__
__MDN_TRACE__
std::fill(enth_by_size.begin(), enth_by_size.end(), 0.0);
std::fill(eng_by_size.begin(), eng_by_size.end(), 0.0);
// std::fill(kes_by_size.begin(), kes_by_size.end(), 0.0);
// std::fill(pes_by_size.begin(), pes_by_size.end(), 0.0);
#ifndef __KE_PE_PRESENT__
pes.clear();
// #else
// std::fill(f_energy.begin(), f_energy.end(), 0.0);
#endif // !__KE_PE_PRESENT__
__MDN_TRACE__
#endif // __CALC_ENTHROPY__
#ifdef __MDN_PROFILING__
TPstep.cutoff();
#endif // __MDN_PROFILING__
__MDN_TRACE__
++done_steps;
Tmisc.check();
if (Tmisc.count() > 60*1000){
__MDN_TRACE__
MPI_File_iwrite_at(fh, off + sizeof(int), ¤tStep, 1, MPI_UINT64_T, &req);
Tmisc.b();
logger.debug("Progress: {}% ({}/{})", (static_cast<long double>(done_steps) / total_steps)*100.0, done_steps, total_steps);
logger.debug("Storage: {}% ({}/{})", (static_cast<long double>(currentStep-steps.first) / (steps.second - steps.first))*100.0, currentStep, (steps.first + steps.second));
__MDN_TRACE__
#ifdef __MDN_PROFILING__
logger.info("Total wall: {} ms", format_duration(Tglobal.current_dur()));
logger.info("├─Per storage: {} ms", TPstorage.current());
logger.info("└─Per step: {} ms", TPstep.current());
logger.info(" ├─Input: {} ms", TADIOS_get_data.sum());
logger.info(" ├─Output: {} ms", TADIOS_write_data.sum());
logger.info(" ├─Calc PE: {} ms", Tcalc_PE.sum());
logger.info(" ├─Calc ENTH: {} ms", Tcalc_enthropy.sum());
// logger.info(" ├─Cleaning: {} ms", Tcleaning.sum());
double auxtime = TPstep.current() - TADIOS_get_data.sum() - TADIOS_write_data.sum(); // - Tcleaning.sum();
#ifdef __CALC_PE__
auxtime -= Tcalc_PE.sum();
#endif // __CALC_PE__
#ifdef __CALC_ENTHROPY__
auxtime -= Tcalc_enthropy.sum();
#endif // __CALC_ENTHROPY__
logger.info(" └─Not stated {} ms", auxtime);
#endif // !__MDN_PROFILING__
__MDN_TRACE__
}
logger.flush();
__MDN_TRACE__
}
__MDN_TRACE__
reader.Close();
__MDN_TRACE__
}catch (std::logic_error& e){
logger.error("Some std::logic_error happened on storage {}, steps: ()", storage.string(), steps.first, steps.second);
logger.error(e.what());
logger.error("Assuming we can go to the next storage");
}catch(std::exception& e){
logger.error("Some std::exception happened on storage {}, steps: ()", storage.string(), steps.first, steps.second);
logger.error(e.what());
logger.error("Assuming we can go to the next storage");
}catch (...){
logger.error("Some error happened on storage {}, steps: ()", storage.string(), steps.first, steps.second);
logger.error("Error is not inherits std::exception, so exiting... Probably the whole program may be aborted");
throw;
}
#ifdef __MDN_PROFILING__
TPstorage.cutoff();
#endif // !__MDN_PROFILING__
__MDN_TRACE__
MPI_File_write_at(fh, off, &storage_index, 1, MPI_INT, &status);
MPI_File_write_at(fh, off + sizeof(int), ¤tStep, 1, MPI_UINT64_T, &status);
logger.info("End storage: {}", storage.string());
__MDN_TRACE__
}
__MDN_TRACE__
writer.Close();
logger.debug("Closed writer");
// done_steps_primary = done_steps;
__MDN_TRACE__
#ifdef __MDN_PROFILING__
TPstep.check();
TPstorage.check();
Tglobal.check();
Tglobal.cutoff();
logger.info("Final profiling results: {} ms per step, {} ms per storage", TPstep.current(), TPstorage.current());
logger.info("Profiling: {} ms total wall time", Tglobal.current());
#endif // !__MDN_PROFILING__
__MDN_TRACE__
MPI_File_close(&fh);
logger.info("End of processing");
}
} // namespace
#endif // !__MDN_LOGIC__