Phyth v0.1.0
A dimensional safe C++ header-only mechanics and electromagnetics simulation library.
Loading...
Searching...
No Matches
VolumeSource.hpp
Go to the documentation of this file.
1#ifndef PHYTH_VOLUME_SOURCE_HPP
2#define PHYTH_VOLUME_SOURCE_HPP
3
4#include <cassert>
5
6#include "ChargeSource.hpp"
8
36 class VolumeCharge : public ChargeSource {
37 public:
68
80 return NumericalIntegrateField(point, 16, 16, 16);
81 }
82
94 return NumericalIntegratePotential(point, 16, 16, 16);
95 }
96
103 return rho_ * size_x_ * size_y_ * size_z_;
104 }
105
118 const auto local = point - center_;
119 auto px = local.Dot(axis_x_);
120 auto py = local.Dot(axis_y_);
121 auto pz = local.Dot(axis_z_);
122
123 px = Utils::clamp(px, -size_x_ / 2, size_x_ / 2);
124 py = Utils::clamp(py, -size_y_ / 2, size_y_ / 2);
125 pz = Utils::clamp(pz, -size_z_ / 2, size_z_ / 2);
126
127 const auto closest = center_ + px * axis_x_ + py * axis_y_ + pz * axis_z_;
128 return (point - closest).Length();
129 }
130
142 const auto local = point - center_;
143 const auto px = Utils::abs(local.Dot(axis_x_));
144 const auto py = Utils::abs(local.Dot(axis_y_));
145 const auto pz = Utils::abs(local.Dot(axis_z_));
146
147 if (px <= size_x_ / 2 && py <= size_y_ / 2 && pz <= size_z_ / 2) {
148 return rho_;
149 }
150 return 0_Cpm3;
151 }
152
153 private:
158
173 const int nx, const int ny, const int nz) const {
175 const auto dx = size_x_ / nx;
176 const auto dy = size_y_ / ny;
177 const auto dz = size_z_ / nz;
178 const auto dV = dx * dy * dz;
179
180 for (int i = 0; i < nx; ++i) {
181 for (int j = 0; j < ny; ++j) {
182 for (int k = 0; k < nz; ++k) {
183 const auto x = (i + 0.5) / nx * size_x_ - size_x_ / 2;
184 const auto y = (j + 0.5) / ny * size_y_ - size_y_ / 2;
185 const auto z = (k + 0.5) / nz * size_z_ - size_z_ / 2;
186 const auto pos = center_ + x * axis_x_ + y * axis_y_ + z * axis_z_;
187
188 const auto r = point - pos;
189 const auto dist = r.Length();
190 if (dist < Quantity<Meter>(Config::epsilon)) continue;
191
192 const auto dq = rho_ * dV;
193 const auto dE = Consts::k_E * dq / (dist * dist) * r.Normalized();
194 E += dE;
195 }
196 }
197 }
198 return E;
199 }
200
215 const int nx, const int ny, const int nz) const {
216 auto V = 0_V;
217 const auto dx = size_x_ / nx;
218 const auto dy = size_y_ / ny;
219 const auto dz = size_z_ / nz;
220 const auto dV = dx * dy * dz;
221
222 for (int i = 0; i < nx; ++i) {
223 for (int j = 0; j < ny; ++j) {
224 for (int k = 0; k < nz; ++k) {
225 const auto x = (i + 0.5) / nx * size_x_ - size_x_ / 2;
226 const auto y = (j + 0.5) / ny * size_y_ - size_y_ / 2;
227 const auto z = (k + 0.5) / nz * size_z_ - size_z_ / 2;
228 const auto pos = center_ + x * axis_x_ + y * axis_y_ + z * axis_z_;
229
230 const auto r = point - pos;
231 const auto dist = r.Length();
232 if (dist < Quantity<Meter>(Config::epsilon)) continue;
233
234 V += Consts::k_E * rho_ * dV / dist;
235 }
236 }
237 }
238 return V;
239 }
240 };
241}
242
243#endif // PHYTH_VOLUME_SOURCE_HPP
Abstract base class for all charge distributions.
Uniform volume charge distribution in a rectangular box.
Quantity< CoulombPerCubicMeter > rho_
Quantity< CoulombPerCubicMeter > GetChargeDensityAt(const Vector3< Quantity< Meter > > &point) const override
Get the charge density at a point.
VolumeCharge(const Vector3< Quantity< Meter > > &center, const Vector3< Scalar > &axis_x, const Vector3< Scalar > &axis_y, const Vector3< Scalar > &axis_z, const Quantity< Meter > size_x, const Quantity< Meter > size_y, const Quantity< Meter > size_z, const Quantity< CoulombPerCubicMeter > rho)
Construct a rectangular volume charge.
Quantity< Coulomb > GetTotalChargeValue() const override
Get the total charge of the volume.
Vector3< Quantity< Meter > > center_
Quantity< Meter > GetMinimumDistanceTo(const Vector3< Quantity< Meter > > &point) const override
Get the minimum distance from a point to the rectangular box.
Vector3< Quantity< NewtonPerCoulomb > > GetElectricFieldAt(const Vector3< Quantity< Meter > > &point) const override
Compute the electric field at a point using numerical integration.
Vector3< Quantity< NewtonPerCoulomb > > NumericalIntegrateField(const Vector3< Quantity< Meter > > &point, const int nx, const int ny, const int nz) const
Numerical integration of electric field over the volume.
Quantity< Volt > GetElectricPotentialAt(const Vector3< Quantity< Meter > > &point) const override
Compute the electric potential at a point using numerical integration.
Quantity< Volt > NumericalIntegratePotential(const Vector3< Quantity< Meter > > &point, const int nx, const int ny, const int nz) const
Numerical integration of electric potential over the volume.
double epsilon
Definition Config.hpp:13
constexpr auto k_E
auto abs(Quantity< UnitT > q)
Absolute value of a quantity.
Quantity< UnitT > clamp(Quantity< UnitT > x, Quantity< UnitT > min, Quantity< UnitT > max)
Clamp a quantity between min and max bounds.
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