Skip to content

Commit

Permalink
Tidies up examples
Browse files Browse the repository at this point in the history
  • Loading branch information
DrPaulSharp committed Jun 19, 2024
1 parent fd59591 commit 29795d8
Show file tree
Hide file tree
Showing 17 changed files with 139 additions and 227 deletions.
15 changes: 7 additions & 8 deletions RAT/examples/absorption/absorption.py
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
"""Custom layers model including absorption"""

import RAT
import RAT.utils.plotting
import RAT.rat_core
import numpy as np

problem = RAT.Project(name="Absorption example", calculation="non polarised", model="custom layers",
geometry="substrate/liquid", absorption=True)

# Add the required parameters (substrate roughness as already there by default)
# Add the required parameters (substrate roughness is already there by default)
problem.parameters.append(name="Alloy Thickness", min=100.0, value=135.6, max=200.0, fit=True)
problem.parameters.append(name="Alloy SLD up", min=6.0e-6, value=9.87e-6, max=1.2e-5, fit=True)
problem.parameters.append(name="Alloy SLD imaginary up", min=1.0e-9, value=4.87e-8, max=1.0e-7, fit=True)
Expand Down Expand Up @@ -43,7 +43,7 @@
problem.scalefactors.append(name="Scalefactor 3", min=0.5, value=1, max=1.5, fit=True)
problem.scalefactors.append(name="Scalefactor 4", min=0.5, value=1, max=1.5, fit=True)

# Similarly, use an individual background for each dataset....
# Similarly, use an individual background for each dataset
del problem.backgrounds[0]
del problem.background_parameters[0]

Expand All @@ -57,7 +57,7 @@
problem.backgrounds.append(name="Background 3", type="constant", value_1="Background parameter 3")
problem.backgrounds.append(name="Background 4", type="constant", value_1="Background parameter 4")

# Make the resolution fittable...
# Make the resolution fittable
problem.resolution_parameters.set_fields(0, fit=True)

# Now add the data we need
Expand All @@ -74,8 +74,7 @@
problem.data.append(name="H2O_up", data=data_4)

# Add the custom file
#problem.custom_files.append(name="DPPC absorption", filename="volumeThiolBilayer.m", language="matlab")
problem.custom_files.append(name="DPPC absorption", filename="volumeThiolBilayer.py", language="python")
problem.custom_files.append(name="DPPC absorption", filename="volume_thiol_bilayer.py", language="python")

# Finally add the contrasts
problem.contrasts.append(name="D2O Down", data="D2O_dn", background="Background 1", bulk_in="Silicon",
Expand All @@ -94,9 +93,9 @@
bulk_out="H2O", scalefactor="Scalefactor 4", resolution="Resolution 1", resample=True,
model=["DPPC absorption"])

# Now make a controls block....
# Now make a controls block
controls = RAT.set_controls(parallel="contrasts", resampleParams=[0.9, 150.0])

# Run the code and plot the results
problem, results = RAT.run(problem, controls)

RAT.utils.plotting.plot_ref_sld(problem, results, True)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def volumeThiolBilayer(params, bulk_in, bulk_out, contrast):
def volume_thiol_bilayer(params, bulk_in, bulk_out, contrast):
"""
volumeThiolBilayer RAT Custom Layer Model File.
Expand All @@ -21,10 +21,6 @@ def volumeThiolBilayer(params, bulk_in, bulk_out, contrast):
The second output parameter should be the substrate roughness.
"""
#params = np.array(params)
#bulk_in = np.array(bulk_in)
#bulk_out = np.array(bulk_out)

subRough = params[0]
alloyThick = params[1]
alloySLDUp = params[2]
Expand Down Expand Up @@ -59,9 +55,9 @@ def volumeThiolBilayer(params, bulk_in, bulk_out, contrast):
bn = 0.936e-4 # Nitrogen
bd = 0.6671e-4 # Deuterium

# Work out the total scattering length in each fragment....
# Define scattering lengths..
# Hydrogenated version....
# Work out the total scattering length in each fragment
# Define scattering lengths
# Hydrogenated version
COO = (2*bo) + (bc)
GLYC = (3*bc) + (5*bh)
CH3 = (1*bc) + (3*bh)
Expand All @@ -72,8 +68,8 @@ def volumeThiolBilayer(params, bulk_in, bulk_out, contrast):
H2O = (2*bh) + (1*bo)
D2O = (2*bd) + (1*bo)

