"""
BornAgain functional test.
Test of rotation/position of core shell particle.
Reference CoreShell particle is compared against the test CoreShell,
which has different dimensions but rotated/translated to be like the reference one.
Particles are placed in the center of middle layer.
"""

import unittest
import PyFuTestInfrastructure as infrastruct
from bornagain import *

mAmbience = Vacuum()
mSubstrate_color = (0.55, 0.55, 0.55)
mSubstrate = RefractiveMaterial("Substrate", mSubstrate_color, 3e-6, 3e-8)
mMiddle_color = (0.70, 0.73, 0.78)
mMiddle = RefractiveMaterial("Teflon", mMiddle_color, 2.9e-6, 6e-9)
mCore_color = (0.86, 0.24, 0.18)
mCore = RefractiveMaterial("Ag", mCore_color, 1.2e-5, 5e-7)
mShell_color = (0.25, 0.65, 0.35)
mShell = RefractiveMaterial("AgO2", mShell_color, 8.6e-6, 3.4e-7)
mOuterShell_color = (0.25, 0.65, 0.35)
mOuterShell = RefractiveMaterial("SiO2", mOuterShell_color, 6.1e-6, 2.1e-7)

layer_thickness = 100.0


class TransformCoreShellBoxTest(unittest.TestCase):

    def get_sample(self, particle):
        vacuum_layer = Layer(mAmbience)
        middle_layer = Layer(mMiddle, layer_thickness)
        middle_layer.deposit2D(Dilute2D(0.001, particle))
        substrate = Layer(mSubstrate)

        sample = Sample()
        sample.addLayer(vacuum_layer)
        sample.addLayer(middle_layer)
        sample.addLayer(substrate)
        return sample

    def get_result(self, particle, use_avg_materials=False):
        sample = self.get_sample(particle)
        simulation = infrastruct.get_simulation_MiniGISAS(sample)
        simulation.options().setUseAvgMaterials(use_avg_materials)
        return simulation.simulate()

    def test_SameMaterialCoreShellBox(self):
        """
        Box particle placed in the center of the middle layer is compared against
        CoreShell particle, where core and shell are made of the same material.
        """

        shell_length = 50.0
        shell_width = 20.0
        shell_height = 10.0
        particle = Particle(mCore, Box(shell_length, shell_width,
                                       shell_height))
        particle.translate(
            R3(0, 0, -layer_thickness / 2.0 - shell_height / 2))

        reference = self.get_result(particle)

        core_length = shell_length / 2.0
        core_width = shell_width / 2.0
        core_height = shell_height / 2.0
        shell = Particle(mCore, Box(shell_length, shell_width, shell_height))
        core = Particle(mCore, Box(core_length, core_width, core_height))
        core.translate(R3(0, 0, (shell_height - core_height) / 2))
        coreshell = CoreAndShell(core, shell)
        coreshell.translate(
            R3(0, 0, -layer_thickness / 2.0 - shell_height / 2))

        data = self.get_result(coreshell)

        self.assertTrue(checkRelativeDifference(data, reference, 1e-10))

    def test_CoreShellBoxRotateZ(self):
        """
        Two CoreShell particles are compared against each other. Reference CoreShell
        particle has dimensions (20,50,10) and is placed in the center of middle layer.
        Second CoreShell particle has dimensions (50,20,10) with one rotationZ to be
        like the reference one.
        """

        # building reference CoreShell particle
        shell_length = 20.0
        shell_width = 50.0
        shell_height = 10.0
        core_length = shell_length / 2.0
        core_width = shell_width / 2.0
        core_height = shell_height / 2.0

        core_ref = Particle(mCore, Box(core_length, core_width, core_height))
        core_ref.translate(R3(0, 0, (shell_height - core_height) / 2))
        shell_ref = Particle(mShell,
                             Box(shell_length, shell_width, shell_height))
        coreshell_ref = CoreAndShell(core_ref, shell_ref)
        coreshell_ref.translate(
            R3(0, 0, -layer_thickness / 2.0 -
               shell_height / 2))  # center of coreshell in center of the layer

        reference = self.get_result(coreshell_ref)

        # building second CoreShell particle
        shell_length = 50.0
        shell_width = 20.0
        shell_height = 10.0
        core_length = shell_length / 2.0
        core_width = shell_width / 2.0
        core_height = shell_height / 2.0

        core = Particle(mCore, Box(core_length, core_width, core_height))
        core.translate(R3(0, 0, (shell_height - core_height) / 2))
        shell = Particle(mShell, Box(shell_length, shell_width, shell_height))
        coreshell = CoreAndShell(core, shell)
        coreshell.rotate(RotationZ(90 * deg))
        coreshell.translate(
            R3(0, 0, -layer_thickness / 2.0 - shell_height /
               2))  # center of coreshell  in center of the layer

        data = self.get_result(coreshell)

        self.assertTrue(checkRelativeDifference(data, reference, 1e-10))

    def test_CoreShellBoxRotateY(self):
        """
        Two CoreShell particles are compared against each other. Reference CoreShell
        particle has dimensions (10,20,50) and is placed in the center of middle layer.
        Second CoreShell particle has dimensions (50,20,10) with one rotationZ to be
        like the reference one.
        """
        # building reference CoreShell particle
        shell_length = 10.0
        shell_width = 20.0
        shell_height = 50.0
        core_length = shell_length / 2.0
        core_width = shell_width / 2.0
        core_height = shell_height / 2.0

        core_ref = Particle(mCore, Box(core_length, core_width, core_height))
        core_ref.translate(R3(0, 0, (shell_height - core_height) / 2))
        shell_ref = Particle(mShell,
                             Box(shell_length, shell_width, shell_height))
        coreshell_ref = CoreAndShell(core_ref, shell_ref)
        coreshell_ref.translate(
            R3(0, 0, -layer_thickness / 2.0 -
               shell_height / 2))  # center of coreshell in center of the layer

        reference = self.get_result(coreshell_ref)

        # building second CoreShell particle
        shell_length = 50.0
        shell_width = 20.0
        shell_height = 10.0
        core_length = shell_length / 2.0
        core_width = shell_width / 2.0
        core_height = shell_height / 2.0

        core = Particle(mCore, Box(core_length, core_width, core_height))
        core.translate(R3(0, 0, (shell_height - core_height) / 2))
        shell = Particle(mShell, Box(shell_length, shell_width, shell_height))
        coreshell = CoreAndShell(core, shell)
        coreshell.rotate(RotationY(90. * deg))
        coreshell.translate(
            R3(0, 0, -layer_thickness /
               2))  # center of coreshell  in center of the layer

        data = self.get_result(coreshell)

        self.assertTrue(checkRelativeDifference(data, reference, 1e-10))

    def test_CoreShellBoxRotateZandY(self):
        """
        Two CoreShell particles are compared against each other. Reference CoreShell
        particle has dimensions (10,50,20) and is placed in the center of middle layer.
        Second CoreShell particle has dimensions (50,20,10) with two rotations to be
        like the reference one.
        """

        # building reference CoreShell particle
        shell_length = 10.0
        shell_width = 50.0
        shell_height = 20.0
        core_length = shell_length / 2.0
        core_width = shell_width / 2.0
        core_height = shell_height / 2.0

        core_ref = Particle(mCore, Box(core_length, core_width, core_height))
        core_ref.translate(R3(0, 0, (shell_height - core_height) / 2))
        shell_ref = Particle(mShell,
                             Box(shell_length, shell_width, shell_height))
        coreshell_ref = CoreAndShell(core_ref, shell_ref)
        coreshell_ref.translate(
            R3(0, 0, -layer_thickness / 2.0 -
               shell_height / 2))  # center of coreshell in center of the layer

        reference = self.get_result(coreshell_ref)

        # building second CoreShell particle
        shell_length = 50.0
        shell_width = 20.0
        shell_height = 10.0
        core_length = shell_length / 2.0
        core_width = shell_width / 2.0
        core_height = shell_height / 2.0

        core = Particle(mCore, Box(core_length, core_width, core_height))
        core.translate(R3(0, 0, (shell_height - core_height) / 2))
        shell = Particle(mShell, Box(shell_length, shell_width, shell_height))
        coreshell = CoreAndShell(core, shell)
        coreshell.rotate(RotationZ(90 * deg))
        coreshell.rotate(RotationY(90 * deg))
        # rotation changes reference point, which now coincide with center of the volume
        coreshell.translate(
            R3(0, 0, -layer_thickness /
               2))  # center of coreshell  in center of the layer

        data = self.get_result(coreshell)

        self.assertTrue(checkRelativeDifference(data, reference, 1e-10))

    def test_SameMaterialOnionBox(self):
        """
        Nested onion particle with identical SLD in all layers must be equivalent
        to a single particle with the outermost geometry.
        """
        outer_length = 40.0
        outer_width = 16.0
        outer_height = 8.0
        inner_shell_length = 30.0
        inner_shell_width = 12.0
        inner_shell_height = 6.0
        inner_core_length = 20.0
        inner_core_width = 8.0
        inner_core_height = 4.0

        reference = Particle(mCore, Box(outer_length, outer_width, outer_height))
        reference.translate(R3(0, 0, -layer_thickness / 2.0 - outer_height / 2.0))

        inner_core = Particle(mCore, Box(inner_core_length, inner_core_width, inner_core_height))
        inner_core.translate(R3(0, 0, (inner_shell_height - inner_core_height) / 2.0))
        inner_shell = Particle(mCore, Box(inner_shell_length, inner_shell_width, inner_shell_height))
        inner_onion = CoreAndShell(inner_core, inner_shell)
        inner_onion.translate(R3(0, 0, (outer_height - inner_shell_height) / 2.0))
        outer_shell = Particle(mCore, Box(outer_length, outer_width, outer_height))
        onion = CoreAndShell(inner_onion, outer_shell)
        onion.translate(R3(0, 0, -layer_thickness / 2.0 - outer_height / 2.0))

        reference_no_avg = self.get_result(reference, False)
        onion_no_avg = self.get_result(onion, False)
        self.assertTrue(checkRelativeDifference(onion_no_avg, reference_no_avg, 1e-10))

        reference_avg = self.get_result(reference, True)
        onion_avg = self.get_result(onion, True)
        self.assertTrue(checkRelativeDifference(onion_avg, reference_avg, 1e-10))

    def test_SameShellMaterialOnionBox(self):
        """
        Onion with identical inner and outer shell SLD must be equivalent to a
        simple core-shell particle with the same core and the outermost shell.
        """
        outer_length = 40.0
        outer_width = 16.0
        outer_height = 8.0
        inner_shell_length = 30.0
        inner_shell_width = 12.0
        inner_shell_height = 6.0
        inner_core_length = 20.0
        inner_core_width = 8.0
        inner_core_height = 4.0

        reference_core = Particle(mCore, Box(inner_core_length, inner_core_width, inner_core_height))
        reference_core.translate(R3(0, 0, 2.0))
        reference_shell = Particle(mShell, Box(outer_length, outer_width, outer_height))
        reference = CoreAndShell(reference_core, reference_shell)
        reference.translate(R3(0, 0, -layer_thickness / 2.0 - outer_height / 2.0))

        inner_core = Particle(mCore, Box(inner_core_length, inner_core_width, inner_core_height))
        inner_core.translate(R3(0, 0, 1.0))
        inner_shell = Particle(mShell, Box(inner_shell_length, inner_shell_width, inner_shell_height))
        inner_onion = CoreAndShell(inner_core, inner_shell)
        inner_onion.translate(R3(0, 0, 1.0))
        outer_shell = Particle(mShell, Box(outer_length, outer_width, outer_height))
        onion = CoreAndShell(inner_onion, outer_shell)
        onion.translate(R3(0, 0, -layer_thickness / 2.0 - outer_height / 2.0))

        reference_data = self.get_result(reference)
        onion_data = self.get_result(onion)
        self.assertTrue(checkRelativeDifference(onion_data, reference_data, 1e-10))

    def test_SameCoreAndInnerShellMaterialOnionBoxRotateZ(self):
        """
        Onion with identical core and inner-shell SLD must be equivalent to a
        simple core-shell particle whose core has the inner-shell geometry.
        """
        outer_length = 40.0
        outer_width = 16.0
        outer_height = 8.0
        inner_shell_length = 30.0
        inner_shell_width = 12.0
        inner_shell_height = 6.0
        inner_core_length = 20.0
        inner_core_width = 8.0
        inner_core_height = 4.0

        reference_core = Particle(mCore, Box(inner_shell_length, inner_shell_width,
                                             inner_shell_height))
        reference_core.translate(R3(0, 0, 1.0))
        reference_shell = Particle(mOuterShell, Box(outer_length, outer_width, outer_height))
        reference = CoreAndShell(reference_core, reference_shell)
        reference.rotate(RotationZ(90 * deg))
        reference.translate(R3(0, 0, -layer_thickness / 2.0 - outer_height / 2.0))

        inner_core = Particle(mCore, Box(inner_core_length, inner_core_width, inner_core_height))
        inner_core.translate(R3(0, 0, 1.0))
        inner_shell = Particle(mCore, Box(inner_shell_length, inner_shell_width, inner_shell_height))
        inner_onion = CoreAndShell(inner_core, inner_shell)
        inner_onion.translate(R3(0, 0, 1.0))
        outer_shell = Particle(mOuterShell, Box(outer_length, outer_width, outer_height))
        onion = CoreAndShell(inner_onion, outer_shell)
        onion.rotate(RotationZ(90 * deg))
        onion.translate(R3(0, 0, -layer_thickness / 2.0 - outer_height / 2.0))

        reference_data = self.get_result(reference)
        onion_data = self.get_result(onion)
        self.assertTrue(checkRelativeDifference(onion_data, reference_data, 1e-10))


if __name__ == '__main__':
    unittest.main()
