<commit_before>/*
 * simulation.cpp
 *
 *  Created on: Nov 23, 2014
 *      Author: tony
 */

#include <iostream>

using std::cin;
using std::cout;
using std::endl;
using std::flush;
using std::string;

#include <sim/simulation.h>




simulation::simulation(const unsigned int type, const unsigned int n,
		const double* par, const string folder, const string fpre) :
								sim_type( type ),
								nCell( n ),
								folderName(folder){
	mycells = new cellmodel::Cells( nCell, this , par );
	if (nCell==1) {
		nCellOutputs = 1;
		nCellsToOutput = new unsigned int[nCellOutputs];
		nCellsToOutput[0] = 0;
		files   = new filesStruct(folder, fpre, 1 );
	} else {
		nCellOutputs = 5;
		nCellsToOutput = new unsigned int[nCellOutputs];
		nCellsToOutput[0] = 0;
		nCellsToOutput[1] = nCell/(nCellOutputs-1);
		for (unsigned int i=2; i<nCellOutputs-1; i++)
			nCellsToOutput[i] = nCellsToOutput[i-1] + nCellsToOutput[1];
		nCellsToOutput[nCellOutputs-1] = nCell-1;
		files   = new filesStruct(folder, fpre, nCellOutputs /* # of cell files */);
	}
	// The next line assumes all cells have the same number of states
	const unsigned int nstates = mycells->getNstates();
	neqns = nstates * nCell;
	Yold	= new double[neqns];
	Ynew	= new double[neqns];
}

simulation::~simulation(){
	// Deallocate work space
	delete[] Yold;
	delete[] Ynew;

	delete[] nCellsToOutput;
	delete files;
	delete mycells;
}

void simulation::SetInitialConditions(){
	// Load in initial conditions for each cell in the array
	mycells->SetInitialConditions();

	switch (sim_type){
	case 0:
		// Sim type 0 is for initial conditions set in code - no file read.
		break;
	case 1:
		// Read in file containing state of 1 cell
		// Sim type 1 typically uses the state of a single cell to set
		//  the states of all the cells in a cable
		// We need to set the initial conditions first - done above - and then
		//  reset the values using the data from the input file
		// This is because we use references in the Cell class/struct
		// Reading references from the input file does not work.
		readsinglecell( );
		break;
	case 2:
		// Sim type 2 starts up from  last cable state - read in the
		// states of all cells from a file
		readcells( );
		break;
	default:
		break;
	}

	// Get a complete copy of all the state variables of all the cells
	mycells->getStateFromCells( Yold );
}



void simulation::printcellAux(ofstream& of, const double t, Cell& c) {
	of << t << ", "
			<< c.getV() << ", " << c.getI_K1() << ", " << c.getI_Na() << ", "
			<< c.getI_axnm1() << ", " << c.getI_axnp1() << ", " << c.getI_axial()
			<< endl;
}

void simulation::printcellEAD(ofstream& of, Cell& c, Cell& c2, int r, int p, double S1) {
	double th_diff = 0;
	if (r == 0)
		th_diff = 1000 * 0.01 / (c.t_thr - c2.t_thr);
	else
		th_diff = 1000 * 0.01 / (c2.t_thr - c.t_thr); // special case for Cell[0]

	of << c.t_min << ", " << c.V_min << ", " << c.t_thr << ", " << c.V_thr
			<< ", " << c.t_max << ", " << c.V_max << ", " << c.t_90 << ", "
			<< c.V_90 << ", " << c.t_EAD << ", " << c.V_EAD << ", " << c.t_EAD2
			<< ", " << c.V_EAD2 << ", " << c.L_EAD << ", " << c.APD_90 << ", "
			<< c.DI << ", " << p << ", " << S1 << " , " << th_diff << ", "
			<< c.peak_slope << endl;
}

/*
 * savecells
 */
