/*******************************************************************************
* McStas, neutron ray-tracing package
*         Copyright 1997-2003, All rights reserved
*         Risoe National Laboratory, Roskilde, Denmark
*         Institut Laue Langevin, Grenoble, France
*
* Component: SANSLiposomes
*
* %I
* Written by: Martin Cramer Pedersen (mcpe@nbi.dk)
* Date: October 17, 2012
* Origin: KU-Science
*
* A sample of polydisperse liposomes in solution (water).
*
* %D
* A component simulating the scattering from a box-shaped, thin solution (water)
* of liposomes described by a pentuple-shell model.
*
* %P
* Radius: [AA]                       Average thickness of the liposomes.
* Thickness: [AA]                    Thickness of the bilayer.
* SigmaRadius: []		Relative Gaussian deviation of the radius in the distribution of liposomes.
* VolumeOfHeadgroup: [AA^3]          Volume of one lipid headgroup - default is POPC.
* VolumeOfCH2Tail: [AA^3]            Volume of the CH2-chains of one lipid - default is POPC.
* VolumeOfCH3Tail: [AA^3]            Volume of the CH3-tails of one lipid - default is POPC.
* ScatteringLengthOfHeadgroup: [cm]  Scattering length of one lipid headgroup - default is POPC in D2O.
* ScatteringLengthOfCH2Tail: [cm]    Scattering length of the CH2-chains of one lipid - default is POPC in D2O.
* ScatteringLengthOfCH3Tail: [cm]    Scattering length of the CH3-tails of one lipid - default is POPC in D2O.
* Concentration: [mM]                Concentration of sample.
* RhoSolvent: [cm/AA^3]              Scattering length density of solvent - default is D2O.
* AbsorptionCrosssection: [1/m]      Absorption cross section of the sample.
* xwidth: [m]                        Dimension of component in the x-direction.
* yheight: [m]                       Dimension of component in the y-direction.
* zdepth: [m]                        Dimension of component in the z-direction.
* SampleToDetectorDistance: [m]      Distance from sample to detector (for focusing the scattered neutrons).
* DetectorRadius: [m]                Radius of the detector (for focusing the scattered neutrons).
*
* %E
*******************************************************************************/

DEFINE COMPONENT SANSLiposomes

DEFINITION PARAMETERS ()

SETTING PARAMETERS (Radius = 800.0, Thickness = 38.89, SigmaRadius = 0.20,
  VolumeOfHeadgroup = 319.0, VolumeOfCH2Tail = 818.8, VolumeOfCH3Tail = 108.6,
  ScatteringLengthOfHeadgroup = 7.05E-12, ScatteringLengthOfCH2Tail = -1.76E-12, ScatteringLengthOfCH3Tail = -9.15E-13,
  Concentration = 0.01, RhoSolvent = 6.4e-14, AbsorptionCrosssection = 0.0,
  xwidth, yheight, zdepth, SampleToDetectorDistance, DetectorRadius)

OUTPUT PARAMETERS ()

