Skip to content

Commit

Permalink
Implemented surface interval usage in algorithm
Browse files Browse the repository at this point in the history
  • Loading branch information
Jiri Pokorny committed Dec 7, 2023
1 parent e4b14d1 commit be66c80
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
6 changes: 3 additions & 3 deletions projects/scuba-physics/src/lib/AlgorithmContext.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Tissue, Tissues } from './Tissues';
import { LoadedTissue, Tissue, Tissues } from './Tissues';
import { Ceiling } from './Profile';
import { BestGasOptions, Gas, Gases, OCGasSource } from './Gases';
import { GradientFactors, SubSurfaceGradientFactors } from './GradientFactors';
Expand Down Expand Up @@ -34,8 +34,8 @@ export class AlgorithmContext {
public segments: Segments,
public options: Options,
public depthConverter: DepthConverter,
currentTissues: Tissues | null = null) {
this.tissues = currentTissues || Tissues.create(depthConverter.surfacePressure);
currentTissues: LoadedTissue[] | null = null) {
this.tissues = currentTissues ? Tissues.createLoaded(currentTissues) : Tissues.create(depthConverter.surfacePressure);
// this.gradients = new SimpleGradientFactors(depthConverter, options, this.tissues, this.segments);
this.gradients = new SubSurfaceGradientFactors(depthConverter, options, this.tissues);
const last = segments.last();
Expand Down
13 changes: 9 additions & 4 deletions projects/scuba-physics/src/lib/BuhlmannAlgorithm.ts
Original file line number Diff line number Diff line change
Expand Up @@ -114,13 +114,14 @@ export class BuhlmannAlgorithm {
* Returns positive number or Infinity, in case there is no more tissues loading.
* Usually at small depths (bellow 10 meters).
*/
public noDecoLimit({ segments, gases, options }: AlgorithmParams): number {
public noDecoLimit({ segments, gases, options, surfaceInterval }: AlgorithmParams): number {
const depthConverter = new DepthConverterFactory(options).create();
// const rested = this.applySurfaceInterval(surfaceInterval);
const context = new AlgorithmContext(gases, segments, options, depthConverter);
return this.swimNoDecoLimit(segments, gases, context);
}

public decompression({ segments, gases, options }: AlgorithmParams): CalculatedProfile {
public decompression({ segments, gases, options, surfaceInterval }: AlgorithmParams): CalculatedProfile {
const depthConverter = new DepthConverterFactory(options).create();
const newSegments = segments.copy();
const errors = this.validate(segments, gases);
Expand All @@ -129,6 +130,7 @@ export class BuhlmannAlgorithm {
return CalculatedProfile.fromErrors(origProfile, errors);
}

const relaxedTissues = this.applySurfaceInterval(surfaceInterval);
const context = new AlgorithmContext(gases, newSegments, options, depthConverter);
this.swimPlan(context);
context.markAverageDepth();
Expand Down Expand Up @@ -168,7 +170,10 @@ export class BuhlmannAlgorithm {
throw Error('Surface interval needs to be positive number or 0.');
}

const tissues = Tissues.createLoaded(previousTissues);
if(surfaceInterval === Number.POSITIVE_INFINITY || surfaceInterval <= 0) {
return previousTissues;
}

// at surface, there is no depth change, even we are at different elevation and we are always breathing air
const segments = new Segments();
const restingSegment = segments.addFlat(0, StandardGases.air, surfaceInterval);
Expand All @@ -178,7 +183,7 @@ export class BuhlmannAlgorithm {
const options = new Options(1, 1, 1.6, 1.6, Salinity.salt);
options.altitude = altitude;
const depthConverter = new DepthConverterFactory(options).create();
const context = new AlgorithmContext(gases, segments, options, depthConverter, tissues);
const context = new AlgorithmContext(gases, segments, options, depthConverter, previousTissues);
this.swim(context, restingSegment);
return context.tissues.finalState();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,20 @@ describe('Buhlmann Algorithm - Repetitive dives', () => {
const stableTissues = applySurfaceInterval(Tissues.create(1).finalState(), 0, Time.oneDay * 9);

describe('Surface interval', () => {
it('Isn\'t applied for 0 seconds surface interval duration.', () => {
const r1 = toTissueResult(stableTissues);
const result = applySurfaceInterval(stableTissues, 0, 0);
const r2 = toTissueResult(result);
expect(r1).toEqual(r2);
});

it('Isn\'t applied for Infinite surface interval duration.', () => {
const r1 = toTissueResult(stableTissues);
const result = applySurfaceInterval(stableTissues, 0, Number.POSITIVE_INFINITY);
const r2 = toTissueResult(result);
expect(r1).toEqual(r2);
});

it('Doesn\'t change not loaded tissues.', () => {
const r1 = toTissueResult(stableTissues);
const result = applySurfaceInterval(stableTissues, 0, Time.oneMinute * 10);
Expand Down

0 comments on commit be66c80

Please sign in to comment.