hp-FEM
Adaptive hp-FEM suite for nano-optics
Loading...
Searching...
No Matches
material.hpp
Go to the documentation of this file.
1#pragma once
8
9#include <complex>
10#include <map>
11#include <memory>
12
13#include <Eigen/Core>
14
15#include "hpfem/core/types.hpp"
16#include "hpfem/mesh/mesh.hpp"
17
19
21using Tensor3 = Eigen::Matrix<Complex, 3, 3>;
22
31class Material {
32 public:
34 Material() = default;
36 Material(Complex eps_r, Complex mu_r) noexcept : eps_r_(eps_r), mu_r_(mu_r) {}
37
38 [[nodiscard]] static Material isotropic(Complex eps_r,
39 Complex mu_r = Complex{1.0, 0.0}) noexcept {
40 return {eps_r, mu_r};
41 }
47 [[nodiscard]] static Material anisotropic(const Tensor3& eps_r, Complex mu_r = Complex{1.0, 0.0});
48 [[nodiscard]] static Material vacuum() noexcept { return {}; }
50 [[nodiscard]] static Material dielectric(Real n) noexcept {
51 return {Complex{n * n, 0.0}, Complex{1.0, 0.0}};
52 }
53
54 [[nodiscard]] bool is_isotropic() const noexcept { return eps_tensor_ == nullptr; }
57 [[nodiscard]] Complex eps_scalar() const {
58 if (eps_tensor_) throw_anisotropic("eps_scalar");
59 return eps_r_;
60 }
62 [[nodiscard]] Complex mu_scalar() const noexcept { return mu_r_; }
64 [[nodiscard]] Tensor3 eps_tensor() const {
65 return eps_tensor_ ? *eps_tensor_ : Tensor3(eps_r_ * Tensor3::Identity());
66 }
68 [[nodiscard]] Tensor3 mu_inv_tensor() const {
69 return Tensor3((1.0 / mu_r_) * Tensor3::Identity());
70 }
73 [[nodiscard]] Complex refractive_index() const {
74 if (eps_tensor_) throw_anisotropic("refractive_index");
75 return std::sqrt(eps_r_ * mu_r_);
76 }
77
79 Material& set_eps_scalar(Complex eps_r) noexcept {
80 eps_r_ = eps_r;
81 eps_tensor_.reset();
82 return *this;
83 }
84 Material& set_mu(Complex mu_r) noexcept {
85 mu_r_ = mu_r;
86 return *this;
87 }
88
90 [[nodiscard]] bool operator==(const Material& other) const;
91
92 private:
93 [[noreturn]] static void throw_anisotropic(const char* accessor);
94
95 Complex eps_r_{1.0, 0.0};
96 Complex mu_r_{1.0, 0.0};
97 std::shared_ptr<const Tensor3> eps_tensor_;
98};
99
103[[nodiscard]] Real max_real_index(const Material& material);
104
107[[noreturn]] void throw_in_plane_coupling();
108
114template <int Dim>
115[[nodiscard]] Eigen::Matrix<Complex, Dim, Dim> permittivity_tensor(const Material& material) {
116 static_assert(Dim == 2 || Dim == 3);
117 const Tensor3 eps = material.eps_tensor();
118 if constexpr (Dim == 3) {
119 return eps;
120 } else {
121 if (eps(0, 2) != Complex{0.0, 0.0} || eps(1, 2) != Complex{0.0, 0.0}) {
123 }
124 return eps.template topLeftCorner<2, 2>();
125 }
126}
127
129[[nodiscard]] inline bool is_lossless(const Material& material) {
130 return material.is_isotropic() ? material.eps_scalar().imag() == 0
131 : material.eps_tensor().imag().isZero(0.0);
132}
133
139template <int N>
140[[nodiscard]] Real loss_density(const Material& material, const Eigen::Matrix<Complex, N, 1>& e) {
141 if (material.is_isotropic()) return material.eps_scalar().imag() * e.squaredNorm();
142 const Eigen::Matrix<Real, N, N> loss = permittivity_tensor<N>(material).imag();
143 return (e.adjoint() * loss.template cast<Complex>() * e)(0, 0).real();
144}
145
150 public:
152
155 [[nodiscard]] const Material& background() const noexcept { return background_; }
156 [[nodiscard]] bool has(mesh::Tag tag) const { return materials_.contains(tag); }
158 [[nodiscard]] const Material& at(mesh::Tag tag) const;
163 void clear_cells() { cells_.clear(); }
164 [[nodiscard]] Index num_cell_overrides() const noexcept {
165 return static_cast<Index>(cells_.size());
166 }
168 template <int Dim>
169 [[nodiscard]] const Material& of_cell(const mesh::Mesh<Dim>& mesh, Index c) const {
170 if (!cells_.empty()) {
171 const auto it = cells_.find(c);
172 if (it != cells_.end()) return it->second;
173 }
174 return at(mesh.cell_tag(c));
175 }
177 [[nodiscard]] bool is_isotropic() const;
178
179 private:
180 Material background_;
181 std::map<mesh::Tag, Material> materials_;
182 std::map<Index, Material> cells_;
183};
184
185} // namespace hpfem::materials
Definition material.hpp:149
const Material & background() const noexcept
Definition material.hpp:155
void clear_cells()
Removes all per-cell overrides.
Definition material.hpp:163
const Material & of_cell(const mesh::Mesh< Dim > &mesh, Index c) const
Material of cell c of a mesh: the cell's override if set, otherwise its tag's.
Definition material.hpp:169
bool has(mesh::Tag tag) const
Definition material.hpp:156
MaterialMap(Material background=Material::vacuum())
Definition material.hpp:151
bool is_isotropic() const
True if every material of the map (background, tags, cell overrides) is isotropic.
const Material & at(mesh::Tag tag) const
Material of a tag, the background if unlisted.
Index num_cell_overrides() const noexcept
Definition material.hpp:164
MaterialMap & set_cell(Index cell, Material material)
MaterialMap & set(mesh::Tag tag, Material material)
Definition material.hpp:31
bool operator==(const Material &other) const
Same kind and the same parameters (tensors compared entry by entry).
Material & set_eps_scalar(Complex eps_r) noexcept
Makes the medium isotropic with permittivity εr (drops a tensor).
Definition material.hpp:79
Complex mu_scalar() const noexcept
Permeability μr (always scalar).
Definition material.hpp:62
static Material isotropic(Complex eps_r, Complex mu_r=Complex{1.0, 0.0}) noexcept
Definition material.hpp:38
static Material anisotropic(const Tensor3 &eps_r, Complex mu_r=Complex{1.0, 0.0})
Tensor3 eps_tensor() const
Permittivity tensor: εr·I for an isotropic medium.
Definition material.hpp:64
Complex refractive_index() const
Definition material.hpp:73
Material(Complex eps_r, Complex mu_r) noexcept
Isotropic medium with permittivity εr and permeability μr.
Definition material.hpp:36
Material()=default
Vacuum.
static Material vacuum() noexcept
Definition material.hpp:48
Tensor3 mu_inv_tensor() const
Inverse permeability tensor .
Definition material.hpp:68
static Material dielectric(Real n) noexcept
Lossless non-magnetic dielectric of refractive index n.
Definition material.hpp:50
Complex eps_scalar() const
Definition material.hpp:57
Material & set_mu(Complex mu_r) noexcept
Definition material.hpp:84
bool is_isotropic() const noexcept
Definition material.hpp:54
Definition mesh.hpp:61
Tag cell_tag(Index c) const
Definition mesh.hpp:296
Definition material.hpp:18
Real loss_density(const Material &material, const Eigen::Matrix< Complex, N, 1 > &e)
Definition material.hpp:140
void throw_in_plane_coupling()
bool is_lossless(const Material &material)
True if the permittivity is real (no loss and no gain), isotropic or tensor.
Definition material.hpp:129
Eigen::Matrix< Complex, 3, 3 > Tensor3
Complex 3×3 tensor in the laboratory frame (x, y, z).
Definition material.hpp:21
Real max_real_index(const Material &material)
Eigen::Matrix< Complex, Dim, Dim > permittivity_tensor(const Material &material)
Definition material.hpp:115
std::int32_t Tag
Integer tag of a physical group (Gmsh convention: positive; 0 means untagged).
Definition mesh.hpp:23
double Real
Definition types.hpp:15
std::int64_t Index
global indices (DoFs, entities)
Definition types.hpp:17
std::complex< double > Complex
Definition types.hpp:16