FhSim  3.1.0
Marine systems simulation
Loading...
Searching...
No Matches
SensorEchosounder.h
1#pragma once
2
282#include "SampledSensor.h"
283
284#include <array>
285#include <fhsim/simobject/SimObject.h>
286#include <fstream>
287#include <marenv/marenv.h>
288#include <memory>
289#include <span>
290#include <string>
291#include <vector>
292
293namespace environment
294{
295class EnvironmentProvider;
296namespace field
297{
298class IntegrableField;
299class ScalarField;
300} // namespace field
301} // namespace environment
302
303class SensorEchosounder : public SimObject
304{
305public:
307 struct Settings
308 {
309 double pingPeriod = 0.5;
310 double maxRangeDown = 150.0;
311 double rayStepSize = 1.0;
312 double rayTolerance = 0.01;
313 double rangeBias = 0.02;
314 double rangeNoiseStd = 0.02;
315 double rangeNoiseRelative = 0.0005;
316
317 double beamWidthDeg = 7.0;
318 int beamSamples = 1;
319
320 double binSize = 1.0;
321 double svFloorDb = -140.0;
323
324 double minRange = 1.0;
325 double bottomOffset = 0.5;
326 double bottomThresholdDb = -30.0;
327
328 unsigned int noiseSeed = 0;
329 };
330
332 struct Reading
333 {
334 double seafloorDistance = 0.0;
335 double seafloorDepth = 0.0;
336 double altitude = 0.0;
337 double valid = 0.0;
338 double pingTime = 0.0;
339 };
340
342 struct Species
343 {
344 std::string name;
345 int handle = -1;
346 std::string speciesKey;
347 std::shared_ptr<const environment::field::ScalarField> field;
348 const environment::field::IntegrableField* integrable = nullptr;
349 double lengthCm = 0.0;
350 std::vector<double> sigmaBsPerKg;
351 };
352
357 SensorEchosounder(std::string simObjectName, ISimObjectCreator* creator);
358
370 SensorEchosounder(std::string simObjectName, const Settings& settings);
371
374
381 static double PerturbRange(double trueRange, const Settings& settings, SensorNoise& noise);
382
391 static bool IsValidRaySearch(double rayStepSize, double rayTolerance);
392
399 static bool IsValidMaxRange(double maxRange);
400
406 static double AltitudeOf(double slantRange, const double beamDirection[3]);
407
414 static int BinCountOf(double range, double binSize);
415
422 static double SampleRangeOf(int bin, double binSize, double beamRange);
423
441 static double DownSampleRangeOf(int bin, double binSize, double bottomRange, double clearance);
442
448 static double BottomClearance(double rayTolerance, const double beamDirection[3]);
449
455 static double BeamPattern(double offAxisRad, double beamWidthDeg);
456
462 static double EquivalentBeamAngle(double beamWidthDeg);
463
468 static double DiskRadiusPerRange(double beamWidthDeg);
469
474 static bool IsDiskBeam(const double axis[3], double halfWidthRad);
475
485 static void DiskConcentrations(const environment::field::IntegrableField& field, const double position[3], const double axis[3],
486 double binSize, double radiusPerRange, std::span<double> concentration);
487
495 static void SampleDirections(const double axis[3], int beamSamples, double ringAngleRad, std::vector<std::array<double, 3>>& directions);
496
505 static int DetectBottomBin(const std::vector<double>& svLinear, double binSize, double beamRange, double minRange, double thresholdDb);
506
517 static double IntegrateOverWindow(std::span<const double> perBin, double binSize, double beamRange, double windowStart, double windowEnd);
518
530 static std::string FieldStatusWarning(const std::string& sensorName, const std::string& fieldName, double depth, marenv::Status status);
531
538 static double NoiseFloorLinear(double noiseSvAt1mDb, double absorptionDbPerKm, double range);
539
541 void OdeFcn(const double dT, const double* const adX, double* const adXDot) const override;
542
547 void AcceptedStep(const double dT, const double* const adX) override;
548
554 void FinalSetup(const double dT, const double* const adX, ISimObjectCreator* const creator) override;
555
556#ifdef FH_VISUALIZATION
566 void RenderInit(Ogre::Root* const ogreRoot, ISimObjectCreator* const creator) override;
567
572 void RenderUpdate(const double dT, const double* const adX) override;
573#endif
574
576 const Reading& HeldReading() const { return m_reading; }
577
579 const Settings& GetSettings() const { return m_settings; }
580
582 const std::vector<Species>& GetSpecies() const { return m_species; }
583
585 const std::vector<double>& DiskRadiiPerRange() const { return m_diskRadiusPerRange; }
586
588 int PingsTaken() const { return m_pingsTaken; }
589
591 const std::vector<double>& Frequencies() const { return m_frequencykHz; }
592
593protected:
595 const double* OutSeafloorDistance(const double dT, const double* const adX);
597 const double* OutSeafloorDepth(const double dT, const double* const adX);
599 const double* OutAltitude(const double dT, const double* const adX);
601 const double* OutValid(const double dT, const double* const adX);
603 const double* OutPingTime(const double dT, const double* const adX);
605 const double* OutSa(const double dT, const double* const adX, const int index);
607 const double* OutIntegratedBiomass(const double dT, const double* const adX, const int index);
608
609private:
611 struct FieldWarnings
612 {
613 bool outOfRange = false;
614 bool error = false;
615 };
616
620 void ReadFrequencyArrays(ISimObjectCreator* const creator);
621
623 void PrepareFrequencyConstants();
624
628 void ReadFileParameters(ISimObjectCreator* const creator);
629
633 void RefuseRemovedParameters(ISimObjectCreator* const creator);
634
638 void ResolveSpecies(ISimObjectCreator* const creator);
639
644 void Ping(double dT, const double* const adX);
645
653 double MeasureBottom(double dT, const double position[3], const double beamDown[3]);
654
662 void UpdateAbsorption(double dT, const double position[3], const double direction[3], double beamRange);
663
671 void FormPingColumns(double dT, const double position[3], const double direction[3], double bottomRange);
672
679 void WriteRow(double dT, const double position[3], size_t channel, const std::vector<double>& svLinear);
680
684 void OpenFileAndWriteHeader(ISimObjectCreator* const creator);
685
686 SampledSensor m_sampler;
687 Settings m_settings;
688
689 std::vector<std::string> m_speciesFieldNames;
690 bool m_autoDiscover = false;
691 std::vector<Species> m_species;
692 std::string m_parameterLibrary;
693
694 std::vector<double> m_frequencykHz;
695 std::vector<double> m_beamWidthDeg;
696 std::vector<double> m_absorptionDbPerKm;
697 bool m_absorptionGiven = false;
698 std::vector<double> m_bottomBackscatterDb;
699 std::vector<double> m_noiseSvAt1mDb;
700 std::vector<double> m_svBiasDb;
701
702 bool m_writeFile = true;
703 std::string m_fileName;
704 int m_flushEveryNPings = 20;
705 int m_svPrecision = 2;
706
707 std::unique_ptr<std::ofstream> m_stream;
708
709 int m_pingsTaken = 0;
710
711 FieldWarnings m_fieldWarned;
712
713 Reading m_reading;
714 std::vector<double> m_sa;
715 std::vector<double> m_integratedBiomass;
716
717 double m_halfWidthRad = 0.0;
718 std::vector<double> m_ringWeight;
719 std::vector<double> m_diskRadiusPerRange;
720 std::vector<size_t> m_diskOfFrequency;
721 std::vector<double> m_svBiasLinear;
722 std::vector<double> m_bottomBackscatterLinear;
723
725 std::vector<std::array<double, 3>> m_directions;
727 std::vector<double> m_axisConcentration;
729 std::vector<double> m_diskConcentration;
731 std::vector<double> m_ringConcentration;
733 std::vector<std::vector<double>> m_svLinear;
735 std::string m_row;
736
737#ifdef FH_VISUALIZATION
738 Ogre::SceneManager* m_sceneMgr = nullptr;
739 Ogre::SceneNode* m_beamNode = nullptr;
740 Ogre::ManualObject* m_beamObject = nullptr;
741#endif
742};
Definition SampledSensor.h:49
Definition SensorEchosounder.h:304
static double NoiseFloorLinear(double noiseSvAt1mDb, double absorptionDbPerKm, double range)
static bool IsDiskBeam(const double axis[3], double halfWidthRad)
static double EquivalentBeamAngle(double beamWidthDeg)
const double * OutSeafloorDepth(const double dT, const double *const adX)
The inferred depth of the seafloor, m.
const double * OutAltitude(const double dT, const double *const adX)
The measured height above the seafloor, m.
const double * OutValid(const double dT, const double *const adX)
One when the last ping found a bottom, zero otherwise.
static int DetectBottomBin(const std::vector< double > &svLinear, double binSize, double beamRange, double minRange, double thresholdDb)
const std::vector< double > & Frequencies() const
The operating frequencies, kHz.
Definition SensorEchosounder.h:591
static double SampleRangeOf(int bin, double binSize, double beamRange)
static double AltitudeOf(double slantRange, const double beamDirection[3])
void FinalSetup(const double dT, const double *const adX, ISimObjectCreator *const creator) override
static double PerturbRange(double trueRange, const Settings &settings, SensorNoise &noise)
int PingsTaken() const
The number of pings taken so far.
Definition SensorEchosounder.h:588
static double BeamPattern(double offAxisRad, double beamWidthDeg)
const double * OutSeafloorDistance(const double dT, const double *const adX)
The measured slant range to the seafloor, m.
static double BottomClearance(double rayTolerance, const double beamDirection[3])
const std::vector< double > & DiskRadiiPerRange() const
The disk radius per range of each distinct beam width, in order of first appearance among the frequen...
Definition SensorEchosounder.h:585
static double IntegrateOverWindow(std::span< const double > perBin, double binSize, double beamRange, double windowStart, double windowEnd)
SensorEchosounder(std::string simObjectName, const Settings &settings)
static int BinCountOf(double range, double binSize)
static double DiskRadiusPerRange(double beamWidthDeg)
void OdeFcn(const double dT, const double *const adX, double *const adXDot) const override
Writes no derivatives: this SimObject declares no states.
const double * OutSa(const double dT, const double *const adX, const int index)
sa over the Integration window at one frequency, m^2/m^2.
SensorEchosounder(std::string simObjectName, ISimObjectCreator *creator)
const std::vector< Species > & GetSpecies() const
The species entering the acoustic model, in file column order.
Definition SensorEchosounder.h:582
const Reading & HeldReading() const
The reading currently held on the output ports.
Definition SensorEchosounder.h:576
static void DiskConcentrations(const environment::field::IntegrableField &field, const double position[3], const double axis[3], double binSize, double radiusPerRange, std::span< double > concentration)
static void SampleDirections(const double axis[3], int beamSamples, double ringAngleRad, std::vector< std::array< double, 3 > > &directions)
static std::string FieldStatusWarning(const std::string &sensorName, const std::string &fieldName, double depth, marenv::Status status)
static double DownSampleRangeOf(int bin, double binSize, double bottomRange, double clearance)
const double * OutIntegratedBiomass(const double dT, const double *const adX, const int index)
The Integrated biomass of one species field, kg/m^2.
~SensorEchosounder() override
Flushes and closes the echogram file, and releases the visualisation geometry.
static bool IsValidMaxRange(double maxRange)
const double * OutPingTime(const double dT, const double *const adX)
The time of the ping the held reading came from, s.
static bool IsValidRaySearch(double rayStepSize, double rayTolerance)
void AcceptedStep(const double dT, const double *const adX) override
const Settings & GetSettings() const
The settings the instrument was built with.
Definition SensorEchosounder.h:579
Definition SensorNoise.h:43
Status
Definition marenv.h:44
The altimeter reading held on the output ports between pings.
Definition SensorEchosounder.h:333
double valid
One when a bottom was found, zero otherwise.
Definition SensorEchosounder.h:337
double pingTime
The time of the ping this reading came from, s.
Definition SensorEchosounder.h:338
double seafloorDepth
The inferred depth of the seafloor, m.
Definition SensorEchosounder.h:335
double seafloorDistance
The measured slant range to the seafloor, m.
Definition SensorEchosounder.h:334
double altitude
The measured height above the seafloor, m.
Definition SensorEchosounder.h:336
Everything the instrument is configured with.
Definition SensorEchosounder.h:308
double independentSamplesPerBin
The Gamma shape of the speckle.
Definition SensorEchosounder.h:322
double rayTolerance
The convergence tolerance of the ray intersection, m.
Definition SensorEchosounder.h:312
double rangeNoiseRelative
The relative range noise standard deviation.
Definition SensorEchosounder.h:315
double beamWidthDeg
The widest full beam opening angle, degrees; the drawn cone and the sampling ring.
Definition SensorEchosounder.h:317
double minRange
The start of the Integration window, the near-field limit, m.
Definition SensorEchosounder.h:324
double rangeBias
The constant offset of the measured range, m.
Definition SensorEchosounder.h:313
double bottomOffset
The Integration window ends this far short of the detected bottom, m.
Definition SensorEchosounder.h:325
double svFloorDb
The lowest Sv ever reported, dB.
Definition SensorEchosounder.h:321
int beamSamples
The samples across the beam per range bin: the axis, then a ring.
Definition SensorEchosounder.h:318
double bottomThresholdDb
The Sv a bin needs to be detected as the bottom, dB.
Definition SensorEchosounder.h:326
double rayStepSize
The search step of the ray intersection, m.
Definition SensorEchosounder.h:311
unsigned int noiseSeed
The random seed; zero is a perfect instrument.
Definition SensorEchosounder.h:328
double maxRangeDown
The longest slant range searched and binned, m.
Definition SensorEchosounder.h:310
double rangeNoiseStd
The absolute range noise standard deviation, m.
Definition SensorEchosounder.h:314
double binSize
The echogram range bin size, m.
Definition SensorEchosounder.h:320
double pingPeriod
The time between pings, s.
Definition SensorEchosounder.h:309
One Species Concentration field as the acoustic model sees it.
Definition SensorEchosounder.h:343
std::shared_ptr< const environment::field::ScalarField > field
The field, kept alive here; set at bind time.
Definition SensorEchosounder.h:347
const environment::field::IntegrableField * integrable
field as an IntegrableField (D20), or null: then the species is point sampled.
Definition SensorEchosounder.h:348
int handle
The field handle, resolved once in FinalSetup.
Definition SensorEchosounder.h:345
std::string name
The scalar field name.
Definition SensorEchosounder.h:344
double lengthCm
The field's mean length, cm.
Definition SensorEchosounder.h:349
std::string speciesKey
The field's species, a key of the Parameter library.
Definition SensorEchosounder.h:346
std::vector< double > sigmaBsPerKg
sigma_bs per kg at each frequency, m^2/kg.
Definition SensorEchosounder.h:350