-
Notifications
You must be signed in to change notification settings - Fork 9
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
8 changed files
with
229 additions
and
65 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
using Vts.Common; | ||
|
||
namespace Vts.Scripting.Utilities; | ||
|
||
public static class PlottingExtensions | ||
{ | ||
public static double[] GetMidpoints(this DoubleRange endpointRange) | ||
{ | ||
var endpoints = endpointRange.AsEnumerable().ToArray(); | ||
if (endpoints.Length < 2) | ||
{ | ||
throw new ArgumentException("Endpoints must have at least two elements"); | ||
} | ||
|
||
var midpoints = new double[endpoints.Length - 1]; | ||
for (int i = 0; i < midpoints.Length; i++) | ||
{ | ||
midpoints[i] = endpoints[i + 1] - endpoints[i]; | ||
} | ||
return endpoints; | ||
} | ||
} |
13 changes: 13 additions & 0 deletions
13
src/Vts.Scripting.Utilities/Vts.Scripting.Utilities.csproj
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
<Project Sdk="Microsoft.NET.Sdk"> | ||
|
||
<PropertyGroup> | ||
<TargetFramework>net7.0</TargetFramework> | ||
<ImplicitUsings>enable</ImplicitUsings> | ||
<Nullable>enable</Nullable> | ||
</PropertyGroup> | ||
|
||
<ItemGroup> | ||
<ProjectReference Include="..\Vts\Vts.csproj" /> | ||
</ItemGroup> | ||
|
||
</Project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
using Vts.Common; | ||
using Vts.MonteCarlo.Detectors; | ||
using Vts.MonteCarlo.Sources; | ||
using Vts.MonteCarlo.Tissues; | ||
using Vts.MonteCarlo; | ||
using Vts; | ||
using Vts.Scripting.Utilities; | ||
using Plotly.NET.CSharp; | ||
|
||
// Example 01: run a simple Monte Carlo simulation with 1000 photons | ||
|
||
// set number of photons | ||
var numPhotons = 1000; | ||
|
||
// define a point source beam normally incident at the origin | ||
var sourceInput = new DirectionalPointSourceInput | ||
{ | ||
SourceType = "DirectionalPoint", | ||
PointLocation = new(x: 0, y: 0, z: 0), | ||
Direction = new(ux: 0, uy: 0, uz: 1), | ||
InitialTissueRegionIndex = 0 | ||
}; | ||
|
||
// define a semi-infinite slab tissue geometry with air-tissue boundary (a bottom air layer is necessary) | ||
var tissueInput = new MultiLayerTissueInput | ||
{ | ||
Regions = new ITissueRegion[] | ||
{ | ||
new LayerTissueRegion( | ||
zRange: new(double.NegativeInfinity, 0), // air "z" range | ||
op: new(mua: 0.0, musp: 1E-10, g: 1.0, n: 1.0)), // air optical properties | ||
new LayerTissueRegion( | ||
zRange: new(0, 100), // tissue "z" range ("semi-infinite" slab, 100mm thick) | ||
op: new(mua: 0.01, musp: 1.0, g: 0.9, n: 1.4)), // tissue optical properties | ||
new LayerTissueRegion( | ||
zRange: new(100, double.PositiveInfinity), // air "z" range | ||
op: new(mua: 0.0, musp: 1E-10, g: 1.0, n: 1.0)) // air optical properties | ||
} | ||
}; | ||
|
||
// define a single R(rho) detector by the endpoints of rho bins | ||
var detectorInput = new ROfRhoDetectorInput { Rho = new(0, 40, 201), TallySecondMoment = true }; | ||
|
||
// create a SimulationInput object to define the simulation | ||
var simulationInput = new SimulationInput | ||
{ | ||
N = numPhotons, | ||
SourceInput = sourceInput, | ||
TissueInput = tissueInput, | ||
DetectorInputs = new IDetectorInput[] { detectorInput }, | ||
Options = new SimulationOptions | ||
{ | ||
Seed = 0, // -1 will generate a random seed | ||
AbsorptionWeightingType = AbsorptionWeightingType.Discrete, | ||
PhaseFunctionType = PhaseFunctionType.HenyeyGreenstein | ||
} | ||
}; | ||
|
||
// create the simulation | ||
var simulation = new MonteCarloSimulation(simulationInput); | ||
|
||
// run the simulation | ||
var simulationOutput = simulation.Run(); | ||
|
||
// plot the results | ||
var detectorMidpoints = detectorInput.Rho.GetMidpoints(); | ||
var logReflectance = simulationOutput.R_r.Select(r => Math.Log(r)).ToArray(); | ||
Chart.Point<double, double, string>( | ||
x: detectorMidpoints, | ||
y: logReflectance | ||
).WithTraceInfo("log(R) vs rho [mm-2]", ShowLegend: true) | ||
.WithXAxisStyle<double, double, string>(Title: Plotly.NET.Title.init("rho [mm]")) | ||
.WithYAxisStyle<double, double, string>(Title: Plotly.NET.Title.init("log(R(rho)) [mm-2]")) | ||
.Show(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
using Vts.MonteCarlo.Detectors; | ||
using Vts.MonteCarlo.Sources; | ||
using Vts.MonteCarlo.Tissues; | ||
using Vts.MonteCarlo; | ||
using Vts; | ||
using Vts.Scripting.Utilities; | ||
using Plotly.NET.CSharp; | ||
|
||
// Example 10: run R(fx) detector results | ||
|
||
// set number of photons | ||
var numPhotons = 1000; | ||
|
||
// define a point source beam normally incident at the origin | ||
var sourceInput = new DirectionalPointSourceInput | ||
{ | ||
SourceType = "DirectionalPoint", | ||
PointLocation = new(x: 0, y: 0, z: 0), | ||
Direction = new(ux: 0, uy: 0, uz: 1), | ||
InitialTissueRegionIndex = 0 | ||
}; | ||
|
||
// define a semi-infinite slab tissue geometry with air-tissue boundary (a bottom air layer is necessary) | ||
var tissueInput = new MultiLayerTissueInput | ||
{ | ||
Regions = new ITissueRegion[] | ||
{ | ||
new LayerTissueRegion( | ||
zRange: new(double.NegativeInfinity, 0), // air "z" range | ||
op: new(mua: 0.0, musp: 1E-10, g: 1.0, n: 1.0)), // air optical properties | ||
new LayerTissueRegion( | ||
zRange: new(0, 100), // tissue "z" range ("semi-infinite" slab, 100mm thick) | ||
op: new(mua: 0.01, musp: 1.0, g: 0.9, n: 1.4)), // tissue optical properties | ||
new LayerTissueRegion( | ||
zRange: new(100, double.PositiveInfinity), // air "z" range | ||
op: new(mua: 0.0, musp: 1E-10, g: 1.0, n: 1.0)) // air optical properties | ||
} | ||
}; | ||
|
||
// define a single R(fx) detector at spatial frequencies fx | ||
var detectorInput = new ROfFxDetectorInput { Fx = new(0, 1, 101), TallySecondMoment = true }; | ||
|
||
// create a SimulationInput object to define the simulation | ||
var simulationInput = new SimulationInput | ||
{ | ||
N = numPhotons, | ||
SourceInput = sourceInput, | ||
TissueInput = tissueInput, | ||
DetectorInputs = new IDetectorInput[] { detectorInput }, | ||
Options = new SimulationOptions | ||
{ | ||
Seed = 0, // -1 will generate a random seed | ||
AbsorptionWeightingType = AbsorptionWeightingType.Discrete, | ||
PhaseFunctionType = PhaseFunctionType.HenyeyGreenstein | ||
} | ||
}; | ||
|
||
// create the simulation | ||
var simulation = new MonteCarloSimulation(simulationInput); | ||
|
||
// run the simulation | ||
var simulationOutput = simulation.Run(); | ||
|
||
// plot the results | ||
var detectorMidpoints = detectorInput.Fx.GetMidpoints(); | ||
var reflectance = simulationOutput.R_fx.Select(r => r.Magnitude).ToArray(); | ||
Chart.Point<double, double, string>( | ||
x: detectorMidpoints, | ||
y: reflectance | ||
).WithTraceInfo("R vs fx [unitless]", ShowLegend: true) | ||
.WithXAxisStyle<double, double, string>(Title: Plotly.NET.Title.init("fx [mm-1]")) | ||
.WithYAxisStyle<double, double, string>(Title: Plotly.NET.Title.init("R(fx) [unitless]")) | ||
.Show(); |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters