Skip to content

Mass Balance API Reference ​

Overview ​

The Mass Balance API provides integrated mass balance calculations for stream-by-stream process analysis. It combines hydraulic calculations with water quality transformations to produce comprehensive mass balances following UK water industry standards.

Core Types ​

MassBalanceScenarioType ​

Scenario type for mass balance analysis.

Values ​

ValueDescription
'Current'Current/existing conditions
'Future'Future design conditions
'AsBuilt'As-built conditions post-construction
'Commissioning'Commissioning phase conditions
'Event'Storm event or special operating conditions

FlowConditionType ​

Flow condition type for mass balance.

Values ​

ValueDescription
'DWF'Dry Weather Flow
'ADF'Average Daily Flow
'FFT'Full Flow to Treatment (Maximum Daily)
'MinInstant'Minimum Instantaneous Flow
'MaxInstant'Maximum Instantaneous Flow
'Peak'Peak Flow from Network

StreamType ​

Stream type classification.

Values ​

ValueDescription
'mainProcess'Main process flow stream
'stormFlow'Storm/wet weather flow
'sludge'Sludge stream
'recirculation'Recycle/recirculation stream
'chemical'Chemical dosing stream
'liquor'Return liquor stream

Stream Data Structures ​

StreamFlowConditions ​

Flow conditions for a stream across all design scenarios.

Properties ​

PropertyTypeDescription
dwfnumberDry weather flow (m³/d)
averagenumberAverage daily flow (m³/d)
fftnumberFull flow to treatment (m³/d)
minInstantaneousnumberundefined
maxInstantaneousnumberundefined
peakFromNetworknumberundefined

Example ​

typescript
const streamFlows: StreamFlowConditions = {
  dwf: 50000,
  average: 60000,
  fft: 90000,
  minInstantaneous: 289,      // 289 L/s
  maxInstantaneous: 1736,     // 1,736 L/s (3×DWF)
  peakFromNetwork: 2315       // 2,315 L/s from network model
};

StreamMassLoads ​

Mass loads for a stream in kg/d.

Properties ​

PropertyTypeDescription
tssnumberSuspended solids load (kg/d)
bodnumberBOD load (kg/d)
ammNnumberAmmonia nitrogen load (kg/d)
tpnumberTotal phosphorus load (kg/d)
codnumberundefined
tknnumberundefined

Example ​

typescript
const influentLoads: StreamMassLoads = {
  tss: 12500,      // 12.5 tonnes/d
  bod: 10000,      // 10 tonnes/d
  ammN: 2250,      // 2.25 tonnes/d
  tp: 400,         // 400 kg/d
  cod: 25000,      // 25 tonnes/d
  tkn: 2700        // 2.7 tonnes/d
};

// Calculate from flow and concentration
function calculateMassLoad(
  flow: number,        // m³/d
  concentration: number // mg/L
): number {
  return (flow * concentration) / 1000;  // kg/d
}

const tssLoad = calculateMassLoad(50000, 250);  // 12,500 kg/d

StreamConcentrations ​

Concentrations for a stream in mg/L.

Properties ​

PropertyTypeDescription
tssnumberSuspended solids concentration (mg/L)
bodnumberBOD concentration (mg/L)
ammNnumberAmmonia nitrogen concentration (mg/L)
tpnumberTotal phosphorus concentration (mg/L)
codnumberundefined
percentDSnumberundefined

Example ​

typescript
const influentConc: StreamConcentrations = {
  tss: 250,
  bod: 200,
  ammN: 45,
  tp: 8,
  cod: 500
};

const sludgeConc: StreamConcentrations = {
  tss: 12000,
  bod: 8000,
  ammN: 600,
  tp: 200,
  percentDS: 1.2    // 1.2% dry solids
};

// Calculate concentration from mass load and flow
function calculateConcentration(
  massLoad: number,   // kg/d
  flow: number        // m³/d
): number {
  if (flow < 0.001) return 0;
  return (massLoad * 1000) / flow;  // mg/L
}

StreamBalance ​

Complete stream balance data for a single stream.

