#include <stdlib.h>
#include "general.h"
#include "vertex.h"
#include "cell.h"
#include <math.h>
char plugin_name[] = "Plane confimenent";
char plugin_description[]= "Confines vesicle between two planes d/2 above z=0 and d/2 below z=0. The plates squeeze vesicle with some predefined force.";
char plugin_author[] = "SAMO PENIC";

ts_plugin_details *init (){
	ts_plugin_details *details=(ts_plugin_details *)calloc(1,sizeof(ts_plugin_details));
	details->name = plugin_name;
//	details->data = (void *)calloc(1,sizeof(ts_double));
	return details;	
}

ts_bool vm_hard_constraint(ts_vesicle *vesicle, ts_vertex *vtx, ts_vertex *ovtx){

// plane confinement check whether the new position of vertex will be out of bounds
	if(vesicle->tape->plane_confinement_switch){
		if(vtx->z>vesicle->confinement_plane.z_max || vtx->z<vesicle->confinement_plane.z_min){
		return TS_FAIL;
		}
	}
	return TS_SUCCESS;
}


ts_double vm_energy_after_execute(ts_vesicle *vesicle, ts_vertex *vtx, ts_vertex *backupvtx){
// plane confinement energy due to compressing force
	ts_double delta_energy=0;
	if(vesicle->tape->plane_confinement_switch){
		if(vesicle->confinement_plane.force_switch){
			//substract old energy
			if(abs(vesicle->tape->plane_d/2.0-vesicle->confinement_plane.z_max)>1e-10) {
				delta_energy-=vesicle->tape->plane_F / pow(vesicle->confinement_plane.z_max-backupvtx[0].z,2);
				delta_energy+=vesicle->tape->plane_F / pow(vesicle->confinement_plane.z_max-vtx->z,2);
			}
			if(abs(-vesicle->tape->plane_d/2.0-vesicle->confinement_plane.z_min)>1e-10) {
				delta_energy-=vesicle->tape->plane_F / pow(vesicle->confinement_plane.z_min-backupvtx[0].z,2);
				delta_energy+=vesicle->tape->plane_F / pow(vesicle->confinement_plane.z_min-vtx->z,2);
			}
		}
	}
	return delta_energy;

}