Skip to content
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion geoana/em/static/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
PointCurrentHalfSpace
DipoleHalfSpace
MagneticPrism
MagneticPolyhedron
MagneticTetrahedron
"""

from geoana.em.static.sphere import (
Expand All @@ -44,7 +46,11 @@
DipoleHalfSpace
)

from geoana.em.static.freespace import MagneticPrism
from geoana.em.static.freespace import (
MagneticPrism,
MagneticPolyhedron,
MagneticTetrahedron,
)


LineCurrentFreeSpace = LineCurrentWholeSpace
Expand Down
150 changes: 145 additions & 5 deletions geoana/em/static/freespace.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

from ..base import BaseLineCurrent
from geoana.utils import check_xyz_dim
from geoana.shapes import BasePrism
from geoana.shapes import BasePrism, BasePolyhedron, BaseTetrahedron
from geoana.kernels import (
prism_fz,
prism_fzz,
Expand All @@ -17,6 +17,8 @@

__all__ = [
"MagneticPrism",
"MagneticPolyhedron",
"MagneticTetrahedron",
]

class MagneticPrism(BasePrism):
Expand Down Expand Up @@ -147,11 +149,17 @@ def magnetic_flux_density(self, xyz):
"""
xyz = check_xyz_dim(xyz)
H = self.magnetic_field(xyz)
is_inside = (
np.all(xyz >= self.min_location, axis=-1)
& np.all(xyz <= self.max_location, axis=-1)
# Fraction of a small sphere about each point that lies inside the prism:
# 1 inside, 1/2 on a face, 1/4 on an edge, 1/8 at a corner. On the
# surface this makes the normal component of B exact (it is continuous)
# and the tangential component the mean of its limits from either side,
# consistent with H there.
lo, hi = self.min_location, self.max_location
per_axis = np.where(
(xyz > lo) & (xyz < hi), 1.0, np.where((xyz == lo) | (xyz == hi), 0.5, 0.0)
)
H[is_inside] = H[is_inside] + self.magnetization
inside = np.prod(per_axis, axis=-1)
H += inside[..., None] * self.magnetization

return mu_0 * H

Expand Down Expand Up @@ -198,3 +206,135 @@ def magnetic_field_gradient(self, xyz):

H_grad = - 1.0/(4 * np.pi) * np.stack((first, second, third), axis=-1)
return H_grad


class _PolyhedronMagnetics:
"""Magnetostatic solutions shared by the uniformly magnetized polyhedral shapes.

By Poisson's relation, the field of a uniformly magnetized body is the
Hessian of the Newtonian integral contracted with the magnetization,
:math:`\\mathbf{H} = \\frac{1}{4\\pi}(\\nabla\\nabla V)\\cdot\\mathbf{M}`,
with :math:`V` evaluated in closed form by
:class:`geoana.shapes.BasePolyhedron`.
"""

@property
def magnetization(self):
return self._magnetization

@magnetization.setter
def magnetization(self, vec):
try:
vec = np.asarray(vec, dtype=float)
except:
raise TypeError(f"magnetization must be array_like of float, got {type(vec)}")
vec = np.squeeze(vec)
if vec.shape != (3,):
raise ValueError(
f"magnetization must be array_like with shape (3,), got {vec.shape}"
)
self._magnetization = vec

@property
def moment(self):
return self.volume * self.magnetization

def scalar_potential(self, xyz):
"""
Magnetic scalar potential due to the body. Defined such that
:math:`H = \\nabla \\phi`.

Parameters
----------
xyz : (..., 3) numpy.ndarray
Observation locations in units m.

Returns
-------
(...) numpy.ndarray
Magnetic scalar potential at location xyz in units :math:`A`.
"""
xyz = check_xyz_dim(xyz)
return 1.0 / (4 * np.pi) * self._newtonian_integrals(xyz, 1) @ self.magnetization

def magnetic_field(self, xyz):
"""
Magnetic field due to the body.

Parameters
----------
xyz : (..., 3) numpy.ndarray
Observation locations in units m.

Returns
-------
(..., 3) numpy.ndarray
Magnetic field at location xyz in units :math:`\\frac{A}{m}`.
"""
xyz = check_xyz_dim(xyz)
return 1.0 / (4 * np.pi) * self._newtonian_integrals(xyz, 2) @ self.magnetization

def magnetic_flux_density(self, xyz):
"""
Magnetic flux density due to the body.

Parameters
----------
xyz : (..., 3) numpy.ndarray
Observation locations in units m.

Returns
-------
(..., 3) numpy.ndarray
Magnetic flux density at location xyz in units :math:`T`.
"""
xyz = check_xyz_dim(xyz)
H = self.magnetic_field(xyz)
# Weighted by the fraction of a small sphere about each point inside the
# body (1/2 on a face), as for MagneticPrism.
H += self._inside_fraction(xyz)[..., None] * self.magnetization
return mu_0 * H


class MagneticPolyhedron(_PolyhedronMagnetics, BasePolyhedron):
"""Class for magnetic field solutions for a polyhedron.

The ``MagneticPolyhedron`` class is used to analytically compute the
magnetic potentials and fields of a closed polyhedron with triangular faces
and constant magnetization. See :class:`geoana.shapes.BasePolyhedron` for
the requirements on ``faces``.

Parameters
----------
vertices : (n_vertices, 3) array_like of float
Vertex locations.
faces : (n_faces, 3) array_like of int
Vertex indices of each triangular face of a closed, consistently
oriented surface.
magnetization : (3,) array_like, optional
Magnetization of the body (:math:`\\frac{A}{m}`).
"""

def __init__(self, vertices, faces, magnetization=None):
if magnetization is None:
magnetization = np.r_[0.0, 0.0, 1.0]
self.magnetization = magnetization
super().__init__(vertices=vertices, faces=faces)


class MagneticTetrahedron(_PolyhedronMagnetics, BaseTetrahedron):
"""Class for magnetic field solutions for a tetrahedron.

Parameters
----------
vertices : (4, 3) array_like of float
The four vertex locations, in any order.
magnetization : (3,) array_like, optional
Magnetization of the tetrahedron (:math:`\\frac{A}{m}`).
"""

def __init__(self, vertices, magnetization=None):
if magnetization is None:
magnetization = np.r_[0.0, 0.0, 1.0]
self.magnetization = magnetization
super().__init__(vertices=vertices)
134 changes: 133 additions & 1 deletion geoana/gravity.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,18 +15,22 @@
PointMass
Sphere
Prism
Polyhedron
Tetrahedron
"""

import numpy as np
from scipy.constants import G
from geoana.utils import check_xyz_dim
from geoana.shapes import BasePrism
from geoana.shapes import BasePrism, BasePolyhedron, BaseTetrahedron
from geoana.kernels import prism_f, prism_fz, prism_fzx, prism_fzy, prism_fzz

__all__ = [
"PointMass",
"Sphere",
"Prism",
"Polyhedron",
"Tetrahedron",
]

class PointMass:
Expand Down Expand Up @@ -692,3 +696,131 @@ def gravitational_gradient(self, xyz):
third = np.stack([gxz, gyz, gzz], axis=-1)

return - G * self.rho * np.stack([first, second, third], axis=-1)


class _PolyhedronGravity:
"""Gravitational solutions shared by the polyhedral shapes."""

@property
def rho(self):
""" The density of the body.

Returns
-------
density : float
In :math:`\\frac{kg}{m^3}`.
"""
return self._rho

@rho.setter
def rho(self, value):
try:
value = float(value)
except:
raise TypeError(f"rho must be a number, got {type(value)}")
self._rho = value

@property
def mass(self):
""" The mass of the body

Returns
-------
mass : float
In :math:`kg`.
"""
return self.volume * self.rho

def gravitational_potential(self, xyz):
"""
Gravitational potential due to the body.

Parameters
----------
xyz : (..., 3) numpy.ndarray
Observation locations in units m.

Returns
-------
(..., ) numpy.ndarray
Gravitational potential at location xyz in units :math:`\\frac{m^2}{s^2}`.
"""
xyz = check_xyz_dim(xyz)
return G * self.rho * self._newtonian_integrals(xyz, 0)

def gravitational_field(self, xyz):
"""
Gravitational field due to the body.

Parameters
----------
xyz : (..., 3) numpy.ndarray
Observation locations in units m.

Returns
-------
(..., 3) numpy.ndarray
Gravitational field at location xyz in units :math:`\\frac{m}{s^2}`.
"""
xyz = check_xyz_dim(xyz)
return G * self.rho * self._newtonian_integrals(xyz, 1)

def gravitational_gradient(self, xyz):
"""
Gravitational gradient due to the body.

Parameters
----------
xyz : (..., 3) numpy.ndarray
Observation locations in units m.

Returns
-------
(..., 3, 3) numpy.ndarray
Gravitational gradient at location xyz in units :math:`\\frac{1}{s^2}`.
"""
xyz = check_xyz_dim(xyz)
return G * self.rho * self._newtonian_integrals(xyz, 2)


class Polyhedron(_PolyhedronGravity, BasePolyhedron):
"""Class for gravitational solutions for a polyhedron.

The ``Polyhedron`` class is used to analytically compute the gravitational
potentials, fields, and gradients of a closed polyhedron with triangular
faces and constant density. See :class:`geoana.shapes.BasePolyhedron` for the
closed-form expressions and the requirements on ``faces``.

Parameters
----------
vertices : (n_vertices, 3) array_like of float
Vertex locations.
faces : (n_faces, 3) array_like of int
Vertex indices of each triangular face of a closed, consistently
oriented surface.
rho : float, optional
Density of the body (:math:`\\frac{kg}{m^3}`).
"""

def __init__(self, vertices, faces, rho=1.0):
self.rho = rho
super().__init__(vertices=vertices, faces=faces)


class Tetrahedron(_PolyhedronGravity, BaseTetrahedron):
"""Class for gravitational solutions for a tetrahedron.

The ``Tetrahedron`` class is used to analytically compute the gravitational
potentials, fields, and gradients of a tetrahedron with constant density.

Parameters
----------
vertices : (4, 3) array_like of float
The four vertex locations, in any order.
rho : float, optional
Density of the tetrahedron (:math:`\\frac{kg}{m^3}`).
"""

def __init__(self, vertices, rho=1.0):
self.rho = rho
super().__init__(vertices=vertices)
22 changes: 22 additions & 0 deletions geoana/kernels/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,21 @@
prism_fxxz
prism_fxyz

Polyhedron Integral Kernels
===========================
Pointwise terms of the closed-form 1/r volume integrals of polyhedra
(Werner and Scheeres, 1996), and those integrals over tetrahedra, which
broadcast over tetrahedra and observation points alike.

.. autosummary::
:toctree: generated/

polyhedron_edge_log
triangle_solid_angle
face_edge_solid_angle
tetrahedron_integrals
tetrahedron_inside_fraction

Layered Electromagnetic Reflection Kernels
==========================================
.. autosummary::
Expand All @@ -50,3 +65,10 @@
prism_fxxz,
prism_fxyz,
)
from geoana.kernels.potential_field_polyhedron import (
polyhedron_edge_log,
triangle_solid_angle,
face_edge_solid_angle,
tetrahedron_integrals,
tetrahedron_inside_fraction,
)
Loading
Loading