Skip to content

Commit 3db4c44

Browse files
committed
New matlab engine rework
1 parent e6ae4de commit 3db4c44

19 files changed

+942
-198
lines changed

.github/workflows/run_tests.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
12
name: Run Unit Tests
23

34
on:

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ tmp/
2121
# Build
2222
_build
2323
docs/.buildinfo
24+
matlab.txt
2425
docs/*.inv
2526
*.rat.cpp
2627
*.so

MANIFEST.in

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
include README.md
1+
include README.md matlab.txt
22
recursive-include cpp *
33
prune tests
44
prune */__pycache__
Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
2+
function [output,subRough] = alloyDomains(params,bulkIn,bulkOut,contrast,domain)
3+
4+
% Simple custom model for testing incoherent summing...
5+
% Simple two layer of permalloy / gold, with up/down domains..
6+
7+
% Split up the parameters....
8+
subRough = params(1);
9+
alloyThick = params(2);
10+
alloySLDup = params(3);
11+
alloySLDdn = params(4);
12+
alloyRough = params(5);
13+
goldThick = params(6);
14+
goldSLD = params(7);
15+
goldRough = params(8);
16+
17+
% Make the layers....
18+
alloyUp = [alloyThick, alloySLDup, alloyRough];
19+
alloyDn = [alloyThick, alloySLDdn, alloyRough];
20+
gold = [goldThick, goldSLD, goldRough];
21+
22+
% Make the model dependiong on which domain we are looking at..
23+
if domain==1
24+
output = [alloyUp ; gold];
25+
else
26+
output = [alloyDn ; gold];
27+
end
28+
29+
end

RATapi/examples/domains/domains_custom_layers.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -30,8 +30,8 @@ def domains_custom_layers():
3030
# Add the custom file
3131
problem.custom_files.append(
3232
name="Alloy domains",
33-
filename="alloy_domains.py",
34-
language="python",
33+
filename="alloyDomains.m",
34+
language="matlab",
3535
path=pathlib.Path(__file__).parent,
3636
)
3737

RATapi/examples/normal_reflectivity/DSPC_custom_layers.py

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
import numpy as np
66

77
import RATapi as RAT
8+
import RATapi.wrappers
89

910

1011
def DSPC_custom_layers():
@@ -50,8 +51,8 @@ def DSPC_custom_layers():
5051
# Add the custom file to the project
5152
problem.custom_files.append(
5253
name="DSPC Model",
53-
filename="custom_bilayer_DSPC.py",
54-
language="python",
54+
filename="customBilayerDSPC.m",
55+
language="matlab",
5556
path=pathlib.Path(__file__).parent,
5657
)
5758

@@ -126,5 +127,13 @@ def DSPC_custom_layers():
126127

127128

128129
if __name__ == "__main__":
130+
import RATapi
131+
132+
RATapi.wrappers.get_matlab_engine().editFile((pathlib.Path(__file__).parent / "customBilayerDSPC.m").as_posix())
133+
134+
import time
135+
136+
time.sleep(20)
129137
problem, results = DSPC_custom_layers()
138+
130139
RAT.plotting.plot_ref_sld(problem, results, True)

RATapi/examples/normal_reflectivity/DSPC_function_background.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -146,8 +146,8 @@ def DSPC_function_background():
146146

