From 569c628a7da7523dd3c42b9ab37b301e00a963d4 Mon Sep 17 00:00:00 2001
From: pospelov <pospelov@fz-juelich.de>
Date: Mon, 8 Jul 2013 15:16:12 +0200
Subject: [PATCH] isgisaxs1 and isgisaxs4 examples

---
 Core/Algorithms/inc/Simulation.h              |   3 -
 Core/Algorithms/src/Simulation.cpp            |  11 --
 Examples/README                               |   7 --
 Examples/python/README                        |  10 ++
 .../CylindersAndPrisms.py                     |  57 +++++++++
 .../CylindersParaCrystal_1DDL.py              |  55 ++++++++
 .../CylindersParaCrystal_2DDL.py              |  59 +++++++++
 Examples/python/utils/__init__.py             |   1 +
 Examples/python/utils/show2d.py               | 119 +++++++++---------
 Examples/python/utils/show2d_root.py          |  75 +++++++++++
 10 files changed, 320 insertions(+), 77 deletions(-)
 delete mode 100644 Examples/README
 create mode 100644 Examples/python/README
 create mode 100644 Examples/python/ex001_CylindersAndPrisms/CylindersAndPrisms.py
 create mode 100644 Examples/python/ex004_CylindersParaCrystal/CylindersParaCrystal_1DDL.py
 create mode 100644 Examples/python/ex004_CylindersParaCrystal/CylindersParaCrystal_2DDL.py
 create mode 100644 Examples/python/utils/__init__.py
 create mode 100755 Examples/python/utils/show2d_root.py

diff --git a/Core/Algorithms/inc/Simulation.h b/Core/Algorithms/inc/Simulation.h
index 4e88964ea89..c933e0b9bbd 100644
--- a/Core/Algorithms/inc/Simulation.h
+++ b/Core/Algorithms/inc/Simulation.h
@@ -114,9 +114,6 @@ class BA_CORE_API_ Simulation : public IParameterized, public ICloneable
     //! Apply smearing of intensity due to tilting of z-axis (DEPRECATED)
     void smearIntensityFromZAxisTilting();
 
-    //! creates default IsGISAXS simulation which is used for comparison
-    static Simulation *createDefaultIsGISAXS();
-
  protected:
     Simulation(const Simulation& other);
 
diff --git a/Core/Algorithms/src/Simulation.cpp b/Core/Algorithms/src/Simulation.cpp
index 5113bcfbeb2..404a6c1a0be 100644
--- a/Core/Algorithms/src/Simulation.cpp
+++ b/Core/Algorithms/src/Simulation.cpp
@@ -409,15 +409,4 @@ void Simulation::addToIntensityMap(double alpha, double phi, double value)
 }
 
 
-Simulation *Simulation::createDefaultIsGISAXS()
-{
-    // Build simulation
-    Simulation *result = new Simulation();
-    result->setDetectorParameters(
-        100,-1.0*Units::degree, 1.0*Units::degree, 100,
-        0.0*Units::degree, 2.0*Units::degree, true);
-    result->setBeamParameters(
-        1.0*Units::angstrom, -0.2*Units::degree, 0.0*Units::degree);
-    return result;
-}
 
