/*******************************************************************************
*         McStas instrument definition URL=http://www.mcstas.org
*
* Instrument: Union_NCrystal_example
*
* %Identification
* Written by: NCrystal developers, Mads Bertelsen
* Date: 2020
* Origin: ESS
* %INSTRUMENT_SITE: NCrystal
*
* Example instrument for use of NCrystal_sample in Union
*
* %Description
* This instrument examplifies how NCrystal may be used in Union constructions
*
*
*  This file is originally part of NCrystal (see https://mctools.github.io/ncrystal/)   
*                                                                            
*  Copyright 2015-2022 NCrystal developers                                   
*                                                                            
*  Licensed under the Apache License, Version 2.0 (the "License");           
*  you may not use this file except in compliance with the License.          
*  You may obtain a copy of the License at                                   
*                                                                            
*      http://www.apache.org/licenses/LICENSE-2.0                            
*                                                                            
*  Unless required by applicable law or agreed to in writing, software       
*  distributed under the License is distributed on an "AS IS" BASIS,         
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
*  See the License for the specific language governing permissions and       
*  limitations under the License.                                            
*                                                                            
*
* Example: sample_cfg="Y2O3_sg206_Yttrium_Oxide.ncmat;density=0.6x" Detector: powder_pattern_detc_I=2.21958e-10 
*
* %Parameters
* sample_cfg: [string] Material string for the 
*
* %Link
* A reference/HTML link for more information
*
* %End
*******************************************************************************/
DEFINE INSTRUMENT Union_NCrystal_example()

DECLARE
%{
%}

INITIALIZE
%{
%}

TRACE

/* Example instrument in which two NCrystal_sample component instances are used
 to model respectively a Ge-511 monochromator and secondly an Yttrium-Oxide
 powder sample. Source and detectors are deliberately kept very simple.

 The orientation of the monochromator and the value of the central wavelength,
 lambda0, is set up in order for the neutrons at lambda0 to experience a 90 degree
 Bragg reflection (thetaBragg=45 degree) on the Ge511 plane, directing the reflected
 neutrons towards the sample (the d-spacing of Ge511 is 1.08876Aa):

           lambda = sin(45degree)*2*1.08876Aa = 1.539739Aa

 Note that the d-spacing, 1.08876Aa, of Ge511 can be found by the command:

    ncrystal_inspectfile --dump  "Ge_sg227.ncmat"

 Alternatively, it could have been extracted programatically here in the instrument file with:

  #include "NCrystal/ncrystal.h"
  ncrystal_info_t monochromator_info = ncrystal_create_info("Ge_sg227.ncmat");
  double dspacing_Ge511 = ncrystal_info_dspacing_from_hkl( monochromator_info, 5,1,1 );
  ncrystal_unref(&monochromator_info);

*/

COMPONENT init = Union_init()
AT (0,0,0) ABSOLUTE

COMPONENT NCrystal_mono = NCrystal_process(cfg="Ge_sg227.ncmat;mos=0.3deg;bkgd=0"
                                          ";dir1=@crys_hkl:5,1,1@lab:0,0,1"
                                          ";dir2=@crys_hkl:0,1,-1@lab:0,1,0")
AT (0,0,0) ABSOLUTE

COMPONENT NCrystal_mono_material = Union_make_material(process_string="NCrystal_mono",
                                                       my_absorption=100*2.2/181.067)
AT (0,0,0) ABSOLUTE

COMPONENT NCrystal_sample = NCrystal_process(cfg="Y2O3_sg206_Yttrium_Oxide.ncmat;density=0.6x")
AT (0,0,0) ABSOLUTE

COMPONENT NCrystal_sample_material = Union_make_material(process_string="NCrystal_sample",
                                                         my_absorption=100*0.512114/1205.6)
AT (0,0,0) ABSOLUTE

COMPONENT origin = Progress_bar()
  AT (0, 0, 0) RELATIVE ABSOLUTE

COMPONENT source =   Source_div(lambda0=1.539739, dlambda=0.01, xwidth=0.001, yheight=0.001, focus_aw=1, focus_ah=1)
  AT (0, 0, 0.3) RELATIVE origin

COMPONENT mono_arm = Arm()
  AT (0, 0, 0.5) RELATIVE source ROTATED (0, 45 , 0) RELATIVE source

COMPONENT monochromator = Union_box(xwidth=0.05,yheight=0.05,zdepth=0.003, priority=1, material_string="NCrystal_mono_material", p_interact=0.2)
  AT (0, 0, 0) RELATIVE mono_arm

COMPONENT master1 = Union_master()
AT (0, 0, 0) RELATIVE mono_arm

COMPONENT mono_out = Arm()
  AT (0, 0, 0) RELATIVE mono_arm ROTATED (0, -90, 0) RELATIVE source

COMPONENT powder_sample = Union_cylinder(yheight=0.01, radius=0.01, priority=1, material_string="NCrystal_sample_material")
AT (0, 0, 0.4) RELATIVE mono_out

COMPONENT master2 = Union_master()
AT (0, 0, 0) RELATIVE powder_sample

COMPONENT powder_pattern_detc = Monitor_nD(
    options = "banana, angle limits=[10 170], bins=500",
    radius = 0.05, yheight = 0.1)
  AT (0, 0, 0) RELATIVE powder_sample

COMPONENT stop = Union_stop()
AT (0,0,0) ABSOLUTE

END
