def calculate_external_CrystalCalculator(GEOMETRY_TYPE, CRYSTAL_NAME, THICKNESS, MILLER_H, MILLER_K, MILLER_L, ASYMMETRY_ANGLE, AZIMUTHAL_ANGLE, ENERGY_POINTS, ENERGY_MIN, ENERGY_MAX, ANGLE_DEVIATION_POINTS, ANGLE_DEVIATION_MIN, ANGLE_DEVIATION_MAX, STOKES_S0, STOKES_S1, STOKES_S2, STOKES_S3, INCLINATION_ANGLE, DUMP_TO_FILE, FILE_NAME="tmp.dat"): # Create a GeometryType object: # Bragg diffraction = 0 # Bragg transmission = 1 # Laue diffraction = 2 # Laue transmission = 3 if GEOMETRY_TYPE == 0: GEOMETRY_TYPE_OBJECT = BraggDiffraction() elif GEOMETRY_TYPE == 1: GEOMETRY_TYPE_OBJECT = BraggTransmission() elif GEOMETRY_TYPE == 2: GEOMETRY_TYPE_OBJECT = LaueDiffraction() elif GEOMETRY_TYPE == 3: GEOMETRY_TYPE_OBJECT = LaueTransmission() else: raise Exception( "CrystalCalculator: The geometry type could not be interpreted!" ) # Create a diffraction setup. # At this stage I translate angles in radians, energy in eV and all other values in SI units. print("CrystalCalculator: Creating a diffraction setup...\n") if ENERGY_POINTS == 1: if ENERGY_MIN != ENERGY_MAX: raise Exception( "CrystalCalculator: Finite energy range with only one sampled value!" ) diffraction_setup = DiffractionSetupSweeps( geometry_type=GEOMETRY_TYPE_OBJECT, # GeometryType object crystal_name=str(CRYSTAL_NAME), # string thickness=float(THICKNESS) * 1e-2, # meters miller_h=int(MILLER_H), # int miller_k=int(MILLER_K), # int miller_l=int(MILLER_L), # int asymmetry_angle=float(ASYMMETRY_ANGLE) / 180 * np.pi, # radians azimuthal_angle=float(AZIMUTHAL_ANGLE) / 180 * np.pi, # radians energy_min=float(ENERGY_MIN), # eV energy_max=float(ENERGY_MAX), # eV energy_points=int(ENERGY_POINTS), # int angle_deviation_min=float(ANGLE_DEVIATION_MIN) * 1e-6, # radians angle_deviation_max=float(ANGLE_DEVIATION_MAX) * 1e-6, # radians angle_deviation_points=int(ANGLE_DEVIATION_POINTS)) # int # Create a Diffraction object. diffraction = Diffraction() # Create a DiffractionResult object holding the results of the diffraction calculations. print("CrystalCalculator: Calculating the diffraction results...\n") diffraction_result = diffraction.calculateDiffraction( diffraction_setup) # Create a StokesVector object. incoming_stokes_vector = StokesVector( [STOKES_S0, STOKES_S1, STOKES_S2, STOKES_S3]) # Create a MuellerDiffraction object. mueller_diffraction = MuellerDiffraction( diffraction_result, incoming_stokes_vector, float(INCLINATION_ANGLE) * np.pi / 180) # Create a MullerResult object. print("CrystalCalculator: Calculating the Stokes vector...\n") mueller_result = mueller_diffraction.calculate_stokes() # Create the data to output. output_data = MailingBox(diffraction_result, mueller_result) # Dump data to file if requested. if DUMP_TO_FILE == 1: print("CrystalCalculator: Writing data in {file}...\n".format( file=FILE_NAME)) with open(FILE_NAME, "w") as file: try: file.write( "VALUES:\n\n" "geometry type: {geometry_type}\n" "crystal name: {crystal_name}\n" "thickness: {thickness}\n" "miller H: {miller_h}\n" "miller K: {miller_k}\n" "miller L: {miller_l}\n" "asymmetry angle: {asymmetry_angle}\n" "azimuthal angle: {azimuthal_angle}\n" "energy points: {energy_points}\n" "energy minimum: {energy_min}\n" "energy maximum: {energy_max}\n" "deviation angle points: {angle_deviation_points}\n" "deviation angle minimum: {angle_deviation_min}\n" "deviation angle maximum: {angle_deviation_max}\n" "inclination angle: {inclination_angle}\n" "incoming Stokes vector: {incoming_stokes_vector}\n\n" "RESULTS:\n\n" "S-Polarization:\n" "Intensity: {s_intensity}\n" "Phase: {s_phase}\n\n" "P-Polarization:\n" "Intensity: {p_intensity}\n" "Phase: {p_phase}\n\n" "SP-Difference:\n" "Intensity: {sp_intensity}\n" "Phase: {sp_phase}\n\n" "Stokes vector:\n" "S0: {s0}\n" "S1: {s1}\n" "S2: {s2}\n" "S3: {s3}\n\n" "Degree of circular polarization: {pol_degree}".format( geometry_type=GEOMETRY_TYPE_OBJECT.description(), crystal_name=CRYSTAL_NAME, thickness=THICKNESS, miller_h=MILLER_H, miller_k=MILLER_K, miller_l=MILLER_L, asymmetry_angle=ASYMMETRY_ANGLE, azimuthal_angle=AZIMUTHAL_ANGLE, energy_points=ENERGY_POINTS, energy_min=ENERGY_MIN, energy_max=ENERGY_MAX, angle_deviation_points=ANGLE_DEVIATION_POINTS, angle_deviation_min=ANGLE_DEVIATION_MIN, angle_deviation_max=ANGLE_DEVIATION_MAX, inclination_angle=INCLINATION_ANGLE, incoming_stokes_vector=incoming_stokes_vector. components(), s_intensity=diffraction_result.sIntensityByEnergy( ENERGY_MIN), s_phase=diffraction_result.sPhaseByEnergy( ENERGY_MIN, deg=True), p_intensity=diffraction_result.pIntensityByEnergy( ENERGY_MIN), p_phase=diffraction_result.pPhaseByEnergy( ENERGY_MIN, deg=True), sp_intensity=diffraction_result. differenceIntensityByEnergy(ENERGY_MIN), sp_phase=diffraction_result. differencePhaseByEnergy(ENERGY_MIN, deg=True), s0=mueller_result.s0_by_energy(ENERGY_MIN), s1=mueller_result.s1_by_energy(ENERGY_MIN), s2=mueller_result.s2_by_energy(ENERGY_MIN), s3=mueller_result.s3_by_energy(ENERGY_MIN), pol_degree=mueller_result. polarization_degree_by_energy(ENERGY_MIN))) file.close() print("File written to disk: %s" % FILE_NAME) except: raise Exception( "CrystalCalculator: The data could not be dumped onto the specified file!" ) return output_data
def testConstructor(self): bragg_diffraction = BraggDiffraction() self.assertEqual(bragg_diffraction.description(), "Bragg diffraction")