Giter Club home page Giter Club logo

srw's Introduction

Synchrotron Radiation Workshop (SRW)

SRW is a physical optics computer code for calculation of detailed characteristics of Synchrotron Radiation (SR) generated by relativistic electrons in magnetic fields of arbitrary configuration and for simulation of the radiation wavefront propagation through optical systems of beamlines.

Frequency-domain near-field methods are used for the SR calculation, and the Fourier-optics based approach is generally used for the wavefront propagation simulation. The code enables both fully- and partially-coherent radiation propagation simulations in steady-state and in frequency-/time-dependent regimes. With these features, the code has already proven its utility for a large number of applications in infrared, UV, soft and hard X-ray spectral range, in such important areas as analysis of spectral performances of new synchrotron radiation sources, optimization of user beamlines, development of new optical elements, source and beamline diagnostics, and even complete simulation of SR based experiments. Besides the SR applications, the code can be efficiently used for various simulations involving conventional lasers and other sources. SRW versions interfaced to Python and to IGOR Pro (WaveMetrics), as well as cross-platform library with C API, are available.

In the following writing, it is assumed that SRW_Dev is absolute path to the full SRW directory (obtained e.g. after downloading from repository).

I. Using pre-compiled SRW libraries and clients / bindings:

The last ~"clean" release of SRW for IGOR Pro and for Python can be found in SRW_Dev/env/release, in particular:

  • installers (of compressed packages) can be found in SRW_Dev/env/release/install;
  • unpacked folder of SRW for IGOR Pro (for Windows only) is: SRW_Dev/env/release/srw_igor;
    This folder contains ReadMe.txt file with general "start-up" notes; detailed documentation for IGOR Pro version can be found in: SRW_Dev/env/release/srw_igor/SRW Help/SRW Help.ifn file (in IGOR formatted notebook format);
  • unpacked folder of SRW for Python (for Windows and Linux) is: SRW_Dev/env/release/srw_python; this folder contains ReadMe.txt file with general "start-up" notes.

The most recent pre-releases and current work versions of SRW for Python and for IGOR Pro can be found in: SRW_Dev/env/work.

Testing of the pre-compiled SRW libraries and clients / bindings can be done using examples included both to Python and IGOR Pro versions of SRW (see "Checking the examples" sections below for different platforms).

II. Compiling and testing SRW Library and its Python and IGOR Pro bindings on Windows

II.1. Compiling SRW library and Python binding using MS Visual C++

II.1.1

Microsoft Visual C++ 2015 (or later version) solution file (SRW.sln), which includes 4 projects:

  • SRW Library (file SRWLIB.vcxproj),
  • SRW Python client / binding (file SRWLClientPython.vcxproj),
  • SRW IGOR Pro client / binding (file SRWLClientIgor.vcxproj),
  • SRW C demo client (file SRWLClientC.vcxproj), can be found in SRW_Dev/cpp/vc.

The SRWLClientPython project file allows for compiling srwlpy.pyd shared library, i.e. SRW for Python 2.7 or/and 3.x (64-bit or 32-bit); SRWLClientIgor allows for compiling SRW.xop shared library, i.e. SRW for IGOR Pro (32-bit only). Free Microsoft Visual Studio Community 2015 (or later versions) can be used.

To compile SRW library supporting OpenMP based parallel calculations (e.g. for XFEL applications):

  • In the Visual C++ Configuration Manager, select "Release_omp" version of the SRWLIB project, then re-compile SRWLIB and SRWLClientPython under the "x64" Solution Platform to produce a 64-bit version of SRW for Python supporting OpenMP based parallel calculations.
  • Note that the "Release_omp" version of the SRWLIB project has only a few differences with respect to the standard version: the "_WITH_OMP" preprocessor definition is added to Configuration Properties -> C/C++ -> Preprocessor -> Preprocessor Definitions, the "Open MP Support" option is set to "Yes (/openmp)" in Configuration Properties -> C/C++ -> Language -> Open MP Support, and the linking is made with older versions of the FFTW library (FFTW 2.5), whereas the "normal" compilation / linking is with the FFTW 3.8.