diff --git a/Examples/README b/Examples/README
deleted file mode 100644
index 46df19ff930..00000000000
--- a/Examples/README
+++ /dev/null
@@ -1,7 +0,0 @@
-BornAgain usage examples.
-
-Content:
-
-
-
-
diff --git a/Examples/python/README b/Examples/python/README
new file mode 100644
index 00000000000..fffd712f200
--- /dev/null
+++ b/Examples/python/README
@@ -0,0 +1,10 @@
+BornAgain usage examples.
+
+Content:
+
+ex001_CylindersAndPrisms     - Mixture of cylinders and prisms without interference (IsGISAXS example #1)
+ex004_CylindersParaCrystal   - 1D and 2D paracrystal (IsGISAXS example #4)
+
+
+
+
diff --git a/Examples/python/ex001_CylindersAndPrisms/CylindersAndPrisms.py b/Examples/python/ex001_CylindersAndPrisms/CylindersAndPrisms.py
new file mode 100644
index 00000000000..c084fad503c
--- /dev/null
+++ b/Examples/python/ex001_CylindersAndPrisms/CylindersAndPrisms.py
@@ -0,0 +1,57 @@
+# IsGISAXS01 example: Mixture of cylinders and prisms without interference
+import sys, os, numpy
+
+sys.path.append(os.path.abspath(
+                os.path.join(os.path.split(__file__)[0], '..', '..', '..', 'lib')
+                ))
+
+sys.path.append(os.path.abspath(os.path.join(os.path.split(__file__)[0],'..')))
+
+from libBornAgainCore import *
+from utils.show2d import PlotNumpyArray
+
+
+# ----------------------------------
+# describe sample and run simulation
+# ----------------------------------
+def RunSimulation():
+    # defining materials
+    mAmbience = MaterialManager.getHomogeneousMaterial("Air", 1.0, 0.0 )
+    mSubstrate = MaterialManager.getHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 )
+    # collection of particles
+    n_particle = complex(1.0-6e-4, 2e-8)
+    cylinder_ff = FormFactorCylinder(5*nanometer, 5*nanometer)
+    cylinder = Particle(n_particle, cylinder_ff)
+    prism_ff = FormFactorPrism3(5*nanometer, 5*nanometer)
+    prism = Particle(n_particle, prism_ff)
+    particle_decoration = ParticleDecoration()
+    particle_decoration.addParticle(cylinder, 0.0, 0.5)
+    particle_decoration.addParticle(prism, 0.0, 0.5)
+    interference = InterferenceFunctionNone()
+    particle_decoration.addInterferenceFunction(interference)
+    # air layer with particles and substrate form multi layer
+    air_layer = Layer(mAmbience)
+    air_layer_decorator = LayerDecorator(air_layer, particle_decoration)
+    substrate_layer = Layer(mSubstrate, 0)
+    multi_layer = MultiLayer()
+    multi_layer.addLayer(air_layer_decorator)
+    multi_layer.addLayer(substrate_layer)
+
+    # build and run experiment
+    simulation = Simulation()
+    simulation.setDetectorParameters(100,-1.0*degree, 1.0*degree, 100, 0.0*degree, 2.0*degree, True)
+    simulation.setBeamParameters(1.0*angstrom, -0.2*degree, 0.0*degree)
+    simulation.setSample(multi_layer)
+    simulation.runSimulation()
+    ## intensity data
+    return GetOutputData(simulation)
+
+
+#-------------------------------------------------------------
+# main()
+#-------------------------------------------------------------
+if __name__ == '__main__':
+    result = RunSimulation()
+    PlotNumpyArray(result)
+
+
diff --git a/Examples/python/ex004_CylindersParaCrystal/CylindersParaCrystal_1DDL.py b/Examples/python/ex004_CylindersParaCrystal/CylindersParaCrystal_1DDL.py
new file mode 100644
index 00000000000..7f45de5f82d
--- /dev/null
+++ b/Examples/python/ex004_CylindersParaCrystal/CylindersParaCrystal_1DDL.py
@@ -0,0 +1,55 @@
+# IsGISAXS04 example: 1D paracrystal
+import sys, os, numpy
+
+sys.path.append(os.path.abspath(
+                os.path.join(os.path.split(__file__)[0], '..', '..', '..', 'lib')
+                ))
+
+sys.path.append(os.path.abspath(os.path.join(os.path.split(__file__)[0],'..')))
+
+from libBornAgainCore import *
+from utils.show2d import PlotNumpyArray
+
+
+# ----------------------------------
+# describe sample and run simulation
+# ----------------------------------
+def RunSimulation():
+    # defining materials
+    mAmbience = MaterialManager.getHomogeneousMaterial("Air", 1.0, 0.0 )
+    mSubstrate = MaterialManager.getHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 )
+    # collection of particles
+    n_particle = complex(1.0-6e-4, 2e-8)   
+    cylinder_ff = FormFactorCylinder(5*nanometer, 5*nanometer)
+    cylinder = Particle(n_particle, cylinder_ff)
+    interference = InterferenceFunction1DParaCrystal(20.0*nanometer,7*nanometer, 1e3*nanometer)
+    particle_decoration = ParticleDecoration()
+    particle_decoration.addParticle(cylinder, 0.0, 1.0)
+    particle_decoration.addInterferenceFunction(interference)
+
+    air_layer = Layer(mAmbience)
+    air_layer_decorator = LayerDecorator(air_layer, particle_decoration)
+    substrate_layer = Layer(mSubstrate, 0)
+
+    multi_layer = MultiLayer()
+    multi_layer.addLayer(air_layer_decorator)
+    multi_layer.addLayer(substrate_layer)
+    # build and run experiment
+    simulation = Simulation()
+    simulation.setDetectorParameters(100,0.0*degree, 2.0*degree, 100, 0.0*degree, 2.0*degree, True)
+    simulation.setBeamParameters(1.0*angstrom, -0.2*degree, 0.0*degree)
+    simulation.setSample(multi_layer)
+    simulation.runSimulation()
+    ## intensity data
+    return GetOutputData(simulation)
+
+
+#-------------------------------------------------------------
+# main()
+#-------------------------------------------------------------
+if __name__ == '__main__':
+    result = RunSimulation()
+    PlotNumpyArray(result)
+
+
+
diff --git a/Examples/python/ex004_CylindersParaCrystal/CylindersParaCrystal_2DDL.py b/Examples/python/ex004_CylindersParaCrystal/CylindersParaCrystal_2DDL.py
new file mode 100644
index 00000000000..a6272baf3a0
--- /dev/null
+++ b/Examples/python/ex004_CylindersParaCrystal/CylindersParaCrystal_2DDL.py
@@ -0,0 +1,59 @@
+# IsGISAXS04 example: 2D paracrystal
+import sys, os, numpy
+
+sys.path.append(os.path.abspath(
+                os.path.join(os.path.split(__file__)[0], '..', '..', '..', 'lib')
+                ))
+
+sys.path.append(os.path.abspath(os.path.join(os.path.split(__file__)[0],'..')))
+
+from libBornAgainCore import *
+from utils.show2d import PlotNumpyArray
+
+
+# ----------------------------------
+# describe sample and run simulation
+# ----------------------------------
+def RunSimulation():
+    # defining materials
+    mAmbience = MaterialManager.getHomogeneousMaterial("Air", 1.0, 0.0 )
+    mSubstrate = MaterialManager.getHomogeneousMaterial("Substrate", 1.0-6e-6, 2e-8 )
+    # collection of particles
+    n_particle = complex(1.0-6e-4, 2e-8)
+
+    cylinder_ff = FormFactorCylinder(5*nanometer, 5*nanometer)
+    cylinder = Particle(n_particle, cylinder_ff)
+
+    interference = InterferenceFunction2DParaCrystal.createHexagonal(20.0*nanometer, 0.0,20.0*micrometer, 20.0*micrometer)
+    pdf = FTDistribution2DCauchy(1.0*nanometer, 1.0*nanometer)
+    interference.setProbabilityDistributions(pdf, pdf)
+
+    particle_decoration = ParticleDecoration()
+    particle_decoration.addParticle(cylinder, 0.0, 1.0)
+    particle_decoration.addInterferenceFunction(interference)
+
+    air_layer = Layer(mAmbience)
+    air_layer_decorator = LayerDecorator(air_layer, particle_decoration)
+    substrate_layer = Layer(mSubstrate, 0)
+
+    multi_layer = MultiLayer()
+    multi_layer.addLayer(air_layer_decorator)
+    multi_layer.addLayer(substrate_layer)
+
+    # build and run experiment
+    simulation = Simulation()
+    simulation.setDetectorParameters(100,0.0*degree, 2.0*degree, 100, 0.0*degree, 2.0*degree, True)
+    simulation.setBeamParameters(1.0*angstrom, -0.2*degree, 0.0*degree)
+    simulation.setSample(multi_layer)
+    simulation.runSimulation()
+    return GetOutputData(simulation)
+
+
+#-------------------------------------------------------------
+# main()
+#-------------------------------------------------------------
+if __name__ == '__main__':
+    result = RunSimulation()
+    PlotNumpyArray(result)
+
+
diff --git a/Examples/python/utils/__init__.py b/Examples/python/utils/__init__.py
new file mode 100644
index 00000000000..b0c679daaa6
--- /dev/null
+++ b/Examples/python/utils/__init__.py
@@ -0,0 +1 @@
+__author__ = ''
diff --git a/Examples/python/utils/show2d.py b/Examples/python/utils/show2d.py
index 18a0548bb80..54ee913b416 100755
--- a/Examples/python/utils/show2d.py
+++ b/Examples/python/utils/show2d.py
@@ -1,69 +1,76 @@
 #!/usr/bin/env python
