Skip to content

Commit

Permalink
Adjusted gas blender precision
Browse files Browse the repository at this point in the history
  • Loading branch information
jirkapok committed Mar 6, 2024
1 parent 4c6c0c2 commit 6e3ff1b
Show file tree
Hide file tree
Showing 5 changed files with 17 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,4 @@ describe('GasBlenderComponent', () => {
it('should create', () => {
expect(component).toBeTruthy();
});

// TODO test cases
// - imperial values: source pressure, target pressure, results, AddO2, AddHe, AddTop
// - imperial values: shows removeFrom source
});
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,5 @@ export class GasBlenderComponent implements OnInit {

// TODO Gas blender component:
// Do we need Reload?
// add service tests
// save/load state
}
5 changes: 5 additions & 0 deletions projects/planner/src/app/shared/gas-blender.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ describe('Gas blender service', () => {
sut = new GasBlenderService(new UnitConversion());
});

// TODO test cases:
// - all result fields
// - add exception handling in case we are unable to create target mix.
// - imperial values: source pressure, target pressure, results, AddO2, AddHe, AddTop
// - imperial values: shows removeFrom source
it('Creates', () => {
expect(sut).toBeTruthy();
});
Expand Down
3 changes: 1 addition & 2 deletions projects/planner/src/app/shared/gas-blender.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ export class GasBlenderService {
}

public get removeFromSource(): number {
return 0;// this.results.removeFromSource;
return this.results.removeFromSource;
}

public get needsRemove(): boolean {
Expand Down Expand Up @@ -77,7 +77,6 @@ export class GasBlenderService {
}
};

// TODO add exception handling in case we are unable to create target mix.
this.results = GasBlender.mix(request);
}
}
18 changes: 11 additions & 7 deletions projects/scuba-physics/src/lib/gasBlender.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Tank, TankFill } from './Tanks';
import { Precision } from "./precision";
import { Precision } from './precision';

/**
* Blending result showing amount of each component used
Expand Down Expand Up @@ -75,10 +75,12 @@ export class GasBlender {
const finalN2 = finalfN2 * request.target.pressure;
const currentfN2 = GasBlender.n2(request.source);
const currentN2 = currentfN2 * request.source.pressure;
const addN2 = finalN2 - currentN2;
let addN2 = finalN2 - currentN2;
addN2 = Precision.round(addN2, 8);

// Even the top mix contains more nitrogen than target, we are still able to mix
// by adding less top mix and more He and O2
if(Precision.round(addN2, 8) < 0) {
if(addN2 < 0) {
const removeSource = -(addN2 / currentfN2);
return GasBlender.mixByRemoving(request, removeSource);
}
Expand All @@ -88,16 +90,18 @@ export class GasBlender {
const targetHe = request.target.pressure * request.target.he;
const sourceHe = request.source.pressure * request.source.he;
const topHe = addTop * request.topMix.he;
const addHe = targetHe - sourceHe - topHe;
let addHe = targetHe - sourceHe - topHe;
addHe = Precision.round(addHe, 8);

if(Precision.round(addHe, 8) < 0) {
if(addHe < 0) {
const removeSource = -(addHe / request.source.he);
return GasBlender.mixByRemoving(request, removeSource);
}

const addO2 = request.target.pressure - request.source.pressure - addHe - addTop;
let addO2 = request.target.pressure - request.source.pressure - addHe - addTop;
addO2 = Precision.round(addO2, 8);

if(Precision.round(addO2, 8) < 0) {
if(addO2 < 0) {
const removeSource = -(addO2 / request.source.o2);
return GasBlender.mixByRemoving(request, removeSource);
}
Expand Down

0 comments on commit 6e3ff1b

Please sign in to comment.