source: sasmodels/api/generate.html @ 68532f3

gh-pages
Last change on this file since 68532f3 was 68532f3, checked in by ajj, 8 years ago

Adding docs

  • Property mode set to 100644
File size: 14.9 KB
Line 
1<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
2  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
3
4
5<html xmlns="http://www.w3.org/1999/xhtml">
6  <head>
7    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
8   
9    <title>3.6. Model parser &mdash; Sasmodels</title>
10   
11    <link rel="stylesheet" href="../_static/haiku-site.css" type="text/css" />
12    <link rel="stylesheet" href="../_static/pygments.css" type="text/css" />
13   
14    <script type="text/javascript">
15      var DOCUMENTATION_OPTIONS = {
16        URL_ROOT:    '../',
17        VERSION:     '0.9',
18        COLLAPSE_INDEX: false,
19        FILE_SUFFIX: '.html',
20        HAS_SOURCE:  true
21      };
22    </script>
23    <script type="text/javascript" src="../_static/jquery.js"></script>
24    <script type="text/javascript" src="../_static/underscore.js"></script>
25    <script type="text/javascript" src="../_static/doctools.js"></script>
26    <script type="text/javascript" src="http://cdn.mathjax.org/mathjax/latest/MathJax.js?config=TeX-AMS-MML_HTMLorMML"></script>
27    <link rel="top" title="Sasmodels" href="../index.html" />
28    <link rel="up" title="3. Reference" href="index.html" />
29    <link rel="next" title="3.7. OpenCL model evaluator" href="kernelcl.html" />
30    <link rel="prev" title="3.5. Annotate exceptions" href="exception.html" /> 
31  </head>
32  <body>
33      <div class="header"><h1 class="heading"><a href="../index.html">
34          <span>Home</span></a></h1>
35        <h2 class="heading"><span>3.6. Model parser</span></h2>
36      </div>
37      <div class="topnav">
38     
39        <p>
40        «&#160;&#160;<a href="exception.html">3.5. Annotate exceptions</a>
41        &#160;&#160;::&#160;&#160;
42        <a class="uplink" href="../index.html">Contents</a>
43        &#160;&#160;::&#160;&#160;
44        <a href="kernelcl.html">3.7. OpenCL model evaluator</a>&#160;&#160;»
45        </p>
46
47      </div>
48      <div class="content">
49       
50       
51  <div class="section" id="model-parser">
52<h1>3.6. Model parser<a class="headerlink" href="#model-parser" title="Permalink to this headline">¶</a></h1>
53<div class="section" id="module-sasmodels.generate">
54<span id="sasmodels-generate"></span><h2>3.6.1. <a class="reference internal" href="#module-sasmodels.generate" title="sasmodels.generate"><tt class="xref py py-mod docutils literal"><span class="pre">sasmodels.generate</span></tt></a><a class="headerlink" href="#module-sasmodels.generate" title="Permalink to this headline">¶</a></h2>
55<p>SAS model constructor.</p>
56<p>Small angle scattering models are defined by a set of kernel functions:</p>
57<blockquote>
58<div><p><em>Iq(q, p1, p2, ...)</em> returns the scattering at q for a form with
59particular dimensions averaged over all orientations.</p>
60<p><em>Iqxy(qx, qy, p1, p2, ...)</em> returns the scattering at qx,qy for a form
61with particular dimensions for a single orientation.</p>
62<p><em>Imagnetic(qx, qy, result[], p1, p2, ...)</em> returns the scattering for the
63polarized neutron spin states (up-up, up-down, down-up, down-down) for
64a form with particular dimensions for a single orientation.</p>
65<p><em>form_volume(p1, p2, ...)</em> returns the volume of the form with particular
66dimension.</p>
67<p><em>ER(p1, p2, ...)</em> returns the effective radius of the form with
68particular dimensions.</p>
69<p><em>VR(p1, p2, ...)</em> returns the volume ratio for core-shell style forms.</p>
70</div></blockquote>
71<p>These functions are defined in a kernel module .py script and an associated
72set of .c files.  The model constructor will use them to create models with
73polydispersity across volume and orientation parameters, and provide
74scale and background parameters for each model.</p>
75<p><em>Iq</em>, <em>Iqxy</em>, <em>Imagnetic</em> and <em>form_volume</em> should be stylized C-99
76functions written for OpenCL.  All functions need prototype declarations
77even if the are defined before they are used.  OpenCL does not support
78<em>#include</em> preprocessor directives, so instead the list of includes needs
79to be given as part of the metadata in the kernel module definition.
80The included files should be listed using a path relative to the kernel
81module, or if using &#8220;lib/file.c&#8221; if it is one of the standard includes
82provided with the sasmodels source.  The includes need to be listed in
83order so that functions are defined before they are used.</p>
84<p>Floating point values should be declared as <em>double</em>.  For single precision
85calculations, <em>double</em> will be replaced by <em>float</em>.  The single precision
86conversion will also tag floating point constants with &#8220;f&#8221; to make them
87single precision constants.  When using integral values in floating point
88expressions, they should be expressed as floating point values by including
89a decimal point.  This includes 0., 1. and 2.</p>
90<p>OpenCL has a <em>sincos</em> function which can improve performance when both
91the <em>sin</em> and <em>cos</em> values are needed for a particular argument.  Since
92this function does not exist in C99, all use of <em>sincos</em> should be
93replaced by the macro <em>SINCOS(value,sn,cn)</em> where <em>sn</em> and <em>cn</em> are
94previously declared <em>double</em> variables.  When compiled for systems without
95OpenCL, <em>SINCOS</em> will be replaced by <em>sin</em> and <em>cos</em> calls.   If <em>value</em> is
96an expression, it will appear twice in this case; whether or not it will be
97evaluated twice depends on the quality of the compiler.</p>
98<p>If the input parameters are invalid, the scattering calculator should
99return a negative number. Particularly with polydispersity, there are
100some sets of shape parameters which lead to nonsensical forms, such
101as a capped cylinder where the cap radius is smaller than the
102cylinder radius.  The polydispersity calculation will ignore these points,
103effectively chopping the parameter weight distributions at the boundary
104of the infeasible region.  The resulting scattering will be set to
105background.  This will work correctly even when polydispersity is off.</p>
106<p><em>ER</em> and <em>VR</em> are python functions which operate on parameter vectors.
107The constructor code will generate the necessary vectors for computing
108them with the desired polydispersity.</p>
109<p>The available kernel parameters are defined as a list, with each parameter
110defined as a sublist with the following elements:</p>
111<blockquote>
112<div><p><em>name</em> is the name that will be used in the call to the kernel
113function and the name that will be displayed to the user.  Names
114should be lower case, with words separated by underscore.  If
115acronyms are used, the whole acronym should be upper case.</p>
116<p><em>units</em> should be one of <em>degrees</em> for angles, <em>Ang</em> for lengths,
117<em>1e-6/Ang^2</em> for SLDs.</p>
118<p><em>default value</em> will be the initial value for  the model when it
119is selected, or when an initial value is not otherwise specified.</p>
120<p>[<em>lb</em>, <em>ub</em>] are the hard limits on the parameter value, used to limit
121the polydispersity density function.  In the fit, the parameter limits
122given to the fit are the limits  on the central value of the parameter.
123If there is polydispersity, it will evaluate parameter values outside
124the fit limits, but not outside the hard limits specified in the model.
125If there are no limits, use +/-inf imported from numpy.</p>
126<p><em>type</em> indicates how the parameter will be used.  &#8220;volume&#8221; parameters
127will be used in all functions.  &#8220;orientation&#8221; parameters will be used
128in <em>Iqxy</em> and <em>Imagnetic</em>&#8220;magnetic* parameters will be used in
129<em>Imagnetic</em> only.  If <em>type</em> is the empty string, the parameter will
130be used in all of <em>Iq</em>, <em>Iqxy</em> and <em>Imagnetic</em>.</p>
131<p><em>description</em> is a short description of the parameter.  This will
132be displayed in the parameter table and used as a tool tip for the
133parameter value in the user interface.</p>
134</div></blockquote>
135<p>The kernel module must set variables defining the kernel meta data:</p>
136<blockquote>
137<div><p><em>name</em> is the model name</p>
138<p><em>title</em> is a short description of the model, suitable for a tool tip,
139or a one line model summary in a table of models.</p>
140<p><em>description</em> is an extended description of the model to be displayed
141while the model parameters are being edited.</p>
142<p><em>parameters</em> is the list of parameters.  Parameters in the kernel
143functions must appear in the same order as they appear in the
144parameters list.  Two additional parameters, <em>scale</em> and <em>background</em>
145are added to the beginning of the parameter list.  They will show up
146in the documentation as model parameters, but they are never sent to
147the kernel functions.</p>
148<p><em>source</em> is the list of C-99 source files that must be joined to
149create the OpenCL kernel functions.  The files defining the functions
150need to be listed before the files which use the functions.</p>
151<p><em>ER</em> is a python function defining the effective radius.  If it is
152not present, the effective radius is 0.</p>
153<p><em>VR</em> is a python function defining the volume ratio.  If it is not
154present, the volume ratio is 1.</p>
155<p><em>form_volume</em>, <em>Iq</em>, <em>Iqxy</em>, <em>Imagnetic</em> are strings containing the
156C source code for the body of the volume, Iq, and Iqxy functions
157respectively.  These can also be defined in the last source file.</p>
158<p><em>Iq</em> and <em>Iqxy</em> also be instead be python functions defining the
159kernel.  If they are marked as <em>Iq.vectorized = True</em> then the
160kernel is passed the entire <em>q</em> vector at once, otherwise it is
161passed values one <em>q</em> at a time.  The performance improvement of
162this step is significant.</p>
163</div></blockquote>
164<p>An <em>info</em> dictionary is constructed from the kernel meta data and
165returned to the caller.</p>
166<p>Additional fields can be defined in the kernel definition file that
167are not needed for sas modelling.</p>
168<blockquote>
169<div><p><em>demo</em> is a dictionary of parameter=value defining a set of
170parameters to use by default when <em>compare</em> is called.</p>
171<p><em>oldname</em> is the name of the model in sasview before sasmodels
172was split into its own package, and <em>oldpars</em> is a dictionary
173of <em>parameter: old_parameter</em> pairs defining the new names for
174the parameters.  This is used by <em>compare</em> to check the values
175of the new model against the values of the old model before
176you are ready to add the new model to sasmodels.</p>
177</div></blockquote>
178<p>The model evaluator, function call sequence consists of q inputs and the return vector,
179followed by the loop value/weight vector, followed by the values for
180the non-polydisperse parameters, followed by the lengths of the
181polydispersity loops.  To construct the call for 1D models, the
182categories <em>fixed-1d</em> and <em>pd-1d</em> list the names of the parameters
183of the non-polydisperse and the polydisperse parameters respectively.
184Similarly, <em>fixed-2d</em> and <em>pd-2d</em> provide parameter names for 2D models.
185The <em>pd-rel</em> category is a set of those parameters which give
186polydispersitiy as a portion of the value (so a 10% length dispersity
187would use a polydispersity value of 0.1) rather than absolute
188dispersity such as an angle plus or minus 15 degrees.</p>
189<p>The <em>volume</em> category lists the volume parameters in order for calls
190to volume within the kernel (used for volume normalization) and for
191calls to ER and VR for effective radius and volume ratio respectively.</p>
192<p>The <em>orientation</em> and <em>magnetic</em> categories list the orientation and
193magnetic parameters.  These are used by the sasview interface.  The
194blank category is for parameters such as scale which don&#8217;t have any
195other marking.</p>
196<p>The doc string at the start of the kernel module will be used to
197construct the model documentation web pages.  Embedded figures should
198appear in the subdirectory &#8220;img&#8221; beside the model definition, and tagged
199with the kernel module name to avoid collision with other models.  Some
200file systems are case-sensitive, so only use lower case characters for
201file names and extensions.</p>
202<p>The function <a class="reference internal" href="#sasmodels.generate.make" title="sasmodels.generate.make"><tt class="xref py py-func docutils literal"><span class="pre">make()</span></tt></a> loads the metadata from the module and returns
203the kernel source.  The function <a class="reference internal" href="#sasmodels.generate.doc" title="sasmodels.generate.doc"><tt class="xref py py-func docutils literal"><span class="pre">doc()</span></tt></a> extracts the doc string
204and adds the parameter table to the top.  The function <a class="reference internal" href="#sasmodels.generate.sources" title="sasmodels.generate.sources"><tt class="xref py py-func docutils literal"><span class="pre">sources()</span></tt></a>
205returns a list of files required by the model.</p>
206<dl class="function">
207<dt id="sasmodels.generate.make">
208<tt class="descclassname">sasmodels.generate.</tt><tt class="descname">make</tt><big>(</big><em>kernel_module</em><big>)</big><a class="headerlink" href="#sasmodels.generate.make" title="Permalink to this definition">¶</a></dt>
209<dd><p>Build an OpenCL/ctypes function from the definition in <em>kernel_module</em>.</p>
210<p>The module can be loaded with a normal python import statement if you
211know which module you need, or with __import__(&#8216;sasmodels.model.&#8217;+name)
212if the name is in a string.</p>
213</dd></dl>
214
215<dl class="function">
216<dt id="sasmodels.generate.doc">
217<tt class="descclassname">sasmodels.generate.</tt><tt class="descname">doc</tt><big>(</big><em>kernel_module</em><big>)</big><a class="headerlink" href="#sasmodels.generate.doc" title="Permalink to this definition">¶</a></dt>
218<dd><p>Return the documentation for the model.</p>
219</dd></dl>
220
221<dl class="function">
222<dt id="sasmodels.generate.sources">
223<tt class="descclassname">sasmodels.generate.</tt><tt class="descname">sources</tt><big>(</big><em>info</em><big>)</big><a class="headerlink" href="#sasmodels.generate.sources" title="Permalink to this definition">¶</a></dt>
224<dd><p>Return a list of the sources file paths for the module.</p>
225</dd></dl>
226
227<dl class="function">
228<dt id="sasmodels.generate.use_single">
229<tt class="descclassname">sasmodels.generate.</tt><tt class="descname">use_single</tt><big>(</big><em>source</em><big>)</big><a class="headerlink" href="#sasmodels.generate.use_single" title="Permalink to this definition">¶</a></dt>
230<dd><p>Convert code from double precision to single precision.</p>
231</dd></dl>
232
233</div>
234</div>
235
236
237      </div>
238      <div class="bottomnav">
239     
240        <p>
241        «&#160;&#160;<a href="exception.html">3.5. Annotate exceptions</a>
242        &#160;&#160;::&#160;&#160;
243        <a class="uplink" href="../index.html">Contents</a>
244        &#160;&#160;::&#160;&#160;
245        <a href="kernelcl.html">3.7. OpenCL model evaluator</a>&#160;&#160;»
246        </p>
247
248      </div>
249
250    <div class="footer">
251        &copy; Copyright 2014, sasview team.
252      Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.3.
253    </div>
254  </body>
255</html>
Note: See TracBrowser for help on using the repository browser.