+# Draws two dimensional numpy array using matplotlib
+# Usage: python show2d.py file_name
 
 from pylab import *
 from matplotlib.colors import LogNorm
 import argparse
 import os
 
-# Define the arguments to the script
-parser = argparse.ArgumentParser(description='Plot 2D data table on log scale.')
-parser.add_argument('-m', '--minz', dest='zmin', metavar='MINZ',
-		help='minimum z-value to display [default: 1]',
-		default='1')
-parser.add_argument('-M', '--maxz', dest='zmax', metavar='MAXZ',
-		help='maximum z-value to display [default: maximum value of data set]',
-		default=None)
-parser.add_argument('filename', metavar='filename',
-		help='filename of the data file')
-
-# Parse the script's arguments
-args = parser.parse_args()
-zmin = float(args.zmin)
-if (args.zmax):
-	zmax = float(args.zmax)
-else:
-	zmax = None
-filename = args.filename
 
 # Define a nice colormap
-cdict = {'red': ((0.0, 0.0, 0.0),
-	         (0.5, 0.0, 0.0),
-		 (1.0, 1.0, 1.0)),
-         'green': ((0.0, 0.0, 0.0),
-	           (0.5, 0.0, 0.0),
-	           (1.0, 1.0, 1.0)),
-	 'blue': ((0.0, 0.0, 0.0),
-		  (0.5, 1.0, 1.0),
-		  (1.0, 1.0, 1.0))}
+cdict = {'red':   ((0.0, 0.0, 0.0), (0.5, 0.0, 0.0), (1.0, 1.0, 1.0)),
+         'green': ((0.0, 0.0, 0.0), (0.5, 0.0, 0.0), (1.0, 1.0, 1.0)),
+         'blue':  ((0.0, 0.0, 0.0), (0.5, 1.0, 1.0), (1.0, 1.0, 1.0))}
 blue_cmap = matplotlib.colors.LinearSegmentedColormap('blue_map', cdict, 256)
 
