Skip to content

Commit

Permalink
add correct maths
Browse files Browse the repository at this point in the history
  • Loading branch information
tizayi committed Oct 6, 2023
1 parent 9b9be6b commit 8001ce2
Show file tree
Hide file tree
Showing 5 changed files with 31 additions and 28 deletions.
1 change: 0 additions & 1 deletion src/plot/centrePlot.tsx
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import { Box, Card, CardContent, Stack } from "@mui/material";
// @ts-ignore
import {
DataToHtml,
DefaultInteractions,
Expand Down
14 changes: 7 additions & 7 deletions src/results/resultsBar.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -166,13 +166,13 @@ export default function ResultsBar(props: {
size="small"
label="units"
value={resultStore.s.units}
onChange={(event) => updateSUnits(event.target.value as ReciprocalWavelengthUnits)}
onChange={(event) => updateSUnits(event.target.value as WavelengthUnits)}
>
<MenuItem value={ReciprocalWavelengthUnits.nanmometres}>
{ReciprocalWavelengthUnits.nanmometres}
<MenuItem value={WavelengthUnits.nanmometres}>
{WavelengthUnits.nanmometres}
</MenuItem>
<MenuItem value={ReciprocalWavelengthUnits.angstroms}>
{ReciprocalWavelengthUnits.angstroms}
<MenuItem value={WavelengthUnits.angstroms}>
{WavelengthUnits.angstroms}
</MenuItem>
</Select>
</FormControl></TableCell>
Expand Down Expand Up @@ -205,8 +205,8 @@ export default function ResultsBar(props: {
key={"things"}
>
<TableCell component="th" scope="row">{ScatteringOptions.twoTheta}</TableCell>
<TableCell align="right">{thetaRange.min.toFixed(4)}</TableCell>
<TableCell align="right">{thetaRange.max.toFixed(4)}</TableCell>
<TableCell align="right">{thetaRange.min.toFixed(5)}</TableCell>
<TableCell align="right">{thetaRange.max.toFixed(5)}</TableCell>
<TableCell align="right">
<FormControl>
<InputLabel>theta</InputLabel>
Expand Down
6 changes: 3 additions & 3 deletions src/results/resultsStore.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,22 +22,22 @@ export interface ResultStore {
updateRequested: (quantity: ScatteringOptions) => void;
updateRequestedRange: (newRange: NumericRange) => void;
updateQUnits: (newunits: ReciprocalWavelengthUnits) => void;
updateSUnits: (newunits: ReciprocalWavelengthUnits) => void;
updateSUnits: (newunits: WavelengthUnits) => void;
updateDUnits: (newunits: WavelengthUnits) => void;
updateThetaUnits: (newunits: AngleUnits, wavelength: number) => void;
}

export const useResultStore = create<ResultStore>((set) => ({
requested: ScatteringOptions.q,
q: new Q(ReciprocalWavelengthUnits.nanmometres),
s: new S(ReciprocalWavelengthUnits.nanmometres),
s: new S(WavelengthUnits.nanmometres),
d: new D(WavelengthUnits.nanmometres),
twoTheta: new TwoTheta(AngleUnits.radians, 0),
requestedRange: new NumericRange(0, 1),
updateRequested: (quantity: ScatteringOptions) => { set({ requested: quantity }) },
updateRequestedRange: (newRange: NumericRange) => { set({ requestedRange: newRange }) },
updateQUnits: (newunits: ReciprocalWavelengthUnits) => set({ q: new Q(newunits) }),
updateSUnits: (newunits: ReciprocalWavelengthUnits) => set({ s: new S(newunits) }),
updateSUnits: (newunits: WavelengthUnits) => set({ s: new S(newunits) }),
updateDUnits: (newunits: WavelengthUnits) => set({ d: new D(newunits) }),
updateThetaUnits: (newunits: AngleUnits, wavelength: number) => set({ twoTheta: new TwoTheta(newunits, wavelength) })
}));
Expand Down
36 changes: 20 additions & 16 deletions src/results/scatteringQuantities.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { AngleUnits, WavelengthUnits, ReciprocalWavelengthUnits } from "../utils/units";
import { AngleUnits, WavelengthUnits, ReciprocalWavelengthUnits, nanometres2Angstroms, angstroms2Nanometres } from "../utils/units";
import { MathUtils } from "three/src/Three.js";

export interface ScatteringQuantity {
units: WavelengthUnits | AngleUnits | ReciprocalWavelengthUnits,
Expand All @@ -13,32 +14,35 @@ export class Q implements ScatteringQuantity {
}
fromQ(quantity: number): number {
if (this.units == ReciprocalWavelengthUnits.angstroms) {
return quantity / 10;
// reciprocal units so we do the inverse
return angstroms2Nanometres(quantity);
}
return quantity
}
tooQ(quantity: number): number {
if (this.units === ReciprocalWavelengthUnits.angstroms) {
return quantity * 10;
// reciprocal units so we do the inverse
return nanometres2Angstroms(quantity);
}
return quantity
}
}

export class S implements ScatteringQuantity {
units: ReciprocalWavelengthUnits;
constructor(units: ReciprocalWavelengthUnits) {
units: WavelengthUnits;
constructor(units: WavelengthUnits) {
this.units = units
}
fromQ(quantity: number): number {
if (this.units === ReciprocalWavelengthUnits.angstroms) {
return (1 / quantity) * 10;
if (this.units === WavelengthUnits.angstroms) {

return nanometres2Angstroms(1 / quantity);
}
return 1 / quantity
}
tooQ(quantity: number): number {
if (this.units === ReciprocalWavelengthUnits.angstroms) {
return 1 / (quantity / 10);
if (this.units === WavelengthUnits.angstroms) {
return 1 / (angstroms2Nanometres(quantity));
}
return 1 / quantity
}
Expand All @@ -51,13 +55,13 @@ export class D implements ScatteringQuantity {
}
fromQ(quantity: number): number {
if (this.units === WavelengthUnits.angstroms) {
return 2 * Math.PI / (quantity) * 10;
return nanometres2Angstroms(2 * Math.PI / quantity);
}
return 2 * Math.PI / (quantity)
}
tooQ(quantity: number): number {
if (this.units === WavelengthUnits.nanmometres) {
return 2 * Math.PI / (quantity / 10);
if (this.units === WavelengthUnits.angstroms) {
return 2 * Math.PI / angstroms2Nanometres(quantity);
}
return 2 * Math.PI / (quantity)
}
Expand All @@ -72,14 +76,14 @@ export class TwoTheta implements ScatteringQuantity {
}
fromQ(quantity: number): number {
if (this.units === AngleUnits.degrees) {
return (2 * Math.asin((quantity * this.wavelength) / (4 * Math.PI)));
return 2 * MathUtils.radToDeg(Math.asin((quantity * this.wavelength) / (4 * Math.PI)));
}
return quantity
return 2 * Math.asin((quantity * this.wavelength) / (4 * Math.PI))
}
tooQ(quantity: number): number {
if (this.units === AngleUnits.degrees) {
return quantity;
return ((4 * Math.PI) / this.wavelength) * Math.sin(MathUtils.degToRad(quantity / 2));
}
return 4 * Math.PI * Math.sin(quantity) / this.wavelength
return ((4 * Math.PI) / this.wavelength) * Math.sin(quantity / 2)
}
}
2 changes: 1 addition & 1 deletion src/utils/units.ts
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,4 @@ export const kiloElectronVolts2ElectronVots = (input: number): number => {

export const electronVots2KiloElectronVolts = (input: number): number => {
return input / 1000;
};
};

0 comments on commit 8001ce2

Please sign in to comment.