-
Notifications
You must be signed in to change notification settings - Fork 43
/
System_methods2.cpp
executable file
·1621 lines (1344 loc) · 57 KB
/
System_methods2.cpp
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 (C) 2015-2022 Alexey V. Akimov
*
* This file is distributed under the terms of the GNU General Public License
* as published by the Free Software Foundation, either version 3 of
* the License, or (at your option) any later version.
* See the file LICENSE in the root directory of this distribution
* or <http://www.gnu.org/licenses/>.
*
*********************************************************************************/
/**
\file System_methods2.cpp
\brief The file implements topology building functions as well as general creation (building) functions
*/
#if defined(USING_PCH)
#include "../../pch.h"
#else
#include <map>
#endif
#include "System.h"
/// liblibra namespace
namespace liblibra{
using namespace std;
/// libchemobjects namespace
namespace libchemobjects{
/// libchemsys namespace
namespace libchemsys{
int is_in_vector(int indx,vector<int> vect){
/**
This functions is not to be exposed to user.
It searches for index "indx" of int type in a vector of ints
Return 1 if "indx" has been found, 0 - otherwise
*/
int res = 0;
int sz = vect.size();
for(int i=0;i<sz;i++){
if(vect[i]==indx) { res = 1; break;}
}
return res;
}
void System::create_bond(int a1,int a2,int exclude12){
Group bond;
bond.globGroup_Size = 2;
bond.locGroup_Size = 2;
bond.Group_Size = 2;
bond.locAtom_Index.push_back(0);
bond.locAtom_Index.push_back(1);
int is_valid = 1;
// If any two atoms are the same - it is wrong angle
if(a1==a2){ is_valid = 0; }
if(is_valid){
//------------------------- Check on existence -------------------
int is_bond_exist = 0;
is_bond_exist = (Find_Bond(a1,a2)>-1);
//---------------------------------------------------------------
// If angle is not yet exist
if(!is_bond_exist){
//----------------- Create normal bond ----------------------
if(bond.globAtom_Index.size()>0){ bond.globAtom_Index.clear(); }
bond.globAtom_Index.push_back(a1);
bond.globAtom_Index.push_back(a2);
bond.globGroup_Index = Number_of_bonds;
Number_of_bonds++;
bond.locGroup_Index = Molecules[Atoms[a1].globMolecule_Index].Molecule_Number_of_angles;
Molecules[Atoms[a1].globMolecule_Index].Molecule_Number_of_bonds++;
bond.globMolecule_Index = Atoms[a1].globMolecule_Index;
Bonds.push_back(bond);
//-------------- Create fragmental bond ------------------------
int g1,g2;
g1 = Atoms[a1].globGroup_Index;
g2 = Atoms[a2].globGroup_Index;
if(g1==g2) { } // All atoms belong to the same group
else{ // At least 1 atom belong to different group
if(!is_in_vector(bond.globGroup_Index, Frag_bonds)){
Frag_bonds.push_back(bond.globGroup_Index);
Number_of_frag_bonds++;
}
}
/*
//-------------- Erase 1,2-pairs if necessary ---------------------
if(exclude12){
int frag_pair_indx = Find_Frag_Pair(a1,a2);
if(frag_pair_indx!=-1){
//------- Effective erasing of the frag_pair_indx-th frag pair ------------
int last = Number_of_frag_pairs-1;
// Copy last pair to i-th position
Frag_pairs[frag_pair_indx] = Frag_pairs[last];
// However, update some information of this pair
Pairs[frag_pair_indx].globGroup_Index = frag_pair_indx;
Pairs[frag_pair_indx].locGroup_Index = frag_pair_indx; // = globGroup_Index
// Delete last pair (just copied - avoid pair repetition)
Frag_pairs.pop_back();
Number_of_frag_pairs--;
}// if frag_pair_indx!=-1
}//if exclude12
*/
}// !is_bond_exist
}// is_valid
}
void System::create_angle(int a1,int a2,int a3,int exclude13){
Group angle;
angle.globGroup_Size = 3;
angle.locGroup_Size = 3;
angle.Group_Size = 3;
angle.locAtom_Index.push_back(0);
angle.locAtom_Index.push_back(1);
angle.locAtom_Index.push_back(2);
int is_valid = 1;
// If any two atoms are the same - it is wrong angle
if((a1==a3)||(a1==a2)||(a2==a3)){ is_valid = 0; }
if(is_valid){
//------------------------- Check on existence -------------------
int is_angle_exist = 0;
is_angle_exist = (Find_Angle(a1,a2,a3)>-1);
//---------------------------------------------------------------
// If angle is not yet exist
if(!is_angle_exist){
//----------------- Create normal angle ----------------------
if(angle.globAtom_Index.size()>0){ angle.globAtom_Index.clear(); }
angle.globAtom_Index.push_back(a1);
angle.globAtom_Index.push_back(a2);
angle.globAtom_Index.push_back(a3);
angle.globGroup_Index = Number_of_angles;
Number_of_angles++;
angle.locGroup_Index = Molecules[Atoms[a1].globMolecule_Index].Molecule_Number_of_angles;
Molecules[Atoms[a1].globMolecule_Index].Molecule_Number_of_angles++;
angle.globMolecule_Index = Atoms[a1].globMolecule_Index;
Angles.push_back(angle);
//-------------- Create fragmental angle ------------------------
int g1,g2,g3;
g1 = Atoms[a1].globGroup_Index;
g2 = Atoms[a2].globGroup_Index;
g3 = Atoms[a3].globGroup_Index;
if( (g1==g2) && (g2==g3) ) { } // All atoms belong to the same group
else{ // At least 1 atom belong to different group
if(!is_in_vector(angle.globGroup_Index, Frag_angles)){
Frag_angles.push_back(angle.globGroup_Index);
Number_of_frag_angles++;
}
}
/*
//-------------- Erase 1,3-pairs is necessary ---------------------
if(exclude13){
int frag_pair_indx = Find_Frag_Pair(a1,a3);
if(frag_pair_indx!=-1){
//------- Effective erasing of the frag_pair_indx-th frag pair ------------
int last = Number_of_frag_pairs-1;
// Copy last pair to i-th position
Frag_pairs[frag_pair_indx] = Frag_pairs[last];
// However, update some information of this pair
Pairs[frag_pair_indx].globGroup_Index = frag_pair_indx;
Pairs[frag_pair_indx].locGroup_Index = frag_pair_indx; // = globGroup_Index
// Delete last pair (just copied - avoid pair repetition)
Frag_pairs.pop_back();
Number_of_frag_pairs--;
}// if frag_pair_indx!=-1
}//if exclude13
*/
}// !is_angle_exist
}// is_valid
}
void System::create_dihedral(int a1,int a2,int a3,int a4,int exclude14){
Group dihedral;
dihedral.globGroup_Size = 4;
dihedral.locGroup_Size = 4;
dihedral.Group_Size = 4;
dihedral.locAtom_Index.push_back(0);
dihedral.locAtom_Index.push_back(1);
dihedral.locAtom_Index.push_back(2);
dihedral.locAtom_Index.push_back(3);
int is_valid = 1;
// If any two atoms are the same - it is wrong dihedral
if((a1==a4)||(a1==a3)||(a1==a2)||(a2==a4)||(a2==a3)||(a3==a4)){ is_valid = 0; }
if(is_valid){
//------------------------- Check on existence -------------------
int is_dihedral_exist = 0;
is_dihedral_exist = (Find_Dihedral(a1,a2,a3,a4)>-1);
//---------------------------------------------------------------
// If dihedral is not yet exist
if(!is_dihedral_exist){
//--------------- Create normal dihedral --------------------------
if(dihedral.globAtom_Index.size()>0){ dihedral.globAtom_Index.clear(); }
dihedral.globAtom_Index.push_back(a1);
dihedral.globAtom_Index.push_back(a2);
dihedral.globAtom_Index.push_back(a3);
dihedral.globAtom_Index.push_back(a4);
dihedral.globGroup_Index = Number_of_dihedrals;
Number_of_dihedrals++;
dihedral.locGroup_Index = Molecules[Atoms[a1].globMolecule_Index].Molecule_Number_of_dihedrals;
Molecules[Atoms[a1].globMolecule_Index].Molecule_Number_of_dihedrals++;
dihedral.globMolecule_Index = Atoms[a1].globMolecule_Index;
Dihedrals.push_back(dihedral);
//-------------- Create fragmental dihedral ------------------------
int g1,g2,g3,g4;
g1 = Atoms[a1].globGroup_Index;
g2 = Atoms[a2].globGroup_Index;
g3 = Atoms[a3].globGroup_Index;
g4 = Atoms[a4].globGroup_Index;
if( (g1==g2) && (g2==g3) && (g3==g4) ) { } // All atoms belong to the same group
else{ // At least 1 atom belong to different group
if(!is_in_vector(dihedral.globGroup_Index, Frag_dihedrals)){
Frag_dihedrals.push_back(dihedral.globGroup_Index);
Number_of_frag_dihedrals++;
}
}
/*
//-------------- Erase 1,4-pairs is necessary ---------------------
if(exclude14){
int frag_pair_indx = Find_Frag_Pair(a1,a4);
if(frag_pair_indx!=-1){
//------- Effective erasing of the frag_pair_indx-th frag pair ------------
int last = Number_of_frag_pairs-1;
// Copy last pair to i-th position
Frag_pairs[frag_pair_indx] = Frag_pairs[last];
// However, update some information of this pair
Pairs[frag_pair_indx].globGroup_Index = frag_pair_indx;
Pairs[frag_pair_indx].locGroup_Index = frag_pair_indx; // = globGroup_Index
// Delete last pair (just copied - avoid pair repetition)
Frag_pairs.pop_back();
Number_of_frag_pairs--;
}// if frag_pair_indx!=-1
}//if exclude14
*/
}// !is_dihedral_exist
}// is_valid
}
void System::create_improper(int a1,int a2,int a3,int a4){
Group improper;
improper.globGroup_Size = 4;
improper.locGroup_Size = 4;
improper.Group_Size = 4;
improper.locAtom_Index.push_back(0);
improper.locAtom_Index.push_back(1);
improper.locAtom_Index.push_back(2);
improper.locAtom_Index.push_back(3);
// Create it if and only if atom 1 has 2 connections (so this will be third)
if(Atoms[a1].globAtom_Adjacent_Atoms.size()==2){
//-------------- Create normal improper -------------------------
if(improper.globAtom_Index.size()>0){ improper.globAtom_Index.clear(); }
improper.globAtom_Index.push_back(a1);
improper.globAtom_Index.push_back(a2);
improper.globAtom_Index.push_back(a3);
improper.globAtom_Index.push_back(a4);
improper.globGroup_Index = Number_of_impropers;
Number_of_impropers++;
improper.locGroup_Index = Molecules[Atoms[a1].globMolecule_Index].Molecule_Number_of_impropers;
Molecules[Atoms[a1].globMolecule_Index].Molecule_Number_of_impropers++;
improper.globMolecule_Index = Atoms[a1].globMolecule_Index;
Impropers.push_back(improper);
//-------------- Create fragmental improper ------------------------
int g1,g2,g3,g4;
g1 = Atoms[a1].globGroup_Index;
g2 = Atoms[a2].globGroup_Index;
g3 = Atoms[a3].globGroup_Index;
g4 = Atoms[a4].globGroup_Index;
if( (g1==g2) && (g2==g3) && (g3==g4) ) { } // All atoms belong to the same group
else{ // At least 1 atom belong to different group
if(!is_in_vector(improper.globGroup_Index, Frag_impropers)){
Frag_impropers.push_back(improper.globGroup_Index);
Number_of_frag_impropers++;
}
}
}// if ==2
// But if atom 1 has 3 connections already => this will be forth - so this
// will not be an improper => erase existing improper with the central atom
// being atom 1
else if(Atoms[a1].globAtom_Adjacent_Atoms.size()==3){
int impr_indx = Find_Improper(Atoms[a1].globAtom_Index);
if(impr_indx>=0){
// Check if this improper is in frag_impropers list
if(is_in_vector(Impropers[impr_indx].globGroup_Index, Frag_impropers)){
int impr_frag_indx;
// Find an index of corresponding Frag_improper
for(int s=0;s<Number_of_frag_impropers;s++){
if(Impropers[impr_indx].globGroup_Index==Frag_impropers[s]) { impr_frag_indx = s; break; }
}
// Erase it from the Frag_impropers list:
//------- Effective erasing of the impr_frag_indx-th (for short i-th) frag improper ------------
int last = Number_of_frag_impropers-1;
// Copy last pair to i-th position
if(Frag_impropers[last]==Impropers[Number_of_impropers-1].globGroup_Index){
Frag_impropers[impr_frag_indx] = impr_indx;
}else{
Frag_impropers[impr_frag_indx] = Frag_impropers[last]; //##### NEW
}
// Delete last pair (just copied - avoid pair repetition)
Frag_impropers.pop_back();
Number_of_frag_impropers--;
//-------------------------------------------------------------------------------------------
}// if included
// Erase it from the Impropers list:
//------- Effective erasing of the impr_indx-th (for short i-th) improper ------------
int last = Number_of_impropers-1;
// Copy last pair to i-th position
Impropers[impr_indx] = Impropers[last];
Impropers[impr_indx].globGroup_Index = impr_indx;
Impropers[impr_indx].locGroup_Index = impr_indx;
// Delete last pair (just copied - avoid pair repetition)
Impropers.pop_back();
Number_of_impropers--;
//-------------------------------------------------------------------------------------------
}// if impr_indx>=0
}// if == 3
else{
// Do nothing: for connections <2 this will not create impropers
// for connections >3 all has been deleted already
}
}
///==========================================================================
void System::update_max_id(){
/**
This function updated maximal ids of Atom, Fragment and Molecule
It is not to be exposed to user. Usually it should be placed at the end
of other building functions.
It is necessary because sometimes atoms, fragments or molecules are
deleted so we need to keep max ids updated.
Note that max_id is not the actual number of object of corresponding type
E.g. in a system of 2 atoms they may have ids 20 and 40, so the max_atom_id
will be 40 not 2(of 1 as index)
*/
int max_atom_id;
int max_frag_id;
int max_mol_id;
// Atoms
if(Number_of_atoms>0){
if(Atoms[0].is_Atom_id){ max_atom_id = Atoms[0].Atom_id;}
for(int i=1;i<Number_of_atoms;i++){
if(Atoms[i].is_Atom_id){
max_atom_id = (max_atom_id>=Atoms[i].Atom_id)?max_atom_id:Atoms[i].Atom_id;
}
}
}
// Fragments
if(Number_of_fragments>0){
if(Fragments[0].is_Group_id){ max_frag_id = Fragments[0].Group_id;}
for(int i=1;i<Number_of_fragments;i++){
if(Fragments[i].is_Group_id){
max_frag_id = (max_frag_id>=Fragments[i].Group_id)?max_frag_id:Fragments[i].Group_id;
}
}
}
// Molecules
if(Number_of_molecules>0){
if(Molecules[0].is_Molecule_id){ max_mol_id = Molecules[0].Molecule_id;}
for(int i=1;i<Number_of_molecules;i++){
if(Molecules[i].is_Molecule_id){
max_mol_id = (max_mol_id>=Molecules[i].Molecule_id)?max_mol_id:Molecules[i].Molecule_id;
}
}
}
max_atom_id = max_atom_id; is_max_atom_id = 1;
max_fragment_id = max_frag_id; is_max_fragment_id = 1;
max_molecule_id = max_atom_id; is_max_molecule_id = 1;
}
void System::CREATE_ATOM(Atom at1){
/**
This function creates atom, fragment and molecule instances which all
describe the same simplest object. Function adds all them to the object
space spacified. All interrelations (cross-references) are also constructed.
*/
Atom at = at1;
if(is_max_atom_id){
at.Atom_id = max_atom_id + 1; at.is_Atom_id = 1;
max_atom_id++;
}else{
at.Atom_id = 1; at.is_Atom_id = 1;
max_atom_id = 1; is_max_atom_id = 1;
}
at.globAtom_Index = Number_of_atoms;
Number_of_atoms++;
// This atom automatically becomes a single group(fragment)
Group fr;
if(is_max_fragment_id){
fr.Group_id = max_fragment_id + 1; fr.is_Group_id = 1;
max_fragment_id++;
}else{
fr.Group_id = 1; fr.is_Group_id = 1;
max_fragment_id = 1; max_fragment_id = 1;
}
fr.globGroup_Index = Number_of_fragments;
Number_of_fragments++;
// This atom automatically becomes a single molecule
Molecule mol;
if(max_molecule_id){
mol.Molecule_id = max_molecule_id + 1; mol.is_Molecule_id = 1;
max_molecule_id++;
}else{
mol.Molecule_id = 1; mol.is_Molecule_id = 1;
max_molecule_id = 1; max_molecule_id = 1;
}
mol.globMolecule_Index = Number_of_molecules;
Number_of_molecules++;
// Make cross-references
at.locAtom_Index = 0;
at.globGroup_Index = fr.globGroup_Index;
at.globMolecule_Index = mol.globMolecule_Index;
fr.Group_Size = 1;
fr.globGroup_Size = 1;
fr.locGroup_Size = 1;
fr.globAtom_Index.push_back(at.globAtom_Index);
fr.locAtom_Index.push_back(at.locAtom_Index);
fr.locGroup_Index = 0;
fr.globMolecule_Index = mol.globMolecule_Index;
mol.Molecule_Size = 1;
mol.globMolecule_Size = 1;
mol.locMolecule_Size = 1;
// AAAAA Due to cancellation of globGroup_Index member of molecule class
// mol.globGroup_Index.push_back(fr.globGroup_Index);
// mol.locGroup_Index.push_back(fr.locGroup_Index);
// AAAAA Instead:
mol.globAtom_Index.push_back(at.globAtom_Index);
mol.locAtom_Index.push_back(at.locAtom_Index);
mol.locMolecule_Index = mol.globMolecule_Index; // !!!
// Create interfragmental pairs
// Run over all atoms that existed BEFORE (that is why there is -1) this atom
//for(int i=0;i<(Number_of_atoms-1);i++){
Atoms.push_back(at);
// Run over ALL atoms that currently exist, so this will include self-self pairs!
for(int i=0;i<Number_of_atoms;i++){
Group pair;
pair.globGroup_Size = 2;
pair.locGroup_Size = 2;
pair.Group_Size = 2;
pair.globAtom_Index.push_back(Atoms[i].globAtom_Index);
pair.globAtom_Index.push_back(at.globAtom_Index);
pair.locAtom_Index.push_back(0);
pair.locAtom_Index.push_back(1);
pair.globGroup_Index = Number_of_frag_pairs; // Index of the pair in array of frag pairs
Number_of_frag_pairs++; // Increment the total number of pairs in our object space
Number_of_pairs++; // Increment the total number of pairs in our object space
//------------- Create Pair itself --------------------------
// In contrast to bonds the atoms of the pair may belong to different molecules
// Thus we can not define local index of the pair - they do not stick to some
// particular molecules. Instead they stick to entire object space
pair.locGroup_Index = pair.globGroup_Index;
// Add the pair to the object space
// Frag_pairs.push_back(pair.globGroup_Index); //
// Pairs.push_back(pair); //
}// for i
// Add all objects to the Object Space
// Atoms.push_back(at);
Fragments.push_back(fr);
Molecules.push_back(mol);
VERTEX<Atom*> vrtx(&Atoms[at.globAtom_Index]);
AtomGraph.ADD_VERTEX(vrtx); is_AtomGraph = 1;
// Increment number of degrees of freedom
Nf_t += 3;
// Undefine translation vectors
// is_TV1 = 0;
// is_TV2 = 0;
// is_TV3 = 0;
is_Box = 0;
}
/*
void System::CREATE_ATOM(){
Atom at; // Using the default constructor
CREATE_ATOM(at);
}
*/
int System::is_in_vector(int indx,vector<int>& vect){
/**
This functions is not to be exposed to user.
It searches for index "indx" of int type in a vector of ints
Return 1 if "indx" has been found, 0 - otherwise
*/
int res = 0;
int sz = vect.size();
for(int i=0;i<sz;i++){
if(vect[i]==indx) { res = 1; break;}
}
return res;
}
void System::LINK_ATOMS(Atom& at1,Atom& at2){
/**
\param[in] at1 One of the connected atoms
\param[in] at2 One of the connected atoms
This function performs one of the simplest operations on two atoms
It connects them. Other connections also taken into account - thus
we create not only bonds (pairs of atoms), but also angles, dihedrals, etc.
*/
int i,adj,adj1,adj2;
// Here are some control parameters which then should be made user-definable
// They control if we want to exclude 1,2, 1,3 and 1,4 - pairs from the pair list
// This is related to force field specifications
// for example 1,4 atoms are not exluded in UFF, while 1,2 and 1,3 are excluded
int exclude12 = 1;
int exclude13 = 1;
int exclude14 = 1;
// Check if this bond already exists in the object space
int is_exist = 0;
int is_included;
int n_bonds = Number_of_bonds;
is_exist = (Find_Bond(at1.globAtom_Index,at2.globAtom_Index)>-1);
if(!is_exist){ // Create only if this bond is not yet defined
if(at1.globMolecule_Index==at2.globMolecule_Index){ }
else{ // Connect atoms of different molecules -> total number of molecules decreases
// Molecule with the smallest index will remain (and actually become a union of both molecules)
// Molecule with the biggest index will be deleted. All other molecules (which had index bigger)
// will be renamed (shifted)
//##################### Step 0 ############################################
// First - look for biggest, smallest and the last indexes
int max = (at1.globMolecule_Index>at2.globMolecule_Index)?at1.globMolecule_Index:at2.globMolecule_Index;
int min = (at1.globMolecule_Index<at2.globMolecule_Index)?at1.globMolecule_Index:at2.globMolecule_Index;
int last_mol = Number_of_molecules-1;
//###################### Step 1 ############################################
// Merge max molecule to min one = Copy and Rename (Reindex)
Molecules[min].globMolecule_Size += Molecules[max].globMolecule_Size;
Molecules[min].locMolecule_Size += Molecules[max].locMolecule_Size;
Molecules[min].Molecule_Size += Molecules[max].Molecule_Size;
Molecules[min].Molecule_Number_of_bonds += Molecules[max].Molecule_Number_of_bonds;
Molecules[min].Molecule_Number_of_angles += Molecules[max].Molecule_Number_of_angles;
Molecules[min].Molecule_Number_of_dihedrals += Molecules[max].Molecule_Number_of_dihedrals;
// Update molecule IDs
//Minimal maximal molecule inherits the ID of minimal one
// Other ID decrease by 1:
//os.ObjectSpace_Molecules[max].Molecule_id =
// Molecule
for(i=0;i<Molecules[max].Molecule_Size;i++){
/* AAAAA Due to cancellation of .globGroup_Index member in Molecule class
// Copy and reindex Fragments
int gr_indx = Molecules[max].globGroup_Index[i];
Molecules[min].globGroup_Index.push_back(gr_indx);
Molecules[min].locGroup_Index.push_back(Molecules[min].Molecule_Size + Molecules[max].locGroup_Index[i]);
*/
// Copy and reindex Atoms
int at_indx = Molecules[max].globAtom_Index[i];
Molecules[min].globAtom_Index.push_back(at_indx);
Molecules[min].locAtom_Index.push_back(Molecules[min].Molecule_Size + Molecules[max].locAtom_Index[i]);
}// for i
// Fragments
for(i=0;i<Number_of_fragments;i++){
if(Fragments[i].globMolecule_Index==max){
Fragments[i].globMolecule_Index=min;
Fragments[i].locGroup_Index += Molecules[min].Molecule_Size;
}
else if(Fragments[i].globMolecule_Index>max){
Fragments[i].globMolecule_Index--;
}
}// for i
// Atoms
for(i=0;i<Number_of_atoms;i++){
if(Atoms[i].globMolecule_Index==max){
Atoms[i].globMolecule_Index=min;
}
else if(Atoms[i].globMolecule_Index>max){
Atoms[i].globMolecule_Index--;
}
}// for i
// Bonds
for(i=0;i<Number_of_bonds;i++){
if(Bonds[i].globMolecule_Index==max){
Bonds[i].globMolecule_Index=min;
Bonds[i].locGroup_Index += Molecules[min].Molecule_Number_of_bonds;
}
else if(Bonds[i].globMolecule_Index>max){
Bonds[i].globMolecule_Index--;
}
}// for i
// Angles
for(i=0;i<Number_of_angles;i++){
if(Angles[i].globMolecule_Index==max){
Angles[i].globMolecule_Index=min;
Angles[i].locGroup_Index += Molecules[min].Molecule_Number_of_angles;
}
else if(Angles[i].globMolecule_Index>max){
Angles[i].globMolecule_Index--;
}
}// for i
// Dihedrals
for(i=0;i<Number_of_dihedrals;i++){
if(Dihedrals[i].globMolecule_Index==max){
Dihedrals[i].globMolecule_Index=min;
Dihedrals[i].locGroup_Index += Molecules[min].Molecule_Number_of_dihedrals;
}
else if(Dihedrals[i].globMolecule_Index>max){
Dihedrals[i].globMolecule_Index--;
}
}// for i
//****************************************************************
// Decrement id's of those molecules for which globMolecule_Index is bigger than max
for(i=max+1;i<Number_of_molecules;i++){
Molecules[i].globMolecule_Index--;
Molecules[i].locMolecule_Index = Molecules[i].globMolecule_Index;
Molecules[i].Molecule_id--;
}
// Also modify the global properties of the object space
max_molecule_id--;
//*****************************************************************
// Delete molecule with Molecule_Index = max and decrement number of molecules
// Old version: Time-consuming
vector<Molecule>::iterator it;
it = Molecules.begin();
Molecules.erase(it+max);
Number_of_molecules--;
// Finally update variables:
at1.globMolecule_Index = min;
at2.globMolecule_Index = min;
}// else if(at1.globMolecule_Index!=at2.globMolecule_Index)
//##################### Step 2 ########################################
// Now we are ready to create all new entries - in fact this should be
// a common procedure for two cases: at1.Mol_Indx==at2.Mol_Indx and
// at1.Mol_Indx!=at2.Mol_Indx
//----------------- Bonds ---------------------
create_bond(at1.globAtom_Index,at2.globAtom_Index,exclude12);
EDGE<Group*> eg(at1.globAtom_Index,at2.globAtom_Index,0);
AtomGraph.ADD_EDGE(eg);
//----------------- Angles --------------------
// Angles of type at1.Atom_Index - at2.Atom_Index - atoms adjacent to at2
for(adj=0;adj<at2.globAtom_Adjacent_Atoms.size();adj++){
int adj_indx = at2.globAtom_Adjacent_Atoms[adj];
create_angle(at1.globAtom_Index,at2.globAtom_Index,adj_indx,exclude13);
}
// Angles of type at2.Atom_Index - at1.Atom_Index - atoms adjacent to at1
for(adj=0;adj<at1.globAtom_Adjacent_Atoms.size();adj++){
int adj_indx = at1.globAtom_Adjacent_Atoms[adj];
create_angle(adj_indx,at1.globAtom_Index,at2.globAtom_Index,exclude13);
}
//----------------- Dihedrals -------------------
// Dihedrals of type adj1 - at1 - at2 - adj2
for(adj1=0;adj1<at1.globAtom_Adjacent_Atoms.size();adj1++){
for(adj2=0;adj2<at2.globAtom_Adjacent_Atoms.size();adj2++){
int adj_indx1 = at1.globAtom_Adjacent_Atoms[adj1];
int adj_indx2 = at2.globAtom_Adjacent_Atoms[adj2];
create_dihedral(adj_indx1,at1.globAtom_Index,at2.globAtom_Index,adj_indx2,exclude14);
}
}
// Dihedrals of type at1 - at2 - adj2 - adj22
for(adj2=0;adj2<at2.globAtom_Adjacent_Atoms.size();adj2++){
Atom& at22 = Atoms[at2.globAtom_Adjacent_Atoms[adj2]];
for(int adj22=0;adj22<at22.globAtom_Adjacent_Atoms.size();adj22++){
int adj_indx2 = at22.globAtom_Adjacent_Atoms[adj22];
create_dihedral(at1.globAtom_Index,at2.globAtom_Index,at22.globAtom_Index,adj_indx2,exclude14);
}
}
// Dihedrals of type adj11 - adj1 - at1 - at2 (on the contrary - this one - adds two)
for(adj1=0;adj1<at1.globAtom_Adjacent_Atoms.size();adj1++){
Atom& at11 = Atoms[at1.globAtom_Adjacent_Atoms[adj1]];
for(int adj11=0;adj11<at11.globAtom_Adjacent_Atoms.size();adj11++){
int adj_indx1 = at11.globAtom_Adjacent_Atoms[adj11];
create_dihedral(adj_indx1,at11.globAtom_Index,at1.globAtom_Index,at2.globAtom_Index,exclude14);
}
}
//--------------- Impropers -------------------------------
if(at1.globAtom_Adjacent_Atoms.size()>=2){
create_improper(at1.globAtom_Index,at1.globAtom_Adjacent_Atoms[0],at1.globAtom_Adjacent_Atoms[1],at2.globAtom_Index);
}
if(at2.globAtom_Adjacent_Atoms.size()>=2){
create_improper(at2.globAtom_Index,at2.globAtom_Adjacent_Atoms[0],at2.globAtom_Adjacent_Atoms[1],at1.globAtom_Index);
}
// Add each other to indexes to corresponding variable of adjacent atom indexes(global)
at1.globAtom_Adjacent_Atoms.push_back(at2.globAtom_Index);
at2.globAtom_Adjacent_Atoms.push_back(at1.globAtom_Index);
}// if not exist
}
void System::LINK_ATOMS(int At1_id,int At2_id){
/**
\param[in] at1 The atom ID (not index!) of one of the connected atoms
\param[in] at2 The atom ID (not index!) of one of the connected atoms
This function is a more user-frendly version of LINK_ATOMS(Atom&,Atom&)
function. It searches for two atoms by their id and then performens the same task on
that atoms as the other variant
*/
int v1,v2;
v1 = get_atom_index_by_atom_id(At1_id);
v2 = get_atom_index_by_atom_id(At2_id);
cout<<"in LINK_ATOMS: v1 = "<<v1<<" v2 = "<<v2<<endl;
if((v1!=-1) && (v2!=-1)){ LINK_ATOMS(Atoms[v1],Atoms[v2]); }
else{
cout<<"Error: Can not link atoms with "<<At1_id<<" and "<<At2_id<<endl;
cout<<"Can not find one or both of the atoms with such id(s)\n";
}
}
void System::UPDATE_FRAG_TOPOLOGY(){
/**
This function is not to be exposed to user (or is to do this - i've not
decided yet). Noramlly it should be used at the end of all functions
dealing with fragments and related topological elements.
It simply excludes frag_bonds, frag_angles and frag_dihedrals.
Note it does not add these topological elements.
*/
int i,j;
int a1,a2,a3,a4;
int g1,g2,g3,g4;
int indx1,indx2;
vector<int>::iterator it;
//------------------ Update bonds ------------------------
it = Frag_bonds.begin();
for(i=0;i<Number_of_bonds;i++){
a1 = Bonds[i].globAtom_Index[0];
a2 = Bonds[i].globAtom_Index[1];
g1 = Atoms[a1].globGroup_Index;
g2 = Atoms[a2].globGroup_Index;
if(g1==g2){
indx1 = Bonds[i].globGroup_Index;
// Check if this group exist
for(j=0;j<Number_of_frag_bonds;j++){
indx2 = Frag_bonds[j];
if(indx1==indx2){ // this group still exist in frag topology
// we need to delete it
Frag_bonds.erase(it+j);
Number_of_frag_bonds--;
j--;
break;
}
}// for j
}// g1==g2
}// for i
//--------------------- Update angles ------------------------
it = Frag_angles.begin();
for(i=0;i<Number_of_angles;i++){
a1 = Angles[i].globAtom_Index[0];
a2 = Angles[i].globAtom_Index[1];
a3 = Angles[i].globAtom_Index[2];
g1 = Atoms[a1].globGroup_Index;
g2 = Atoms[a2].globGroup_Index;
g3 = Atoms[a3].globGroup_Index;
if((g1==g2)&&(g2==g3)){
indx1 = Angles[i].globGroup_Index;
// Check if this group exist
for(j=0;j<Number_of_frag_angles;j++){
indx2 = Frag_angles[j];
if(indx1==indx2){ // this group still exist in frag topology
// we need to delete it
Frag_angles.erase(it+j);
Number_of_frag_angles--;
j--;
break;
}
}// for j
}// g1==g2
}// for i
//--------------------- Update dihedrals ------------------------
it = Frag_dihedrals.begin();
for(i=0;i<Number_of_dihedrals;i++){
a1 = Dihedrals[i].globAtom_Index[0];
a2 = Dihedrals[i].globAtom_Index[1];
a3 = Dihedrals[i].globAtom_Index[2];
a4 = Dihedrals[i].globAtom_Index[3];
g1 = Atoms[a1].globGroup_Index;
g2 = Atoms[a2].globGroup_Index;
g3 = Atoms[a3].globGroup_Index;
g4 = Atoms[a4].globGroup_Index;
if((g1==g2)&&(g2==g3)&&(g3==g4)){
indx1 = Dihedrals[i].globGroup_Index;
// Check if this group exist
for(int j=0;j<Number_of_frag_dihedrals;j++){
indx2 = Frag_dihedrals[j];
if(indx1==indx2){ // this group still exist in frag topology
// we need to delete it
Frag_dihedrals.erase(it+j);
Number_of_frag_dihedrals--;
j--;
break;
}
}// for j
}// g1==g2
}// for i
//--------------------- Update impropers ------------------------
it = Frag_impropers.begin();
for(i=0;i<Number_of_impropers;i++){
a1 = Impropers[i].globAtom_Index[0];
a2 = Impropers[i].globAtom_Index[1];
a3 = Impropers[i].globAtom_Index[2];
a4 = Impropers[i].globAtom_Index[3];
g1 = Atoms[a1].globGroup_Index;
g2 = Atoms[a2].globGroup_Index;
g3 = Atoms[a3].globGroup_Index;
g4 = Atoms[a4].globGroup_Index;
if((g1==g2)&&(g2==g3)&&(g3==g4)){
indx1 = Impropers[i].globGroup_Index;
// Check if this group exist
for(j=0;j<Number_of_frag_impropers;j++){
indx2 = Frag_impropers[j];
if(indx1==indx2){ // this group still exist in frag topology
// we need to delete it
Frag_impropers.erase(it+j);
Number_of_frag_impropers--;
j--;
break;
}
}// for j
}// g1==g2
}// for i
//------------------ Update frag pairs ------------------------
vector<int>::iterator it1;
it1 = Frag_pairs.begin();
for(i=0;i<Number_of_frag_pairs;i++){
int fp = Frag_pairs[i];
a1 = Pairs[fp].globAtom_Index[0];
a2 = Pairs[fp].globAtom_Index[1];
g1 = Atoms[a1].globGroup_Index;
g2 = Atoms[a2].globGroup_Index;
if(g1==g2){
//------- Effective erasing of the i-th frag pair ------------
int last = Number_of_frag_pairs-1;
// Copy last pair to i-th position
Frag_pairs[i] = Frag_pairs[last];
// However, update some information of this pair
// WARNING!!! [i] must be changed to something elese!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
Pairs[i].globGroup_Index = i;
Pairs[i].locGroup_Index = i; // = globGroup_Index
// Delete last pair (just copied - avoid pair repetition)
Frag_pairs.pop_back();
Number_of_frag_pairs--;
// Make one step back so to include just copied pair into consideration
i--;
}
}// for i
//----------------- Update excluded pairs ------------------------
for(i=0;i<Number_of_pairs;i++){
a1 = Pairs[i].globAtom_Index[0];
a2 = Pairs[i].globAtom_Index[1];
g1 = Atoms[a1].globGroup_Index;
g2 = Atoms[a2].globGroup_Index;
// Atoms belong to the same group - excluded
if(g1==g2){
//!!!!!!!!!!!!!!!!!!!!!! WARNING : Exclusions are turned off !!!!!!!!!!!!!!!11
// Pairs[i].Group_pair_excluded = 1;
// Pairs[i].is_Group_pair_excluded = 1;
}else{
//------- Check if these atoms form interfragmental bond -------
int res = 0;
int b, b_indx;
for(b=0;b<Number_of_frag_bonds;b++){
b_indx = Frag_bonds[b];
if(( (Bonds[b_indx].globAtom_Index[0]==a1)