-# Load the file's data
-print( "loading..." )
-a=np.loadtxt(filename)
-file_no_prefix = os.path.splitext(os.path.basename(filename))[0]
-print 'Filename: ', file_no_prefix
-print 'Data shape: ', a.shape
-print 'Minimum value of data: ', a.flatten().min()
-print 'Maximum value of data: ', a.flatten().max()
 
-# Make a plot of the data
-print( "plotting..." )
-amax=a.flatten().max()
-amean=np.sum(a)/a.size
-aminplot=amean**2/amax
-a=np.maximum(a, zmin)
-dataarray=np.flipud(np.transpose(a))
-plt.xlabel(r'$\phi_f$', fontsize=20)
-plt.ylabel(r'$\alpha_f$', fontsize=20)
-# Use one of the predefined colormaps or the above defined 'blue_cmap':
-im=plt.imshow(dataarray, norm=LogNorm(), vmax=zmax, cmap=cm.jet) #, interpolation='none')
-plt.gca().axes.get_xaxis().set_ticks([])
-plt.gca().axes.get_yaxis().set_ticks([])
-plt.colorbar(im)
+def PlotNumpyArray(a, zmin = 1, zmax = None):
+    # Make a plot of the data
+    #print( "plotting..." )
+    amax=a.flatten().max()
+    amean=np.sum(a)/a.size
+    aminplot=amean**2/amax
+    a=np.maximum(a, zmin)
+    dataarray=np.flipud(np.transpose(a))
+    plt.xlabel(r'$\phi_f$', fontsize=20)
+    plt.ylabel(r'$\alpha_f$', fontsize=20)
+    # Use one of the predefined colormaps or the above defined 'blue_cmap':
+    im=plt.imshow(dataarray, norm=LogNorm(), vmax=zmax, cmap=cm.jet) #, interpolation='none')
+    plt.gca().axes.get_xaxis().set_ticks([])
+    plt.gca().axes.get_yaxis().set_ticks([])
+    plt.colorbar(im)
+
+    # Show the plot
+    #print( "showing..." )
+    # Uncomment the next lines to generate a pdf from the plot
+    #extension = 'pdf'
+    #plt.savefig(file_no_prefix + '.%s' %extension, format=extension)
+    plt.show()
+
+
+#-------------------------------------------------------------
+# main()
+#-------------------------------------------------------------
+if __name__ == '__main__':
+    # Define the arguments to the script
+    parser = argparse.ArgumentParser(description='Plot 2D data table on log scale.')
+    parser.add_argument('-m', '--minz', dest='zmin', metavar='MINZ',
+        help='minimum z-value to display [default: 1]',
+        default='1')
+    parser.add_argument('-M', '--maxz', dest='zmax', metavar='MAXZ',
+        help='maximum z-value to display [default: maximum value of data set]',
+        default=None)
+    parser.add_argument('filename', metavar='filename',
+        help='filename of the data file')
+
+    # Parse the script's arguments
+    args = parser.parse_args()
+    zmin = float(args.zmin)
+    if (args.zmax):
+        zmax = float(args.zmax)
+    else:
+        zmax = None
+    filename = args.filename
+
+    # Load the file's data
+    print( "loading..." )
+    a=np.loadtxt(filename)
+    file_no_prefix = os.path.splitext(os.path.basename(filename))[0]
+    print 'Filename: ', file_no_prefix
+    print 'Data shape: ', a.shape
+    print 'Minimum value of data: ', a.flatten().min()
+    print 'Maximum value of data: ', a.flatten().max()
+
+    PlotNumpyArray(a, zmin, zmax)
 