Properties ​

PropertyTypeDescription
streamIdstringStream ID (F1, F2, etc.)
streamNumbernumberStream number (1, 2, 3...)
descriptionstringStream description
sourceUnitstringSource process unit
targetUnitstringTarget process unit
streamTypeStreamTypeStream type classification
flowsStreamFlowConditionsFlow conditions
massLoadsStreamMassLoadsMass loads (kg/d)
concentrationsStreamConcentrationsConcentrations at average flow (mg/L)
isSludgeStreambooleanWhether this is a sludge stream
notesstringundefined

Example ​

typescript
const stream1: StreamBalance = {
  streamId: 'F1',
  streamNumber: 1,
  description: 'Raw Influent',
  sourceUnit: 'Inlet Works',
  targetUnit: 'Primary Clarifiers',
  streamType: 'mainProcess',
  flows: {
    dwf: 50000,
    average: 60000,
    fft: 90000,
    minInstantaneous: 289,
    maxInstantaneous: 1736
  },
  massLoads: {
    tss: 12500,
    bod: 10000,
    ammN: 2250,
    tp: 400
  },
  concentrations: {
    tss: 250,
    bod: 200,
    ammN: 45,
    tp: 8
  },
  isSludgeStream: false,
  notes: 'Design basis from flow survey 2024'
};

const rasStream: StreamBalance = {
  streamId: 'F8',
  streamNumber: 8,
  description: 'Return Activated Sludge',
  sourceUnit: 'Secondary Clarifiers',
  targetUnit: 'Aeration Tanks',
  streamType: 'recirculation',
  flows: {
    dwf: 50000,
    average: 60000,
    fft: 90000
  },
  massLoads: {
    tss: 600000,    // 600 tonnes/d MLSS recirculation
    bod: 0,
    ammN: 0,
    tp: 0
  },
  concentrations: {
    tss: 10000,     // 10,000 mg/L RAS solids
    bod: 0,
    ammN: 0,
    tp: 0
  },
  isSludgeStream: true,
  notes: '1:1 RAS ratio at average flow'
};

Process Unit Data Structures ​

ProcessUnitBalance ​

Process unit configuration for mass balance.

Properties ​

PropertyTypeDescription
puIdstringProcess unit ID (PU1, PU2, etc.)
puNumbernumberProcess unit number
namestringProcess unit name
unitTypeProcessUnitTypeForBalanceProcess unit type
quantitynumberNumber of units
dutyConfigDutyConfigundefined
statusEquipmentStatusUnit status ('existing', 'new', 'to-be-removed')
inletStreamsstring[]Inlet stream IDs
outletStreamsstring[]Outlet stream IDs
removalEfficienciesRemovalEfficienciesundefined

Duty Configuration ​

PropertyTypeDescription
dutynumberNumber of duty units
assistnumberundefined
standbynumberundefined
labelstringConfiguration label (e.g., "2D+1S")

Removal Efficiencies ​

PropertyTypeDescription
bodnumberundefined
tssnumberundefined
ammNnumberundefined
tpnumberundefined

Example ​

typescript
const primaryClarifiers: ProcessUnitBalance = {
  puId: 'PU1',
  puNumber: 1,
  name: 'Primary Clarifiers',
  unitType: 'PST',
  quantity: 3,
  dutyConfig: {
    duty: 2,
    standby: 1,
    label: '2D+1S'
  },
  status: 'existing',
  inletStreams: ['F1'],
  outletStreams: ['F2', 'F3'],
  removalEfficiencies: {
    bod: 0.35,      // 35% BOD removal
    tss: 0.60,      // 60% TSS removal
    ammN: 0.0,      // No ammonia removal
    tp: 0.10        // 10% TP removal (settling)
  }
};

