Phyth v0.1.0
A dimensional safe C++ header-only mechanics and electromagnetics simulation library.
Loading...
Searching...
No Matches
ElectricField.hpp
Go to the documentation of this file.
1#ifndef PHYTH_ELECTRIC_FIELD_HPP
2#define PHYTH_ELECTRIC_FIELD_HPP
3
4#include <algorithm>
5#include <memory>
6
9
31 class ElectricField : public Field<Vector3<Quantity<NewtonPerCoulomb> > > {
32 public:
33 ElectricField() = default;
34
43 void AddSource(std::shared_ptr<ChargeSource> source) {
44 if (source) {
45 sources_.insert(std::move(source));
46 }
47 }
48
58 template<typename... Args>
59 void AddSources(Args &&... args) {
60 (AddSource(std::forward<Args>(args)), ...);
61 }
62
72 GetValueAt(const Vector3<Quantity<Meter> > &point) const override {
74 for (const auto &source: sources_) {
75 total += source->GetElectricFieldAt(point);
76 }
77 return total;
78 }
79
90 auto total = 0_V;
91 for (const auto &source: sources_) {
92 total += source->GetElectricPotentialAt(point);
93 }
94 return total;
95 }
96
107 auto total = 0_Cpm3;
108 for (const auto &source: sources_) {
109 total += source->GetChargeDensityAt(point);
110 }
111 return total;
112 }
113
124 template<typename T, typename = std::enable_if<is_charge_source_v<T> > >
125 [[nodiscard]] std::unordered_set<std::shared_ptr<T> >
127 std::unordered_set<std::shared_ptr<T> > sources;
128 for (const auto &source: sources_) {
129 if (auto transformed_source = std::dynamic_pointer_cast<T>(source)) {
131 }
132 }
133 return sources;
134 }
135
144 auto total = 0_C;
145 for (const auto &source: sources_) {
146 total += source->GetTotalChargeValue();
147 }
148 return total;
149 }
150
152 void Clear() {
153 sources_.clear();
154 }
155
163 void RemoveSource(const std::shared_ptr<ChargeSource> &source) {
164 if (sources_.find(source) != sources_.end()) {
165 sources_.erase(source);
166 }
167 }
168
175 template<typename... Args>
176 void RemoveSources(Args &&... args) {
177 (RemoveSource(std::forward<Args>(args)), ...);
178 }
179
181 [[nodiscard]] std::unordered_set<std::shared_ptr<ChargeSource> > GetSources() const {
182 return sources_;
183 }
184
190 std::string toString() const {
191 return "ElectricField{"
192 "total_charge=" + QuantityToString(GetTotalChargeValue()) +
193 "}";
194 }
195
196 private:
197 std::unordered_set<std::shared_ptr<ChargeSource> > sources_;
198 };
199
205 inline std::ostream &operator<<(std::ostream &os, const ElectricField &field) {
206 os << field.toString();
207 return os;
208 }
209}
210
211#endif // PHYTH_ELECTRIC_FIELD_HPP
Electric field composed of multiple charge sources.
void AddSources(Args &&... args)
Add multiple charge sources.
std::unordered_set< std::shared_ptr< T > > GetChargeSources() const
Get all sources of a specific charge source type.
void Clear()
Remove all sources from the field.
void RemoveSources(Args &&... args)
Remove multiple sources.
Vector3< Quantity< NewtonPerCoulomb > > GetValueAt(const Vector3< Quantity< Meter > > &point) const override
Compute the total electric field at a point.
std::string toString() const
Convert the field to a string representation.
Quantity< Coulomb > GetTotalChargeValue() const
Get the total charge of all sources combined.
Quantity< CoulombPerCubicMeter > GetChargeDensityAt(const Vector3< Quantity< Meter > > &point) const
Compute the total charge density at a point.
void RemoveSource(const std::shared_ptr< ChargeSource > &source)
Remove a specific source from the field.
void AddSource(std::shared_ptr< ChargeSource > source)
Add a single charge source to the field.
std::unordered_set< std::shared_ptr< ChargeSource > > sources_
Quantity< Volt > GetElectricPotential(const Vector3< Quantity< Meter > > &point) const
Compute the total electric potential at a point.
std::unordered_set< std::shared_ptr< ChargeSource > > GetSources() const
Get all sources currently in the field.
Base class for physical fields (scalar or vector)
Definition Field.hpp:38
std::ostream & operator<<(std::ostream &os, const ElectricField &field)
Stream insertion operator for ElectricField.
std::string QuantityToString(const Quantity< UnitT > &q)
Convert a Quantity to a human-readable string.
Definition Quantity.hpp:738
constexpr bool is_unit_v
Definition Unit.hpp:79
Physical quantity with a value and a compile-time unit.
Definition Quantity.hpp:27
3D vector with Quantity components
Definition Vector3.hpp:28