II.2. Checking the examples

II.2.1

The SRW for Python examples can be tested using e.g. "IDLE" (Python native GUI). To do so, start this application (e.g. from Windows Start menu), open an example file in it, e.g. SRW_Dev\env\work\srw_python\SRWLIB_Example01.py, and run it from the IDLE.

Alternatively, the example scripts can be executed from the Windows Command Prompt, e.g. from within the SRW_Dev\env\work\srw_python directory. For convenience, correct path to python.exe file may need to be specified in the Windows system PATH variable prior to these tests.

II.2.2

The SRW for IGOR Pro examples can be tested from "SRWE" and "SRWP" menus, "Help" sub-menus, of the IGOR Pro.

III. Compiling and testing SRW Library and its Python binding on Linux

III.1. Compiling SRW library and Python binding

This can be done either using Python "setuptools" module (see section III.1.1 below) or without it (see section III.1.2).

III.1.1. Compiling using Python "setuptools" module

Make sure the "setuptools" module of the Python version you would like to use is properly installed and configured. If this is done, the compilation and installation is simple:

cd SRW_Dev
make all

To compile SRW library supporting OpenMP based parallel calculations (e.g. for XFEL applications) add "MODE=omp" after "make all":

make all MODE=omp

This should compile libsrw.a and srwlpy.so, and copy srwlpy.so to SRW_Dev/env/work/srw_python/

III.1.2. Compiling without "setuptools"

III.1.2.1. Download and compile fftw-2.1.5 or/and fftw-3.3.8 library as required for SRW

Make sure files fftw-3.3.8.tar.gz and fftw-2.1.5.tar.gz are located in SRW_Dev/ext_lib directory (if necessary, download these files from FFTW site, probably http://www.fftw.org/download.html).
Do the following to compile fftw-3.3.8 for using single-precision numbers as required for most FFT-based operations in SRW:

cd SRW_Dev/ext_lib
tar -zxvf fftw-3.3.8.tar.gz
cd fftw-3.3.8
./configure --enable-float --with-pic

Manually (using editor) add -fPIC option to CFLAGS in Makefile

make -j8 && cp .libs/libfftw3f.a ../

Do the following to compile fftw-3.3.8 for using double-precision numbers required for some FFT-based operations in SRW:

cd SRW_Dev/ext_lib
tar -zxvf fftw-3.3.8.tar.gz
cd fftw-3.3.8
./configure --with-pic

Manually (using editor) add -fPIC option to CFLAGS in Makefile

make -j8 && cp .libs/libfftw3.a ../

Do the following to compile fftw-2.1.5 for using single-precision numbers required for supporting OpenMP based parallel calculations in SRW:

cd SRW_Dev/ext_lib
tar -zxvf fftw-2.1.5.tar.gz
cd fftw-2.1.5
./configure --enable-float --with-pic

Manually (using editor) add -fPIC option to CFLAGS in Makefile

make -j8 && cp fftw/.libs/libfftw.a ../

III.1.2.2. Compiling the SRW library and Python binding

cd SRW_Dev/cpp/gcc

Make sure Python 3.3 or higher (or Python 2.7) is installed.
In the SRW_Dev/cpp/gcc/Makefile, modify/correct PYPATH and PYFLAGS variables, i.e. specify path to Python header and library files. Depending on Linux environment, it may also be necessary to modify the name of compiler to be used, e.g.:

CC  = gcc
CXX = g++
#CC  = cc
#CXX = c++

After this, execute the following:

rm libsrw.a
make all

To compile SRW library in the mode supporting OpenMP based parallel calculations (e.g. for XFEL applications) add "MODE=omp" after "make all":

make all MODE=omp

Then copy srwlpy.so to SRW_Dev/env/work/srw_python/:

cp srwlpy.so ../../env/work/srw_python/

III.2. Checking the examples

Make sure the path to Python 3.x (or 2.7) is added to the PATH variable and "srw_python" to PYTHONPATH variable:

export PATH="$PATH:<absolute path to Python 3.x>" # this is not necessary if you install python using the distro's package manager
export PYTHONPAH="$PYTHONPATH:SRW_Dev/env/work/srw_python/" #temporary solution

or

echo "export PYTHONPATH=$PYTHONPATH:SRW_Dev/env/work/srw_python/" >> ~/.bashrc #permanent solution for a single user

Setting up PYTHONPATH allows to import srwlpy module from any directory. Testing of the examples would preferably done in the srw_python directory:

cd SRW_Dev/env/work/srw_python
python SRWLIB_ExampleXX.py

IV. Compiling and testing SRW Library and its Python binding on Mac OSX

Try to follow the steps described in section III (describing options for compiling and testing SRW on Linux).

We were informed that the actions described in III.1.1 lead to successful compilation on OSX 10.14.5 after the following modifications in SRW_Dev/cpp/gcc/Makefile:
Change CXX variable as follows:

#CXX = c++
CXX = g++ -stdlib=libc++ -mmacosx-version-min=10.9

Make sure to explicitly use the C++ compiler (CXX) for compiling all *.cpp files, e.g.:

%.o: $(SH_SRC_PARSE_DIR)/%.cpp
    $(CXX) $(CFLAGS) -c $<

It may be necessary also to set also the CC variable to came value as CXX (?):

#CC = cc
CC = g++ -stdlib=libc++ -mmacosx-version-min=10.9

Previously, we were informed that the actions described in III.1.2.2 lead to successful compilation with gcc/g++ provided by Xcode 10.1, after the following modifications in SRW_Dev/cpp/gcc/Makefile:

CC  = gcc
CXX = g++
#CC  = cc
#CXX = c++
...
PYPATH=/Library/Frameworks/Python.framework/Versions/3.6
PYFLAGS=-I$(PYPATH)/include/python3.6m -I$(PYPATH)/include/python3.6m -L$(PYPATH)/lib/python3.6/config-3.6m-darwin -lpython3.6m -ldl

The correct path and flags can be obtained e.g. by executing from command line:

python3-config --includes --ldflags

and removing the option "-framework"

With earlier versions of Xcode, the following manipulations, consisting in installation of "macports" and obtaining the whole gcc toolchain, were reported to be successful:

sudo port install gcc47

Modify the SRW_Dev/cpp/gcc/Makefile so that CC=<path to macports>/gcc and CXX=<path to macports>/g++, and proceed to the compilation as described in III.1.2.2.

V. Compiling and testing SRW Library and its Python binding on Windows and Linux (via CMake/Pip)

Run the following in a Visual Studio Developer Command Line/Linux Terminal:

cmake -B build
cmake --build build -j

The pip installable version of the package can be obtained by running the following in a Visual Studio Developer Command Line/Linux Terminal:

cd env/python
pip install -e .

VI. GPU Acceleration of SRW

SRW has basic support for GPU acceleration of some routines through CUDA. Compilation of SRW with GPU acceleration requires the CUDA HPC SDK to be installed and, on Linux can be performed with:

MODE=cuda make

To compile on Windows, open the SRW solution in Visual Studio, set the target to the _cuda variants and update the library and include paths for the SRWLIB project. You may also have to copy the following DLLs from the HPC SDK install into the env/python/srwpy directory:

  • cudart64_110.dll
  • cufft64_10.dll

Authors and Contributors to SRW project

O. Chubar (ESRF - SOLEIL - BNL)
P. Elleaume (ESRF)
J. Chavanne (ESRF)
R. Celestre (ESRF)
P. Dumas (SOLEIL)
O. Marcouille (SOLEIL)
L. Samoylova (E-XFEL)
A. Buzmakov (E-XFEL)
G. Geloni (E-XFEL)
I. Agapov (E-XFEL)
J. Sutter (DIAMOND)
D. Laundy (DIAMOND)
A. He (BNL)
R. Coles (BNL)
R. Li (BNL)
M. Rakitin (BNL)
H. Goel (SBU - BNL)
N. Canestrari (ESRF - BNL)
A. Suvorov (BNL)
R. Reininger (ANL)
X. Shi (ANL)
R. Lindberg (ANL)
L. Rebuffi (ELETTRA - ANL)
D. Bruhwiler (RadiaSoft LLC)
R. Nagler (RadiaSoft LLC)
P. Moeller (RadiaSoft LLC)
B. Nash (RadiaSoft LLC)

srw's People

Contributors

himanshugoel2797 avatar mrakitin avatar ochubar avatar

Watchers

 avatar  avatar  avatar

Forkers

gaoboyy

srw's Issues

Rename mask element

A "Mask" is not very technical term, need to rename the optical element in both SRW and Sirepo to something like "Wavefront Sensor". This should be done in accordance with radiasoft/sirepo#450.

Allow tabulated undulator definition by a single .dat file

Currently the tabulated undulator is defined by a magnetic measurements summary file (*.txt), which lists *.dat files with the magnetic measurements. Now need to implement definition of the tabulated undulator by a single *.dat file.

Rename _traj* functions

Change uti_plot.py (and other files) to have single-column and multiple column cases (rename _traj* functions).

Revise examples 15 and 16

Need to update examples 15 and 16 to allow saving of the .dat files even if there is no matplotlib or scipy installed. Based on comments in ochubar#7.

Error with calculation of FWHM

mrakitin@cpu-001:~/calc/SMI/tab_und/2017-05-09_Espread_sims$ view_srw res_spec_me_01_emx_0.79369_emy_10.53617_ens_0.7.dat
Traceback (most recent call last):
  File "/home/mrakitin/src/mrakitin/SRW/env/work/srw_python/SRWLIB_ExampleViewDataFile.py", line 38, in <module>
    opt.scale, opt.width_pixels) #MR29072016
  File "/nfs/mrakitin/src/mrakitin/SRW/env/work/srw_python/uti_plot.py", line 170, in uti_data_file_plot
    _scale, _width_pixels) #MR20012017
  File "/nfs/mrakitin/src/mrakitin/SRW/env/work/srw_python/uti_plot_matplotlib.py", line 114, in uti_data_file_plot
    fwhm_dict = uti_math.fwhm(np.linspace(allrange[0], allrange[1], allrange[2]), data, return_as_dict=True)
  File "/nfs/mrakitin/src/mrakitin/SRW/env/work/srw_python/uti_math.py", line 648, in fwhm
    raise Exception('Number of roots is less than 2!')