const aerationTanks: ProcessUnitBalance = {
  puId: 'PU3',
  puNumber: 3,
  name: 'Aeration Tanks',
  unitType: 'ASP',
  quantity: 4,
  dutyConfig: {
    duty: 4,
    label: '4D'
  },
  status: 'existing',
  inletStreams: ['F2', 'F8'],
  outletStreams: ['F4'],
  removalEfficiencies: {
    bod: 0.95,      // 95% BOD removal
    tss: 0.0,       // TSS generated (MLSS)
    ammN: 0.90,     // 90% ammonia removal
    tp: 0.30        // 30% TP removal (luxury uptake)
  }
};

Balance Verification ​

BalanceCheck ​

Balance check result for mass conservation.

Properties ​

PropertyTypeDescription
parameterBalanceParameterParameter being checked
locationstringProcess unit or junction
totalInnumberTotal input
totalOutnumberTotal output
differencenumberDifference (out - in)
percentDifferencenumberPercentage difference
balancedbooleanWhether balance closes within tolerance
notesstringundefined

Balance Parameters ​

'Flow' | 'BOD' | 'TSS' | 'AmmN' | 'TP'

Example ​

typescript
const flowBalance: BalanceCheck = {
  parameter: 'Flow',
  location: 'Primary Clarifiers',
  totalIn: 60000,        // m³/d
  totalOut: 60000,       // m³/d
  difference: 0,
  percentDifference: 0,
  balanced: true
};

const bodBalance: BalanceCheck = {
  parameter: 'BOD',
  location: 'Primary Clarifiers',
  totalIn: 12000,        // kg/d in
  totalOut: 8200,        // kg/d out (7800 effluent + 400 sludge)
  difference: -3800,     // 3800 kg/d removed
  percentDifference: -31.7,
  balanced: true,
  notes: '35% BOD removal, rest to sludge'
};

// Check if balance closes
function isBalanced(check: BalanceCheck, tolerance: number = 5): boolean {
  return Math.abs(check.percentDifference) <= tolerance;
}

Complete Mass Balance ​

ProcessCalcMassBalance ​

Complete process calculation mass balance.

Properties ​

PropertyTypeDescription
scenarioNamestringScenario name
scenarioTypeMassBalanceScenarioTypeScenario type
descriptionstringundefined
generatedAtDateGeneration timestamp
streamsStreamBalance[]All streams in the process
processUnitsProcessUnitBalance[]All process units
balanceChecksBalanceCheck[]Balance check results
notesStreamNote[]Stream notes and annotations
balanceStatusBalanceStatus'Balanced', 'Unbalanced', 'Warning'
summaryMassBalanceSummarySummary statistics

Example ​

typescript
const massBalance: ProcessCalcMassBalance = {
  scenarioName: 'Future Upgrade - Average Flow',
  scenarioType: 'Future',
  description: 'Future conditions after Phase 2 upgrades',
  generatedAt: new Date(),
  streams: [stream1, stream2, stream3],
  processUnits: [primaryClarifiers, aerationTanks],
  balanceChecks: [flowBalance, bodBalance],
  notes: [
    {
      streamId: 'F1',
      note: 'Design flows from 2024 flow survey',
      type: 'assumption'
    }
  ],
  balanceStatus: 'Balanced',
  summary: {
    totalInletFlow: 60000,
    totalEffluentFlow: 59500,
    totalSludgeProduction: 2400,
    overallBODRemoval: 95,
    overallTSSRemoval: 96,
    overallAmmNRemoval: 90,
    overallPRemoval: 75,
    numberOfProcessUnits: 12,
    numberOfStreams: 24
  }
};

MassBalanceSummary ​

Summary statistics for mass balance.

Properties ​

PropertyTypeDescription
totalInletFlownumberTotal inlet flow (m³/d)
totalEffluentFlownumberTotal final effluent flow (m³/d)
totalSludgeProductionnumberTotal sludge production (kg DS/d)
overallBODRemovalnumberBOD removal efficiency (%)
overallTSSRemovalnumberTSS removal efficiency (%)
overallAmmNRemovalnumberAmmonia removal efficiency (%)
overallPRemovalnumberP removal efficiency (%)
numberOfProcessUnitsnumberNumber of process units
numberOfStreamsnumberNumber of streams

Example ​

