Skip to content

Commit

Permalink
refactor!: Remove output parameter from `MagneticFieldProvider::getFi…
Browse files Browse the repository at this point in the history
…eldGradient`
  • Loading branch information
andiwand committed Dec 13, 2024
1 parent 2d5ed72 commit 50e84d1
Show file tree
Hide file tree
Showing 12 changed files with 57 additions and 64 deletions.
10 changes: 5 additions & 5 deletions Core/include/Acts/MagneticField/ConstantBField.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,19 @@ class ConstantBField final : public MagneticFieldProvider {
return Result<Vector3>::success(m_BField);
}

/// @copydoc MagneticFieldProvider::getFieldGradient(const Vector3&,ActsMatrix<3,3>&,MagneticFieldProvider::Cache&) const
/// @copydoc MagneticFieldProvider::getFieldGradient(const Vector3&,MagneticFieldProvider::Cache&) const
///
/// @note The @p position is ignored and only kept as argument to provide
/// a consistent interface with other magnetic field services.
/// @note currently the derivative is not calculated
/// @todo return derivative
Result<Vector3> getFieldGradient(
const Vector3& position, ActsMatrix<3, 3>& derivative,
Result<std::pair<Vector3, SquareMatrix3>> getFieldGradient(
const Vector3& position,
MagneticFieldProvider::Cache& cache) const override {
(void)position;
(void)derivative;
(void)cache;
return Result<Vector3>::success(m_BField);
return Result<std::pair<Vector3, SquareMatrix3>>::success(
std::pair{m_BField, SquareMatrix3::Zero()});
}

/// @copydoc MagneticFieldProvider::makeCache(const MagneticFieldContext&) const
Expand Down
13 changes: 7 additions & 6 deletions Core/include/Acts/MagneticField/InterpolatedBFieldMap.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
#include "Acts/MagneticField/MagneticFieldContext.hpp"
#include "Acts/MagneticField/MagneticFieldError.hpp"
#include "Acts/MagneticField/MagneticFieldProvider.hpp"
#include "Acts/Utilities/Grid.hpp"
#include "Acts/Utilities/Interpolation.hpp"
#include "Acts/Utilities/Result.hpp"

Expand Down Expand Up @@ -309,16 +308,18 @@ class InterpolatedBFieldMap : public InterpolatedMagneticField {
return Result<Vector3>::success((*lcache.fieldCell).getField(gridPosition));
}

/// @copydoc MagneticFieldProvider::getFieldGradient(const Vector3&,ActsMatrix<3,3>&,MagneticFieldProvider::Cache&) const
/// @copydoc MagneticFieldProvider::getFieldGradient(const Vector3&,MagneticFieldProvider::Cache&) const
///
/// @note currently the derivative is not calculated
/// @note Cache is not used currently
/// @todo return derivative
Result<Vector3> getFieldGradient(
const Vector3& position, ActsMatrix<3, 3>& derivative,
Result<std::pair<Vector3, SquareMatrix3>> getFieldGradient(
const Vector3& position,
MagneticFieldProvider::Cache& cache) const final {
(void)derivative;
return getField(position, cache);
(void)position;
(void)cache;
return Result<std::pair<Vector3, SquareMatrix3>>::failure(
MagneticFieldError::NotImplemented);
}

private:
Expand Down
17 changes: 5 additions & 12 deletions Core/include/Acts/MagneticField/MagneticFieldProvider.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,6 @@
#include "Acts/Utilities/Any.hpp"
#include "Acts/Utilities/Result.hpp"

#include <array>
#include <memory>

namespace Acts {

/// @defgroup MagneticField Magnetic field
Expand Down Expand Up @@ -45,20 +42,16 @@ class MagneticFieldProvider {
virtual Result<Vector3> getField(const Vector3& position,
Cache& cache) const = 0;

/// Retrieve magnetic field value its its gradient. Requires a cache object
/// created through makeCache().
/// Retrieve the magnetic field value and its gradient at a given location.
/// Requires a cache object created through makeCache().
///
/// @param [in] position global 3D position
/// @param [out] derivative gradient of magnetic field vector as (3x3) matrix
/// @param [in,out] cache Field provider specific cache object
/// @return magnetic field vector
virtual Result<Vector3> getFieldGradient(const Vector3& position,
ActsMatrix<3, 3>& derivative,
Cache& cache) const = 0;
virtual Result<std::pair<Vector3, SquareMatrix3>> getFieldGradient(
const Vector3& position, Cache& cache) const = 0;

virtual ~MagneticFieldProvider();
virtual ~MagneticFieldProvider() = default;
};

inline MagneticFieldProvider::~MagneticFieldProvider() = default;

} // namespace Acts
6 changes: 3 additions & 3 deletions Core/include/Acts/MagneticField/MultiRangeBField.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
#pragma once
#include "Acts/Definitions/Algebra.hpp"
#include "Acts/MagneticField/MagneticFieldContext.hpp"
#include "Acts/MagneticField/MagneticFieldError.hpp"
#include "Acts/MagneticField/MagneticFieldProvider.hpp"
#include "Acts/Utilities/RangeXD.hpp"

Expand Down Expand Up @@ -60,8 +59,9 @@ class MultiRangeBField final : public MagneticFieldProvider {
/// @brief Get the field gradient at a given position.
///
/// @warning This is not currently implemented.
Result<Vector3> getFieldGradient(
const Vector3& position, ActsMatrix<3, 3>& /*unused*/,
Result<std::pair<Vector3, SquareMatrix3>> getFieldGradient(
const Vector3& position,
MagneticFieldProvider::Cache& cache) const override;
};

} // namespace Acts
16 changes: 6 additions & 10 deletions Core/include/Acts/MagneticField/NullBField.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,22 @@ class NullBField final : public MagneticFieldProvider {
MagneticFieldProvider::Cache& cache) const override {
(void)position;
(void)cache;
return Result<Vector3>::success(m_BField);
return Result<Vector3>::success(Vector3::Zero());
}

/// @copydoc MagneticFieldProvider::getFieldGradient(const Vector3&,ActsMatrix<3,3>&,MagneticFieldProvider::Cache&) const
/// @copydoc MagneticFieldProvider::getFieldGradient(const Vector3&,MagneticFieldProvider::Cache&) const
///
/// @note The @p position is ignored and only kept as argument to provide
/// a consistent interface with other magnetic field services.
/// @note currently the derivative is not calculated
/// @todo return derivative
Result<Vector3> getFieldGradient(
const Vector3& position, ActsMatrix<3, 3>& derivative,
Result<std::pair<Vector3, SquareMatrix3>> getFieldGradient(
const Vector3& position,
MagneticFieldProvider::Cache& cache) const override {
(void)position;
(void)derivative;
(void)cache;
return Result<Vector3>::success(m_BField);
return Result<std::pair<Vector3, SquareMatrix3>>::success(
std::pair{Vector3::Zero(), SquareMatrix3::Zero()});
}

/// @copydoc MagneticFieldProvider::makeCache(const MagneticFieldContext&) const
Expand All @@ -63,9 +63,5 @@ class NullBField final : public MagneticFieldProvider {
/// otherwise @c false
/// @note The method will always return true for the null B-Field
bool isInside(const Vector3& /*position*/) const { return true; }

private:
/// magnetic field vector
const Vector3 m_BField = Vector3::Zero();
};
} // namespace Acts
7 changes: 3 additions & 4 deletions Core/include/Acts/MagneticField/SolenoidBField.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@
#include "Acts/Utilities/Result.hpp"

#include <cstddef>
#include <functional>

namespace Acts {

Expand Down Expand Up @@ -111,12 +110,12 @@ class SolenoidBField final : public MagneticFieldProvider {
Result<Vector3> getField(const Vector3& position,
MagneticFieldProvider::Cache& cache) const override;

/// @copydoc MagneticFieldProvider::getFieldGradient(const Vector3&,ActsMatrix<3,3>&,MagneticFieldProvider::Cache&) const
/// @copydoc MagneticFieldProvider::getFieldGradient(const Vector3&,MagneticFieldProvider::Cache&) const
///
/// @note currently the derivative is not calculated
/// @todo return derivative
Result<Vector3> getFieldGradient(
const Vector3& position, ActsMatrix<3, 3>& derivative,
Result<std::pair<Vector3, SquareMatrix3>> getFieldGradient(
const Vector3& position,
MagneticFieldProvider::Cache& cache) const override;

private:
Expand Down
12 changes: 8 additions & 4 deletions Core/src/MagneticField/MultiRangeBField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@

#include "Acts/MagneticField/MultiRangeBField.hpp"

#include "Acts/MagneticField/MagneticFieldError.hpp"

namespace Acts {

MultiRangeBField::Cache::Cache(const MagneticFieldContext& /*unused*/) {}
Expand Down Expand Up @@ -70,9 +72,11 @@ Result<Vector3> MultiRangeBField::getField(
}
}

Result<Vector3> MultiRangeBField::getFieldGradient(
const Vector3& position, ActsMatrix<3, 3>& /*unused*/,
MagneticFieldProvider::Cache& cache) const {
return getField(position, cache);
Result<std::pair<Vector3, SquareMatrix3>> MultiRangeBField::getFieldGradient(
const Vector3& /*position*/,
MagneticFieldProvider::Cache& /*cache*/) const {
return Result<std::pair<Vector3, SquareMatrix3>>::failure(
MagneticFieldError::NotImplemented);
}

} // namespace Acts
10 changes: 6 additions & 4 deletions Core/src/MagneticField/SolenoidBField.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@

#include "Acts/MagneticField/SolenoidBField.hpp"

#include "Acts/MagneticField/MagneticFieldError.hpp"
#include "Acts/Utilities/VectorHelpers.hpp"

#include <algorithm>
#include <cmath>
#include <numbers>

Expand Down Expand Up @@ -58,10 +58,12 @@ Acts::Vector2 Acts::SolenoidBField::getField(const Vector2& position) const {
return multiCoilField(position, m_scale);
}

Acts::Result<Acts::Vector3> Acts::SolenoidBField::getFieldGradient(
const Vector3& position, ActsMatrix<3, 3>& /*derivative*/,
Acts::Result<std::pair<Acts::Vector3, Acts::SquareMatrix3>>
Acts::SolenoidBField::getFieldGradient(
const Vector3& /*position*/,
MagneticFieldProvider::Cache& /*cache*/) const {
return Result<Vector3>::success(getField(position));
return Result<std::pair<Vector3, SquareMatrix3>>::failure(
MagneticFieldError::NotImplemented);
}

Acts::Vector2 Acts::SolenoidBField::multiCoilField(const Vector2& pos,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -71,11 +71,12 @@ class ScalableBField final : public Acts::MagneticFieldProvider {
/// a consistent interface with other magnetic field services.
/// @note currently the derivative is not calculated
/// @todo return derivative
Acts::Result<Acts::Vector3> getFieldGradient(
const Acts::Vector3& /*position*/, Acts::ActsMatrix<3, 3>& /*derivative*/,
Acts::Result<std::pair<Acts::Vector3, Acts::SquareMatrix3>> getFieldGradient(
const Acts::Vector3& /*position*/,
MagneticFieldProvider::Cache& gCache) const override {
Cache& cache = gCache.as<Cache>();
return Acts::Result<Acts::Vector3>::success(m_BField * cache.scalor);
return Acts::Result<std::pair<Acts::Vector3, Acts::SquareMatrix3>>::success(
std::pair{m_BField * cache.scalor, Acts::SquareMatrix3::Zero()});
}

Acts::MagneticFieldProvider::Cache makeCache(
Expand Down Expand Up @@ -106,6 +107,6 @@ class ScalableBField final : public Acts::MagneticFieldProvider {
private:
/// magnetic field vector
Acts::Vector3 m_BField;
}; // namespace BField
};

} // namespace ActsExamples
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

#pragma once

#include "Acts/Definitions/Algebra.hpp"
#include "Acts/MagneticField/MagneticFieldProvider.hpp"

#include <memory>
Expand All @@ -30,8 +31,8 @@ class DD4hepFieldAdapter : public Acts::MagneticFieldProvider {
Result<Vector3> getField(const Vector3& position,
MagneticFieldProvider::Cache& cache) const override;

Result<Vector3> getFieldGradient(
const Vector3& position, ActsMatrix<3, 3>& derivative,
Result<std::pair<Vector3, SquareMatrix3>> getFieldGradient(
const Vector3& position,
MagneticFieldProvider::Cache& cache) const override;

private:
Expand Down
7 changes: 4 additions & 3 deletions Plugins/DD4hep/src/DD4hepFieldAdapter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ Result<Vector3> DD4hepFieldAdapter::getField(
return Result<Vector3>::success(result);
}

Result<Vector3> DD4hepFieldAdapter::getFieldGradient(
const Vector3& /*position*/, ActsMatrix<3, 3>& /*derivative*/,
Result<std::pair<Vector3, SquareMatrix3>> DD4hepFieldAdapter::getFieldGradient(
const Vector3& /*position*/,
MagneticFieldProvider::Cache& /*cache*/) const {
return Result<Vector3>::failure(MagneticFieldError::NotImplemented);
return Result<std::pair<Vector3, SquareMatrix3>>::failure(
MagneticFieldError::NotImplemented);
}

} // namespace Acts
Original file line number Diff line number Diff line change
Expand Up @@ -11,22 +11,17 @@
#include "Acts/Definitions/Algebra.hpp"
#include "Acts/MagneticField/InterpolatedBFieldMap.hpp"
#include "Acts/MagneticField/MagneticFieldContext.hpp"
#include "Acts/MagneticField/MagneticFieldProvider.hpp"
#include "Acts/Tests/CommonHelpers/FloatComparisons.hpp"
#include "Acts/Utilities/Axis.hpp"
#include "Acts/Utilities/AxisFwd.hpp"
#include "Acts/Utilities/Grid.hpp"
#include "Acts/Utilities/Result.hpp"
#include "Acts/Utilities/VectorHelpers.hpp"
#include "Acts/Utilities/detail/grid_helper.hpp"

#include <array>
#include <cstddef>
#include <functional>
#include <optional>
#include <tuple>
#include <utility>
#include <vector>

using Acts::VectorHelpers::perp;

Expand Down Expand Up @@ -127,12 +122,12 @@ BOOST_AUTO_TEST_CASE(InterpolatedBFieldMap_rz) {
pos << 1, 1, -5.5; // this position is outside the grid
BOOST_CHECK(!b.isInside(pos));
BOOST_CHECK(!b.getField(pos, bCacheAny).ok());
BOOST_CHECK(!b.getFieldGradient(pos, deriv, bCacheAny).ok());
BOOST_CHECK(!b.getFieldGradient(pos, bCacheAny).ok());

pos << 1, 6, -1.7; // this position is outside the grid
BOOST_CHECK(!b.isInside(pos));
BOOST_CHECK(!b.getField(pos, bCacheAny).ok());
BOOST_CHECK(!b.getFieldGradient(pos, deriv, bCacheAny).ok());
BOOST_CHECK(!b.getFieldGradient(pos, bCacheAny).ok());

pos << 0, 1.5, -2.5;
BOOST_CHECK(b.isInside(pos));
Expand Down

0 comments on commit 50e84d1

Please sign in to comment.