Exception: Number of roots is less than 2!

Need to show the plot anyway.

SRW - implement estimation of the convergence

Convergence of the long-running SRW calculations could be estimated based on two criteria after N iterations (e.g., N=1000):

  • the RMS and/or the FWHM convergence;
  • the following ratio:
    $\frac{\int{[I_{n}(x, y)-I_{n-1}(x, y)]^{2}dxdy}}{[\int{I_{n}(x, y)dxdy}]^{2}}$

SRW_new+Sirepo: Bending Magnet Radiation example fails

The following example in Sirepo fails - Synchrotron Radiation Workshop - SR Calculator - Bending Magnet Radiation:

  File "/home/vagrant/src/radiasoft/sirepo/sirepo/pkcli/srw.py", line 134, in _run_srw
    srwl_bl.SRWLBeamline(_name=v.name, _mag_approx=mag).calc_all(v, op)
  File "/home/vagrant/.pyenv/versions/2.7.10/lib/python2.7/site-packages/srwl_bl.py", line 1366, in calc_all
    _zc = _v.und_zc)
  File "/home/vagrant/.pyenv/versions/2.7.10/lib/python2.7/site-packages/srwl_bl.py", line 276, in set_und_sin
    if(self.mag_approx == None): self.mag_approx = SRWLMagFldC()
AttributeError: 'SRWLBeamline' object has no attribute 'mag_approx'

