Skip to content

Commit 975b332

Browse files
committed
Another try
1 parent 10d3222 commit 975b332

File tree

8 files changed

+396
-116
lines changed

8 files changed

+396
-116
lines changed
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.resolve(),
3636
)
3737

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.resolve(),
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/wrappers.py

Lines changed: 35 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
import pathlib
44
import platform
55
import shutil
6+
import subprocess
67
from typing import Callable
78

89
import numpy as np
@@ -14,33 +15,39 @@
1415
MATLAB_PATH_FILE = os.path.join(os.path.dirname(os.path.realpath(__file__)), "matlab.txt")
1516

1617

17-
def set_matlab_path(path):
18-
if not path:
18+
def set_matlab_path(matlab_path):
19+
if not matlab_path:
1920
return
2021

21-
path = pathlib.Path(path)
22-
if not path.is_dir():
23-
path = path.parent
22+
if platform.system() == "Windows":
23+
process = subprocess.Popen(f'"{matlab_path}" -batch "comserver(\'register\')"')
24+
process.wait()
25+
26+
27+
with open(MATLAB_PATH_FILE, "w") as path_file:
28+
path_file.write(matlab_path)
2429

25-
if path.stem != 'bin':
26-
path = path / 'bin'
2730

31+
def get_matlab_paths(exe_path):
32+
if not exe_path:
33+
raise FileNotFoundError()
34+
35+
bin_path = pathlib.Path(exe_path).parent
36+
if bin_path.stem != 'bin':
37+
raise FileNotFoundError()
38+
2839
if platform.system() == "Windows":
2940
arch = "win64"
3041
elif platform.system() == "Darwin":
31-
arch = "maci64" if (path / "maci64").exists() else "maca64"
42+
arch = "maci64" if (bin_path / "maci64").exists() else "maca64"
3243
else:
3344
arch = "glnxa64"
3445

35-
path = path / arch
36-
if not path.exists():
37-
raise FileNotFoundError(f"The expected MATLAB folders were in found at the path: {path}")
38-
39-
with open(MATLAB_PATH_FILE, "w") as path_file:
40-
path_file.write(path.as_posix())
41-
42-
return path.as_posix()
46+
dll_path = bin_path / arch
47+
if not dll_path.exists():
48+
raise FileNotFoundError(f"The expected MATLAB folders were in found at the path: {dll_path}")
4349

50+
return f'{bin_path.as_posix()}/' , f'{dll_path.as_posix()}/'
4451

4552
def start_matlab():
4653
"""Start MATLAB asynchronously and returns a future to retrieve the engine later.
@@ -58,19 +65,25 @@ def start_matlab():
5865
matlab_path = ""
5966

6067
if not matlab_path:
61-
matlab_path = set_matlab_path(shutil.which("matlab"))
68+
matlab_path = shutil.which("matlab")
6269
if matlab_path is None:
6370
matlab_path = ""
64-
65-
if matlab_path:
66-
os.environ["PATH"] += os.pathsep + matlab_path
71+
else:
72+
temp = pathlib.Path(matlab_path)
73+
if temp.is_symlink():
74+
matlab_path = temp.readlink().as_posix()
75+
set_matlab_path(matlab_path)
76+
77+
if matlab_path:
78+
bin_path, dll_path = get_matlab_paths(matlab_path)
79+
os.environ["MATLAB_DLL_PATH"] = dll_path
80+
os.environ["PATH"] = bin_path + os.pathsep + os.environ["PATH"]
81+
os.environ["PATH"] = dll_path + os.pathsep + os.environ["PATH"]
6782
engine = RATapi.rat_core.MatlabEngine()
68-
engine.start()
6983

7084
return engine
7185

7286

73-
7487
class MatlabWrapper:
7588
"""Creates a python callback for a MATLAB function.
7689

0 commit comments

Comments
 (0)