Commit 7a95b2d7 authored by Ali Safari's avatar Ali Safari
Browse files

Initial commit

parents
Loading
Loading
Loading
Loading

.gitignore

0 → 100644
+1 −0
Original line number Diff line number Diff line
percolation

FileReader.cpp

0 → 100644
+113 −0
Original line number Diff line number Diff line

#include "FileReader.h"
#include <map>
#include<fstream>
#include<iostream>
#include<sstream>
#include<algorithm>
using namespace std;


void FileReader::registerIntParameter(const std::string &key, int init)
{

     intmap.insert ( std::pair<const std::string,int>(key,init) );

}

void FileReader::registerRealParameter(const std::string &key, real init)
{

     realmap.insert ( std::pair<const std::string,real>(key,init) );

}

void FileReader::registerStringParameter(const std::string &key, const std::string &init)
{

     stringmap.insert ( std::pair<const std::string,const std::string>(key,init) );

}

void FileReader::setParameter(const std::string &key, const std::string &in)
{
    stringmap[key]=in;

}

void FileReader::setParameter(const std::string &key, real in)
{
    realmap[key]=in;
}

void FileReader::setParameter(const std::string &key, int in)
{
    intmap[key]=in;
}


bool FileReader::readFile(const std::string &name)
{
   ifstream infile(name.c_str());
   if (!infile) {
       std::cout<<"opening file failed"<<endl;
       exit(0);
       }
       string line;
       string key;
       int intvalue;
       real realvalue;
       string stringvalue;

       while(getline(infile,line)){
		// finding # and deleting strings after that
		char symbol('#');
		size_t found=line.find(symbol);
		if (found!=std::string::npos)
		line.erase(line.begin() + found,line.end());


	     // skipping whitespace
	        line.erase(line.begin(), find_if(line.begin(), line.end(), not1(ptr_fun<int, int>(isspace))));


		stringstream	ss(line);
		ss>>key;

	       	for(sii it=intmap.begin(); it!=intmap.end(); ++it)
			 if (key.compare((*it).first)==0){
				                 	  ss>>intvalue;
						 	  intmap[key]=intvalue;
							 }
		for(sri it=realmap.begin(); it!=realmap.end(); ++it)
               		 if (key.compare((*it).first)==0){
		  				           ss>>realvalue;
					                   realmap[key]=realvalue;
						  	  }
		for(ssi it=stringmap.begin(); it!=stringmap.end(); ++it)
                	 if (key.compare((*it).first)==0){
		   					     ss>>stringvalue;
							     stringmap[key]=stringvalue;
							     }


                                   }

		   return true;

}



void FileReader::printParameters() const
{
 for(sii it=intmap.begin(); it!=intmap.end(); ++it)
 cout<<(*it).first<<" "<<(*it).second<<endl;

 for(sri it=realmap.begin(); it!=realmap.end(); ++it)
 cout<<(*it).first<<" "<<(*it).second<<endl;

 for(ssi it=stringmap.begin(); it!=stringmap.end(); ++it)
 cout<<(*it).first<<" "<<(*it).second<<endl;

}

FileReader.h

0 → 100644
+110 −0
Original line number Diff line number Diff line
#ifndef FILEREADER_HPP
#define FILEREADER_HPP

#include "Types.hpp"
#include <string>




//*******************************************************************************************************************
/*! Class for reading configuration from a file
*
* Configuration File Syntax:
*   - everything between a '#' character and the beginning of the next line is ignored
*   - lines can be empty
*   - line contain parameter key followed by white-spaces followed by their value
*
*  All possible keys (and the datatype of the value) have to be registered first:
*   - for example usage have a look at the FileReaderTest
*
*
*
*  This Skeleton is just a suggestion. If you are familiar with template programming
*  a possible alternative would be a version that does not need registering of all keys
*  and has a getter function like the following:
*      template<typename ExpectedType>
*      ExpectedType getValue( const std::string & key );
*/
//*******************************************************************************************************************
class FileReader
{
public:

	//register a new parameter with name key and initial int value
	void registerIntParameter( const std::string & key, int init = 0 );

	//register a new parameter with name key and initial double value
	void registerRealParameter( const std::string & key, real init = 0 );

	//register a new parameter with name key and initial string value
	void registerStringParameter( const std::string & key, const std::string & init = "" );

	//set a value for the key string with value in
	void setParameter( const std::string & key, const std::string & in );

