Skip to content
Merged
Show file tree
Hide file tree
Changes from 11 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
130 changes: 130 additions & 0 deletions RATapi/examples/convert_rascal_project/Model_IIb.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,130 @@
function [output,sub_rough] = Model_IIb(params,bulk_in,bulk_out,contrast)
%MODEL_1 RASCAL Custom Layer Model File.
%
%
% This file accepts 3 vectors containing the values for
% Params, bulk in and bulk out
% The final parameter is an index of the contrast being calculated
% The m-file should output a matrix of layer values, in the form..
% Output = [thick 1, SLD 1, Rough 1, Percent Hydration 1, Hydrate how 1
% ....
% thick n, SLD n, Rough n, Percent Hydration n, Hydration how n]
% The "hydrate how" parameter decides if the layer is hydrated with
% Bulk out or Bulk in phases. Set to 1 for Bulk out, zero for Bulk in.
% Alternatively, leave out hydration and just return..
% Output = [thick 1, SLD 1, Rough 1,
% ....
% thick n, SLD n, Rough n] };
% The second output parameter should be the substrate roughness


Roughness = params(1);
APM = params(2);
thickHead = params(3);
theta = params(4);
% vTail = params(4);
% vHead = params(5);

%Make a flag to say which deuteration this is calculating
%[subs head tail]
deut = [0 0 1;
1 0 1;
0 1 0;
1 1 0;
0 1 1;
1 1 1;
1 0 0];


%Neutron b's..
%define all the neutron b's.
bc = 0.6646e-4; %Carbon
bo = 0.5843e-4; %Oxygen
bh = -0.3739e-4; %Hydrogen
bp = 0.513e-4; %Phosphorus
bn = 0.936e-4; %Nitrogen
bd = 0.6671e-4; %Deuterium

%Work out the total scattering length in each fragment....
%Define scattering lengths..
%Hydrogenated version....
COO = (4*bo) + (2*bc);
GLYC = (3*bc) + (5*bh);
CH3 = (2*bc) + (6*bh);
PO4 = (1*bp) + (4*bo);
CH2 = (1*bc) + (2*bh);
CHOL = (5*bc) + (12*bh) + (1*bn);
H2O = (2*bh) + (1*bo);

%..and deuterated...
dGLYC = (3*bc) + (5*bd);
dCH3 = (2*bc) + (6*bd);
dCH2 = (1*bc) + (2*bd);
dCHOL = (5*bc) + (12*bd) + (1*bn);
D2O = (2*bd) + (1*bo);

%And also volumes....
vCH3 = 52.7;
vCH2 = 28.1;
vCOO = 39.0;
vGLYC = 68.8;
vPO4 = 53.7;
vCHOL = 120.4;
vWAT = 30.4;

vHead = vCHOL + vPO4 + vGLYC + 2*vCOO;
vTail = 2*(16*vCH2)+ 2*(vCH3);

%Make the SLD's first...
thisMask = deut(contrast,:);

switch thisMask(1)
case 0
thisWater = (H2O * 0.9249) + (D2O * 0.0871);
case 1
thisWater = D2O;
end

%Calculate mole fraction of D2O from the bulk SLD..
d2o_molfr = (1/D2O-H2O)*((bulk_out(contrast)/0.036182336306)-H2O);
thisWater = (d2o_molfr * D2O) + ((1-d2o_molfr)*H2O);


switch thisMask(2)
case 0
thisHead = CHOL + PO4 + GLYC + COO;
case 1
thisHead = dCHOL + PO4 + GLYC + COO;
end

switch thisMask(3);
case 0
thisTail = (32*CH2) + CH3;
case 1
thisTail = (32*dCH2) + dCH3;
end

noWat = ((thickHead*APM)-vHead)/vWAT;
thisHead = thisHead + noWat*thisWater;
vHead = vHead + noWat*vWAT;


sldHead = thisHead/vHead;
%thickHead = vHead/APM;

sldTail = thisTail/vTail;
%thickTail = vTail/APM;

thickTail = (1.5 + 16*1.265)*cosd(theta);

output = [thickTail sldTail Roughness;
thickHead sldHead Roughness;];


sub_rough = Roughness;






