Advertisement
Digonto

percolation without error

Nov 2nd, 2017
162
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 11.30 KB | None | 0 0
  1. //code which creates network for 25 times and does percolation over it 1000times and saves the file
  2.  
  3. #include<iostream>
  4. #include<vector>
  5. #include<string>
  6. #include<cstdlib>
  7. #include<iomanip>
  8. #include<ctime>
  9. #include<cmath>
  10. #include<random>
  11. #include<fstream>
  12. #include<algorithm>
  13. #include<omp.h>
  14.  
  15. //#include "openacc_curand.h"
  16. using namespace std;
  17.  
  18. int EMPTY = 0;
  19. int connectionNumber = 0; // it indexes the connection between different nodes of the network
  20.  
  21. // this function does the union find work for the percolation
  22.  
  23. int findroot(int ptr[],int j)
  24. {
  25.     if(ptr[j]<0) return j;
  26.    
  27.     return ptr[j]=findroot(ptr,ptr[j]);
  28. }  
  29.  
  30. //this function does the same thing but without recursion
  31. /*
  32. int findroot(int ptr[],int i)
  33. {
  34.     //cao = 1;
  35.     int r,s;
  36.     r = s = i;
  37.     while (ptr[r]>=0)
  38.     {
  39.         ptr[s] = ptr[r];
  40.         s = r;
  41.         r = ptr[r];
  42.     }
  43.     return r;
  44. }
  45. */
  46. int main()
  47. {
  48.     int seed,vertices,m,run,filenum;
  49.    
  50.     /*
  51.    
  52.     I am just going to set the initial value for your need
  53.    
  54.     cout<<"enter seed size: ";
  55.     cin>>seed;
  56.     cout<<endl;
  57.     cout<<"enter vertice number: ";
  58.     cin>>vertices;
  59.     cout<<endl;
  60.     cout<<"order number: ";
  61.     cin>>m;
  62.     cout<<endl;
  63.     cout<<"enter ensemble number: ";
  64.     cin>>run;
  65.     cout<<endl;
  66.     cout<<"enter filenumber: ";
  67.     cin>>filenum;
  68.     cout<<endl;
  69. */
  70.     seed = 6;
  71.     vertices = 4000;
  72.     m = 5;
  73.     run = 25000;
  74.     filenum = 6;
  75.  
  76.     //this sets up the connection and initializes the array;
  77.     int con = 0;
  78.  
  79.     for(int i=1;i<seed;i++)
  80.     {
  81.         con = con + i;
  82.     }
  83.    
  84.     con = con + (vertices-seed)*m;
  85.  
  86.     int* netmap1 = new int[con+1]; //node 1 that is connected to a certain connectionNumber
  87.     int* netmap2 = new int[con+1]; //node 2 that is connected to a certain connectionNumber
  88.    
  89.     for(int i=1;i<=con;i++)
  90.     {
  91.         netmap1[i] = 0;
  92.         netmap2[i] = 0;
  93.     }
  94.  
  95.     connectionNumber = con;
  96.  
  97.     srand(time(NULL));
  98.     int A,B,C;
  99.     A = vertices;
  100.     B = run;
  101.     C = filenum;
  102.  
  103.     //saved filename
  104.  
  105.     string filename1;
  106.     filename1 = "maxclus_";
  107.     string filename2;
  108.     filename2 = "delmx_";
  109.     string filename3;
  110.     filename3 = "entropy_";
  111.  
  112.     filename1 = filename1+to_string(A)+"node_"+to_string(m)+"M_"+to_string(B)+"ens"+to_string(C)+".dat";
  113.     filename2 = filename2+to_string(vertices)+"node_"+to_string(m)+"M_"+to_string(run)+"ens"+to_string(filenum)+".dat";
  114.     filename3 = filename3+to_string(vertices)+"node_"+to_string(m)+"M_"+to_string(run)+"ens"+to_string(filenum)+".dat";
  115.  
  116.     ofstream fout1,fout2,fout3;
  117.    
  118.    
  119.    
  120.     double* maxclus = NULL;
  121.     maxclus = new double[connectionNumber+1];
  122.    
  123.     double* delmx = NULL;
  124.     delmx = new double[connectionNumber+1];
  125.    
  126.     double* entropycalc = NULL;
  127.     entropycalc = new double[connectionNumber+1];
  128.  
  129.     for(int i=0;i<=connectionNumber;++i)
  130.     {
  131.         maxclus[i]=0;
  132.         delmx[i]=0;
  133.         entropycalc[i]=0;
  134.     }
  135.  
  136.     int* random = new int[connectionNumber+1];
  137.            
  138.             for(int i=0;i<=connectionNumber;i++)
  139.             {
  140.                 random[i] = i;
  141.             }
  142.            
  143.             random_shuffle(&random[1],&random[connectionNumber]);
  144.             //this is the pointer that needs to be made private for all the parallel loops
  145.             int* ptr = new int[vertices+1];
  146.     for(int i=0;i<vertices+1;i++) ptr[i]=0;
  147.  
  148.     //the main program starts here
  149.  
  150.    
  151.    
  152.     for(int i=1;i<=25;i++)
  153.     {
  154.         cout<<"run : "<<i<<endl;
  155.         vector<int> network;   
  156.         connectionNumber = 0;  
  157.        
  158.         //seeds are connected to the network   
  159.         for(int i=1;i<=seed;i++)
  160.         {
  161.             for(int j=1;j<=seed;j++)
  162.             {
  163.                 if(j>i)
  164.                 {
  165.                
  166.                     connectionNumber=connectionNumber + 1;
  167.                     netmap1[connectionNumber]=i; //connections are addressed
  168.                     netmap2[connectionNumber]=j;
  169.                     network.push_back(i); // the vector is updated for making connection
  170.                     network.push_back(j);
  171.                 }
  172.             }
  173.         }  
  174.        
  175.         int concheck = 0;
  176.         int ab[m]; //this array checks if a node is chosen twice
  177.         int countm = 0;
  178.  
  179.         for(int i = seed+1;i<=vertices; ++i)
  180.         {
  181.             countm = 0;
  182.             for(int k=0;k<m;k++) ab[k] = 0;
  183.             //if(i%50000==0)cout<<"connection given to : "<<i<<endl;
  184.             for(int j = 1; ;j++)
  185.             {
  186.                 concheck = 0;
  187.                 int N1=network.size() ;
  188.                 int M1=0;                  
  189.                 int u = M1 + rand()/(RAND_MAX/(N1-M1+1) + 1);
  190.                
  191.                 for(int n=0;n<m;n++)
  192.                 {
  193.                     if(ab[n] == network[u]) concheck = 1;
  194.                 }
  195.                
  196.                 //if condition is fulfilled the connection are given to the nodes
  197.                 //the data is saved in the arrays of the connection
  198.                 if(concheck == 0 && network[u]!=i)  
  199.                 {
  200.                     ab[countm] = network[u];
  201.                     countm=countm+1;
  202.    
  203.                    
  204.                
  205.                     connectionNumber=connectionNumber+1;
  206.                     netmap1[connectionNumber] = i;
  207.                     netmap2[connectionNumber] = network[u];
  208.  
  209.                     network.push_back(i);
  210.                     network.push_back(network[u]);
  211.                 }
  212.  
  213.                 if(countm==m) break;
  214.  
  215.             }
  216.         }
  217.        
  218.        
  219.         double rand_seed = time(NULL);
  220.        
  221.        
  222.        
  223.         connectionNumber = con;
  224.         //check data
  225.         for(int i=0;i<=connectionNumber;i++)
  226.             {
  227.                
  228.                 cout<<"maxclus: "<<maxclus[i]<<'\t'<<"delmx: "<<delmx[i]<<'\t'<<"entropy: "<<entropycalc[i]<<'\t'<<"net1:"<<netmap1[i]<<'\t'<<"net2:"<<netmap2[i]<<endl;
  229.                 cout<<"con: "<<connectionNumber<<'\t'<<"vertices:"<<vertices<<endl;
  230.             }
  231.            
  232.            
  233.            
  234.             //this is where the problem lies
  235.         //basically i want to make all the rx loops parallel in such a way that every parallel loop will have their own copy of ptr[ ] and random[ ] which they can modify themselves
  236.         // this whole part does the 'explosive percolation' and saves the data in maxclus, delmx, entropycalc array of different runs
  237.            
  238.         //#pragma omp parallel for shared(maxclus,delmx,entropycalc,netmap1,netmap2) schedule(guided)          
  239.         for(int rx=1;rx<=10;++rx)
  240.         {  
  241.             //EMPTY = 0;
  242.             //connectionNumber = con;
  243.             //int* random = new int[connectionNumber+1];
  244.             //int* ptr = new int[vertices+1];
  245.             //for(int i=0;i<=connectionNumber;++i)
  246.             //{
  247.             //  random[i] = i;
  248.             //}
  249.            
  250.             random_shuffle(&random[1],&random[connectionNumber]);
  251.            
  252.            
  253.             //this is the pointer that needs to be made private for all the parallel loops
  254.            
  255.             for(int i=0;i<=vertices;++i) ptr[i] = 0;
  256.            
  257.             //cout<<"run commencing : "<<rx<<endl;
  258.  
  259.            
  260.             int index=0,big=0,bigtemp=0,jump=0,en1=0,en2=0;
  261.             int node1=0,node2=0,node3=0,node4=0,nodeA=0,nodeB=0;
  262.             int clus1=0,clus2=0,clus3=0,clus4=0;
  263.             double entropy = log(vertices);
  264.            
  265.                            
  266.             for(int i=1;i<=connectionNumber;++i)
  267.             {
  268.                
  269.                 if(i!=connectionNumber)
  270.                 {
  271.                
  272.                     node1 = netmap1[random[i]];
  273.                     node2 = netmap2[random[i]];
  274.                     node3 = netmap1[random[i+1]];
  275.                     node4 = netmap2[random[i+1]];
  276.                
  277.                     if(ptr[node1]==EMPTY) clus1 = 1;
  278.                     else
  279.                    
  280.                     {
  281.                         int x = findroot(ptr,node1);
  282.                         clus1 = -ptr[x];
  283.                     }
  284.                    
  285.                     if(ptr[node2]==EMPTY) clus2 = 1;
  286.                     else
  287.                    
  288.                     {
  289.                         int b = findroot(ptr,node2);
  290.                         clus2 = -ptr[b];
  291.                     }
  292.                
  293.                     if(ptr[node3]==EMPTY) clus3 = 1;
  294.                     else
  295.                    
  296.                     {
  297.                         int c = findroot(ptr,node3);
  298.                         clus3 = -ptr[c];
  299.                     }
  300.  
  301.                     if(ptr[node4]==EMPTY) clus4 = 1;
  302.                     else
  303.                    
  304.                     {
  305.                         int d = findroot(ptr,node4);
  306.                         clus4 = -ptr[d];
  307.                     }
  308.                
  309.                     if(clus1*clus2<clus3*clus4)
  310.                     {
  311.                         nodeA = node1;
  312.                         nodeB = node2;
  313.                
  314.                
  315.                         int N1=connectionNumber;
  316.                         int M1=i+1;
  317.                                
  318.                         int u = M1 + (rand()/RAND_MAX)*(N1-M1);        
  319.                         //int u = M1 + rand()/(RAND_MAX/(N1-M1+1) + 1);
  320.                
  321.                         int temp = random[u];
  322.                         random[u] = random[i+1];
  323.                         random[i+1] = temp;
  324.                     }
  325.                     else
  326.                     {
  327.                         nodeA = node3;
  328.                         nodeB = node4;
  329.                    
  330.                         int temp = random[i+1];
  331.                         random[i+1] = random[i];
  332.                         random[i] = temp;
  333.                         int N1=connectionNumber;
  334.                         int M1=i+1;    
  335.                         int u = M1 + (rand()/RAND_MAX)*(N1-M1);        
  336.                         //int u = M1 + rand()/(RAND_MAX/(N1-M1+1) + 1);
  337.                        
  338.                
  339.                         temp = random[u];
  340.                         random[u] = random[i+1];
  341.                         random[i+1] = temp;
  342.                     }
  343.                 }
  344.                 else
  345.                 {
  346.                     nodeA = netmap1[random[i]];
  347.                     nodeB = netmap2[random[i]];
  348.                 }
  349.    
  350.                
  351.                
  352.  
  353.                 if(ptr[nodeA]==EMPTY && ptr[nodeB]==EMPTY)
  354.                 {
  355.                
  356.                 en1=1;
  357.                 en2=1;
  358.                 ptr[nodeA] = -2;
  359.                 ptr[nodeB] = nodeA;
  360.                 index = nodeA;
  361.                 entropy = (double)(entropy-(-2.0/vertices*log(1.0/vertices))+(-2.0/vertices*log(2.0/vertices)));
  362.                 if(entropy<0) entropy = 0;
  363.                 }
  364.                 else if(ptr[nodeA]==EMPTY && ptr[nodeB]!=EMPTY)
  365.                 {
  366.                
  367.                 en1=1;
  368.                 int e;
  369.                
  370.                e = findroot(ptr,nodeB);
  371.                 en2 = -(ptr[e]);
  372.                 ptr[nodeA] = e;
  373.                 ptr[e] += -1;
  374.                 index = e;
  375.                 entropy = entropy-(-(double)1.0/vertices*log(1.0/(double)vertices))-(-(double)en2/vertices*log((double)en2/vertices))+(-( double)(-ptr[index])/vertices*log((-ptr[index])/(double)vertices));
  376.                 if(entropy<0) entropy = 0;
  377.                
  378.                
  379.                 }
  380.                 else if(ptr[nodeA]!=EMPTY && ptr[nodeB]==EMPTY)
  381.                 {
  382.                
  383.                 en2 = 1;
  384.                 int f;
  385.                 f = findroot(ptr,nodeA);
  386.                 en1 = -(ptr[f]);
  387.                 ptr[nodeB] = f;
  388.                 ptr[f] += -1;
  389.                 index = f;
  390.                 entropy = entropy-(-(double)1.0/(double)vertices*log(1.0/(double)vertices))-(-(double)en1/(double)vertices*log((double)en1/vertices))+(-(double)(-ptr[index])/vertices*log((-ptr[index])/(double)vertices));
  391.                 if(entropy<0) entropy = 0;
  392.                 }
  393.                 else if(ptr[nodeA]!=EMPTY && ptr[nodeB]!=EMPTY)
  394.                 {
  395.                
  396.                     int g,h;
  397.                    
  398.                     g = findroot(ptr,nodeA);
  399.                     en1 = -(ptr[g]);
  400.                    
  401.                     h = findroot(ptr,nodeB);
  402.                     en2 = -(ptr[h]);
  403.                     if(g!=h)
  404.                     {
  405.                         if(ptr[g]<ptr[h])
  406.                         {
  407.                
  408.                             ptr[g] += ptr[h];
  409.                             ptr[h] = g;
  410.                             index = g;
  411.                         }
  412.                         else
  413.                         {
  414.                
  415.                             ptr[h] += ptr[g];
  416.                             ptr[g] = h;
  417.                             index = h;
  418.                         }
  419.                         entropy = entropy-(-(double)en1/(double)vertices*log((double)en1/(double)vertices))-(-(double)en2/vertices*log((double)en2/(double)vertices))+(-(double)(-ptr[index])/vertices*log((double)(-ptr[index])/(double)vertices));
  420.                     if(entropy<0) entropy = 0;
  421.                     }
  422.                     else
  423.                     {
  424.                
  425.                         jump=big-bigtemp;
  426.                         //#pragma omp atomic update
  427.                         maxclus[i] += big;
  428.                         //#pragma omp atomic update
  429.                         delmx[i] += jump;
  430.                         if(entropy<0) entropy = 0;
  431.                         //#pragma omp atomic update
  432.                         entropycalc[i] += entropy;
  433.                         bigtemp = big;
  434.                
  435.                         continue;
  436.                     }
  437.                 }  
  438.        
  439.                 if(-ptr[index]>big) big = -ptr[index];
  440.            
  441.                 jump = big - bigtemp;
  442.                 //#pragma omp atomic update
  443.                 maxclus[i] += big;
  444.                 //#pragma omp atomic update
  445.                 delmx[i] += jump;
  446.                 //#pragma omp atomic update
  447.                 entropycalc[i] += entropy;
  448.                 bigtemp = big;
  449.        
  450.                
  451.             }
  452.             //cout<<"a"<<endl;
  453.             //delete [] ptr;
  454.             //ptr = NULL;
  455.             //delete[] random;
  456.             //random = NULL;
  457.             //#pragma omp barrier
  458.         }
  459.    
  460.         //delete [] ptr;
  461.             //ptr = NULL;
  462.         //delete[] random;
  463.             //for(int i=0;i<connectionNumber;i++)
  464.             //{
  465.                
  466.             //  cout<<"maxclus: "<<maxclus[i]<<'\t'<<"delmx: "<<delmx[i]<<'\t'<<"entropy: "<<entropycalc[i]<<'\t'<<endl;
  467.                
  468.             //}
  469.     }
  470.  
  471.     fout1.open(filename1.c_str());
  472.     fout2.open(filename2.c_str());
  473.     fout3.open(filename3.c_str());
  474.    
  475.     connectionNumber = con;
  476.    
  477.     for(int i=1;i<=connectionNumber;i++)
  478.     {
  479.         fout1<<(double)i/vertices<<'\t'<<(double)maxclus[i]/vertices/run<<endl;
  480.         fout2<<(double)i/vertices<<'\t'<<(double)delmx[i]/run<<endl;
  481.         fout3<<(double)i/vertices<<'\t'<<(double)entropycalc[i]/run<<endl;
  482.     }
  483.  
  484.     fout1.close();
  485.     fout2.close();
  486.     fout3.close();
  487.        
  488.  
  489.    
  490.     delete[] maxclus;
  491.     maxclus = NULL;
  492.     delete[] delmx;
  493.     delmx = NULL;
  494.     delete[] entropycalc;
  495.     entropycalc = NULL;
  496.    
  497.     delete [] netmap1;
  498.     netmap1 = NULL;
  499.     delete [] netmap2;
  500.     netmap2 = NULL;
  501.     delete [] ptr;
  502.     ptr = NULL;
  503.     delete[] random;
  504.     random = NULL;
  505.    
  506.    
  507.     return 0;
  508. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement