void ConnectivityGlobalPlugin::update(CC3DXMLElement *_xmlData, bool _fullInitFlag) {

	if (potts->getDisplayUnitsFlag()) {
		Unit energyUnit = potts->getEnergyUnit();




		CC3DXMLElement * unitsElem = _xmlData->getFirstElement("Units");
		if (!unitsElem) { //add Units element
			unitsElem = _xmlData->attachElement("Units");
		}

		if (unitsElem->getFirstElement("PenaltyUnit")) {
			unitsElem->getFirstElement("PenaltyUnit")->updateElementValue(energyUnit.toString());
		}
		else {
			CC3DXMLElement * energyElem = unitsElem->attachElement("PenaltyUnit", energyUnit.toString());
		}
	}

	penaltyVec.clear();

	Automaton *automaton = potts->getAutomaton();
	ASSERT_OR_THROW("CELL TYPE PLUGIN WAS NOT PROPERLY INITIALIZED YET. MAKE SURE THIS IS THE FIRST PLUGIN THAT YOU SET", automaton)
		set<unsigned char> cellTypesSet;


	map<unsigned char, double> typeIdConnectivityPenaltyMap;

	if (_xmlData->getFirstElement("DoNotPrecheckConnectivity")) {
		doNotPrecheckConnectivity = true;
	}

	if (_xmlData->getFirstElement("FastAlgorithm")) {
		fast_algorithm = true;
		changeEnergyFcnPtr = &ConnectivityGlobalPlugin::changeEnergyFast;
	}


	CC3DXMLElementList penaltyVecXML = _xmlData->getElements("Penalty");

	CC3DXMLElementList connectivityOnVecXML = _xmlData->getElements("ConnectivityOn");

	ASSERT_OR_THROW("You cannot use Penalty and ConnectivityOn tags together. Stick to one convention", !(connectivityOnVecXML.size() && penaltyVecXML.size()));

	// previous ASSERT_OR_THROW will encure that only one of the subsequent for loops be executed 

	for (int i = 0; i < penaltyVecXML.size(); ++i) {
		typeIdConnectivityPenaltyMap.insert(make_pair(automaton->getTypeId(penaltyVecXML[i]->getAttribute("Type")), penaltyVecXML[i]->getDouble()));


		//inserting all the types to the set (duplicate are automatically eleminated) to figure out max value of type Id
		cellTypesSet.insert(automaton->getTypeId(penaltyVecXML[i]->getAttribute("Type")));

	}



	for (int i = 0; i < connectivityOnVecXML.size(); ++i) {
		typeIdConnectivityPenaltyMap.insert(make_pair(automaton->getTypeId(connectivityOnVecXML[i]->getAttribute("Type")), 1.0));
		//inserting all the types to the set (duplicate are automatically eleminated) to figure out max value of type Id
		cellTypesSet.insert(automaton->getTypeId(connectivityOnVecXML[i]->getAttribute("Type")));

	}


	//Now that we know all the types used in the simulation we will find size of the penaltyVec
	vector<unsigned char> cellTypesVector(cellTypesSet.begin(), cellTypesSet.end());//coping set to the vector

	int size = 0;
	if (cellTypesVector.size()) {
		size = *max_element(cellTypesVector.begin(), cellTypesVector.end());
	}

	maxTypeId = size;

	size += 1;//if max element is e.g. 5 then size has to be 6 for an array to be properly allocated



	int index;
	penaltyVec.assign(size, 0.0);
	//inserting connectivity penalty values to penaltyVec;
	for (map<unsigned char, double>::iterator mitr = typeIdConnectivityPenaltyMap.begin(); mitr != typeIdConnectivityPenaltyMap.end(); ++mitr) {
		penaltyVec[mitr->first] = fabs(mitr->second);
	}

	cerr << "size=" << size << endl;
	for (int i = 0; i < size; ++i) {
		cerr << "penaltyVec[" << i << "]=" << penaltyVec[i] << endl;
	}

	//Here I initialize max neighbor index for direct acces to the list of neighbors 
	boundaryStrategy = BoundaryStrategy::getInstance();
	maxNeighborIndex = 0;


	maxNeighborIndex = boundaryStrategy->getMaxNeighborIndexFromNeighborOrder(1);


	cerr << "ConnectivityGlobal maxNeighborIndex=" << maxNeighborIndex << endl;

	
	WatchableField3D<CellG *> *cellFieldG = (WatchableField3D<CellG *> *)potts->getCellFieldG();
	Dim3D fieldDim = cellFieldG->getDim();

	// max_neighbor_index_local_search is different depending whether we are on hex or cartesian lattice and if this is 2D or 3D simulation
	if (boundaryStrategy->getLatticeType() == HEXAGONAL_LATTICE) { // on hex lattice in 2D and 3D nearest neighbors "completely cover" a given pixel 
		max_neighbor_index_local_search = boundaryStrategy->getMaxNeighborIndexFromNeighborOrder(1);
	}
	else {
		if (fieldDim.x == 1 || fieldDim.y == 1 || fieldDim.z == 1) { //2D simulation 
			max_neighbor_index_local_search = boundaryStrategy->getMaxNeighborIndexFromNeighborOrder(2);
		}
		else { //3D
			max_neighbor_index_local_search = boundaryStrategy->getMaxNeighborIndexFromNeighborOrder(3);
		}
	}

}
void ConnectivityGlobalPlugin::update(CC3DXMLElement *_xmlData, bool _fullInitFlag){

	if(potts->getDisplayUnitsFlag()){
		Unit energyUnit=potts->getEnergyUnit();




		CC3DXMLElement * unitsElem=_xmlData->getFirstElement("Units"); 
		if (!unitsElem){ //add Units element
			unitsElem=_xmlData->attachElement("Units");
		}

		if(unitsElem->getFirstElement("PenaltyUnit")){
			unitsElem->getFirstElement("PenaltyUnit")->updateElementValue(energyUnit.toString());
		}else{
			CC3DXMLElement * energyElem = unitsElem->attachElement("PenaltyUnit",energyUnit.toString());
		}
	}

	penaltyVec.clear();

	Automaton *automaton = potts->getAutomaton();
	ASSERT_OR_THROW("CELL TYPE PLUGIN WAS NOT PROPERLY INITIALIZED YET. MAKE SURE THIS IS THE FIRST PLUGIN THAT YOU SET", automaton)
		set<unsigned char> cellTypesSet;

	
	map<unsigned char,double> typeIdConnectivityPenaltyMap;

	if(_xmlData->getFirstElement("DoNotPrecheckConnectivity")){
		doNotPrecheckConnectivity=true;
	}

	CC3DXMLElementList penaltyVecXML=_xmlData->getElements("Penalty");

	

	for (int i = 0 ; i<penaltyVecXML.size(); ++i){
		typeIdConnectivityPenaltyMap.insert(make_pair(automaton->getTypeId(penaltyVecXML[i]->getAttribute("Type")),penaltyVecXML[i]->getDouble()));


		//inserting all the types to the set (duplicate are automatically eleminated) to figure out max value of type Id
		cellTypesSet.insert(automaton->getTypeId(penaltyVecXML[i]->getAttribute("Type")));


	}

	
	//Now that we know all the types used in the simulation we will find size of the penaltyVec
	vector<unsigned char> cellTypesVector(cellTypesSet.begin(),cellTypesSet.end());//coping set to the vector

	int size=0;
	if (cellTypesVector.size()){
		size= * max_element(cellTypesVector.begin(),cellTypesVector.end());
	}

	maxTypeId=size;

	size+=1;//if max element is e.g. 5 then size has to be 6 for an array to be properly allocated

	

	int index ;
	penaltyVec.assign(size,0.0);
	//inserting connectivity penalty values to penaltyVec;
	for(map<unsigned char , double>::iterator mitr=typeIdConnectivityPenaltyMap.begin() ; mitr!=typeIdConnectivityPenaltyMap.end(); ++mitr){
		penaltyVec[mitr->first]=fabs(mitr->second);
	}

	cerr<<"size="<<size<<endl;
	for(int i = 0 ; i < size ; ++i){
		cerr<<"penaltyVec["<<i<<"]="<<penaltyVec[i]<<endl;
	}

	//Here I initialize max neighbor index for direct acces to the list of neighbors 
	boundaryStrategy=BoundaryStrategy::getInstance();
	maxNeighborIndex=0;


	maxNeighborIndex=boundaryStrategy->getMaxNeighborIndexFromNeighborOrder(1);


	cerr<<"ConnectivityGlobal maxNeighborIndex="<<maxNeighborIndex<<endl;   

}