Binary file not shown.
152 changes: 152 additions & 0 deletions RATapi/examples/convert_rascal_project/convert_rascal.ipynb
Original file line number Diff line number Diff line change
@@ -0,0 +1,152 @@
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Convert between RasCAL1 and RAT"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"RasCAL1 (R1) project structs can be converted to RAT `Project` classes, and vice versa.\n",
"This is done via the functions `r1_to_project_class` and `project_class_to_r1`."
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### RasCAL1 to RAT\n",
"Converting from R1 to a `Project` is very simple. We use the example R1 project in the file `R1monolayerVolumeModel.mat`, which is a project for analysing a monolayer of DSPC with various deuterations (tail-deuterated, head-deuterated, fully deuterated, hydrogenated)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Simply give the file path to the function `r1_to_project_class`, and it returns a RAT `Project` that you can use exactly like any other."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from RATapi.utils.convert import r1_to_project_class\n",
"\n",
"project = r1_to_project_class(\"R1monolayerVolumeModel.mat\")\n",
"print(project)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Note that there are various features of RAT which do not feature in R1, such as `prior_type`, `mu` and `sigma` for parameters. These are given sensible default values (again e.g. for parameters, `prior_type = uniform`, `mu = 0.0`, `sigma=inf`), but you may change these if you would like to use these new features:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"project.parameters[\"Head Thickness\"].prior_type = 'gaussian'\n",
"project.parameters[\"Theta\"].mu = 2.0\n",
"project.parameters[\"Area per molecule\"].sigma = 50.0\n",
"# etc...\n",
"print(project.parameters)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Also note that any custom files must be available to RAT. By default, RAT will assume these files are in the same directory that you are running RAT from, but if they are elsewhere you may change the relevant file location: "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# e.g. if our model is in the directory `my_models/`\n",
"project.custom_files[0].filename = \"my_models/Model_IIb.m\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### RAT to RasCAL1\n",
"\n",
"To demonstrate the other way around, we will use the DSPC lipid bilayer model project from another tutorial."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from RATapi.examples import DSPC_standard_layers\n",
"lipid_bilayer_project = DSPC_standard_layers()[0]\n",
"print(lipid_bilayer_project)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"`project_class_to_r1` takes parameters `project` and `filename`, which are the `Project` object and filename for the produced .mat file respectively. This .mat file can then be loaded into RasCAL-1.\n",
"\n",
"Alternatively, if one sets `return_struct=True`, the struct is returned as a Python dictionary instead of being saved.\n",
"\n",
"Note that a MATLAB engine is used to save the project to a .mat file, so the Python library `matlabengine` must be installed."
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"from RATapi.utils.convert import project_class_to_r1\n",
"from pprint import pp # for printing the struct\n",
"\n",
"# save to a file called lipid_bilayer.mat\n",
"project_class_to_r1(lipid_bilayer_project, filename=\"lipid_bilayer.mat\")\n",
"\n",
"# return as a Python dictionary\n",
"struct = project_class_to_r1(lipid_bilayer_project, return_struct=True)\n",
"pp(struct)"
]
}
],
"metadata": {
"kernelspec": {
"display_name": ".venv",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.10.12"
}
},
"nbformat": 4,
"nbformat_minor": 2
}
21 changes: 21 additions & 0 deletions RATapi/examples/convert_rascal_project/convert_rascal.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
from pprint import pp

from RATapi.examples import DSPC_standard_layers
from RATapi.utils.convert import project_class_to_r1, r1_to_project_class

# convert R1 project to Project class
project = r1_to_project_class("R1monolayerVolumeModel.mat")
print(project)

# change values if you like, including ones not supported by R1
project.parameters["Head Thickness"].prior_type = "gaussian"
project.parameters["Theta"].mu = 2.0
project.parameters["Area per molecule"].sigma = 50.0

# convert DSPC standard layers example to a struct and save as file
lipid_bilayer_project = DSPC_standard_layers()[0]
project_class_to_r1(lipid_bilayer_project, filename="lipid_bilayer.mat")

# convert and return as a Python dictionary
struct = project_class_to_r1(lipid_bilayer_project, return_struct=True)
pp(struct)
Loading