Phyth v0.1.0
A dimensional safe C++ header-only mechanics and electromagnetics simulation library.
Loading...
Searching...
No Matches
MagneticField.hpp
Go to the documentation of this file.
1#ifndef PHYTH_MAGNETIC_FIELD_HPP
2#define PHYTH_MAGNETIC_FIELD_HPP
3
4#include <algorithm>
5#include <memory>
6
9
29 class MagneticField : public Field<Vector3<Quantity<Tesla> > > {
30 public:
31 MagneticField() = default;
32
41 void AddSource(std::shared_ptr<MagneticSource> source) {
42 if (source) {
43 sources_.insert(std::move(source));
44 }
45 }
46
56 template<typename... Args>
57 void AddSources(Args &&... args) {
58 (AddSource(std::forward<Args>(args)), ...);
59 }
60
70 GetValueAt(const Vector3<Quantity<Meter> > &point) const override {
72 for (const auto &source: sources_) {
73 total += source->GetMagneticFieldAt(point);
74 }
75 return total;
76 }
77
89 template<typename T, typename = std::enable_if<is_magnetic_source_v<T> > >
90 [[nodiscard]] std::unordered_set<std::shared_ptr<T> >
92 std::unordered_set<std::shared_ptr<T> > sources;
93 for (const auto &source: sources_) {
94 if (auto transformed_source = std::dynamic_pointer_cast<T>(source)) {
96 }
97 }
98 return sources;
99 }
100
102 void Clear() {
103 sources_.clear();
104 }
105
113 void RemoveSource(const std::shared_ptr<MagneticSource> &source) {
114 if (sources_.find(source) != sources_.end()) {
115 sources_.erase(source);
116 }
117 }
118
125 template<typename... Args>
126 void RemoveSources(Args &&... args) {
127 (RemoveSource(std::forward<Args>(args)), ...);
128 }
129
131 [[nodiscard]] std::unordered_set<std::shared_ptr<MagneticSource> > GetSources() const {
132 return sources_;
133 }
134
135 private:
136 std::unordered_set<std::shared_ptr<MagneticSource> > sources_;
137 };
138}
139
140#endif // PHYTH_MAGNETIC_FIELD_HPP
Magnetic field composed of multiple magnetic sources.
void AddSource(std::shared_ptr< MagneticSource > source)
Add a single magnetic source to the field.
void RemoveSources(Args &&... args)
Remove multiple sources.
std::unordered_set< std::shared_ptr< MagneticSource > > GetSources() const
Get all sources currently in the field.
Vector3< Quantity< Tesla > > GetValueAt(const Vector3< Quantity< Meter > > &point) const override
Compute the total magnetic field at a point.
void Clear()
Remove all sources from the field.
std::unordered_set< std::shared_ptr< MagneticSource > > sources_
void AddSources(Args &&... args)
Add multiple magnetic sources.
std::unordered_set< std::shared_ptr< T > > GetMagneticSources() const
Get all sources of a specific type.
void RemoveSource(const std::shared_ptr< MagneticSource > &source)
Remove a specific source from the field.
Base class for physical fields (scalar or vector)
Definition Field.hpp:38
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