<commit_before>// 5/10/2016
//################################################
//protocol for a grid off cells
//class definitions
//################################################

#include "gridProtocol.h"
#include "gridCell.h"

gridProtocol::gridProtocol()  : CurrentClamp(){
    gridCell* temp = new gridCell();
    cell = temp;
    grid = temp->getGrid();
    baseCellMap = cellMap;
    baseCellMap["Inexcitable Cell"] = [] () {return new Cell;};
    cellMap.clear();
    cellMap["gridCell"] = [] () {return (Cell*) new gridCell;};
    GetSetRef toInsert;
    pars["gridFile"]= toInsert.Initialize("file", [temp] () {return temp->gridfile();}, [temp] (const string& value) {temp->setGridfile(value);});
    pars["measNodes"]= toInsert.Initialize("set", [this] () {return setToString(dataNodes);}, [this] (const string& value) {dataNodes = stringToSet(value);});
    pars["stimNodes"]= toInsert.Initialize("set", [this] () {return setToString(stimNodes);}, [this] (const string& value) {stimNodes = stringToSet(value);});
    pars.erase("numtrials");
    pars["paceflag"].set("true");
    pars.erase("paceflag");
}
//overriden deep copy funtion
gridProtocol* gridProtocol::clone(){
    return new gridProtocol(*this);
};
gridProtocol::gridProtocol(const gridProtocol& toCopy) : CurrentClamp(toCopy){
    this->CCcopy(toCopy);
}
void gridProtocol::CCcopy(const gridProtocol& toCopy) {
    this->dataNodes = toCopy.dataNodes;
    this->stimNodes = toCopy.stimNodes;
    this->grid = ((gridCell*)this->cell)->getGrid();
}
// External stimulus.
int gridProtocol::stim()
{
    for(auto it : stimNodes) {
        Cell* cell = grid->findNode(it)->cell;
        if(cell->t>=stimt&&cell->t<(stimt+stimdur)){
            if(stimflag==0){
                stimcounter++;
                stimflag=1;
                if(stimcounter>int(numstims)){
                    doneflag = 0;
                    return 0;
                }
            }
        cell->externalStim(stimval);
    }
        else if(stimflag==1){     //trailing edge of stimulus
            stimt=stimt+bcl;
            stimflag=0;
            cell->apTime = 0.0;
        }

        cell->apTime = cell->apTime+cell->dt;

        doneflag = 1;
    }
    return 1;
};

bool gridProtocol::runTrial() {
        char writefile[150];     // Buffer for storing filenames

((gridCell*)cell)->addBuffer();
//to be moved to a better location
set<string> temp;
temp.insert(pnames.begin(),pnames.end());
cell->setConstantSelection(temp);
temp.clear();
{ int i = 0;
for(auto& it : grid->fiber) {
    int j = 0;
    for(auto& iv : it.nodes) {
        pair<int,int> p(i,j);
        for(auto& meas : measures) {
        if(iv->cell->vars.find(meas.first) != cell->vars.end()) {
            realMeasures[p].insert(pair<string,Measure>(meas.first,meas.second)).second;
        }
        j++;
    }
    i++;
    }
}
}

//should not be here

        time = cell->t = 0.0;      // reset time
        doneflag=1;     // reset doneflag

        if(readCellState) {
            sprintf(writefile,(cellStateDir + "/" + cellStateFile).c_str(),trial);
            cell->readCellState(writefile);
        }
 
        //###############################################################
        // Every time step, currents, concentrations, and Vm are calculated.
        //###############################################################
        int pCount = 0;
        //open i/o streams
        for(auto& it : realMeasures) {
            for(auto& iv : it.second) {
                sprintf(writefile,propertyoutfile.c_str(),trial,iv.first.c_str());
                sprintf(writefile, (datadir + "/" + "cell_%i_%i_" + string(writefile)).c_str(),  it.first.second, it.first.first, iv.first.c_str());
                iv.second.setOutputfile(writefile);
            }
        }

        sprintf(writefile,dvarsoutfile.c_str(),trial);
        sprintf(writefile, (datadir + "/" + "cell_%%i_%%i_" + string(writefile)).c_str());
        cell->setOuputfileVariables(writefile);

        while(int(doneflag)&&(time<tMax)){
            
            time = cell->tstep(stimt);    // Update time
            cell->updateCurr();    // Update membrane currents
            if(int(paceflag)==1) {  // Apply stimulus
                stim();
            }
            cell->updateV();

            cell->updateConc();   // Update ion concentrations

            //##### Output select variables to file  ####################
            if(int(measflag)==1&&cell->t>meastime){
                for(auto& iv : dataNodes) {
                    for (auto& it : realMeasures[iv]) {
                        it.second.measure(grid->findNode(iv)->cell->t,*grid->findNode(iv)->cell->vars[it.second.varname]);
                        if(int(writeflag)==1) {
                            it.second.write(true, !(int(doneflag)&&(time<tMax)));
                        }
                    }
                }
            }
            if (int(writeflag)==1&&time>writetime&&pCount%int(writeint)==0) {
                cell->writeVariables();
            }
            pCount++;
        }
    
      // Output final (ss) property values for each trial
        for(auto& it : realMeasures) {
            for(auto& iv : it.second) {
                sprintf(writefile,finalpropertyoutfile.c_str(),trial,it->second.varname.c_str());
                sprintf(writefile, (datadir + "/" + "cell_%i_%i_" + string(writefile)).c_str(),it.first.second, it.first.first,);
                it->second.setOutputfile(writefile);
                it->second.write(false, true);
                it->second.reset();
            }
        }
      
      // Output parameter values for each trial
      sprintf(writefile,finaldvarsoutfile.c_str(),trial);
      sprintf(writefile, (datadir + "/" + "cell_%%i_%%i_" + string(writefile)).c_str());
      cell->setOutputfileConstants(writefile);
      cell->writeConstants();
      for(map<string,Measure>::iterator it = measures.begin(); it != measures.end(); it++) {
          it->second.closeFiles();
      }
      cell->closeFiles();
      if(writeCellState) {
          sprintf(writefile,(datadir + "/" + cellStateFile).c_str(),trial);
          cell->writeCellState(writefile);
      }


      return true; 
}