# And also volumes....
vCH3 = 52.7 # CH3 volume in the paper appears to be for 2* CH3's
# And also volumes
vCH3 = 52.7 # CH3 volume in the paper appears to be for 2 * CH3's
vCH2 = 28.1
vCOO = 39.0
vGLYC = 68.8
Expand All @@ -83,13 +79,13 @@ def volumeThiolBilayer(params, bulk_in, bulk_out, contrast):
vCHCH = 42.14

vHead = vCHOL + vPO4 + vGLYC + 2*vCOO
vTail = (28*vCH2) + (1*vCHCH) + (2*vCH3) # Tail_volume
vTail = (28*vCH2) + (1*vCHCH) + (2*vCH3) # Tail volume

# Calculate sum_b's for other fragments
sumbHead = CHOL + PO4 + GLYC + 2*COO
sumbTail = (28*CH2) + (2*CH) + 2*CH3

# Calculate SLD's and Thickness'
# Calculate SLDs and Thickness
sldHead = sumbHead/vHead
thickHead = vHead/thiolAPM

Expand All @@ -100,7 +96,7 @@ def volumeThiolBilayer(params, bulk_in, bulk_out, contrast):
thiolHeadHydr = thiolHeadHydr/100
sldHead = (sldHead * (1 - thiolHeadHydr) + (thiolHeadHydr * bulk_out[contrast]))

# Now correct both the SLD's for the coverage parameter
# Now correct both the SLDs for the coverage parameter
sldTail = (thiolCoverage*sldTail) + ((1-thiolCoverage) * bulk_out[contrast])
sldHead = (thiolCoverage*sldHead) + ((1-thiolCoverage) * bulk_out[contrast])

Expand All @@ -109,7 +105,7 @@ def volumeThiolBilayer(params, bulk_in, bulk_out, contrast):

# Now do the same for the bilayer
vHead = vCHOL + vPO4 + vGLYC + 2*vCOO
vTail = (28*vCH2) # Tail_volume
vTail = (28*vCH2) # Tail volume
vMe = (2*vCH3)

sumbHead = CHOL + PO4 + GLYC + 2*COO
Expand Down
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@

def alloyDomains(params, bulkIn, bulkOut, contrast, domain):
"""Simple custom model for testing incoherent summing...
Simple two layer of permalloy / gold, with up/down domains..
def alloy_domains(params, bulkIn, bulkOut, contrast, domain):
"""Simple custom model for testing incoherent summing.
Simple two layer of permalloy / gold, with up/down domains.
"""

# Split up the parameters....
# Split up the parameters
subRough = params[0]
alloyThick = params[1]
alloySLDup = params[2]
Expand All @@ -14,7 +14,7 @@ def alloyDomains(params, bulkIn, bulkOut, contrast, domain):
goldSLD = params[6]
goldRough = params[7]

# Make the layers....
# Make the layers
alloyUp = [alloyThick, alloySLDup, alloyRough]
alloyDn = [alloyThick, alloySLDdn, alloyRough]
gold = [goldThick, goldSLD, goldRough]
Expand Down
4 changes: 2 additions & 2 deletions RAT/examples/domains/domains_XY.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Simple example of a layer containing domains using a custom XY model"""

import RAT
import RAT.utils.plotting
import RAT.rat_core

problem = RAT.Project(calculation="domains", model="custom xy", geometry="substrate/liquid")

Expand All @@ -18,7 +18,7 @@
problem.bulk_out.append(name="SLD H2O", min=-0.6e-6, value=-0.56e-6, max=-0.5e-6)

# Add the custom file
problem.custom_files.append(name="Domain Layer", filename="domainsXY.m", language="matlab")
problem.custom_files.append(name="Domain Layer", filename="domains_XY_model.py", language="python")

