/***************************************************************************
 *                           RK2.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/RK2.h"
#include "../../include/neuron_model/TimeDrivenNeuronModel.h"
#include "../../include/neuron_model/VectorNeuronState.h"

RK2::RK2(TimeDrivenNeuronModel * NewModel, int N_neuronStateVariables, int N_differentialNeuronState, int N_timeDependentNeuronState):FixedStep(NewModel,"RK2",N_neuronStateVariables, N_differentialNeuronState, N_timeDependentNeuronState, false, false){
}

RK2::~RK2(){
}
		
bool RK2::NextDifferentialEcuationValues(int index, float * NeuronState){

	float previous_V=NeuronState[0];

	float AuxNeuronState[MAX_VARIABLES];
	float AuxNeuronState1[MAX_VARIABLES];
	float AuxNeuronState2[MAX_VARIABLES];


	//1st term
	this->model->EvaluateDifferentialEcuation(NeuronState, AuxNeuronState1, index, elapsedTimeInNeuronModelScale);
	
	//2nd term
	memcpy(AuxNeuronState, NeuronState,sizeof(float)*N_NeuronStateVariables);
	for (int j=0; j<N_DifferentialNeuronState; j++){
		AuxNeuronState[j]= NeuronState[j] + AuxNeuronState1[j]*elapsedTimeInNeuronModelScale;
	}

	this->model->EvaluateTimeDependentEcuation(AuxNeuronState, elapsedTimeInNeuronModelScale, 0);
	this->model->EvaluateDifferentialEcuation(AuxNeuronState, AuxNeuronState2, index, elapsedTimeInNeuronModelScale);


	for (int j=0; j<N_DifferentialNeuronState; j++){
		NeuronState[j]+=(AuxNeuronState1[j]+AuxNeuronState2[j])*elapsedTimeInNeuronModelScale*0.5f;
	}

	//Update the last spike time.
	this->model->GetVectorNeuronState()->AddElapsedTime(index,elapsedTimeInSeconds);

	this->model->EvaluateTimeDependentEcuation(NeuronState, elapsedTimeInNeuronModelScale, 0);

	bool spike=this->model->EvaluateSpikeCondition(previous_V, NeuronState, index, elapsedTimeInNeuronModelScale);

	return spike;
}

ostream & RK2::PrintInfo(ostream & out){
	out << "Integration Method Type: " << this->GetType() << endl;

	return out;
}	


void RK2::Calculate_conductance_exp_values(){
	this->model->Initialize_conductance_exp_values(this->N_TimeDependentNeuronState,1);
	//index 0
	this->model->Calculate_conductance_exp_values(0, elapsedTimeInNeuronModelScale);
}