Phyth v0.1.0
A dimensional safe C++ header-only mechanics and electromagnetics simulation library.
Loading...
Searching...
No Matches
LineCharge.hpp
Go to the documentation of this file.
1#ifndef PHYTH_LINE_CHARGE_HPP
2#define PHYTH_LINE_CHARGE_HPP
3
4#include "ChargeSource.hpp"
5
28 class LineCharge : public ChargeSource {
29 public:
44
57 }
58
74 }
75
82 return lambda_ * length_;
83 }
84
93 const auto v = point - start_;
94 const auto t = Utils::clamp(v.Dot(direction_), 0_m, length_);
95 const auto closest_point = start_ + t * direction_;
96 return (point - closest_point).Length();
97 }
98
99 private:
104
123 const auto r1 = (point - start_).Length();
124 const auto r2 = (point - end_).Length();
125
127 throw std::runtime_error("LineCharge: potential diverges on line charge");
128 }
129
130 const auto numerator = r1 + r2 + length_;
131 const auto denominator = r1 + r2 - length_;
132
134 throw std::runtime_error("LineCharge: point lies on the line charge");
135 }
136
138 }
139
153 const auto dl = length_ / segments;
154
155 for (int i = 0; i < segments; ++i) {
156 const double t = (i + 0.5) / segments;
157 const auto pos = start_ + t * (end_ - start_);
158 const auto r = point - pos;
159 const auto dist = r.Length();
160
161 if (dist < Quantity<Meter>(Config::epsilon)) continue;
162
163 const auto dq = lambda_ * dl;
164 const auto dE = Consts::k_E * dq / (dist * dist) * r.Normalized();
165 E += dE;
166 }
167 return E;
168 }
169 };
170}
171
172#endif // PHYTH_LINE_CHARGE_HPP
Abstract base class for all charge distributions.
Uniform line charge along a straight segment.
Quantity< Coulomb > GetTotalChargeValue() const override
Get the total charge of the line.
Vector3< Quantity< Meter > > start_
Vector3< Quantity< NewtonPerCoulomb > > GetElectricFieldAt(const Vector3< Quantity< Meter > > &point) const override
Compute the electric field at a point using numerical integration.
LineCharge(const Vector3< Quantity< Meter > > &start, const Vector3< Quantity< Meter > > &end, const Quantity< CoulombPerMeter > lambda)
Construct a uniform line charge.
Quantity< Meter > GetMinimumDistanceTo(const Vector3< Quantity< Meter > > &point) const override
Get the minimum distance from a point to the line segment.
Quantity< Volt > AnalyticPotential(const Vector3< Quantity< Meter > > &point) const
Analytic potential for finite line charge.
Vector3< Quantity< Meter > > end_
Quantity< CoulombPerMeter > lambda_
Quantity< Volt > GetElectricPotentialAt(const Vector3< Quantity< Meter > > &point) const override
Compute the electric potential at a point using analytic formula.
Vector3< Quantity< NewtonPerCoulomb > > NumericalIntegrateField(const Vector3< Quantity< Meter > > &point, const int segments) const
Numerical integration of electric field along the line.
double epsilon
Definition Config.hpp:13
constexpr auto k_E
Scalar log(const Scalar q)
Natural logarithm (base e)
Quantity< UnitT > clamp(Quantity< UnitT > x, Quantity< UnitT > min, Quantity< UnitT > max)
Clamp a quantity between min and max bounds.
Dimension< std::ratio< 1 >, std::ratio< 0 >, std::ratio< 0 >, std::ratio< 0 >, std::ratio< 0 >, std::ratio< 0 >, std::ratio< 0 > > Length
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