-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonte-carlo-adhesion.cpp
1806 lines (1493 loc) · 44.9 KB
/
monte-carlo-adhesion.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
// This C++ code can be used to run Monte Carlo simulations of a lattice-based mesoscale model introduced in:
// - Long Li, Jinglei Hu, Bartosz Rozycki, Fan Song, Nano Letters 20(1):722-728 (2020)
// - Long Li, Jinglei Hu, Xinghua Shi, Bartosz Rozycki, Fan Song, Soft Matter 17(7):1912-1920 (2021)
//
// Authors: Lukasz Milewski & Bartosz Rozycki
// Institute of Physics
// Polish Academy of Sciences
// October 2023
//
// The development of this code was supported by the National Science Centre via grant number 2021/40/Q/NZ1/00017.
//
#include <iostream>
#include <fstream>
#include <random>
#include <cmath>
#include <string>
#include <cstdlib>
#include <sstream>
using namespace std;
#define K 50
// K = linear extension of the square lattice
random_device rd;
mt19937 gen(rd());
int seed = 1;
uniform_real_distribution<> dis(-0.5, 0.5);
uniform_real_distribution<> dist(0.0, 1.0);
// random number generator
int bc[K][2];
// auxiliary matrix for periodic boundary conditions
// arrays L, M, N, PNUM, RNUM below: [K][K][0] refers to the upper membrane, [K][K][1] refers to the lower membrane
double L[K][K][2];
// location of the two membranes relative to the reference plane z=0
int M[K][K][2];
// array with values 0 or 1 to indicate absence or presence of protein particles
int N[K][K][2];
// array with values 0 or 1 to indicate absence or presence of lipid rafts
int PNUM[K][K][2];
// indexes of the receptors and ligands
int RNUM[K][K][2];
// indexes of rafts on the two membranes
vector<int> particle_x, particle_y;
// Cartesian coordinates of all the protein particles
vector<int> raft_x, raft_y;
// Cartesian coordinates of all th lipid rafts
double Cp = 0.05;
// area concentration of the protein particles (in units of 1/a^2)
double X = 0.1;
// area fraction of the lipid rafts
int N0 = int(round(Cp * K * K));
// number of receptors
int N1 = int(round(Cp * K * K));
// number of ligands
int N2 = int(round(X * K * K));
// number of lipid rafts on the upper membrane
int N3 = int(round(X * K * K));
// number of lipid rafts on the lower membrane
double a = 10.0;
// lattice constant (in units of nm)
double kappa = 10.0;
// bending rigidity modulus (in kT units)
double U = 1.6;
// energy of contact between rafts (in kT units)
double U_a = 3.0;
// energy of association of a protein particle with a raft (in kT units)
double U_b = 6.0;
// energy of receptor-ligand binding (in kT units)
double lmax = 4.0;
// maximal variation of membrane vertical displacements (in units of nm)
double l_c = 15.0;
// length of the extracellular domains of the receptor-ligand complex (in units of nm)
double l_b = 1.0;
// width of the receptor-ligand binding potential (in units of nm)
double l_minus = l_c - 0.5 * l_b;
// lower end of the receptor-ligand binding range (in units of nm)
double l_plus = l_c + 0.5 * l_b;
// upper end of the receptor-ligand binding range (in units of nm)
double pos0 = 0.5 * l_c;
double pos1 = -0.5 * l_c;
// initial positions of the membranes (planar at the start of the simulation)
int nxi = 100;
// number of MC cycles between recording observables
int nlmax = 1000;
// number of MC cycles between computing the acceptance rate during the equilibration
int neq = 1000;
// number of MC cycles for equilibration
int ncyc = 1300;
// number of MC cycles used for data acquisition
int res = 0;
// set it to 1 if you read in an initial configuration from a restart file
string input_out;
//name of the file to read an initial parameters from
string input_conf;
// name of the file to read an initial configuration from
int nsnapshot = 100;
// number of MC cycles between recording system configurations
string header, filename, data_out, snapname, snapname_history;
// output file names and header
void arguments_values(int argc, char *argv[]);
// input parameters
void initialise();
// initialization of arrays and vectors, and placing particles and rafts randomly on the membranes
void save_config(ofstream &, int);
// saving configurations to file
void get_values_from_file(const string& filename);
// reading starting variables from previous file name
void read_config(const string& filename);
// reading configurations from a file
void acceptance_rate_try(int, int);
// calculate acceptance rate
double averageL();
// calculate the lattice-averaged distance between the two membranes
double calc_xi();
// calculate the instantaneous relative roughness of the membranes
int how_many_pairs();
// calculate the number or receptor-ligand complexes
double energy_interactions_move(int);
// calculate the receptor-ligand interaction energy for protein particle trial moves and receptor-ligand pair trial moves
double energy_interactions_bending(double, double);
// calculate change in the receptor-ligand interaction energy for membranes bending trial moves
double energy_bending(int, int, int, double, double);
// calculate change in the membrane bending energy
double energy_r_r(int, int);
// calculate change in the energy of contacts between lipid rafts
double energy_r_p_particle(int, int);
// calculate the raft-particle association energy for protein particle trial moves
double energy_r_p_raft(int, int);
// calculate the raft-particle association energy for raft trial moves
void dynamics_particle();
// protein particle trial moves
void dynamics_pairs();
// receptor-ligand pair trial moves
void dynamics_rafts();
// lipid raft trial moves
int dynamics_bending();
// membrane bending trial moves
double total_me_energy(int);
// calculate the total energy of membrane bending
double total_r_p_energy();
// calculate the total energy of raft-particle association
double total_r_r_energy();
// calculate the total energy of raft-raft contacts
double total_RL_energy(int);
// calculate the total energy of receptor-ligand interactions
int main(int argc, char *argv[])
{
arguments_values(argc, argv);
// input parameters
srand(seed);
// feed the random number generator with a seed
initialise();
ofstream xi_out;
// stream for data output file
ofstream snapshot;
// stream for configuration output file
ofstream snapshot_history;
// stream for configuration history output file
if (res == 0)
{
xi_out.open(data_out, ios_base::trunc);
}
else if (res == 1)
{
xi_out.open(data_out, ios_base::app);
}
// open data output file
if (res == 0)
{
snapshot_history.open(snapname_history, ios_base::trunc);
}
else if (res == 1)
{
snapshot_history.open(snapname_history, ios_base::app);
}
// open configuration history output file
//open file
if (res==1)
{
read_config(input_conf);
}
if (N0 > K * K or N1 > K * K or N0 < 0 or N1 < 0 or ncyc < neq or l_c < 0 or l_b < 0 or lmax < 0 or kappa < 0 or nsnapshot > ncyc)
{
cerr << "Wrong parameters! Program has been terminated" << endl;
exit(1);
}
if (res == 0)
{
xi_out << header << "\n";
xi_out << "# column 1: time" << endl;
xi_out << "# column 2: average membrane separation" << endl;
xi_out << "# column 3: membrane roughness" << endl;
xi_out << "# column 4: number of R-L bonds" << endl;
xi_out << "# column 5: number of free particles" << endl;
xi_out << "# column 6: membrane bending energy" << endl;
xi_out << "# column 7: raft contact energy" << endl;
xi_out << "# column 8: particle-raft association energy" << endl;
xi_out << "# column 9: energy of R-L bonds" << endl;
xi_out << "# column 10: total energy" << endl;
}
if (res == 1)
{
xi_out << "# Continue with neq = " << neq << " and ncyc = " << ncyc << endl;
}
double xi, avl, acc_rate;
// xi = membrane roughness
// avl = lattice-averaged distance between the membranes
// acc_rate = acceptance rate
double num = 0;
double E_me, E_rr, E_rp, E_RL, E_total;
// E_me = total energy of membrane bending
// E_rr = total energy of raft-raft interactions
// E_rp = total energy of raft-particle association
// E_RL = total energy of receptor-ligand binding
// E_total = total energy of the simulation system
int pairs = 0;
// number of receptor-ligands complexes
int free = 0;
// number of free receptors
int ntry = 0;
// total number of trial moves for acceptance rate calculation
int nacc = 0;
// number of accepted trial moves for acceptance rate calculation
for (int nmc = 1; nmc <= ncyc; nmc++)
{
// loop over MC cycles
for (int kdyn = 0; kdyn < N2 + N3; kdyn++)
{
dynamics_rafts();
// raft trial moves repeated N2+N3 times
}
for (int kdyn = 0; kdyn < N0 + N1; kdyn++)
{
dynamics_particle();
// protein particle trial moves repeated N2+N3 times
dynamics_pairs();
// receptor-ligand pairs trial moves repeated N2+N3 times
}
for (int ix = 0; ix < K; ix++)
{
for (int iy = 0; iy < K; iy++)
{
for (int k = 0; k < 2; k++)
{
nacc = nacc + dynamics_bending();
// membrane bending trial moves repeated 2*K*K times
// dynamics_bending() returns 1 if trial move is accepted
// dynamics_bending() returns 0 if trial move is rejected
ntry++;
}
}
}
if (nmc % nxi == 0 and nmc > neq)
{
avl = averageL();
xi = calc_xi();
pairs = how_many_pairs();
free = N0 - pairs;
E_me = total_me_energy(0) + total_me_energy(1);
E_rr = total_r_r_energy();
E_rp = total_r_p_energy();
E_RL = total_RL_energy(pairs);
E_total = E_me + E_rr + E_rp + E_RL;
xi_out << nmc - neq << " " << avl << " " << xi << " " << pairs << " " << N0 - pairs << " " << E_me << " "
<< E_rr << " " << E_rp << " " << E_RL << " " << E_total << endl;
// write data to output file
}
if (nmc % nsnapshot == 0)
{
snapshot.open(snapname, ios_base::trunc);
// open configuration output file
save_config(snapshot, -1);
// -1 means do not write MC Step number
// write configuration to file
snapshot.close();
// close configuration output file
save_config(snapshot_history, nmc);
// write configuration to history file
}
if (nmc % nlmax == 0 and nmc < neq)
{
acceptance_rate_try(nacc, ntry);
// compute acceptance rate and adjust lmax
nacc = 0;
ntry = 0;
}
}
snapshot_history.close();
// close configuration history output file
xi_out.close();
// close data output file
return 0;
}
void arguments_values(int argc, char *argv[])
// parameters given as input when program starts
{
if (argc == 12)
{
Cp = stod(argv[1]);
X = stod(argv[2]);
kappa = stod(argv[3]);
U = stod(argv[4]);
U_a = stod(argv[5]);
U_b = stod(argv[6]);
neq = stoi(argv[7]);
ncyc = stoi(argv[8]);
nxi = stoi(argv[9]);
nsnapshot = stoi(argv[10]);
seed = stoi(argv[11]);
res = 0;
}
else if (argc == 5)
{
int neq_new,ncyc_new;
input_out = argv[1];
input_conf = argv[2];
neq_new = stoi(argv[3]);
ncyc_new = stoi(argv[4]);
res = 1;
get_values_from_file(input_out);
//get parameters from previous output file
neq = neq_new;
ncyc = ncyc_new;
cout << "parameter values from previous output file, numbers of equilibration and production cycles new" << endl;
}
else if (argc == 3)
{
input_out = argv[1];
input_conf = argv[2];
res = 1;
get_values_from_file(input_out);
//get parameters from previous output file
cout << "parameter values from previous output file: " << endl;
}
else
{
cout << "default values\n";
}
N0 = int(round(Cp * K * K));
N1 = int(round(Cp * K * K));
N2 = int(round(X * K * K));
N3 = int(round(X * K * K));
//get number of particles and rafts from Cp and X values
}
void initialise()
// initialization of arrays and vectors
// initial random distribution of protein particles and rafts randomly on the membranes
{
header = "# 3-dimensional membrane model \n# square lattice with "
+ to_string(K * K) + " sites\n"
+ "# K = " + to_string(K) + "\n"
+ "# Cp = " + to_string(Cp) + "\n"
+ "# X = " + to_string(X) + "\n"
+ "# N0 = " + to_string(N0) + "\n"
+ "# N1 = " + to_string(N1) + "\n"
+ "# N2 = " + to_string(N2) + "\n"
+ "# N3 = " + to_string(N3) + "\n"
+ "# a = " + to_string(a) + "\n"
+ "# kappa = " + to_string(kappa) + "\n"
+ "# lmax0 = " + to_string(lmax) + "\n"
+ "# U = " + to_string(U) + "\n"
+ "# Ua = " + to_string(U_a) + "\n"
+ "# Ub = " + to_string(U_b) + "\n"
+ "# lc = " + to_string(l_c) + "\n"
+ "# lb = " + to_string(l_b) + "\n"
+ "# neq = " + to_string(neq) + "\n"
+ "# ncyc = " + to_string(ncyc) + "\n"
+ "# nxi = " + to_string(nxi) + "\n"
+ "# nlmax = " + to_string(nlmax) + "\n"
+ "# nsnapshot = " + to_string(nsnapshot) + "\n"
+ "# RNG seed = " + to_string(seed);
filename = "K_" + to_string(K)
+ "_cp_" + to_string(Cp)
+ "_x_" + to_string(X)
+ "_U_" + to_string(U)
+ "_Ua_" + to_string(U_a)
+ "_Ub_" + to_string(U_b)
+ "_seed_" + to_string(int(seed));
data_out = filename + "_data.out";
snapname = filename + "_snapshot.txt";
snapname_history = filename + "_snapshot_history.txt";
particle_x.resize(N0 + N1);
// set the size of the vector with the x-coordinates of protein particles
particle_y.resize(N0 + N1);
// set the size of the vector with the y-coordinates of protein particles
raft_x.resize(N2 + N3);
// set the size of the vector with the x-coordinates of raft particles
raft_y.resize(N2 + N3);
// set the size of the vector with the y-coordinates of raft particles
for (int i = 0; i < K; i++)
{
bc[i][0] = i + 1;
bc[i][1] = i - 1;
}
bc[K - 1][0] = 0;
bc[0][1] = K - 1;
// bc matrix is used to implement periodic boundary conditions
for (int i = 0; i < K; i++)
{
for (int j = 0; j < K; j++)
{
for (int k = 0; k < 2; k++)
{
M[i][j][k] = 0;
PNUM[i][j][k] = -1;
N[i][j][k] = 0;
RNUM[i][j][k] = -1;
// at start, membranes with no receptors, ligands or rafts
}
}
}
if (res != 1)
// randomly distribute protein particles and rafts on the membranes
{
for (int i = 0; i < K; i++)
{
for (int j = 0; j < K; j++)
{
L[i][j][0] = pos0;
L[i][j][1] = pos1;
// planar membranes located at z = pos0 (upper membrane) and z = pos1 (lower membrane)
}
}
int aa = 0;
// counter
int rx, ry;
while (aa < N0)
// distribute N0 receptors numbered from 0 to N0-1
{
rx = rand() % K;
ry = rand() % K;
// randomly choose x- and y-coordinates to place a receptor on the upper membrane
if (M[rx][ry][0] == 0)
{
M[rx][ry][0] = 1;
// if the chosen membrane patch is not occupied by another receptor, placed the receptor with index aa here
particle_x[aa] = rx;
particle_y[aa] = ry;
// store the x- and y-coordinates of the receptor with index aa
PNUM[rx][ry][0] = aa;
// receptor index aa stored in particle index array
aa++;
}
}
aa = 0;
// reset counter
while (aa < N1)
// distribute N1 ligands numbered from N0 to N0+N1-1
{
rx = rand() % K;
ry = rand() % K;
// randomly choose x- and y-coordinates to place a ligand on the lower membrane
if (M[rx][ry][1] == 0)
{
M[rx][ry][1] = 1;
// if the chosen membrane patch is not occupied by another ligand, placed the ligand with index aa here
particle_x[aa + N0] = rx;
particle_y[aa + N0] = ry;
// store the x- and y-coordinates of the ligand with index aa
PNUM[rx][ry][1] = aa;
// ligand index aa stored in particle index array
aa++;
}
}
aa = 0;
//reset counter
while (aa < N2)
// distribute N2 rafts numbered from 0 to N2-1 on the upper membrane
{
rx = rand() % K;
ry = rand() % K;
// randomly choose x- and y-coordinates to place a raft on the upper membrane
if (N[rx][ry][0] == 0)
{
N[rx][ry][0] = 1;
// if the chosen membrane patch is not occupied by another raft, placed the raft with index aa here
raft_x[aa] = rx;
raft_y[aa] = ry;
// store the x- and y-coordinates of the raft with index aa
RNUM[rx][ry][0] = aa;
// raft index aa stored in raft index array
aa++;
}
}
aa = 0;
//reset counter
while (aa < N3)
// distribute N3 rafts numbered from N2 to N2+N3-1 on the lower membrane
{
rx = rand() % K;
ry = rand() % K;
// randomly choose x- and y-coordinates to place a raft on the lower membrane
if (N[rx][ry][1] == 0)
{
N[rx][ry][1] = 1;
// if the chosen membrane patch is not occupied by another raft, placed the raft with index aa here
raft_x[aa + N2] = rx;
raft_y[aa + N2] = ry;
// store the x- and y-coordinates of the raft with index aa
RNUM[rx][ry][1] = aa;
// raft index aa stored in raft index array
aa++;
}
}
}
}
void save_config(ofstream &fout, int t)
// save configurations to file
{
int rn, ln, lun, lln;
// rn = index of a receptor
// ln = index of a ligand
// lun = index of an upper membrane raft
// lln = index of a lower membrane raft
if (t != -1)
//write MC step only to snapshot history output file
{
fout << "# MC step: " << t << endl;
}
for (int x = 0; x < K; x++)
{
for (int y = 0; y < K; y++)
{
rn = PNUM[x][y][0] + 1;
ln = PNUM[x][y][1] + 1;
lun = RNUM[x][y][0] + 1;
lln = RNUM[x][y][1] + 1;
// 1 is added to have indexes as natural numbers larger than 0
fout << x << " " << y << " " << L[x][y][0] << " " << L[x][y][1] << " " << rn << " " << ln << " " << lun
<< " " << lln << endl;
// eight columns:
// 1. x coordinate
// 2. y coordinate
// 3. local position of the upper membrane at this lattice site
// 4. local position of the lower membrane at this lattice site
// 5. receptor index (0 if none) at this lattice site
// 6. ligand index (0 if none) at this lattice site
// 7. raft index (0 if none) at this lattice site
// 8. raft index (0 if none) at this lattice site
}
}
}
void get_values_from_file(const string& filename)
{
ifstream inputFile(filename);
if (!inputFile.is_open())
{
cerr << "Failed to open the input file. Program has been terminated." << endl;
exit(-1);
}
string line;
while (getline(inputFile, line))
{
istringstream iss(line);
string par;
iss >> par;
int K_test;
if (par == "Cp")
{
iss >> Cp;
}
if (par == "K")
{
iss >> K_test;
cout << K_test;
if (K_test != K)
{
cerr << "You cannot continue previous calculation with different K (Program should be compiled with K = "<<K_test<<")! Program has been terminated." << endl;
exit(-1);
}
}
else if (par == "X")
{
iss >> X;
}
else if (par == "a")
{
iss >> a;
}
else if (par == "kappa")
{
iss >> kappa;
}
else if (par == "U")
{
iss >> U;
}
else if (par == "Ua")
{
iss >> U_a;
}
else if (par == "Ub")
{
iss >> U_b;
}
else if (par == "lc")
{
iss >> l_c;
}
else if (par == "lb")
{
iss >> l_b;
}
else if (par == "lmax0")
{
iss >> lmax;
}
else if (par == "nxi")
{
iss >> nxi;
}
else if (par == "neq")
{
iss >> neq;
}
else if (par == "ncyc")
{
iss >> ncyc;
}
else if (par == "nsnapshot")
{
iss >> nsnapshot;
}
else if (par == "nlmax")
{
iss >> nlmax;
}
else if (par == "seed")
{
iss >> seed;
}
}
inputFile.close();
double l_minus = l_c - 0.5 * l_b;
double l_plus = l_c + 0.5 * l_b;
double pos0 = 0.5 * l_c;
double pos1 = -0.5 * l_c;
}
void read_config(const string& filename)
{
int x, y, rn, ln, lun, lln;
double l0, l1;
ifstream inputFile(filename);
if (!inputFile.is_open())
{
cerr << "Failed to open the input file. Program has been terminated." << endl;
exit(-1);
}
string line;
while (getline(inputFile, line))
{
istringstream iss(line);
for (int x = 0; x < K; x++)
{
for (int y = 0; y < K; y++)
{
iss >> x >> y >> l0 >> l1 >> rn >> ln >> lun >> lln;
if (x < 0 or y < 0 or rn < 0 or ln < 0 or lun < 0 or lln < 0)
{
cerr << "Wrong values!" << endl;
cerr << "Program has been terminated." << endl;
exit(1);
}
else if (l0 < l1)
{
cerr << "Wrong values!" << endl;
cerr << "Program has been terminated." << endl;
exit(1);
}
L[x][y][0] = l0;
L[x][y][1] = l1;
PNUM[x][y][0] = rn - 1;
PNUM[x][y][1] = ln - 1;
RNUM[x][y][0] = lun - 1;
RNUM[x][y][1] = lln - 1;
// 1 was added to have indexes in configuration file as natural numbers larger than 0
}
}
}
inputFile.close();
}
void acceptance_rate_try(int nacc, int ntry)
// calculate the acceptance rate for membrane bending trial moves and adjust lmax
{
double a, b, r;
a = nacc;
b = ntry;
r = a / b;
// acceptance rate = number of accepted trial moves divided by total number of trial moves
if (r > 0.5)
//if acceptance rate higher than 50%
{
lmax = lmax * 1.05;
}
if (r < 0.3)
//if acceptance rate lower than 30%
{
lmax = lmax * 0.95;
}
}
double averageL()
// lattice-averaged distance between the membranes
{
double ll;
// lattice-averaged distance between the membranes
double li;
// local distance between the membranes at a given lattice site
double sum = 0.0;
// sum of li
for (int ix = 0; ix < K; ix++)
{
for (int iy = 0; iy < K; iy++)
{
li = L[ix][iy][0] - L[ix][iy][1];
// local distance between the membranes = difference between the local positions
sum = sum + li;
}
}
ll = sum / (K * K);
return ll;
}
double calc_xi()
// instantaneous relative roughness of the membranes
{
double xi;
// membrane roughness
double li;
// local distance between the membranes
double sum, sumsqr, mean, meansqr;
sum = 0.0;
sumsqr = 0.0;
for (int i = 0; i < K; i++)
{
for (int j = 0; j < K; j++)
{
li = L[i][j][0] - L[i][j][1];
// local distance between the membranes = difference between the local positions
sum = sum + li;
sumsqr = sumsqr + li * li;
}
}
mean = sum / (K * K);
meansqr = sumsqr / (K * K);
xi = sqrt(meansqr - (mean * mean));
return xi;
}
int how_many_pairs()
// number of receptor-ligand complexes
{
int pairs = 0;
double li;
// local distance between the membranes
for (int i = 0; i < K; i++)
{
for (int j = 0; j < K; j++)
{
if (M[i][j][0] == 1 and M[i][j][1] == 1)
// there is a receptor on the upper membrane and a ligand on the lower membrane
{
li = L[i][j][0] - L[i][j][1];
// local distance between the membranes = difference between the local positions
if (li > l_minus and li < l_plus)
// specified distance range required for the receptor-ligand binding
{
pairs++;
}
}
}
}
return pairs;
}
double energy_interactions_move(int nr)
// receptor-ligand interaction energy for trial moves of protein particles and their pairs
// nr = particle index
{
double E = 0;
// receptor-ligand interaction energy
double li;
// local distance between the membranes
int x, y;
// x- and y-coordinates
int n, m;
// receptor absence (n=0) or presence (n=1)
// ligand absence (m=0) or presence (m=1)
x = particle_x[nr];
y = particle_y[nr];
// x- and y-coordinates of particle with index nr
n = M[x][y][0];
// receptor absence (n=0) or presence (n=1) at this patch of the upper membrane
m = M[x][y][1];
// ligand absence (m=0) or presence (m=1) at this patch of the lower membrane
li = L[x][y][0] - L[x][y][1];
// local distance between the membranes
if (n == 1 and m == 1)
// there is a receptor on the upper membrane and a ligand on the lower membrane
{
if (li > l_minus and li < l_plus)
// specified distance range required for the receptor-ligand binding
{
E = -U_b;
// receptor-ligand interaction energy < 0
}
}
return E;
}
double energy_interactions_bending(double li_old, double li_new)
// change in the receptor-ligand interaction energy for trial moves of membrane bending
// li_old and li_new, respectively, denote the local distance between the membranes before and after the trial move
{
double E_old = 0;
// receptor-ligand energy before the membrane bending trial move
double E_new = 0;
// receptor-ligand energy after the membrane bending trial move
if (li_new > l_minus and li_new < l_plus)
// specified distance range required for the receptor-ligand binding
{
E_new = -U_b;
}
if (li_old > l_minus and li_old < l_plus)
// specified distance range required for the receptor-ligand binding
{
E_old = -U_b;
}
return E_new - E_old;
// receptor-ligand interaction energy change
}
double energy_bending(int x, int y, int k, double l_old, double l_new)
// change in the membrane bending energy for membrane trial moves
// x and y are the coordinates of the selected site
// k is the membrane index: k=0 for the upper membrane, k=1 for the lower membrane
// l_old is local membrane position at the selected site before the trial move
// l_new is local membrane position at the selected site after the trial move
{
vector<double> l(13);
// local positions of the given site and of its 12 neighbors
int x_right, x_left, x_right_right, x_left_left, y_up, y_down, y_up_up, y_down_down;