-# Show the plot
-print( "showing..." )
-# Uncomment the next lines to generate a pdf from the plot
-#extension = 'pdf'
-#plt.savefig(file_no_prefix + '.%s' %extension, format=extension)
-plt.show()
diff --git a/Examples/python/utils/show2d_root.py b/Examples/python/utils/show2d_root.py
new file mode 100755
index 00000000000..3691c0e7ce8
--- /dev/null
+++ b/Examples/python/utils/show2d_root.py
@@ -0,0 +1,75 @@
+#!/usr/bin/env python
+# Draws two dimensional numpy array using ROOT
+# Usage: python show2d.py file_name
+
+import argparse
+import os
+import numpy
+import ROOT
+from pylab import *
+
+
+def PlotNumpyArrayWithROOT(a, zmin = 1, zmax = None):
+    nx = a.shape[0]
+    ny = a.shape[1]
+    hist = ROOT.TH2D("hist","hist",nx,0,nx, ny, 0, ny)
+    for (x,y), value in numpy.ndenumerate(a):
+        hist.Fill(x,y,value)
+
+    c1 = ROOT.TCanvas("c1","numpy array", 1024,768)
+    c1.cd()
+    c1.SetLogz()
+    hist.Draw("CONT4Z")
+    c1.Update()
+    #ROOT.gApplication.Run()
+    Interrupt = False
+    while not Interrupt:
+        Interrupt = ROOT.gSystem.ProcessEvents()
+        ROOT.gSystem.Sleep(10)
+
+
+#-------------------------------------------------------------
+# main()
+#-------------------------------------------------------------
+if __name__ == '__main__':
+    # Define the arguments to the script
+    parser = argparse.ArgumentParser(description='Plot 2D data table on log scale.')
+    parser.add_argument('-m', '--minz', dest='zmin', metavar='MINZ',
+        help='minimum z-value to display [default: 1]',
+        default='1')
+    parser.add_argument('-M', '--maxz', dest='zmax', metavar='MAXZ',
+        help='maximum z-value to display [default: maximum value of data set]',
+        default=None)
+    parser.add_argument('filename', metavar='filename',
+        help='filename of the data file')
+
+    # Parse the script's arguments
+    args = parser.parse_args()
+    zmin = float(args.zmin)
+    if (args.zmax):
+        zmax = float(args.zmax)
+    else:
+        zmax = None
+    filename = args.filename
+
+    # Load the file's data
+    print( "loading..." )
+    a=np.loadtxt(filename)
+    file_no_prefix = os.path.splitext(os.path.basename(filename))[0]
+    print 'Filename: ', file_no_prefix
+    print 'Data shape: ', a.shape
+    print 'Minimum value of data: ', a.flatten().min()
+    print 'Maximum value of data: ', a.flatten().max()
+
+    PlotNumpyArrayWithROOT(a, zmin, zmax)
+
+    #rep = ''
+    #while not rep in [ 'q', 'Q' ]:
+        #rep = raw_input( 'enter "q" to quit: ' )
+        #if 1 < len(rep):
+            #rep = rep[0]    
+
+    ROOT.gApplication.Run()
+    
+    #while ROOT.gSystem.ProcessEvent():
+        #print "aaa"
\ No newline at end of file
-- 
GitLab