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 — 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 | «  <a href="exception.html">3.5. Annotate exceptions</a> |
---|
41 |   ::   |
---|
42 | <a class="uplink" href="../index.html">Contents</a> |
---|
43 |   ::   |
---|
44 | <a href="kernelcl.html">3.7. OpenCL model evaluator</a>  Â» |
---|
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 |
---|
59 | particular dimensions averaged over all orientations.</p> |
---|
60 | <p><em>Iqxy(qx, qy, p1, p2, ...)</em> returns the scattering at qx,qy for a form |
---|
61 | with particular dimensions for a single orientation.</p> |
---|
62 | <p><em>Imagnetic(qx, qy, result[], p1, p2, ...)</em> returns the scattering for the |
---|
63 | polarized neutron spin states (up-up, up-down, down-up, down-down) for |
---|
64 | a 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 |
---|
66 | dimension.</p> |
---|
67 | <p><em>ER(p1, p2, ...)</em> returns the effective radius of the form with |
---|
68 | particular 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 |
---|
72 | set of .c files. The model constructor will use them to create models with |
---|
73 | polydispersity across volume and orientation parameters, and provide |
---|
74 | scale 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 |
---|
76 | functions written for OpenCL. All functions need prototype declarations |
---|
77 | even 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 |
---|
79 | to be given as part of the metadata in the kernel module definition. |
---|
80 | The included files should be listed using a path relative to the kernel |
---|
81 | module, or if using “lib/file.c” if it is one of the standard includes |
---|
82 | provided with the sasmodels source. The includes need to be listed in |
---|
83 | order 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 |
---|
85 | calculations, <em>double</em> will be replaced by <em>float</em>. The single precision |
---|
86 | conversion will also tag floating point constants with “f” to make them |
---|
87 | single precision constants. When using integral values in floating point |
---|
88 | expressions, they should be expressed as floating point values by including |
---|
89 | a decimal point. This includes 0., 1. and 2.</p> |
---|
90 | <p>OpenCL has a <em>sincos</em> function which can improve performance when both |
---|
91 | the <em>sin</em> and <em>cos</em> values are needed for a particular argument. Since |
---|
92 | this function does not exist in C99, all use of <em>sincos</em> should be |
---|
93 | replaced by the macro <em>SINCOS(value,sn,cn)</em> where <em>sn</em> and <em>cn</em> are |
---|
94 | previously declared <em>double</em> variables. When compiled for systems without |
---|
95 | OpenCL, <em>SINCOS</em> will be replaced by <em>sin</em> and <em>cos</em> calls. If <em>value</em> is |
---|
96 | an expression, it will appear twice in this case; whether or not it will be |
---|
97 | evaluated twice depends on the quality of the compiler.</p> |
---|
98 | <p>If the input parameters are invalid, the scattering calculator should |
---|
99 | return a negative number. Particularly with polydispersity, there are |
---|
100 | some sets of shape parameters which lead to nonsensical forms, such |
---|
101 | as a capped cylinder where the cap radius is smaller than the |
---|
102 | cylinder radius. The polydispersity calculation will ignore these points, |
---|
103 | effectively chopping the parameter weight distributions at the boundary |
---|
104 | of the infeasible region. The resulting scattering will be set to |
---|
105 | background. 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. |
---|
107 | The constructor code will generate the necessary vectors for computing |
---|
108 | them with the desired polydispersity.</p> |
---|
109 | <p>The available kernel parameters are defined as a list, with each parameter |
---|
110 | defined 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 |
---|
113 | function and the name that will be displayed to the user. Names |
---|
114 | should be lower case, with words separated by underscore. If |
---|
115 | acronyms 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 |
---|
119 | is 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 |
---|
121 | the polydispersity density function. In the fit, the parameter limits |
---|
122 | given to the fit are the limits on the central value of the parameter. |
---|
123 | If there is polydispersity, it will evaluate parameter values outside |
---|
124 | the fit limits, but not outside the hard limits specified in the model. |
---|
125 | If there are no limits, use +/-inf imported from numpy.</p> |
---|
126 | <p><em>type</em> indicates how the parameter will be used. “volume” parameters |
---|
127 | will be used in all functions. “orientation” parameters will be used |
---|
128 | in <em>Iqxy</em> and <em>Imagnetic</em>. “magnetic* parameters will be used in |
---|
129 | <em>Imagnetic</em> only. If <em>type</em> is the empty string, the parameter will |
---|
130 | be 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 |
---|
132 | be displayed in the parameter table and used as a tool tip for the |
---|
133 | parameter 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, |
---|
139 | or 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 |
---|
141 | while the model parameters are being edited.</p> |
---|
142 | <p><em>parameters</em> is the list of parameters. Parameters in the kernel |
---|
143 | functions must appear in the same order as they appear in the |
---|
144 | parameters list. Two additional parameters, <em>scale</em> and <em>background</em> |
---|
145 | are added to the beginning of the parameter list. They will show up |
---|
146 | in the documentation as model parameters, but they are never sent to |
---|
147 | the kernel functions.</p> |
---|
148 | <p><em>source</em> is the list of C-99 source files that must be joined to |
---|
149 | create the OpenCL kernel functions. The files defining the functions |
---|
150 | need 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 |
---|
152 | not present, the effective radius is 0.</p> |
---|
153 | <p><em>VR</em> is a python function defining the volume ratio. If it is not |
---|
154 | present, 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 |
---|
156 | C source code for the body of the volume, Iq, and Iqxy functions |
---|
157 | respectively. 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 |
---|
159 | kernel. If they are marked as <em>Iq.vectorized = True</em> then the |
---|
160 | kernel is passed the entire <em>q</em> vector at once, otherwise it is |
---|
161 | passed values one <em>q</em> at a time. The performance improvement of |
---|
162 | this step is significant.</p> |
---|
163 | </div></blockquote> |
---|
164 | <p>An <em>info</em> dictionary is constructed from the kernel meta data and |
---|
165 | returned to the caller.</p> |
---|
166 | <p>Additional fields can be defined in the kernel definition file that |
---|
167 | are not needed for sas modelling.</p> |
---|
168 | <blockquote> |
---|
169 | <div><p><em>demo</em> is a dictionary of parameter=value defining a set of |
---|
170 | parameters 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 |
---|
172 | was split into its own package, and <em>oldpars</em> is a dictionary |
---|
173 | of <em>parameter: old_parameter</em> pairs defining the new names for |
---|
174 | the parameters. This is used by <em>compare</em> to check the values |
---|
175 | of the new model against the values of the old model before |
---|
176 | you 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, |
---|
179 | followed by the loop value/weight vector, followed by the values for |
---|
180 | the non-polydisperse parameters, followed by the lengths of the |
---|
181 | polydispersity loops. To construct the call for 1D models, the |
---|
182 | categories <em>fixed-1d</em> and <em>pd-1d</em> list the names of the parameters |
---|
183 | of the non-polydisperse and the polydisperse parameters respectively. |
---|
184 | Similarly, <em>fixed-2d</em> and <em>pd-2d</em> provide parameter names for 2D models. |
---|
185 | The <em>pd-rel</em> category is a set of those parameters which give |
---|
186 | polydispersitiy as a portion of the value (so a 10% length dispersity |
---|
187 | would use a polydispersity value of 0.1) rather than absolute |
---|
188 | dispersity 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 |
---|
190 | to volume within the kernel (used for volume normalization) and for |
---|
191 | calls 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 |
---|
193 | magnetic parameters. These are used by the sasview interface. The |
---|
194 | blank category is for parameters such as scale which don’t have any |
---|
195 | other marking.</p> |
---|
196 | <p>The doc string at the start of the kernel module will be used to |
---|
197 | construct the model documentation web pages. Embedded figures should |
---|
198 | appear in the subdirectory “img” beside the model definition, and tagged |
---|
199 | with the kernel module name to avoid collision with other models. Some |
---|
200 | file systems are case-sensitive, so only use lower case characters for |
---|
201 | file 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 |
---|
203 | the 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 |
---|
204 | and 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> |
---|
205 | returns 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 |
---|
211 | know which module you need, or with __import__(‘sasmodels.model.’+name) |
---|
212 | if 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 | «  <a href="exception.html">3.5. Annotate exceptions</a> |
---|
242 |   ::   |
---|
243 | <a class="uplink" href="../index.html">Contents</a> |
---|
244 |   ::   |
---|
245 | <a href="kernelcl.html">3.7. OpenCL model evaluator</a>  Â» |
---|
246 | </p> |
---|
247 | |
---|
248 | </div> |
---|
249 | |
---|
250 | <div class="footer"> |
---|
251 | © Copyright 2014, sasview team. |
---|
252 | Created using <a href="http://sphinx-doc.org/">Sphinx</a> 1.2.3. |
---|
253 | </div> |
---|
254 | </body> |
---|
255 | </html> |
---|