Skip to content

Commit 730fe29

Browse files
authored
Adds conversion between R1 .mat files and Python Projects (#55)
* working r1-to-rat converter and tests * adds project class to r1 converter * project to r1 now functional * fixed various issues - tests all now pass * added scipy to setup.py and requirements * added test data * removed errant comment * removed case sensitivity workaround * added conversion tutorial * review fixes and tutorials added * set to skip for now * review fixes * added Python custom file * review fixes * fixed circular import
1 parent f6bf9e9 commit 730fe29

File tree

15 files changed

+1844
-2
lines changed

15 files changed

+1844
-2
lines changed

RATapi/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,6 @@
44
from RATapi.controls import Controls
55
from RATapi.project import Project
66
from RATapi.run import run
7-
from RATapi.utils import plotting
7+
from RATapi.utils import convert, plotting
88

9-
__all__ = ["examples", "models", "events", "ClassList", "Controls", "Project", "run", "plotting"]
9+
__all__ = ["examples", "models", "events", "ClassList", "Controls", "Project", "run", "plotting", "convert"]

RATapi/examples/__init__.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from RATapi.examples.absorption.absorption import absorption
2+
from RATapi.examples.convert_rascal_project import convert_rascal
23
from RATapi.examples.domains.domains_custom_layers import domains_custom_layers
34
from RATapi.examples.domains.domains_custom_XY import domains_custom_XY
45
from RATapi.examples.domains.domains_standard_layers import domains_standard_layers
@@ -14,4 +15,5 @@
1415
"DSPC_custom_layers",
1516
"DSPC_custom_XY",
1617
"DSPC_standard_layers",
18+
"convert_rascal",
1719
]
Lines changed: 130 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,130 @@
1+
function [output,sub_rough] = Model_IIb(params,bulk_in,bulk_out,contrast)
2+
%MODEL_1 RASCAL Custom Layer Model File.
3+
%
4+
%
5+
% This file accepts 3 vectors containing the values for
6+
% Params, bulk in and bulk out
7+
% The final parameter is an index of the contrast being calculated
8+
% The m-file should output a matrix of layer values, in the form..
9+
% Output = [thick 1, SLD 1, Rough 1, Percent Hydration 1, Hydrate how 1
10+
% ....
11+
% thick n, SLD n, Rough n, Percent Hydration n, Hydration how n]
12+
% The "hydrate how" parameter decides if the layer is hydrated with
13+
% Bulk out or Bulk in phases. Set to 1 for Bulk out, zero for Bulk in.
14+
% Alternatively, leave out hydration and just return..
15+
% Output = [thick 1, SLD 1, Rough 1,
16+
% ....
17+
% thick n, SLD n, Rough n] };
18+
% The second output parameter should be the substrate roughness
19+
20+
21+
Roughness = params(1);
22+
APM = params(2);
23+
thickHead = params(3);
24+
theta = params(4);
25+
% vTail = params(4);
26+
% vHead = params(5);
27+
28+
%Make a flag to say which deuteration this is calculating
29+
%[subs head tail]
30+
deut = [0 0 1;
31+
1 0 1;
32+
0 1 0;
33+
1 1 0;
34+
0 1 1;
35+
1 1 1;
36+
1 0 0];
37+
38+
39+
%Neutron b's..
40+
%define all the neutron b's.
41+
bc = 0.6646e-4; %Carbon
42+
bo = 0.5843e-4; %Oxygen
43+
bh = -0.3739e-4; %Hydrogen
44+
bp = 0.513e-4; %Phosphorus
45+
bn = 0.936e-4; %Nitrogen
46+
bd = 0.6671e-4; %Deuterium
47+
48+
%Work out the total scattering length in each fragment....
49+
%Define scattering lengths..
50+
%Hydrogenated version....
51+
COO = (4*bo) + (2*bc);
52+
GLYC = (3*bc) + (5*bh);
53+
CH3 = (2*bc) + (6*bh);
54+
PO4 = (1*bp) + (4*bo);
55+
CH2 = (1*bc) + (2*bh);
56+
CHOL = (5*bc) + (12*bh) + (1*bn);
57+
H2O = (2*bh) + (1*bo);
58+
59+
%..and deuterated...
60+
dGLYC = (3*bc) + (5*bd);
61+
dCH3 = (2*bc) + (6*bd);
62+
dCH2 = (1*bc) + (2*bd);
63+
dCHOL = (5*bc) + (12*bd) + (1*bn);
64+
D2O = (2*bd) + (1*bo);
65+
66+
%And also volumes....
67+
vCH3 = 52.7;
68+
vCH2 = 28.1;
69+
vCOO = 39.0;
70+
vGLYC = 68.8;
71+
vPO4 = 53.7;
72+
vCHOL = 120.4;
73+
vWAT = 30.4;
74+
75+
vHead = vCHOL + vPO4 + vGLYC + 2*vCOO;
76+
vTail = 2*(16*vCH2)+ 2*(vCH3);
77+
78+
%Make the SLD's first...
79+
thisMask = deut(contrast,:);
80+
81+
switch thisMask(1)
82+
case 0
83+
thisWater = (H2O * 0.9249) + (D2O * 0.0871);
84+
case 1
85+
thisWater = D2O;
86+
end
87+
88+
%Calculate mole fraction of D2O from the bulk SLD..
89+
d2o_molfr = (1/D2O-H2O)*((bulk_out(contrast)/0.036182336306)-H2O);
90+
thisWater = (d2o_molfr * D2O) + ((1-d2o_molfr)*H2O);
91+
92+
93+
switch thisMask(2)
94+
case 0
95+
thisHead = CHOL + PO4 + GLYC + COO;
96+
case 1
97+
thisHead = dCHOL + PO4 + GLYC + COO;
98+
end
99+
100+
switch thisMask(3);
101+
case 0
102+
thisTail = (32*CH2) + CH3;
103+
case 1
104+
thisTail = (32*dCH2) + dCH3;
105+
end
106+
107+
noWat = ((thickHead*APM)-vHead)/vWAT;
108+
thisHead = thisHead + noWat*thisWater;
109+
vHead = vHead + noWat*vWAT;
110+
111+
112+
sldHead = thisHead/vHead;
113+
%thickHead = vHead/APM;
114+
115+
sldTail = thisTail/vTail;
116+
%thickTail = vTail/APM;
117+
118+
thickTail = (1.5 + 16*1.265)*cosd(theta);
119+
120+
output = [thickTail sldTail Roughness;
121+
thickHead sldHead Roughness;];
122+
123+
124+
sub_rough = Roughness;
125+
126+
127+
128+
129+
130+
Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,84 @@
1+
from math import cos, radians
2+
3+
4+
def Model_IIb(params, bulk_in, bulk_out, contrast):
5+
# converted from matlab file Model_IIb.m
6+
7+
Roughness, APM, thickHead, theta = params
8+
9+
# Make a flag to say which deuteration this is calculating
10+
# [subs head tail]
11+
deut = [[0, 0, 1], [1, 0, 1], [0, 1, 0], [1, 1, 0], [0, 1, 1], [1, 1, 1], [1, 0, 0]]
12+
13+
bc = 0.6646e-4 # Carbon
14+
bo = 0.5843e-4 # Oxygen
15+
bh = -0.3739e-4 # Hydrogen
16+
bp = 0.513e-4 # Phosphorus
17+
bn = 0.936e-4 # Nitrogen
18+
bd = 0.6671e-4 # Deuterium
19+
20+
# Work out the total scattering length in each fragment
21+
# for hydrogenated:
22+
COO = (4 * bo) + (2 * bc)
23+
GLYC = (3 * bc) + (5 * bh)
24+
CH3 = (2 * bc) + (6 * bh)
25+
PO4 = (1 * bp) + (4 * bo)
26+
CH2 = (1 * bc) + (2 * bh)
27+
CHOL = (5 * bc) + (12 * bh) + (1 * bn)
28+
H2O = (2 * bh) + (1 * bo)
29+
30+
# for deuterated:
31+
dCH3 = (2 * bc) + (6 * bd)
32+
dCH2 = (1 * bc) + (2 * bd)
33+
dCHOL = (5 * bc) + (12 * bd) + (1 * bn)
34+
D2O = (2 * bd) + (1 * bo)
35+
36+
# for volumes:
37+
vCH3 = 52.7
38+
vCH2 = 28.1
39+
vCOO = 39.0
40+
vGLYC = 68.8
41+
vPO4 = 53.7
42+
vCHOL = 120.4
43+
vWAT = 30.4
44+
45+
vHead = vCHOL + vPO4 + vGLYC + 2 * vCOO
46+
vTail = 2 * (16 * vCH2) + 2 * (vCH3)
47+
48+
# make SLDs
49+
thisMask = deut[contrast]
50+
51+
if thisMask[0] == 0:
52+
thisWater = (H2O * 0.9249) + (D2O * 0.0871)
53+
else:
54+
thisWater = D2O
55+
56+
# Calculate mole fraction of D2O from the bulk SLD
57+
d2o_molfr = (1 / D2O - H2O) * ((bulk_out[contrast] / 0.036182336306) - H2O)
58+
thisWater = (d2o_molfr * D2O) + ((1 - d2o_molfr) * H2O)
59+
60+
if thisMask[1] == 0:
61+
thisHead = CHOL + PO4 + GLYC + COO
62+
else:
63+
thisHead = dCHOL + PO4 + GLYC + COO
64+
65+
if thisMask[2] == 0:
66+
thisTail = (32 * CH2) + CH3
67+
else:
68+
thisTail = (32 * dCH2) + dCH3
69+
70+
noWat = ((thickHead * APM) - vHead) / vWAT
71+
thisHead = thisHead + noWat * thisWater
72+
vHead = vHead + noWat * vWAT
73+
74+
sldHead = thisHead / vHead
75+
76+
sldTail = thisTail / vTail
77+
78+
thickTail = (1.5 + 16 * 1.265) * cos(radians(theta))
79+
80+
output = [[thickTail, sldTail, Roughness], [thickHead, sldHead, Roughness]]
81+
82+
sub_rough = Roughness
83+
84+
return output, sub_rough
Binary file not shown.

RATapi/examples/convert_rascal_project/__init__.py

Whitespace-only changes.

0 commit comments

Comments
 (0)