FhSim  3.1.0
Marine systems simulation
Loading...
Searching...
No Matches
BlendedField.h
1#pragma once
4
5#include <marenv/marenv.h>
6#include <marenv/wake/FieldSketch.h>
7#include <marenv/wake/JetField.h>
8#include <marenv/wake/WakeBlend.h>
9#include <marenv/wake/WakeField.h>
10
11#include <array>
12#include <cstddef>
13#include <cstdint>
14#include <memory>
15#include <span>
16
17namespace marenv::wake
18{
19namespace detail
20{
22template<class S>
23const double* ZeroDeficitVelocity(const S& snapshot)
24{
25 static constexpr double kZero[3] = {0.0, 0.0, 0.0};
26 if constexpr (requires { snapshot.sourceVelocity; })
27 return snapshot.sourceVelocity;
28 else
29 return kZero;
30}
31} // namespace detail
32
37{
38 using Field = WakeField;
39 using Value = WakeSample;
40 using Sum = WakeBlendSum;
43
45 template<class S>
46 static Status ToSample(const Sum& sum, const S& current, Value& out)
47 {
48 return sum.ToSample(detail::ZeroDeficitVelocity(current), out);
49 }
50
52 template<class S>
53 static void FillParts(Parts& parts, const S& current)
54 {
55 const double* velocity = detail::ZeroDeficitVelocity(current);
56 for (int i = 0; i < 3; ++i)
57 parts.zeroDeficitVelocity[i] = velocity[i];
58 }
59};
60
65{
66 using Field = JetField;
67 using Value = JetSample;
68 using Sum = JetBlendSum;
71
73 template<class S>
74 static Status ToSample(const Sum& sum, const S& /*current*/, Value& out)
75 {
76 return sum.ToSample(out);
77 }
78
80 template<class S>
81 static void FillParts(Parts& /*parts*/, const S& /*current*/)
82 {
83 }
84};
85
102template<class Snapshot, class Traits = WakeFieldTraits>
103class BlendedField : public Traits::Field
104{
105public:
106 using Value = typename Traits::Value;
107
110 Status Sample(double t, const double pos[3], Value& out) const override { return SampleWith(t, pos, SnapshotSampler {}, out); }
111
115 typename Traits::Parts SampleParts(double t, const double pos[3]) const override { return PartsWith(t, pos, SnapshotSampler {}); }
116
120 bool MayReach(const WakeQueryRegion& region) const override
121 {
122 const auto state = m_blend.Load();
123 return !state || Blend::MayReach(*state, region);
124 }
125
127 std::uint64_t Revision() const override { return m_blend.Revision(); }
128
130 bool StaticFrom(double t) const override { return m_blend.StaticFrom(t); }
131
133 void SampleMany(double t, std::span<const std::array<double, 3>> positions, std::span<Value> out, std::span<Status> status) const override
134 {
135 const auto state = m_blend.Load();
136 for (std::size_t i = 0; i < positions.size(); ++i)
137 status[i] = SampleState(state.get(), t, positions[i].data(), SnapshotSampler {}, out[i]);
138 }
139
141 void Sketch(double t, FieldSketch& out) const override
142 {
143 if (const auto state = m_blend.Load())
144 state->current->Sketch(t, out);
145 }
146
147protected:
149
152 template<class Sampler>
153 Status SampleWith(double t, const double pos[3], const Sampler& sampler, Value& out) const
154 {
155 const auto state = m_blend.Load();
156 return SampleState(state.get(), t, pos, sampler, out);
157 }
158
160 template<class Sampler>
161 typename Traits::Parts PartsWith(double t, const double pos[3], const Sampler& sampler) const
162 {
163 const auto state = m_blend.Load();
164 typename Traits::Parts parts;
165 if (!state || !Blend::Blends(*state, t)) {
166 parts.status = SampleState(state.get(), t, pos, sampler, parts.sample);
167 return parts;
168 }
169 parts.blending = true;
170 parts.blend = Blend::Samples(*state, t, pos, sampler);
171 Traits::FillParts(parts, *state->current);
172 return parts;
173 }
174
176
177private:
179 struct SnapshotSampler
180 {
181 bool operator()(const Snapshot& snapshot, double t, const double* pos, Value& out) const { return snapshot.Sample(t, pos, out); }
182 };
183
185 template<class Sampler>
186 static Status SampleState(const typename Blend::State* state, double t, const double pos[3], const Sampler& sampler, Value& out)
187 {
188 if (!state) {
189 out = Value {};
191 }
192 return Traits::ToSample(Blend::Evaluate(*state, t, pos, sampler), *state->current, out);
193 }
194};
195} // namespace marenv::wake
Definition BlendedField.h:104
void Sketch(double t, FieldSketch &out) const override
Appends the sketch of the snapshot blended to at t (see WakeField::Sketch()); nothing before the firs...
Definition BlendedField.h:141
bool MayReach(const WakeQueryRegion &region) const override
Definition BlendedField.h:120
Status SampleWith(double t, const double pos[3], const Sampler &sampler, Value &out) const
Definition BlendedField.h:153
Blend m_blend
The published snapshots and the blend between them (single writer).
Definition BlendedField.h:175
void SampleMany(double t, std::span< const std::array< double, 3 > > positions, std::span< Value > out, std::span< Status > status) const override
Sample() at every point, the published state loaded once (see WakeField::SampleMany()).
Definition BlendedField.h:133
std::uint64_t Revision() const override
The number of publishes (see WakeField::Revision()).
Definition BlendedField.h:127
bool StaticFrom(double t) const override
No blend at t or later and the published snapshot at rest (see WakeField::StaticFrom()).
Definition BlendedField.h:130
Traits::Parts PartsWith(double t, const double pos[3], const Sampler &sampler) const
SampleParts() with each snapshot sampled by sampler, as SampleWith().
Definition BlendedField.h:161
Status Sample(double t, const double pos[3], Value &out) const override
Definition BlendedField.h:110
Traits::Parts SampleParts(double t, const double pos[3]) const override
Definition BlendedField.h:115
Definition JetField.h:134
Definition WakeBlend.h:68
std::uint64_t Revision() const
The number of publishes so far (WakeField::Revision()).
Definition WakeBlend.h:136
std::shared_ptr< const State > Load() const
The published state, one atomic load; null before the first Publish(). A query loads it once and uses...
Definition WakeBlend.h:86
static bool Blends(const State &state, double t)
Whether the state blends at t or later: the weight of t is below 1, so it still changes (owner ruling...
Definition WakeBlend.h:133
static typename Traits::Samples Samples(const State &state, double t, const double pos[3])
Definition WakeBlend.h:155
static bool MayReach(const State &state, const WakeQueryRegion &region)
Definition WakeBlend.h:180
static Sum Evaluate(const State &state, double t, const double pos[3])
The sums of the snapshots of state at pos and t, weighted by the blend.
Definition WakeBlend.h:105
bool StaticFrom(double t) const
Definition WakeBlend.h:142
Definition WakeField.h:56
Status
Definition marenv.h:40
@ ERROR_NOT_SET
No valid data (outside a field's horizontal extent, a NaN position, no field); the value is not set (...
const double * ZeroDeficitVelocity(const S &snapshot)
The source velocity a wake field reports where d = 0: the snapshot's sourceVelocity if it has one (Gr...
Definition BlendedField.h:23
Definition FieldSketch.h:16
Definition JetField.h:65
Definition JetField.h:30
Status ToSample(JetSample &out) const
Definition JetField.h:53
Definition BlendedField.h:65
static void FillParts(Parts &, const S &)
A jet has no zero-deficit velocity: nothing to set.
Definition BlendedField.h:81
static Status ToSample(const Sum &sum, const S &, Value &out)
The field's sample from the sum of the blend.
Definition BlendedField.h:74
Definition JetField.h:92
Definition JetField.h:20
Definition WakeSampleParts.h:79
Definition WakeSampleParts.h:29
Status ToSample(const double zeroDeficitVelocity[3], WakeSample &out) const
Definition WakeSampleParts.h:58
Definition BlendedField.h:37
static Status ToSample(const Sum &sum, const S &current, Value &out)
The field's sample from the sums of the blend; where d = 0 the source velocity of the snapshot blende...
Definition BlendedField.h:46
static void FillParts(Parts &parts, const S &current)
Sets the source velocity the parts report where d = 0 (as ToSample()).
Definition BlendedField.h:53
Definition WakeField.h:23
Definition WakeSampleParts.h:111
double zeroDeficitVelocity[3]
Blending: the source velocity the field reports where d = 0, m/s, NED.
Definition WakeSampleParts.h:116
Definition WakeTypes.h:21