SHARE
%{
  // Functions used for compution the intensity from a given liposome
#pragma acc routine
  double FormfactorSphere(double q, double R)
	{
		return 3 * (sin(q * R) - q * R * cos(q * R)) / pow(q * R, 3);
	}

#pragma acc routine
	double IntensityOfLiposome(double q, double R, double ThicknessHead, double ThicknessTail, double ThicknessCH3, double DeltaRhoHead, double DeltaRhoCH2, double DeltaRhoCH3)
	{
		const double RHeadOut = R + ThicknessHead + ThicknessTail + ThicknessCH3;
		const double RTailOut = R + ThicknessTail + ThicknessCH3;
		const double RCH3Out  = R + ThicknessCH3;
		const double RCH3In   = R - ThicknessCH3;
		const double RTailIn  = R - ThicknessTail - ThicknessCH3;
		const double RHeadIn  = R - ThicknessHead - ThicknessTail - ThicknessCH3;

		const double VolumeHeadOut = 4.0 / 3.0 * PI * pow(RHeadOut, 3) - 4.0 / 3.0 * PI * pow(RTailOut, 3);
		const double VolumeTailOut = 4.0 / 3.0 * PI * pow(RTailOut, 3) - 4.0 / 3.0 * PI * pow(RCH3Out, 3);
		const double VolumeCH3     = 4.0 / 3.0 * PI * pow(RCH3Out, 3)  - 4.0 / 3.0 * PI * pow(RCH3In, 3);
		const double VolumeTailIn  = 4.0 / 3.0 * PI * pow(RCH3In, 3)   - 4.0 / 3.0 * PI * pow(RTailIn, 3);
		const double VolumeHeadIn  = 4.0 / 3.0 * PI * pow(RTailIn, 3)  - 4.0 / 3.0 * PI * pow(RHeadIn, 3);

		const double AmplitudeHeadOut = DeltaRhoHead * VolumeHeadOut * (pow(RHeadOut, 3) * FormfactorSphere(q, RHeadOut) - pow(RTailOut, 3) * FormfactorSphere(q, RTailOut)) / (pow(RHeadOut, 3) - pow(RTailOut, 3));
		const double AmplitudeTailOut = DeltaRhoCH2  * VolumeTailOut * (pow(RTailOut, 3) * FormfactorSphere(q, RTailOut) - pow(RCH3Out, 3) * FormfactorSphere(q, RCH3Out)) / (pow(RTailOut, 3) - pow(RCH3Out, 3));
		const double AmplitudeCH3     = DeltaRhoCH3  * VolumeCH3 * (pow(RCH3Out, 3) * FormfactorSphere(q, RCH3Out) - pow(RCH3In, 3) * FormfactorSphere(q, RCH3In)) / (pow(RCH3Out, 3) - pow(RCH3In, 3));
		const double AmplitudeTailIn  = DeltaRhoCH2  * VolumeTailIn * (pow(RCH3In, 3) * FormfactorSphere(q, RCH3In) - pow(RTailIn, 3) * FormfactorSphere(q, RTailIn)) / (pow(RCH3In, 3) - pow(RTailIn, 3));
		const double AmplitudeHeadIn  = DeltaRhoHead * VolumeHeadIn * (pow(RTailIn, 3) * FormfactorSphere(q, RTailIn) - pow(RHeadIn, 3) * FormfactorSphere(q, RHeadIn)) / (pow(RTailIn, 3) - pow(RHeadIn, 3));
		const double Intensity = pow(AmplitudeHeadOut + AmplitudeTailOut + AmplitudeCH3 + AmplitudeTailIn + AmplitudeHeadIn, 2);
		return Intensity;
	}
%}

DECLARE
%{
	// Declarations
	double Absorption;
	double NumberDensity;

	int NumberOfStepsInR;
	double RMin;
	double RMax;
	double RStep;

	// Scattering lengths
	double DeltaRhoHead;
	double DeltaRhoCH2Tail;
	double DeltaRhoCH3Tail;

	// Thickness
	double ThicknessOfHead;
	double ThicknessOfCH2Tail;
	double ThicknessOfCH3Tail;
%}

