FhSim  3.1.0
Marine systems simulation
Loading...
Searching...
No Matches
SensorEchosounder.h
1#pragma once
2
281#include "SampledSensor.h"
282
283#include <array>
284#include <fhsim/simobject/SimObject.h>
285#include <fstream>
286#include <marenv/field/IntegrableField.h>
287#include <marenv/field/ScalarField.h>
288#include <marenv/marenv.h>
289#include <memory>
290#include <span>
291#include <string>
292#include <vector>
293
294namespace environment
295{
296class EnvironmentProvider;
297namespace field
298{
299class IntegrableField;
300class ScalarField;
301} // namespace field
302} // namespace environment
303
304class SensorEchosounder : public SimObject
305{
306public:
308 struct Settings
309 {
310 double pingPeriod = 0.5;
311 double maxRangeDown = 150.0;
312 double rayStepSize = 1.0;
313 double rayTolerance = 0.01;
314 double rangeBias = 0.02;
315 double rangeNoiseStd = 0.02;
316 double rangeNoiseRelative = 0.0005;
317
318 double beamWidthDeg = 7.0;
319 int beamSamples = 1;
320
321 double binSize = 1.0;
322 double svFloorDb = -140.0;
324
325 double minRange = 1.0;
326 double bottomOffset = 0.5;
327 double bottomThresholdDb = -30.0;
328
329 unsigned int noiseSeed = 0;
330 };
331
333 struct Reading
334 {
335 double seafloorDistance = 0.0;
336 double seafloorDepth = 0.0;
337 double altitude = 0.0;
338 double valid = 0.0;
339 double pingTime = 0.0;
340 };
341
343 struct Species
344 {
345 std::string name;
346 int handle = -1;
347 std::string speciesKey;
348 std::shared_ptr<const marenv::field::ScalarField> field;
350 double lengthCm = 0.0;
351 std::vector<double> sigmaBsPerKg;
352 };
353
358 SensorEchosounder(std::string simObjectName, ISimObjectCreator* creator);
359
371 SensorEchosounder(std::string simObjectName, const Settings& settings);
372
375
382 static double PerturbRange(double trueRange, const Settings& settings, SensorNoise& noise);
383
392 static bool IsValidRaySearch(double rayStepSize, double rayTolerance);
393
400 static bool IsValidMaxRange(double maxRange);
401
407 static double AltitudeOf(double slantRange, const double beamDirection[3]);
408
415 static int BinCountOf(double range, double binSize);
416
423 static double SampleRangeOf(int bin, double binSize, double beamRange);
424
442 static double DownSampleRangeOf(int bin, double binSize, double bottomRange, double clearance);
443
449 static double BottomClearance(double rayTolerance, const double beamDirection[3]);
450
456 static double BeamPattern(double offAxisRad, double beamWidthDeg);
457
463 static double EquivalentBeamAngle(double beamWidthDeg);
464
469 static double DiskRadiusPerRange(double beamWidthDeg);
470
475 static bool IsDiskBeam(const double axis[3], double halfWidthRad);
476
486 static void DiskConcentrations(const marenv::field::IntegrableField& field, const double position[3], const double axis[3],
487 double binSize, double radiusPerRange, std::span<double> concentration);
488
496 static void SampleDirections(const double axis[3], int beamSamples, double ringAngleRad, std::vector<std::array<double, 3>>& directions);
497
506 static int DetectBottomBin(const std::vector<double>& svLinear, double binSize, double beamRange, double minRange, double thresholdDb);
507
518 static double IntegrateOverWindow(std::span<const double> perBin, double binSize, double beamRange, double windowStart, double windowEnd);
519
531 static std::string FieldStatusWarning(const std::string& sensorName, const std::string& fieldName, double depth, marenv::Status status);
532
539 static double NoiseFloorLinear(double noiseSvAt1mDb, double absorptionDbPerKm, double range);
540
542 void OdeFcn(const double dT, const double* const adX, double* const adXDot) const override;
543
548 void AcceptedStep(const double dT, const double* const adX) override;
549
555 void FinalSetup(const double dT, const double* const adX, ISimObjectCreator* const creator) override;
556
557#ifdef FH_VISUALIZATION
567 void RenderInit(Ogre::Root* const ogreRoot, ISimObjectCreator* const creator) override;
568
573 void RenderUpdate(const double dT, const double* const adX) override;
574
578 void RenderRelease() override;
579#endif
580
582 const Reading& HeldReading() const { return m_reading; }
583
585 const Settings& GetSettings() const { return m_settings; }
586
588 const std::vector<Species>& GetSpecies() const { return m_species; }
589
591 const std::vector<double>& DiskRadiiPerRange() const { return m_diskRadiusPerRange; }
592
594 int PingsTaken() const { return m_pingsTaken; }
595
597 const std::vector<double>& Frequencies() const { return m_frequencykHz; }
598
599protected:
601 const double* OutSeafloorDistance(const double dT, const double* const adX);
603 const double* OutSeafloorDepth(const double dT, const double* const adX);
605 const double* OutAltitude(const double dT, const double* const adX);
607 const double* OutValid(const double dT, const double* const adX);
609 const double* OutPingTime(const double dT, const double* const adX);
611 const double* OutSa(const double dT, const double* const adX, const int index);
613 const double* OutIntegratedBiomass(const double dT, const double* const adX, const int index);
614
615private:
617 struct FieldWarnings
618 {
619 bool outOfRange = false;
620 bool error = false;
621 };
622
626 void ReadFrequencyArrays(ISimObjectCreator* const creator);
627
629 void PrepareFrequencyConstants();
630
634 void ReadFileParameters(ISimObjectCreator* const creator);
635
639 void RefuseRemovedParameters(ISimObjectCreator* const creator);
640
644 void ResolveSpecies(ISimObjectCreator* const creator);
645
650 void Ping(double dT, const double* const adX);
651
659 double MeasureBottom(double dT, const double position[3], const double beamDown[3]);
660
668 void UpdateAbsorption(double dT, const double position[3], const double direction[3], double beamRange);
669
677 void FormPingColumns(double dT, const double position[3], const double direction[3], double bottomRange);
678
685 void WriteRow(double dT, const double position[3], size_t channel, const std::vector<double>& svLinear);
686
690 void OpenFileAndWriteHeader(ISimObjectCreator* const creator);
691
692 SampledSensor m_sampler;
693 Settings m_settings;
694
695 std::vector<std::string> m_speciesFieldNames;
696 bool m_autoDiscover = false;
697 std::vector<Species> m_species;
698 std::string m_parameterLibrary;
699
700 std::vector<double> m_frequencykHz;
701 std::vector<double> m_beamWidthDeg;
702 std::vector<double> m_absorptionDbPerKm;
703 bool m_absorptionGiven = false;
704 std::vector<double> m_bottomBackscatterDb;
705 std::vector<double> m_noiseSvAt1mDb;
706 std::vector<double> m_svBiasDb;
707
708 bool m_writeFile = true;
709 std::string m_fileName;
710 int m_flushEveryNPings = 20;
711 int m_svPrecision = 2;
712
713 std::unique_ptr<std::ofstream> m_stream;
714
715 int m_pingsTaken = 0;
716
717 FieldWarnings m_fieldWarned;
718
719 Reading m_reading;
720 std::vector<double> m_sa;
721 std::vector<double> m_integratedBiomass;
722
723 double m_halfWidthRad = 0.0;
724 std::vector<double> m_ringWeight;
725 std::vector<double> m_diskRadiusPerRange;
726 std::vector<size_t> m_diskOfFrequency;
727 std::vector<double> m_svBiasLinear;
728 std::vector<double> m_bottomBackscatterLinear;
729
731 std::vector<std::array<double, 3>> m_directions;
733 std::vector<double> m_axisConcentration;
735 std::vector<double> m_diskConcentration;
737 std::vector<double> m_ringConcentration;
739 std::vector<std::vector<double>> m_svLinear;
741 std::string m_row;
742
743#ifdef FH_VISUALIZATION
744 Ogre::SceneManager* m_sceneMgr = nullptr;
745 Ogre::SceneNode* m_beamNode = nullptr;
746 Ogre::ManualObject* m_beamObject = nullptr;
747#endif
748};
Definition SampledSensor.h:49
Definition SensorEchosounder.h:305
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:597
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:594
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:591
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.
static void DiskConcentrations(const marenv::field::IntegrableField &field, const double position[3], const double axis[3], double binSize, double radiusPerRange, std::span< double > concentration)
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:588
const Reading & HeldReading() const
The reading currently held on the output ports.
Definition SensorEchosounder.h:582
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:585
Definition SensorNoise.h:43
Definition IntegrableField.h:41
Status
Definition marenv.h:44
Definition FlukeAnchor.h:124
The altimeter reading held on the output ports between pings.
Definition SensorEchosounder.h:334
double valid
One when a bottom was found, zero otherwise.
Definition SensorEchosounder.h:338
double pingTime
The time of the ping this reading came from, s.
Definition SensorEchosounder.h:339
double seafloorDepth
The inferred depth of the seafloor, m.
Definition SensorEchosounder.h:336
double seafloorDistance
The measured slant range to the seafloor, m.
Definition SensorEchosounder.h:335
double altitude
The measured height above the seafloor, m.
Definition SensorEchosounder.h:337
Everything the instrument is configured with.
Definition SensorEchosounder.h:309
double independentSamplesPerBin
The Gamma shape of the speckle.
Definition SensorEchosounder.h:323
double rayTolerance
The convergence tolerance of the ray intersection, m.
Definition SensorEchosounder.h:313
double rangeNoiseRelative
The relative range noise standard deviation.
Definition SensorEchosounder.h:316
double beamWidthDeg
The widest full beam opening angle, degrees; the drawn cone and the sampling ring.
Definition SensorEchosounder.h:318
double minRange
The start of the Integration window, the near-field limit, m.
Definition SensorEchosounder.h:325
double rangeBias
The constant offset of the measured range, m.
Definition SensorEchosounder.h:314
double bottomOffset
The Integration window ends this far short of the detected bottom, m.
Definition SensorEchosounder.h:326
double svFloorDb
The lowest Sv ever reported, dB.
Definition SensorEchosounder.h:322
int beamSamples
The samples across the beam per range bin: the axis, then a ring.
Definition SensorEchosounder.h:319
double bottomThresholdDb
The Sv a bin needs to be detected as the bottom, dB.
Definition SensorEchosounder.h:327
double rayStepSize
The search step of the ray intersection, m.
Definition SensorEchosounder.h:312
unsigned int noiseSeed
The random seed; zero is a perfect instrument.
Definition SensorEchosounder.h:329
double maxRangeDown
The longest slant range searched and binned, m.
Definition SensorEchosounder.h:311
double rangeNoiseStd
The absolute range noise standard deviation, m.
Definition SensorEchosounder.h:315
double binSize
The echogram range bin size, m.
Definition SensorEchosounder.h:321
double pingPeriod
The time between pings, s.
Definition SensorEchosounder.h:310
One Species Concentration field as the acoustic model sees it.
Definition SensorEchosounder.h:344
std::shared_ptr< const marenv::field::ScalarField > field
The field, kept alive here; set at bind time.
Definition SensorEchosounder.h:348
int handle
The field handle, resolved once in FinalSetup.
Definition SensorEchosounder.h:346
std::string name
The scalar field name.
Definition SensorEchosounder.h:345
double lengthCm
The field's mean length, cm.
Definition SensorEchosounder.h:350
const marenv::field::IntegrableField * integrable
field as an IntegrableField (D20), or null: then the species is point sampled.
Definition SensorEchosounder.h:349
std::string speciesKey
The field's species, a key of the Parameter library.
Definition SensorEchosounder.h:347
std::vector< double > sigmaBsPerKg
sigma_bs per kg at each frequency, m^2/kg.
Definition SensorEchosounder.h:351