	//set a value for the key string with value in
	void setParameter( const std::string & key, real in );

	//set a value for the key string with value in
	void setParameter( const std::string & key, int in );

	// get the int value of key 
	inline int getIntParameter( const std::string & key ) const;

	// get the double value of key 
	inline real getRealParameter( const std::string & key ) const;

	// get the string value of key 
	inline std::string getStringParameter( const std::string & key ) const;

	//try to read all registered parameters from file name
	bool readFile( const std::string & name );

	//print out all parameters to std:out
       	void printParameters() const;
	
	//checking wether the value is found
	//bool checkStrings(const std::string & key);
	//bool checkInt(const std::string & key);
	//bool checkReal(const std::string & key);
	
	
private:
	std::map<std::string,int>intmap;
	std::map<std::string,real>realmap;
	std::map<std::string,std::string> stringmap;

};




inline int FileReader::getIntParameter(const std::string &key) const
{
 //if( intmap.find(key)== intmap.end() )
   //   return "noKey" ; 
    //else
      return intmap.find(key)->second;
}

inline real FileReader::getRealParameter(const std::string &key) const
{
   //if( realmap.find(key)== realmap.end() )
     // return "noKey" ; 
    //else
      return (realmap.find(key))->second;
}

inline std::string FileReader::getStringParameter(const std::string &key) const
{
    if( stringmap.find(key)== stringmap.end() ) 
      return "noKey" ; 
    else
      return (stringmap.find(key))->second;
}



#endif //FILEREADER_HPP
+13 −0
Original line number Diff line number Diff line
seed  1                #seed for random number generator
N  1048576                #n of nodes
steps  18               #n of hierarchical levels/steps
fixed_links  10          #fixed number of links between block pairs
realizations  1
prob  0.30
Emin          3.0         #Lower  bound of search interval
Emax          3.2         #upper bound of search interval
M0            250         #Initial guess for subspace dimension to be used
M             250          #Total number of eigenvalues found in the interval
loop           0         #Number of refinement loop
info           0         #Errors
epsout         0.0         #Relative error on the trace

NetworkCSR.h

0 → 100644
+68 −0
Original line number Diff line number Diff line
#ifndef __NetworkCSR_H__
#define __NetworkCSR_H__
#include <iostream>
#include <vector>
using namespace std;

template<typename NodeInd, typename NodeVal>
class NetworkCSR {
    NodeInd n_of_nodes;
    NodeInd n_of_links;

public:
    NetworkCSR( vector<Node<NodeInd> > & n,
               NodeVal default_val) {
        n_of_nodes = n.size();
        n_of_links = 0;
        for(auto& a : n) {
            n_of_links += a.neighbors.size();
        }
        //std::cout<<n_of_nodes<<"------------"<<n_of_links<<std::endl;
      }

  void fill_csr3_matrix(NodeInd* ia, NodeInd* ja, NodeVal * a, NodeInd nnz,
  					   vector<Node<NodeInd> >& nodelist) {
     	//IndexT is the type use for indices (integer)
     	//ValueT for values (floating point)
      NodeInd N = nodelist.size();
      //std::cout<<"---------------------"<<N<<std::endl;
      NodeInd rowpt = 0;
      NodeInd valpt = 0;
      ia[rowpt] = 1;
      for(NodeInd row_id=0; row_id<N; ++row_id) {
          vector<int> &neighbors = nodelist[row_id].neighbors;
          int degree = neighbors.size();
          vector<NodeInd> els;
          //always store diagonal elements, even if =0, MKL likes it this way
          els.push_back(row_id);
          for(auto& nn: neighbors) {
              if(nn>row_id) els.push_back(nn);
          }
          //sort column indices is ascending order, MKL likes it this way
          sort(els.begin(),els.end());
          for(auto e: els) {
              ja[valpt] = e+1;//changed
              //cout<<row_id<<" ______"<<e<<endl;
              //for Laplacian matrix:
              //a[valpt] = (e!=row_id)? -1.0 : static_cast<NodeVal>(degree);
              //for adjacency matrix:
              a[valpt] = (e!=row_id)? 1.0 : 0.0;
              ++valpt;
              //cout<<valpt<<endl;
          }
          ia[++rowpt] = valpt+1;//changed
      }

  }


    NodeInd const & get_n_of_links(){
      return n_of_links;
    }

    NodeInd const & get_n_of_nodes(){
      return n_of_nodes;
    }

};
#endif