/***************************************************************************
 *                           FixedStep.cpp                                 *
 *                           -------------------                           *
 * copyright            : (C) 2013 by Francisco Naveros                    *
 * email                : fnaveros@ugr.es                                  *
 ***************************************************************************/

/***************************************************************************
 *                                                                         *
 *   This program is free software; you can redistribute it and/or modify  *
 *   it under the terms of the GNU General Public License as published by  *
 *   the Free Software Foundation; either version 3 of the License, or     *
 *   (at your option) any later version.                                   *
 *                                                                         *
 ***************************************************************************/


#include "../../include/integration_method/BiFixedStep.h"
#include "../../include/neuron_model/TimeDrivenNeuronModel.h"


BiFixedStep::BiFixedStep(TimeDrivenNeuronModel * NewModel, string integrationMethodType, int N_neuronStateVariables, int N_differentialNeuronState, int N_timeDependentNeuronState, bool jacobian, bool inverse):IntegrationMethod(NewModel,integrationMethodType,N_neuronStateVariables, N_differentialNeuronState, N_timeDependentNeuronState, jacobian, inverse){

}

BiFixedStep::~BiFixedStep(){
	delete integrationMethodCounter;
	delete integrationMethodState;
}


void BiFixedStep::loadParameter(FILE *fh, long * Currentline) throw (EDLUTFileException){

	skip_comments(fh,*Currentline);
	if(fscanf(fh,"%f",&elapsedTimeInSeconds)==1){
		if(elapsedTimeInSeconds<=0.0){
////NEW CODE------------------------------------------------------------------------------
			throw EDLUTFileException(4,7,6,1,*Currentline);
////--------------------------------------------------------------------------------------
		}
	}else{
//NEW CODE------------------------------------------------------------------------------
		throw EDLUTFileException(4,7,6,1,*Currentline);
//--------------------------------------------------------------------------------------
	}

	//Calculate the elapsed time size in neuron model time scale.
	elapsedTimeInNeuronModelScale=elapsedTimeInSeconds*model->GetTimeScale();


	skip_comments(fh,*Currentline);
	if(fscanf(fh,"%d",&N_BiFixedSteps)==1){
		if(N_BiFixedSteps<=0){
////NEW CODE------------------------------------------------------------------------------
			throw EDLUTFileException(4,7,6,1,*Currentline);
////--------------------------------------------------------------------------------------
		}
	}else{
		N_BiFixedSteps=2;
	}


	bifixedElapsedTimeInNeuronModelScale=elapsedTimeInNeuronModelScale/N_BiFixedSteps;
	bifixedElapsedTimeInSeconds=elapsedTimeInSeconds/N_BiFixedSteps;

}


void BiFixedStep::SetBiFixedStepParameters(float startVoltageThreshold, float endVoltageThreshold, float timeAfterEndVoltageThreshold){
	this->startVoltageThreshold=startVoltageThreshold;
	this->endVoltageThreshold=endVoltageThreshold;
	this->N_postBiFixedSteps=ceil(timeAfterEndVoltageThreshold/bifixedElapsedTimeInNeuronModelScale)+1;
}