typescript
// Calculate overall removal efficiency
function calculateOverallRemoval(
  influentLoad: number,
  effluentLoad: number
): number {
  return ((influentLoad - effluentLoad) / influentLoad) * 100;
}

const summary: MassBalanceSummary = {
  totalInletFlow: 60000,
  totalEffluentFlow: 59500,
  totalSludgeProduction: 2400,
  overallBODRemoval: calculateOverallRemoval(12000, 600),  // 95%
  overallTSSRemoval: calculateOverallRemoval(15000, 600),  // 96%
  overallAmmNRemoval: calculateOverallRemoval(2700, 270),  // 90%
  overallPRemoval: calculateOverallRemoval(480, 120),      // 75%
  numberOfProcessUnits: 12,
  numberOfStreams: 24
};

Grid Display ​

GridRowDefinition ​

Grid row definition for mass balance table display.

Properties ​

PropertyTypeDescription
idstringRow ID
parameterstringParameter name
unitstringUnit of measurement
categoryGridRowCategoryCategory for grouping
getValue(stream: StreamBalance) => number | nullFunction to get value from stream
decimalsnumberNumber of decimal places

Grid Row Categories ​

  • 'flow' - Flow conditions
  • 'massLoad' - Mass loading rates
  • 'concentration' - Quality concentrations
  • 'special' - Special parameters (% DS, etc.)

Example ​

typescript
const customGridRows: GridRowDefinition[] = [
  {
    id: 'dwf',
    parameter: 'Dry weather flow',
    unit: 'm³/day',
    category: 'flow',
    getValue: (s) => s.flows.dwf,
    decimals: 0
  },
  {
    id: 'bodLoad',
    parameter: 'BOD',
    unit: 'kg/day',
    category: 'massLoad',
    getValue: (s) => s.massLoads.bod,
    decimals: 1
  },
  {
    id: 'tssConc',
    parameter: 'Avg conc SS',
    unit: 'mg/L',
    category: 'concentration',
    getValue: (s) => s.concentrations.tss,
    decimals: 0
  },
  {
    id: 'percentDS',
    parameter: '% DS',
    unit: '%',
    category: 'special',
    getValue: (s) => s.concentrations.percentDS ?? null,
    decimals: 2
  }
];

Constants ​

BALANCE_TOLERANCE_PERCENT ​

Balance tolerance percentage for mass conservation checks.

Value: 5 (5%)

MIN_FLOW_FOR_CONCENTRATION ​

Minimum flow for concentration calculation to avoid division by zero.

Value: 0.001 (m³/d)


Usage Examples ​

Creating a Mass Balance ​

typescript
// Build streams
const streams: StreamBalance[] = [
  {
    streamId: 'F1',
    streamNumber: 1,
    description: 'Raw Influent',
    sourceUnit: 'Inlet',
    targetUnit: 'PST',
    streamType: 'mainProcess',
    flows: { dwf: 50000, average: 60000, fft: 90000 },
    massLoads: { tss: 15000, bod: 12000, ammN: 2700, tp: 480 },
    concentrations: { tss: 250, bod: 200, ammN: 45, tp: 8 },
    isSludgeStream: false
  }
];

// Perform balance checks
const checks: BalanceCheck[] = streams.map(stream => ({
  parameter: 'Flow',
  location: stream.targetUnit,
  totalIn: stream.flows.average,
  totalOut: stream.flows.average,
  difference: 0,
  percentDifference: 0,
  balanced: true
}));

// Create mass balance
const massBalance: ProcessCalcMassBalance = {
  scenarioName: 'Current Conditions',
  scenarioType: 'Current',
  generatedAt: new Date(),
  streams,
  processUnits: [],
  balanceChecks: checks,
  notes: [],
  balanceStatus: 'Balanced',
  summary: {
    totalInletFlow: 60000,
    totalEffluentFlow: 59500,
    totalSludgeProduction: 2400,
    overallBODRemoval: 95,
    overallTSSRemoval: 96,
    overallAmmNRemoval: 90,
    overallPRemoval: 75,
    numberOfProcessUnits: 8,
    numberOfStreams: 20
  }
};