Skip to content

Commit

Permalink
Fixed initialization of a,b coefficients and moved saturation to tissue
Browse files Browse the repository at this point in the history
  • Loading branch information
jirkapok committed Dec 11, 2024
1 parent 870f3a7 commit 3d53f66
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 24 deletions.
2 changes: 1 addition & 1 deletion projects/scuba-physics/src/lib/GradientFactors.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class SubSurfaceGradientFactors {
*/
private toleratedTissues(surface: number, lowestCeiling: number, gfHigh: number, gfLow: number): number {
const compartments = this.tissues.compartments;
let tolerated = 0;
let tolerated = 0; // this prevents negative values

for (let index = 0; index < compartments.length; index++) {
const compartment = compartments[index];
Expand Down
12 changes: 5 additions & 7 deletions projects/scuba-physics/src/lib/Tissues.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -42,10 +42,10 @@ describe('Tissues', () => {


describe('Tissue', () => {
it('Not loaded tissue ceiling is 0 m', () => {
it('Not loaded tissue ceiling above surface', () => {
const tissue = createTissue();
const ceiling = tissue.ceiling(1);
expect(ceiling).toBe(0);
expect(ceiling).toBeCloseTo(-0.23884756, 8);
});

it('loaded tissue have non 0 m ceiling', () => {
Expand All @@ -61,11 +61,9 @@ describe('Tissues', () => {
const segment = new LoadSegment(6, Time.oneMinute * 60, 0);

xit('Is 0 at surface', () => {
// simple depth conversion at surface
const tissues = Tissues.create(1);
tissues.load(segment, StandardGases.air);
const saturationRatios = tissues.saturationRatio(2, 1, 1);
expect(saturationRatios[0]).toBeCloseTo(1, 8);
const tissue = createTissue();
const saturationRatio = tissue.saturationRatio(1, 1, 1);
expect(saturationRatio).toBeCloseTo(0, 8);
});

xit('Is less than 0 when descending ongassing tissues', () => {
Expand Down
49 changes: 33 additions & 16 deletions projects/scuba-physics/src/lib/Tissues.ts
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,7 @@ export class Tissue extends Compartment implements LoadedTissue {
this._pN2 = Tissue.inspiredN2Pressure(surfacePressure);
this._pHe = 0;
this.updateTotal();
this.updateCoefficients();
}

/**
Expand Down Expand Up @@ -85,6 +86,7 @@ export class Tissue extends Compartment implements LoadedTissue {
copy._a = loaded.a;
copy._b = loaded.b;
copy.updateTotal();
copy.updateCoefficients();
return copy;
}

Expand Down Expand Up @@ -126,9 +128,30 @@ export class Tissue extends Compartment implements LoadedTissue {
return (this.pTotal - pressure) / (surfaceMValue - pressure);
}

/**
* Calculates saturation ratio for all tissues as percents relative to ambient pressure.
* -1..0: is offgasing, -1 = surface pressure.
* =0: is equilibrium (tissue is not offgasing or ongasing), at ambient pressure.
* >0: is ongasing, +1 = 100% gradient i.e. at m-value, more means exceeded limit.
* @param ambientPressure The current ambient pressure in bars.
* @param surfacePressure The surface pressure in bars at beginning of the dive.
* @param gradient Gradient factor constant in range 0-1
*/
public saturationRatio(ambientPressure: number, surfacePressure: number, gradient: number): number {
// TODO do we need adjust m-value against user gradient factors and surface pressure?
// We need use Gradient here, since we want to show the saturation aligned with profile and ceilings.
// Or should the heat map change when changing gradient factors?
if (this.pTotal < ambientPressure) {
return this.pTotal / ambientPressure - 1;
}

return this.gradientFactor(ambientPressure);
}

/**
* Returns pressure in bars of the depth representing maximum ceiling
* reduced by the provided gradient.
* May return negative value in case the ceiling is lower than surface pressure.
*
* @param gradient Gradient factor constant in range 0-1
*/
Expand All @@ -143,10 +166,7 @@ export class Tissue extends Compartment implements LoadedTissue {
this._pHe = this.loadGas(segment, gas.fHe, this.pHe, this.heHalfTime);
const prevTotal = this.pTotal;
this.updateTotal();

this._a = ((this.n2A * this.pN2) + (this.heA * this.pHe)) / (this.pTotal);
this._b = ((this.n2B * this.pN2) + (this.heB * this.pHe)) / (this.pTotal);

this.updateCoefficients();
// return difference - how much load was added
return this.pTotal - prevTotal;
}
Expand Down Expand Up @@ -180,6 +200,11 @@ export class Tissue extends Compartment implements LoadedTissue {
private updateTotal(): void {
this._pTotal = this.pN2 + this.pHe;
}

private updateCoefficients() {
this._a = ((this.n2A * this.pN2) + (this.heA * this.pHe)) / (this.pTotal);
this._b = ((this.n2B * this.pN2) + (this.heB * this.pHe)) / (this.pTotal);
}
}

export class Tissues {
Expand Down Expand Up @@ -267,17 +292,8 @@ export class Tissues {
* @param gradient Gradient factor constant in range 0-1
*/
public saturationRatio(ambientPressure: number, surfacePressure: number, gradient: number): number[] {
// TODO do we need adjust m-value against user gradient factors and surface pressure?
// We need use Gradient here, since we want to show the saturation aligned with profile and ceilings.
// Or should the heat map change when changing gradient factors?
// return [];
return _(this._compartments).map(t => {
if (t.pTotal < ambientPressure) {
return t.pTotal / ambientPressure - 1;
}

return t.gradientFactor(ambientPressure);
}).value();
return _(this._compartments).map(t => t.saturationRatio(ambientPressure, surfacePressure, gradient))
.value();
}

/**
Expand All @@ -288,7 +304,7 @@ export class Tissues {
* @returns Zero in case there is no ceiling, otherwise ceiling pressure in bars
*/
public ceiling(gradient: number): number {
let ceiling = 0;
let ceiling = 0; // this prevents negative values

for (let index = 0; index < this._compartments.length; index++) {
const tissueCeiling = this._compartments[index].ceiling(gradient);
Expand Down Expand Up @@ -332,6 +348,7 @@ export interface LoadedTissue {
*/
pHe: number;

// TODO remove a and b coefficients, since they are calculated from pN2 and pHe and compartment coefficients.
/**
* Buhlmann m-value constant a
*/
Expand Down

0 comments on commit 3d53f66

Please sign in to comment.