Compilation of SRW fails on Ubuntu 14.04 x64

x86_64-linux-gnu-gcc -pthread -fno-strict-aliasing -DNDEBUG -g -fwrapv -O2 -Wall -Wstrict-prototypes -fPIC -DMAJOR_VERSION=1 -DMINOR_VERSION=0 -I/home/vagrant/src/mrakitin/SRW/cpp/src/lib -I/usr/include/python2.7 -c /home/vagrant/src/mrakitin/SRW/cpp/src/clients/python/srwlpy.cpp -o build/temp.linux-x86_64-2.7/home/vagrant/src/mrakitin/SRW/cpp/src/clients/python/srwlpy.o
cc1plus: warning: command line option ‘-Wstrict-prototypes’ is valid for C/ObjC but not for C++ [enabled by default]
/home/vagrant/src/mrakitin/SRW/cpp/src/clients/python/srwlpy.cpp:36:20: fatal error: Python.h: No such file or directory
 #include "Python.h"
                    ^
compilation terminated.
error: command 'x86_64-linux-gnu-gcc' failed with exit status 1
make[1]: *** [srwlpy.so] Error 1
make[1]: Leaving directory `/home/vagrant/src/mrakitin/SRW/cpp/py'
make: *** [pylib] Error 2

Cannot make SRW on Ubuntu 14.04 x64

The issue is noticed by @buzmakov:

vagrant@vagrant-ubuntu-trusty-64:~/tmp$
vagrant@vagrant-ubuntu-trusty-64:~/tmp$ wget https://github.com/ochubar/SRW/archive/master.zip
--2016-10-11 14:15:22--  https://github.com/ochubar/SRW/archive/master.zip
Resolving github.com (github.com)... 192.30.253.112
Connecting to github.com (github.com)|192.30.253.112|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://codeload.github.com/ochubar/SRW/zip/master [following]
--2016-10-11 14:15:22--  https://codeload.github.com/ochubar/SRW/zip/master
Resolving codeload.github.com (codeload.github.com)... 192.30.253.121
Connecting to codeload.github.com (codeload.github.com)|192.30.253.121|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: unspecified [application/zip]
Saving to: ‘master.zip’

    [                                    <=>                                                                                             ] 406,147,926 8.67MB/s   in 46s

2016-10-11 14:16:11 (8.42 MB/s) - ‘master.zip’ saved [406147926]

vagrant@vagrant-ubuntu-trusty-64:~/tmp$
vagrant@vagrant-ubuntu-trusty-64:~/tmp$ unzip -o master.zip
Archive:  master.zip
087ce511950156b7f499539eb45e36595c425b06
   creating: SRW-master/
   ...
vagrant@vagrant-ubuntu-trusty-64:~/tmp$ cd SRW-master/
vagrant@vagrant-ubuntu-trusty-64:~/tmp/SRW-master$ l
total 48
drwxrwxr-x 6 vagrant vagrant 4096 Sep 30 17:01 ./
drwxrwxr-x 3 vagrant vagrant 4096 Oct 11 14:16 ../
-rw-rw-r-- 1 vagrant vagrant 3804 Sep 30 17:01 COPYRIGHT.txt
drwxrwxr-x 6 vagrant vagrant 4096 Sep 30 17:01 cpp/
drwxrwxr-x 4 vagrant vagrant 4096 Sep 30 17:01 env/
drwxrwxr-x 3 vagrant vagrant 4096 Sep 30 17:01 ext_lib/
-rw-rw-r-- 1 vagrant vagrant    7 Sep 30 17:01 .gitignore
drwxrwxr-x 3 vagrant vagrant 4096 Sep 30 17:01 literature/
-rw-rw-r-- 1 vagrant vagrant 2566 Sep 30 17:01 Makefile
-rw-rw-r-- 1 vagrant vagrant 6598 Sep 30 17:01 README.txt
-rw-rw-r-- 1 vagrant vagrant  372 Sep 30 17:01 repository_struct.txt
vagrant@vagrant-ubuntu-trusty-64:~/tmp/SRW-master$ make all
Makefile:1: *** missing separator.  Stop.
vagrant@vagrant-ubuntu-trusty-64:~/tmp/SRW-master$

Memory allocation for wavefronts using NumPy arrays

Alexey Buzmakov implemented memory allocation for large wavefronts (~ several GBs):

It's claimed it works hundred times faster. Need to consider implementing it in main version of SRW.


def srwl_uti_array_alloc(_type, _n):
    try:
        import numpy
        # resArr = array(str(u'f'),
        #     numpy.zeros((_n,), dtype='float32').tobytes())
        resArr = numpy.zeros((_n,), dtype='float32')
        return resArr
    except ImportError:
        pass
    ...

Check argparse in PowerShell

The behavior of the new argparser is different in Windows command line (cmd) and in the PowerShell if we pass the list of options like propagation parameters.

Improve logging feature

Need to improve logging feature:

  • Rename log file from srw_mpi.* to srw_status.*
  • Add a unique identifier of the log (like timestamp).
  • Place the logs to a separate (technical) directory.
  • Allow logs printing for both parallel and serial calculations.

Rework eBeam processing in SRW

Need to avoid using user-defined beam in SRW via ebm_ud_* variables. Instead, need to update env.work.srw_python.srwl_bl.SRWLBeamline#set_e_beam() function so that it could accept both Twiss and Moments parameters directly.

Samples - add more functionality

Samples library:

  • Update to input a rectangle area for the desired region.
  • Remove the code responsible for removal of the bottom part of the image in uti_io.py.
  • Angle - implement rotation of samples.

Example 10 fails without mpi4py library

Example 10 fails on Mac after the 4th step with the error:

Calculation will be sequential (non-parallel), because "mpi4py" module can not be loaded
i= 0 Electron Coord.: x= -2.74452048352e-05 x'= -5.30201947148e-06 y= 3.33399322641e-06 y'= 8.88316322966e-07 E= 3.00238284361
i= 1 Electron Coord.: x= 2.94014986023e-05 x'= -3.43237532114e-06 y= -1.01652414766e-06 y'= -5.38704099806e-06 E= 3.00112419227
i= 2 Electron Coord.: x= 2.30356693789e-05 x'= 1.81178681222e-06 y= 2.70769424073e-06 y'= 1.69800387734e-06 E= 3.00249151578
i= 3 Electron Coord.: x= -4.12356508492e-05 x'= -2.36273974903e-05 y= 2.75360743031e-06 y'= 4.41985214074e-06 E= 2.99916732752
i= 4 Electron Coord.: x= 6.21094516163e-05 x'= -1.07174990888e-05 y= -1.19992938014e-06 y'= -3.97142680411e-06 E= 3.0004317949
Traceback (most recent call last):
  File "SRWLIB_Example10.py", line 217, in <module>
    os.path.join(os.getcwd(), strExDataFolderName, strIntPropME_OutFileName), sampFactNxNyForProp, optBL)
  File "/Users/vagrant/src/mrakitin/SRW/env/work/srw_python/srwlib.py", line 4593, in srwl_wfr_emit_prop_multi_e
    srwl_uti_save_stat_wfr_emit_prop_multi_e(i + 1, total_num_of_particles, filename=log_path)
  File "/Users/vagrant/src/mrakitin/SRW/env/work/srw_python/srwlib.py", line 3839, in srwl_uti_save_stat_wfr_emit_prop_multi_e
    srwl_uti_save_text(text_to_save, status_text_file, mode=mode)
  File "/Users/vagrant/src/mrakitin/SRW/env/work/srw_python/srwlib.py", line 3697, in srwl_uti_save_text
    with open(_file_path, mode) as f:
IOError: [Errno 2] No such file or directory: '/Users/vagrant/src/mrakitin/SRW/env/work/srw_python/__srwl_logs__/srwl_stat_wfr_emit_prop_multi_e_2017-01-30_19-59-32.log'

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.