map<string, CellInitializer>& gridProtocol::getCellMap() {
    return baseCellMap;
}
set<pair<int,int>>& gridProtocol::getStimNodes() {<commit_msg>fixed compile bug in gridProtoco<commit_after>// 5/10/2016
//################################################
//protocol for a grid off cells
//class definitions
//################################################

#include "gridProtocol.h"
#include "gridCell.h"

gridProtocol::gridProtocol()  : CurrentClamp(){
    gridCell* temp = new gridCell();
    cell = temp;
    grid = temp->getGrid();
    baseCellMap = cellMap;
    baseCellMap["Inexcitable Cell"] = [] () {return new Cell;};
    cellMap.clear();
    cellMap["gridCell"] = [] () {return (Cell*) new gridCell;};
    GetSetRef toInsert;
    pars["gridFile"]= toInsert.Initialize("file", [temp] () {return temp->gridfile();}, [temp] (const string& value) {temp->setGridfile(value);});
    pars["measNodes"]= toInsert.Initialize("set", [this] () {return setToString(dataNodes);}, [this] (const string& value) {dataNodes = stringToSet(value);});
    pars["stimNodes"]= toInsert.Initialize("set", [this] () {return setToString(stimNodes);}, [this] (const string& value) {stimNodes = stringToSet(value);});
    pars.erase("numtrials");
    pars["paceflag"].set("true");
    pars.erase("paceflag");
}
//overriden deep copy funtion
gridProtocol* gridProtocol::clone(){
    return new gridProtocol(*this);
};
gridProtocol::gridProtocol(const gridProtocol& toCopy) : CurrentClamp(toCopy){
    this->CCcopy(toCopy);
}
void gridProtocol::CCcopy(const gridProtocol& toCopy) {
    this->dataNodes = toCopy.dataNodes;
    this->stimNodes = toCopy.stimNodes;
    this->grid = ((gridCell*)this->cell)->getGrid();
}
// External stimulus.
int gridProtocol::stim()
{
    for(auto it : stimNodes) {
        Cell* cell = grid->findNode(it)->cell;
        if(cell->t>=stimt&&cell->t<(stimt+stimdur)){
            if(stimflag==0){
                stimcounter++;
                stimflag=1;
                if(stimcounter>int(numstims)){
                    doneflag = 0;
                    return 0;
                }
            }
        cell->externalStim(stimval);
    }
        else if(stimflag==1){     //trailing edge of stimulus
            stimt=stimt+bcl;
            stimflag=0;
            cell->apTime = 0.0;
        }

        cell->apTime = cell->apTime+cell->dt;

        doneflag = 1;
    }
    return 1;
};

bool gridProtocol::runTrial() {
        char writefile[150];     // Buffer for storing filenames

((gridCell*)cell)->addBuffer();
//to be moved to a better location
set<string> temp;
temp.insert(pnames.begin(),pnames.end());
cell->setConstantSelection(temp);
temp.clear();
{ int i = 0;
for(auto& it : grid->fiber) {
    int j = 0;
    for(auto& iv : it.nodes) {
        pair<int,int> p(i,j);
        for(auto& meas : measures) {
        if(iv->cell->vars.find(meas.first) != cell->vars.end()) {
            realMeasures[p].insert(pair<string,Measure>(meas.first,meas.second)).second;
        }
        j++;
    }
    i++;
    }
}
}

//should not be here

        time = cell->t = 0.0;      // reset time
        doneflag=1;     // reset doneflag

        if(readCellState) {
            sprintf(writefile,(cellStateDir + "/" + cellStateFile).c_str(),trial);
            cell->readCellState(writefile);
        }
 
        //###############################################################
        // Every time step, currents, concentrations, and Vm are calculated.
        //###############################################################
        int pCount = 0;
        //open i/o streams
        for(auto& it : realMeasures) {
            for(auto& iv : it.second) {
                sprintf(writefile,propertyoutfile.c_str(),trial,iv.first.c_str());
                sprintf(writefile, (datadir + "/" + "cell_%i_%i_" + string(writefile)).c_str(),  it.first.second, it.first.first, iv.first.c_str());
                iv.second.setOutputfile(writefile);
            }
        }

        sprintf(writefile,dvarsoutfile.c_str(),trial);
        sprintf(writefile, (datadir + "/" + "cell_%%i_%%i_" + string(writefile)).c_str());
        cell->setOuputfileVariables(writefile);

        while(int(doneflag)&&(time<tMax)){
            
            time = cell->tstep(stimt);    // Update time
            cell->updateCurr();    // Update membrane currents
            if(int(paceflag)==1) {  // Apply stimulus
                stim();
            }
            cell->updateV();

            cell->updateConc();   // Update ion concentrations

            //##### Output select variables to file  ####################
            if(int(measflag)==1&&cell->t>meastime){
                for(auto& iv : dataNodes) {
                    for (auto& it : realMeasures[iv]) {
                        it.second.measure(grid->findNode(iv)->cell->t,*grid->findNode(iv)->cell->vars[it.second.varname]);
                        if(int(writeflag)==1) {
                            it.second.write(true, !(int(doneflag)&&(time<tMax)));
                        }
                    }
                }
            }
            if (int(writeflag)==1&&time>writetime&&pCount%int(writeint)==0) {
                cell->writeVariables();
            }
            pCount++;
        }
    
      // Output final (ss) property values for each trial
        for(auto& it : realMeasures) {
            for(auto& iv : it.second) {
                sprintf(writefile,finalpropertyoutfile.c_str(),trial,iv.second.varname.c_str());
                sprintf(writefile, (datadir + "/" + "cell_%i_%i_" + string(writefile)).c_str(),it.first.second, it.first.first);
                iv.second.setOutputfile(writefile);
                iv.second.write(false, true);
                iv.second.reset();
            }
        }
      
      // Output parameter values for each trial
      sprintf(writefile,finaldvarsoutfile.c_str(),trial);
      sprintf(writefile, (datadir + "/" + "cell_%%i_%%i_" + string(writefile)).c_str());
      cell->setOutputfileConstants(writefile);
      cell->writeConstants();
      for(map<string,Measure>::iterator it = measures.begin(); it != measures.end(); it++) {
          it->second.closeFiles();
      }
      cell->closeFiles();
      if(writeCellState) {
          sprintf(writefile,(datadir + "/" + cellStateFile).c_str(),trial);
          cell->writeCellState(writefile);
      }


      return true; 
}

map<string, CellInitializer>& gridProtocol::getCellMap() {
    return baseCellMap;
}
set<pair<int,int>>& gridProtocol::getStimNodes() {
{'fraction_non_alphanumeric': 0.193631669535284, 'fraction_numerical': 0.004850571115631357, 'mean_word_length': 2.3648328507501972, 'pattern_counts': {'":': 0, '<': 23, '<?xml version=': 0, '>': 117, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3291743', 'n_tokens_mistral': 3969, 'n_tokens_neox': 3789, 'n_words': 656}