Advertisement
Digonto

percolation with error

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