147147
problem.custom_files.append(
148148
name="D2O Background Function",
149-
filename="background_function.py",
150-
language="python",
149+
filename="backgroundFunction.m",
150+
language="matlab",
151151
path=pathlib.Path(__file__).parent,
152152
)
153153

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
function background = backgroundFunction(xdata,params)
2+
3+
% Split up the params array....
4+
Ao = params(1);
5+
k = params(2);
6+
backConst = params(3);
7+
8+
% Make an exponential decay background....
9+
background = zeros(numel(xdata),1);
10+
for i = 1:numel(xdata)
11+
background(i) = Ao*exp(-k*xdata(i)) + backConst;
12+
end
13+
14+
end
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
function [output,sub_rough] = customBilayerDSPC(params,bulk_in,bulk_out,contrast)
2+
%CUSTOMBILAYER 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+
sub_rough = params(1);
21+
oxide_thick = params(2);
22+
oxide_hydration = params(3);
23+
lipidAPM = params(4);
24+
headHydration = params(5);
25+
bilayerHydration = params(6);
26+
bilayerRough = params(7);
27+
waterThick = params(8);
28+
29+
30+
% We have a constant SLD for the bilayer
31+
oxide_SLD = 3.41e-6;
32+
33+
% Now make the lipid layers..
34+
% Use known lipid volume and compositions
35+
% to make the layers
36+
37+
% define all the neutron b's.
38+
bc = 0.6646e-4; %Carbon
39+
bo = 0.5843e-4; %Oxygen
40+
bh = -0.3739e-4; %Hydrogen
41+
bp = 0.513e-4; %Phosphorus
42+
bn = 0.936e-4; %Nitrogen
43+
bd = 0.6671e-4; %Deuterium
44+
45+
% Now make the lipid groups..
46+
COO = (4*bo) + (2*bc);
47+
GLYC = (3*bc) + (5*bh);
48+
CH3 = (2*bc) + (6*bh);
49+
PO4 = (1*bp) + (4*bo);
50+
CH2 = (1*bc) + (2*bh);
51+
CHOL = (5*bc) + (12*bh) + (1*bn);
52+
53+
% Group these into heads and tails:
54+
Head = CHOL + PO4 + GLYC + COO;
55+
Tails = (34*CH2) + (2*CH3);
56+
57+
% We need volumes for each.
58+
% Use literature values:
59+
vHead = 319;
60+
vTail = 782;
61+
62+
% we use the volumes to calculate the SLD's
63+
SLDhead = Head / vHead;
64+
SLDtail = Tails / vTail;
65+
66+
% We calculate the layer thickness' from
67+
% the volumes and the APM...
68+
headThick = vHead / lipidAPM;
69+
tailThick = vTail / lipidAPM;
70+
71+
% Manually deal with hydration for layers in
72+
% this example.
73+
oxSLD = (oxide_hydration * bulk_out(contrast)) + ((1 - oxide_hydration) * oxide_SLD);
74+
headSLD = (headHydration * bulk_out(contrast)) + ((1 - headHydration) * SLDhead);
75+
tailSLD = (bilayerHydration * bulk_out(contrast)) + ((1 - bilayerHydration) * SLDtail);
76+
77+
% Make the layers
78+
oxide = [oxide_thick oxSLD sub_rough];
79+
water = [waterThick bulk_out(contrast) bilayerRough];
80+
head = [headThick headSLD bilayerRough];
81+
tail = [tailThick tailSLD bilayerRough];
82+
83+
output = [oxide ; water ; head ; tail ; tail ; head];
84+
85+
86+

RATapi/inputs.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@
88
import numpy as np
99

1010
import RATapi
11-
import RATapi.controls
1211
import RATapi.wrappers
1312
from RATapi.rat_core import Checks, Control, NameStore, ProblemDefinition
1413
from RATapi.utils.enums import Calculations, Languages, LayerModels, TypeOptions
@@ -81,9 +80,9 @@ def get_handle(self, index: int):
8180
if custom_file["language"] == Languages.Python:
8281
file_handle = get_python_handle(custom_file["filename"], custom_file["function_name"], custom_file["path"])
8382
elif custom_file["language"] == Languages.Matlab:
84-
file_handle = RATapi.wrappers.MatlabWrapper(full_path).getHandle()
83+
file_handle = RATapi.wrappers.MatlabWrapper(full_path).get_handle()
8584
elif custom_file["language"] == Languages.Cpp:
86-
file_handle = RATapi.wrappers.DylibWrapper(full_path, custom_file["function_name"]).getHandle()
85+
file_handle = RATapi.wrappers.DylibWrapper(full_path, custom_file["function_name"]).get_handle()
8786

8887
return file_handle
8988

0 commit comments

Comments
 (0)