Changes between Initial Version and Version 1 of DevNotes/Obsolete/SASNewModel


Ignore:
Timestamp:
Aug 16, 2012 4:39:47 AM (12 years ago)
Author:
kieranrcampbell
Comment:

Legend:

Unmodified
Added
Removed
Modified
  • DevNotes/Obsolete/SASNewModel

    v1 v1  
     1= Creating New Models in Sansview = 
     2 
     3This document provides an introduction for creating new scattering models in the software Sansview. For the project homepage visit http://sourceforge.net/projects/sansviewproject/ 
     4 
     5 
     6== Architecture == 
     7 
     8Most of Sansview including the GUI, plotting and fitting functions is written in python. Due to well known performance issues it makes sense to write any models involved in heavy computation in a faster language, here both C and C++. In sansview the interface between the Python and C++ is done with the ctypes library, though in practice we hardly need to deal with this. 
     9 
     10 
     11== Writing your C++ Model == 
     12 
     13Here we discuss only the basics of getting a model to work; for a more detailed tutorial on computation of form factors see the sans toolbox at http://www.ncnr.nist.gov/staff/hammouda/the_SANS_toolbox.pdf or many other good online tutorials. 
     14 
     15To begin writing the model, check out the project trunk on subversion using the link to sourceforge at the top of the page. Assuming you install it into your home directory ~/ (which in practice is a bad idea), check the project builds okay by running 
     16 
     17        {{{~/$ sudo python setup.py install}}} 
     18 
     19To check this has worked worked, or indeed at any time run your own version of sansview do 
     20 
     21        {{{~/$ python sansview/sansview.py}}} 
     22 
     23Our C++ model takes the form of a class that naturally has a a header file and body. Here we call our model testModel that correspondingly has testModel.h and ''testModel.cpp''. We place the header file in ~/sansmodels/include/ and the source file in ~/sansmodels/src/c_models/ .  
     24 
     25We begin with the preprocessor directives for ''testModel.h''. The usual #ifndef....#define....#endif are of course required as usual for header files. 
     26 
     27{{{#include "parameters.hh"}}} 
     28 
     29Parameters.hh holds the class definition for the Parameter class, the basic representation of any parameter in your form factor. We then fill out a few lines of commented code that are required by the wrapper class to understand what your model does. 
     30{{{ 
     31//[PYTHONCLASS] = testModel 
     32//[DISP_PARAMS] = myparam 
     33//[DESCRIPTION] =<text> test class for sansview </text> 
     34}}} 
     35 
     36PYTHONCLASS and DESCRIPTION are both self explanatory. The DISP_PARAMS section should include a list of the parameters your model uses. Here we define one – myparam. 
     37 
     38Next comes the class declaration itself. 
     39 
     40{{{ 
     41class testModel { 
     42 
     43public: 
     44  Parameter myparam; 
     45 
     46  testModel(); 
     47 
     48  // Operators to get I(Q) 
     49  double operator()(double q); 
     50  double operator()(double qx,double qy); 
     51 
     52  double evaluate_rphi(double q, double phi); 
     53  double calculate_ER(); 
     54  double calculate_VR(); 
     55 
     56}; 
     57 
     58}}} 
     59 
     60As you will see we have the declaration of our parameter, followed by the constructor. Next are the 5 methods required by the wrapper function in order to generate a proper model. The first two operators represent the one and two dimensional form factor functions respectively. Next come the methods for evaluating the rphi function and calculating the effective radius. 
     61 
     62Next onto ''testModel.cpp'' implementing these functions. First of course comes the includes (don't forget ''testModel.h''. 
     63 
     64{{{ 
     65 
     66#include <math.h> 
     67#include "parameters.hh" 
     68#include "testModel.h" 
     69}}} 
     70 
     71''Math.h'' is of course an optional import but is pretty necessary for most form factor calculations.  
     72 
     73Then we get on to the body of the functions which is implemented in the most trivial way possible. 
     74 
     75{{{ 
     76testModel::testModel() { 
     77  myparam = Parameter(1.0); 
     78} 
     79double testModel::operator()(double q) { 
     80  return 1.0; 
     81} 
     82double testModel::operator()(double qx, double qy) { 
     83  return 1.0; 
     84} 
     85 
     86double testModel::evaluate_rphi(double q, double phi) { 
     87  return 1.0; 
     88} 
     89 
     90double testModel::calculate_ER() { 
     91  return 1.0; 
     92} 
     93 
     94double testModel::calculate_VR() { 
     95  return 1.0; 
     96} 
     97 
     98}}} 
     99 
     100Now we have all the code we need to create a flat line across our screen at 1.0 in the model plot. 
     101 
     102 
     103== Incorporating your Model in Sansview == 
     104 
     105 
     106The wrapper for python will create 3 separate chunks of code –''CtestModel.cpp, CtestModel.py and testModel.py''– this being what is actually called from Sansview. Make sure your ''testModel.cpp'' is placed correctly in sansmodels/src/c_models/ and the Sansview installation process will pick it up and do all the wrapping automatically.  
     107 
     108Next, head over to the file 
     109 
     110        fittingview/src/sans/perspectives/fitting/models.py 
     111 
     112This is where the model is actually added to the selection list for modelling and gui. In the class ModelManagerBase exists a method called _getModelList. In the relevant part of the method (you will easily see where) add 
     113 
     114{{{ 
     115from sans.models.testModel import testModel 
     116self.shape_list.append(testModel) 
     117self.model_name_list.append(testModel.__name__) 
     118}}} 
     119 
     120And that's it! A model called testModel should appear in the drop down menu in fittingview under 'Shapes'. Change the middle line above suitably if you would prefer it to appear under a different category.