void simulation::savecells( ) {
	// if n == 1 --> save a single cell state
	//    n > 1  --> save multiple cells state
	const unsigned int ncells = mycells->getNcells();
	const char *f = (ncells==1)	? (folderName + "/" + cellstateFileName).c_str()
								: (folderName + "/" + stateFileName).c_str();
	cout << " Writing to: " << f << ". ";
	FILE *fp = fopen(f, "wb");
	int nw=0;
	for (unsigned int i = 0; i < ncells; i++) {
		Cell temp = mycells->getCell( i );
		nw += fwrite(temp.state, sizeof(Cell::state), 1, fp);
	}
	fclose(fp);
	std::cout << " Wrote state of " << nw << " cell(s)" << std::endl << flush;
}

void simulation::readcells( ) {
	const char *f = (folderName + "/" + stateFileName).c_str();
	const unsigned int ncells = mycells->getNcells();

	cout << " Reading data for " << ncells << " cells from " << f << ".";
	FILE *fp = fopen(f, "r");
	int nr = 0;
	unsigned int ntotal = 0;
	// References are messed up after reading structs from a file
	for (unsigned int i = 0; i < ncells; i++) {
		Cell temp;
		nr = fread(temp.state, sizeof(Cell::state), 1, fp);
		if (nr <= 0)
			break;
		mycells->copyCell( i, temp);
		ntotal++;
	}
	fclose(fp);
	if (ntotal < ncells)
		cout << " --> Read " << nr << " cells. ";
	cout << " Done reading." << endl << flush;
}

