Alternative Custom Model Languages#
In this example, we setup a custom layers problem using three different programming language (MATLAB, Python, and C++) to write the custom model function. The provided C++ function needs to be compiled into a dynamic library using instructions given in Custom Models in Python or C++ or instructions specific to your compiler.
This example can be run using the instructions below.
Note
The custom models used are - examples/miscellaneous/languages/alloyDomains.m, examples/miscellaneous/languages/alloyDomains.m, examples/miscellaneous/languages/alloyDomains.m.
Warning
For Python custom functions, you will need to setup the python environment for MATLAB, see Setup MATLAB to use Python
Run Script:
root = getappdata(0, 'root');
cd(fullfile(root, 'examples', 'miscellaneous', 'alternativeLanguages'));
customModelLanguagesScript
Run Interactively:
root = getappdata(0, 'root');
cd(fullfile(root, 'examples', 'miscellaneous', 'alternativeLanguages'));
edit customModelLanguagesSheet.mlx
Custom Layers Example for Supported DSPC layer.
Example of using alternative languages to make a Custom layers to model a DSPC supportd bilayer.
First, we'll run the project using a conventional Matlab custom model. Then, we will do the same calculation using the same model, but written in Python or C++.
1. Normal Matlab Custom Model.
Start by making the class and setting it to a custom layers type:
problem = createProject(name='Orso lipid example - custom layers', ...
model=modelTypes.CustomLayers.value, ...
geometry=geometryOptions.SubstrateLiquid.value);
First we need to set up a parameters group. We will be using a pre-prepared custom model file, (at the end of the worksheet). Use this to set up the parameters block...
We need to add the relevant parameters we are going to need to define the model (note that Substrate Roughness' always exists as parameter 1..
Parameters = {
% Name min val max fit?
{'Oxide thick', 5, 20, 60, true };
{'Oxide Hydration' 0, 0.2, 0.5, true };
{'Lipid APM' 45 55 65 true };
{'Head Hydration' 0 0.2 0.5 true };
{'Bilayer Hydration' 0 0.1 0.2 true };
{'Bilayer Roughness' 2 4 8 true };
{'Water Thickness' 0 2 10 true };
};
problem.addParameterGroup(Parameters);
problem.setParameter(1,'min',1,'max',10); % Change the substrate roughness limits
Need to add the relevant Bulk SLD's. Change the bulk in from air to silicon, and add two additional water contrasts:
% Change bulk in from air to silicon....
problem.setBulkIn(1,'name','Silicon','min',2.07e-6,'value',2.073e-6,'max',2.08e-6,'fit',false);
% Add two more values for bulk out....
problem.addBulkOut('SLD SMW',1e-6,2.073e-6,3e-6,true);
problem.addBulkOut('SLD H2O',-0.6e-6,-0.56e-6,-0.3e-6,true);
problem.setBulkOut(1,'fit',true,'min',5e-6);
Now add the datafiles. We have three datasets we need to consider - the bilayer against D2O, Silicon Matched water and H2O. Load these datafiles in and put them in the data block....
% Read in the datafiles
root = getappdata(0, 'root');
D2O_data = dlmread(fullfile(root, '/examples/normalReflectivity/customLayers/c_PLP0016596.dat'));
SMW_data = dlmread(fullfile(root, '/examples/normalReflectivity/customLayers/c_PLP0016601.dat'));
H2O_data = dlmread(fullfile(root, '/examples/normalReflectivity/customLayers/c_PLP0016607.dat'));
% Add the data to the project
problem.addData('Bilayer / D2O', D2O_data(:,1:3));
problem.addData('Bilayer / SMW', SMW_data(:,1:3));
problem.addData('Bilayer / H2O', H2O_data(:,1:3));
problem.setData(2,'dataRange',[0.013 0.37]);
problem.setData(3,'dataRange',[0.013 0.32996]);
problem.setData(4,'dataRange',[0.013 0.3048]);
Add the custom file to the project....
% name filename language path
problem.addCustomFile('DSPC Model', 'customBilayer.m', supportedLanguages.Matlab, pwd);
Also, add the relevant background parameters - one each for each contrast:
% Change the name of the existing parameters to refer to D2O
problem.setBackgroundParam(1,'name','Backs par D2O','fit',true,'min',1e-10,'max',1e-5,'value',1e-6);
% Add two new backs parameters for the other two..
problem.addBackgroundParam('Backs par SMW',1e-10,1e-6,1e-5,true);
problem.addBackgroundParam('Backs par H2O',1e-10,1e-6,1e-5,true);
% And add the two new constant backgrounds..
problem.addBackground('Background SMW','constant','Backs par SMW');
problem.addBackground('Background H2O','constant','Backs par H2O');
% And edit the other one....
problem.setBackground(1,'name','Background D2O','source','Backs par D2O');
% Finally modify some of the other parameters to be more suitable values
% for a solid / liquid experiment.
% Set the scalefactor...
problem.setScalefactor(1,'Value',1,'min',0.5,'max',2,'fit',true);
% Finally modify some of the other parameters to be more suitable values
% for a solid / liquid experiment.
% Set the scalefactor...
problem.setScalefactor(1,'Value',1,'min',0.5,'max',2,'fit',true);
Now add the three contrasts as before:
% D2O contrast..
problem.addContrast('name', 'Bilayer / D2O',...
'background', 'Background D2O',...
'resolution', 'Resolution 1',...
'scalefactor', 'Scalefactor 1',...
'BulkOut', 'SLD D2O',...
'BulkIn', 'Silicon',...
'data', 'Bilayer / D2O',...
'model', 'DSPC Model');
% SMW contrast..
problem.addContrast('name', 'Bilayer / SMW',...
'background', 'Background SMW',...
'resolution', 'Resolution 1',...
'scalefactor', 'Scalefactor 1',...
'BulkOut', 'SLD SMW',...
'BulkIn', 'Silicon',...
'data', 'Bilayer / SMW',...
'model', 'DSPC Model');
% SMW contrast..
problem.addContrast('name', 'Bilayer / H2O',...
'background', 'Background H2O',...
'resolution', 'Resolution 1',...
'scalefactor', 'Scalefactor 1',...
'BulkOut', 'SLD H2O',...
'BulkIn', 'Silicon',...
'data', 'Bilayer / H2O',...
'model', 'DSPC Model');
Look at the complete model definition before sending it to RAT;
disp(problem)
Make a controls block....
controls = controlsClass();
controls.procedure = 'DE';
controls.display = 'final';
controls.parallel = 'contrasts';
And send this to RAT...
[problem,results] = RAT(problem,controls);
plotRefSLD(problem,results);
Using a Python Custom Model.
RAT also allows the use of Python custom models (instead of Matlab). The purpose of this is because it is more logical to work with Python custom functions when working with the Python API for RAT, but also since there may be existing libraries in Python which the user might want to use in a custom model.
The format of the custom model is very similar to the Matlab version. In this example, our model is called customBilayer.py..
type customBilayer.py
We add this to the project in exactly the same way as a Matlab custom model....
problem.addCustomFile('pyDSPC','customBilayer.py','Python',pwd);
...then set the models of our contrasts accordingly....
for i = 1:3
problem.setContrast(i,'model', 'pyDSPC');
end