INITIALIZE
%{
	// Rescale concentration into number of aggregates per m^3 times 10^-4
	NumberDensity = Concentration * 6.02214129e19;

	// Computations
	if (!xwidth || !yheight || !zdepth) {
		printf("%s: Sample has no volume, check parameters!\n", NAME_CURRENT_COMP);
	}

	Absorption = AbsorptionCrosssection;

	// Variables needed for integration over the polydispersity
	NumberOfStepsInR = 100;

	RMin = Radius - 3.0 * SigmaRadius * Radius;

	if (RMin < Thickness / 2.0) {
		RMin = Thickness / 2.0;
	}

	RMax = Radius + 3.0 * SigmaRadius * Radius;

	RStep = (RMax - RMin) / (1.0f * NumberOfStepsInR);

	// Molecular properties of liposomes
	const double RhoHead    = ScatteringLengthOfHeadgroup / VolumeOfHeadgroup;
	const double RhoCH2Tail = ScatteringLengthOfCH2Tail   / VolumeOfCH2Tail;
	const double RhoCH3Tail = ScatteringLengthOfCH3Tail   / VolumeOfCH3Tail;

	DeltaRhoHead    = RhoHead    - RhoSolvent;
	DeltaRhoCH2Tail = RhoCH2Tail - RhoSolvent;
	DeltaRhoCH3Tail = RhoCH3Tail - RhoSolvent;

	ThicknessOfHead    = Thickness * VolumeOfHeadgroup / (VolumeOfHeadgroup + VolumeOfCH2Tail + VolumeOfCH3Tail);
	ThicknessOfCH2Tail = Thickness * VolumeOfCH2Tail   / (VolumeOfHeadgroup + VolumeOfCH2Tail + VolumeOfCH3Tail);
	ThicknessOfCH3Tail = Thickness * VolumeOfCH3Tail   / (VolumeOfHeadgroup + VolumeOfCH2Tail + VolumeOfCH3Tail);
%}

TRACE
%{
	// Declarations
	double t0;
	double t1;
	double l_full;
	double l;
	double l1;
	double Intensity;
	double Weight1;
	double Weight2;
	double IntensityPart;
	double SolidAngle;
	double q;
	double qx;
	double qy;
	double qz;
	double v;
	double dt;
	double vx_i;
	double vy_i;
	double vz_i;
	char Intersect = 0;
	double R;
	int i;

	// Computation
	Intersect = box_intersect(&t0, &t1, x, y, z, vx, vy, vz, xwidth, yheight, zdepth);

	if (Intersect) {

		if (t0 < 0.0) {
			fprintf(stderr, "Neutron already inside sample %s - absorbing...\n", NAME_CURRENT_COMP);
			ABSORB;
    	}

		// Compute properties of neutron
		v = sqrt(pow(vx, 2) + pow(vy, 2) + pow(vz, 2));
		l_full = v * (t1 - t0);
		dt = rand01() * (t1 - t0) + t0;
		PROP_DT(dt);
		l = v * (dt - t0);

		// Store properties of incoming neutron
		vx_i = vx;
		vy_i = vy;
		vz_i = vz;

		// Generate new direction of neutron
		randvec_target_circle(&vx, &vy, &vz, &SolidAngle, 0, 0, SampleToDetectorDistance, DetectorRadius);

		NORM(vx, vy, vz);

		vx *= v;
		vy *= v;
		vz *= v;

		// Compute q
		qx = V2K * (vx_i - vx);
		qy = V2K * (vy_i - vy);
		qz = V2K * (vz_i - vz);

		q = sqrt(pow(qx, 2) + pow(qy, 2) + pow(qz, 2));

		// Compute scattering
		l1 = v * t1;

		Intensity = 0.0;

		Weight1 = 1.0 / (SigmaRadius * Radius * sqrt(2.0 * PI));

		for (i = 0; i < NumberOfStepsInR; ++i) {
			R = RMin + RStep * (i + 0.5);

			IntensityPart = IntensityOfLiposome(q, R, ThicknessOfHead, ThicknessOfCH2Tail, ThicknessOfCH3Tail, DeltaRhoHead, DeltaRhoCH2Tail, DeltaRhoCH3Tail);

			Weight2 = exp(- pow((R - Radius) / (sqrt(2.0) * SigmaRadius * Radius), 2));

			Intensity += Weight1 * Weight2 * IntensityPart * RStep;
		}

		p *= l_full * SolidAngle / (4.0 * PI) * NumberDensity * Intensity * exp(- Absorption * (l + l1) / v);

		SCATTER;
	}
%}

MCDISPLAY
%{	
	box(0, 0, 0, xwidth, yheight, zdepth);
%}

END