# Make contrasts
problem.contrasts.append(name="D2O", background="Background 1", resolution="Resolution 1", scalefactor="Scalefactor 1",
Expand Down
Original file line number Diff line number Diff line change
@@ -1,64 +1,60 @@
import numpy as np
import scipy as sp

def domainsXY(params, bulk_in, bulk_out, contrast, domain):

debugPlot = False # optional debug plot
def domains_XY_model(params, bulk_in, bulk_out, contrast, domain):

# Split up the parameters for convenience...
# Split up the parameters for convenience
subRough = params[0]
oxideThick = params[1]
layerThick = params[2]
layerSLD = params[3]
layerRough = params[4]
domainSLD = params[5]

# Make an array of z values for our model...
# Make an array of z values for our model
z = np.arange(0, 141)

# Make the volume fraction distribution for our Silicon substrate....
[vfSilicon, siSurf] = makeLayer(z,-25,50,1,subRough,subRough)
# Make the volume fraction distribution for our Silicon substrate
[vfSilicon, siSurf] = makeLayer(z, -25, 50, 1, subRough, subRough)

# ..and the Oxide....
[vfOxide, oxSurface] = makeLayer(z,siSurf,oxideThick,1,subRough,subRough)
# ... and the Oxide ...
[vfOxide, oxSurface] = makeLayer(z, siSurf, oxideThick, 1, subRough, subRough)

# ..also our layer.
[vfLayer, laySurface] = makeLayer(z,oxSurface,layerThick,1,subRough,layerRough)
# ... and also our layer.
[vfLayer, laySurface] = makeLayer(z, oxSurface, layerThick, 1, subRough, layerRough)

# Everything that is not already occupied will be filled will water..
# Everything that is not already occupied will be filled will water
totalVF = vfSilicon + vfOxide + vfLayer
vfWater = 1 - totalVF

# Now convert the Volume Fractions to SLD's...
# Now convert the Volume Fractions to SLDs
siSLD = vfSilicon * bulk_in
oxSLD = vfOxide * 3.41e-6

# Layer SLD depends on whether we are calculating the domain or not...
# Layer SLD depends on whether we are calculating the domain or not
if domain == 1:
laySLD = vfLayer * layerSLD
else:
laySLD = vfLayer * domainSLD

#...finally the water.
waterSLD = vfWater * bulk_out(contrast)
# ... and finally the water SLD.
waterSLD = vfWater * bulk_out[contrast]

# Make the total SLD by just adding them all up..
# Make the total SLD by just adding them all up
totalSLD = siSLD + oxSLD + laySLD + waterSLD

# The output is just a [n x 2] array of z against SLD..
SLD = [z, totalSLD]
# The output is just a [n x 2] array of z against SLD
SLD = np.column_stack([z, totalSLD])

return SLD, subRough


# -------------------------------------------------------------------------


def makeLayer(z, prevLaySurf, thickness, height, Sigma_L, Sigma_R):
""" This produces a layer, with a defined thickness, height and roughness.
Each side of the layer has its own roughness value.
"""
# Find the edges.....
# Find the edges
l = prevLaySurf
r = prevLaySurf + thickness

Expand All @@ -69,4 +65,4 @@ def makeLayer(z, prevLaySurf, thickness, height, Sigma_L, Sigma_R):

thisLaySurf = r

return VF, thisLaySurf
return VF, thisLaySurf
10 changes: 5 additions & 5 deletions RAT/examples/domains/domains_custom_layers.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""An example custom layers domains project involving incoherent summing on a permalloy layer"""

import RAT
import RAT.utils.plotting
import RAT.rat_core

problem = RAT.Project(calculation="domains", model="custom layers", geometry="substrate/liquid")

Expand All @@ -17,15 +17,15 @@
# Set the bulk SLD
problem.bulk_in.set_fields(0, name="Silicon", value=2.073e-6, max=1.0)

# Add the custom file...
problem.custom_files.append(name="Alloy domains", filename="alloyDomains.m", language="matlab")
# Add the custom file
problem.custom_files.append(name="Alloy domains", filename="alloy_domains.py", language="python")

# Make a contrast...
# Make a contrast
problem.contrasts.append(name="D2O Contrast", data="Simulation", background="Background 1", bulk_in="Silicon",
bulk_out="SLD D2O", scalefactor="Scalefactor 1", resolution="Resolution 1", resample=False,
domain_ratio="Domain Ratio 1", model=["Alloy domains"])

controls = RAT.set_controls()
problem, results = RAT.run(problem, controls)

problem, results = RAT.run(problem, controls)
RAT.utils.plotting.plot_ref_sld(problem, results, True)
13 changes: 5 additions & 8 deletions RAT/examples/domains/standard_layers.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import RAT
import RAT.utils.plotting
import RAT.rat_core

problem = RAT.Project(calculation="domains")

# Define the parameters we need to define our two domains...
# Define the parameters we need to define our two domains
problem.parameters.append(name="L1 Thickness", min=5.0, value=20.0, max=60.0, fit=True)
problem.parameters.append(name="L1 SLD", min=3.0e-6, value=4.1e-6, max=5.0e-6, fit=False)
problem.parameters.append(name="L1 Roughness", min=2.0, value=5.0, max=20.0, fit=True)
Expand All @@ -28,10 +27,8 @@
problem.layers.append(name="Layer 2", thickness="L2 Thickness", SLD="L2 SLD", roughness="L2 Roughness",
hydration="L2 Hydration", hydrate_with="bulk out")

#problem.layers.append(name="Layer 3", thickness="L3 Thickness", SLD="L3 SLD", roughness="L3 Roughness",
# hydration="L3 Hydration", hydrate_with="bulk out")
problem.layers.append(name="Layer 3", thickness="L2 Thickness", SLD="L2 SLD", roughness="L2 Roughness",
hydration="L2 Hydration", hydrate_with="bulk out")
problem.layers.append(name="Layer 3", thickness="L3 Thickness", SLD="L3 SLD", roughness="L3 Roughness",
hydration="L3 Hydration", hydrate_with="bulk out")


# If we look at the project, there are two extra groups as compared to a normal standard layers calculation:
Expand All @@ -45,8 +42,8 @@
domain_ratio="Domain Ratio 1", data="Simulation", model=["Domain 1", "Domain 2"])


# Now we can run our simulation as usual, and plot the results....
# Now we can run our simulation as usual, and plot the results
controls = RAT.set_controls()
problem, results = RAT.run(problem, controls)

problem, results = RAT.run(problem, controls)
RAT.utils.plotting.plot_ref_sld(problem, results, True)
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
//customBilayer.cpp
//custom_bilayer.cpp

#include <vector>

Expand All @@ -8,10 +8,10 @@
#define LIB_EXPORT
#endif

// We user extern "C" decorator to avoid name mangling....
// We user extern "C" decorator to avoid name mangling
extern "C" {

LIB_EXPORT void customBilayer(std::vector<double>& params, std::vector<double>& bulkIn, std::vector<double>& bulkOut, int contrast, std::vector<double>& output, double* outputSize, double* rough)
LIB_EXPORT void custom_bilayer(std::vector<double>& params, std::vector<double>& bulkIn, std::vector<double>& bulkOut, int contrast, std::vector<double>& output, double* outputSize, double* rough)
{
double subRough = params[0];
double oxideThick = params[1];
Expand All @@ -25,19 +25,19 @@ extern "C" {
// We have a constant SLD for the oxide
double oxideSLD = 3.41e-6;

// Now make the lipid layers..
// Now make the lipid layers
// Use known lipid volume and compositions
// to make the layers

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

// Now make the lipid groups..
// Now make the lipid groups
double COO = (4*bo) + (2*bc);
double GLYC = (3*bc) + (5*bh);
double CH3 = (2*bc) + (6*bh);
Expand All @@ -54,12 +54,12 @@ extern "C" {
double vHead = 319;
double vTail = 782;

// we use the volumes to calculate the SLD's
// we use the volumes to calculate the SLDs
double SLDhead = Head / vHead;
double SLDtail = Tails / vTail;

// We calculate the layer thickness' from
// the volumes and the APM...
// We calculate the layer thickness from
// the volumes and the APM
double headThick = vHead / lipidAPM;
double tailThick = vTail / lipidAPM;

Expand Down Expand Up @@ -102,7 +102,7 @@ extern "C" {

*rough = subRough;

outputSize[0] = 6; // row - Necessary to ouptut how many layers in stack
outputSize[0] = 6; // row - Necessary to output how many layers in stack
outputSize[1] = 3; // col - Should be different depending on calculation
}
} // extern "C"
Loading

0 comments on commit 29795d8

Please sign in to comment.