void simulation::readsinglecell( ) {
	const char *f = (folderName + "/" + cellstateFileName).c_str();
	const unsigned int ncells = mycells->getNcells();

	cout << " Reading data for " << ncells << " cells from " << f << ".";
	FILE *fp = fopen(f, "r");
	Cell temp;
	int nr = fread(temp.state, sizeof(Cell::state), 1, fp);
	switch (nr){
	case 0:
		cout << " --> Could not read cell state!!! ";
		break;
	case 1:<commit_msg>Fixed up simulation.cpp<commit_after>/*
 * simulation.cpp
 *
 *  Created on: Nov 23, 2014
 *      Author: tony
 */

#include <iostream>

using std::cin;
using std::cout;
using std::endl;
using std::flush;
using std::string;

#include <sim/simulation.h>




simulation::simulation(const unsigned int type, const unsigned int n,
		const double* par, const string folder, const string fpre) :
								sim_type( type ),
								nCell( n ),
								folderName(folder){
	mycells = new cellmodel::Cells( nCell, this , par );
	if (nCell==1) {
		nCellOutputs = 1;
		nCellsToOutput = new unsigned int[nCellOutputs];
		nCellsToOutput[0] = 0;
		files   = new filesStruct(folder, fpre, 1 );
	} else {
		nCellOutputs = 5;
		nCellsToOutput = new unsigned int[nCellOutputs];
		nCellsToOutput[0] = 0;
		nCellsToOutput[1] = nCell/(nCellOutputs-1);
		for (unsigned int i=2; i<nCellOutputs-1; i++)
			nCellsToOutput[i] = nCellsToOutput[i-1] + nCellsToOutput[1];
		nCellsToOutput[nCellOutputs-1] = nCell-1;
		files   = new filesStruct(folder, fpre, nCellOutputs /* # of cell files */);
	}
	// The next line assumes all cells have the same number of states
	const unsigned int nstates = mycells->getNstates();
	neqns = nstates * nCell;
	Yold	= new double[neqns];
	Ynew	= new double[neqns];
}

simulation::~simulation(){
	// Deallocate work space
	delete[] Yold;
	delete[] Ynew;

	delete[] nCellsToOutput;
	delete files;
	delete mycells;
}

void simulation::SetInitialConditions(){
	// Load in initial conditions for each cell in the array
	mycells->SetInitialConditions();

	switch (sim_type){
	case 0:
		// Sim type 0 is for initial conditions set in code - no file read.
		break;
	case 1:
		// Read in file containing state of 1 cell
		// Sim type 1 typically uses the state of a single cell to set
		//  the states of all the cells in a cable
		// We need to set the initial conditions first - done above - and then
		//  reset the values using the data from the input file
		// This is because we use references in the Cell class/struct
		// Reading references from the input file does not work.
		readsinglecell( );
		break;
	case 2:
		// Sim type 2 starts up from  last cable state - read in the
		// states of all cells from a file
		readcells( );
		break;
	default:
		break;
	}

	// Get a complete copy of all the state variables of all the cells
	mycells->getStateFromCells( Yold );
}



void simulation::printcellAux(ofstream& of, const double t, Cell& c) {
	of << t << ", "
			<< c.getV() << ", " << c.getI_K1() << ", " << c.getI_Na() << ", "
			<< c.getI_axnm1() << ", " << c.getI_axnp1() << ", " << c.getI_axial()
			<< endl;
}

void simulation::printcellEAD(ofstream& of, Cell& c, Cell& c2, int r, int p, double S1) {
	double th_diff = 0;
	if (r == 0)
		th_diff = 1000 * 0.01 / (c.t_thr - c2.t_thr);
	else
		th_diff = 1000 * 0.01 / (c2.t_thr - c.t_thr); // special case for Cell[0]

	of << c.t_min << ", " << c.V_min << ", " << c.t_thr << ", " << c.V_thr
			<< ", " << c.t_max << ", " << c.V_max << ", " << c.t_90 << ", "
			<< c.V_90 << ", " << c.t_EAD << ", " << c.V_EAD << ", " << c.t_EAD2
			<< ", " << c.V_EAD2 << ", " << c.L_EAD << ", " << c.APD_90 << ", "
			<< c.DI << ", " << p << ", " << S1 << " , " << th_diff << ", "
			<< c.peak_slope << endl;
}

/*
 * savecells
 */
void simulation::savecells( ) {
	// if n == 1 --> save a single cell state
	//    n > 1  --> save multiple cells state
	const unsigned int ncells = mycells->getNcells();
	// The following two lines resulted in f pointing to an empty string
	//const char *f = (ncells==1)	? (folderName + "/" + cellstateFileName).c_str()
	//							: (folderName + "/" + stateFileName).c_str();

	string f = (ncells==1)	? (folderName + "/" + cellstateFileName)
									: (folderName + "/" + stateFileName);

	cout << " Writing to: " << f << ". " << flush;
	FILE *fp = fopen(f.c_str(), "wb");
	int nw=0;
	for (unsigned int i = 0; i < ncells; i++) {
		Cell temp = mycells->getCell( i );
		nw += fwrite(temp.state, sizeof(Cell::state), 1, fp);
	}
	fclose(fp);
	std::cout << " Wrote state of " << nw << " cell(s)" << std::endl << flush;
}

void simulation::readcells( ) {
	// The next line resulted in f pointing to an empty string
	// const char *f = (folderName + "/" + cellstateFileName).c_str();
	string f = folderName + "/" + cellstateFileName;
	const unsigned int ncells = mycells->getNcells();

	cout << " Reading data for " << ncells << " cells from " << f << ".";
	FILE *fp = fopen(f.c_str(), "r");
	int nr = 0;
	unsigned int ntotal = 0;
	// References are messed up after reading structs from a file
	for (unsigned int i = 0; i < ncells; i++) {
		Cell temp;
		nr = fread(temp.state, sizeof(Cell::state), 1, fp);
		if (nr <= 0)
			break;
		mycells->copyCell( i, temp);
		ntotal++;
	}
	fclose(fp);
	if (ntotal < ncells)
		cout << " --> Read " << nr << " cells. ";
	cout << " Done reading." << endl << flush;
}

void simulation::readsinglecell( ) {
	string f = folderName + "/" + cellstateFileName;
	const unsigned int ncells = mycells->getNcells();

	cout << " Reading data for " << ncells << " cells from " << f << ".";
	FILE *fp = fopen(f.c_str(), "r");
	Cell temp;
	int nr = fread(temp.state, sizeof(Cell::state), 1, fp);
	switch (nr){
	case 0:
		cout << " --> Could not read cell state!!! ";
		break;
	case 1:
{'fraction_non_alphanumeric': 0.17171123518464101, 'fraction_numerical': 0.014986776373787834, 'mean_word_length': 3.111961337092227, 'pattern_counts': {'":': 0, '<': 327, '<?xml version=': 0, '>': 33, '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': '3270520', 'n_tokens_mistral': 4081, 'n_tokens_neox': 3689, 'n_words': 1119}