Skip to content

Commit

Permalink
Some cleanup & minor refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
GwGibson committed May 3, 2024
1 parent ec3446c commit 73885e7
Show file tree
Hide file tree
Showing 32 changed files with 244 additions and 388 deletions.
32 changes: 18 additions & 14 deletions Dependencies.cmake
Original file line number Diff line number Diff line change
Expand Up @@ -46,22 +46,26 @@ function(psim_setup_dependencies)
endif()
endif()

if(NOT TARGET tbb)
find_package(TBB QUIET)
if(NOT WIN32)
if(NOT TARGET tbb)
find_package(TBB QUIET)

if(TBB_FOUND)
message(STATUS "Found TBB: Using system TBB")
else()
message(STATUS "TBB not found: Using CPM to fetch TBB")
CPMAddPackage(
NAME tbb
GIT_REPOSITORY https://github.com/oneapi-src/oneTBB.git
GIT_TAG v2021.11.0
OPTIONS
"TBB_TEST OFF"
"TBB_STRICT OFF"
)
if(TBB_FOUND)
message(STATUS "Found TBB: Using system TBB")
else()
message(STATUS "TBB not found: Using CPM to fetch TBB")
CPMAddPackage(
NAME tbb
GIT_REPOSITORY https://github.com/oneapi-src/oneTBB.git
GIT_TAG v2021.11.0
OPTIONS
"TBB_TEST OFF"
"TBB_STRICT OFF"
)
endif()
endif()
else()
message(STATUS "Skipping TBB fetch on Windows.")
endif()

endfunction()
25 changes: 9 additions & 16 deletions psim/include/psim/cell.h
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@
#ifndef PSIM_CELL_H
#define PSIM_CELL_H

#include "compositeSurface.h"// for CompositeSurface
#include "geometry.h"// for Triangle, Line, Point
#include "material.h"// for Material, Material::Table
#include "sensor.h"// for Sensor
#include <array>// for array
#include <cstddef>// for size_t
#include <exception>// for exception
#include <iosfwd>// for ostream
#include <string>// for string
#include <string_view>// for string_view
#include "compositeSurface.h"
#include "geometry.h"
#include "sensor.h"

class Phonon;


// TODO - Inherit from Geometry::Triangle? Composition seems best for Sensor but requires a lot of forwarding
class Cell {
public:
using Point = Geometry::Point;
using Line = Geometry::Line;
using Triangle = Geometry::Triangle;

Cell(Triangle cell, Sensor& sensor, double spec = 1.);
Cell(const Triangle& cell, Sensor& sensor, double spec = 1.);

[[nodiscard]] const Material& getMaterial() const noexcept {
return sensor_.getMaterial();
Expand Down Expand Up @@ -53,6 +45,7 @@ class Cell {
[[nodiscard]] Point getRandPoint(double r1, double r2) const noexcept {// NOLINT
return cell_.getRandPoint(r1, r2);
}

// Throws if the incoming cell overlaps, contains or is contained within this cell - both cells cannot coexist
// in the same model
void validate(const Cell& other) const;
Expand All @@ -71,7 +64,7 @@ class Cell {
return sensor_.scatterUpdate(p);// NOLINT
}
void updateEmitTables() noexcept;
void updateHeatParams(const Phonon& p, std::size_t step) noexcept;// NOLINT
void updateHeatParams(const Phonon& p, std::size_t step) const noexcept;// NOLINT
void findTransitionSurface(Cell& other);
void handleSurfaceCollision(Phonon& p, const Point& poi, double step_time) const noexcept;// NOLINT

Expand All @@ -82,7 +75,7 @@ class Cell {

private:
Triangle cell_;
Sensor& sensor_;
Sensor& sensor_;// hmmm...

// Unused space on each line defaults to a boundary surface and portions of this boundary surface
// are allocated to different surface types as necessary
Expand Down Expand Up @@ -112,7 +105,7 @@ class CellError : public std::exception {

class IntersectError : public CellError {
public:
IntersectError(Geometry::Triangle existing, Geometry::Triangle incoming);
IntersectError(const Geometry::Triangle& existing, const Geometry::Triangle& incoming);

private:
Geometry::Triangle existing_;
Expand All @@ -121,7 +114,7 @@ class IntersectError : public CellError {

class OverlapError : public CellError {
public:
OverlapError(Geometry::Triangle bigger, Geometry::Triangle smaller);
OverlapError(const Geometry::Triangle& bigger, const Geometry::Triangle& smaller);

private:
Geometry::Triangle bigger_;
Expand Down
10 changes: 1 addition & 9 deletions psim/include/psim/compositeSurface.h
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
#ifndef PSIM_COMPOSITESURFACE_H
#define PSIM_COMPOSITESURFACE_H

#include "geometry.h"// for Line, Point
#include "surface.h"// for Surface, EmitSurface, TransitionSurface
#include <exception>// for exception
#include <iosfwd>// for ostream
#include <string>// for string
#include <string_view>// for string_view
#include <vector>// for vector
#include "surface.h"

class Cell;
class Material;
class Phonon;


class CompositeSurface {
public:
using Point = Geometry::Point;
Expand Down Expand Up @@ -80,5 +73,4 @@ class CompositeSurfaceError : public std::exception {
Geometry::Line inc_;
};


#endif// PSIM_COMPOSITESURFACE_H
62 changes: 35 additions & 27 deletions psim/include/psim/geometry.h
Original file line number Diff line number Diff line change
@@ -1,41 +1,36 @@
#ifndef PSIM_GEOMETRY_H
#define PSIM_GEOMETRY_H

#include <array>// for array
#include <exception>// for exception
#include <optional>// for optional
#include <ostream>// for ostream
#include <string>// for string
#include <string_view>// for string_view
#include <utility>// for pair
#include <array>
#include <exception>
#include <limits>
#include <optional>
#include <ostream>
#include <string>
#include <utility>


namespace Geometry {

inline constexpr double GEOEPS = std::numeric_limits<double>::epsilon() * 1E9;
struct Point;
using PointPair = std::pair<Point, Point>;

struct Vector2D {
constexpr Vector2D(double x_coord, double y_coord) noexcept
: x{ x_coord }
, y{ y_coord } {
}
double x;
double y;
};

struct Point {
constexpr Point(double x_coord, double y_coord) noexcept
: x{ x_coord }
, y{ y_coord } {
}
double x;
double y;

bool operator==(const Point& rhs) const;
bool operator!=(const Point& rhs) const;
};

bool operator==(const Point& lhs, const Point& rhs);
bool operator!=(const Point& lhs, const Point& rhs);
std::ostream& operator<<(std::ostream& os, const Point& point);// NOLINT
Point operator-(const Point& lhs, const Point& rhs);

using PointPair = std::pair<Point, Point>;

struct Line {
Line(Point pt1, Point pt2);
Point p1;
Expand All @@ -44,10 +39,10 @@ struct Line {
// maybe only store the length and bounding box
double slope;
double intercept;
PointPair boundingBox;
std::pair<Point, Point> boundingBox;
double length;

[[nodiscard]] PointPair getPoints() const noexcept {
[[nodiscard]] std::pair<Point, Point> getPoints() const noexcept {
return { p1, p2 };
}

Expand All @@ -71,6 +66,7 @@ struct Line {
return length > rhs.length;
}
};

std::ostream& operator<<(std::ostream& os, const Line& line);// NOLINT

struct Triangle {
Expand All @@ -93,8 +89,18 @@ struct Triangle {
bool operator==(const Triangle& rhs) const;
};
std::ostream& operator<<(std::ostream& os, const Triangle& triangle);// NOLINT
}// namespace Geometry

/**
* @brief Check if two floating-point values are approximately equal within a specified tolerance.
*
* @param a The first value to compare.
* @param b The second value to compare.
* @param epsilon The tolerance used for the comparison (default: EPSILON).
* @return true if the values are approximately equal within the given tolerance, false otherwise.
*/
[[nodiscard]] inline bool approxEqual(double a, double b, double epsilon = GEOEPS) noexcept {// NOLINT
return std::abs(a - b) < epsilon;
}

class ShapeError : public std::exception {
public:
Expand All @@ -113,18 +119,20 @@ class ShapeError : public std::exception {

class LineError : public ShapeError {
public:
explicit LineError(Geometry::Line line);
explicit LineError(Line line);

private:
Geometry::Line line_;
Line line_;
};

class TriangleError : public ShapeError {
public:
explicit TriangleError(Geometry::Triangle triangle);
explicit TriangleError(const Geometry::Triangle& triangle);

private:
Geometry::Triangle triangle_;
Triangle triangle_;
};

}// namespace Geometry

#endif// PSIM_GEOMETRY_H
14 changes: 5 additions & 9 deletions psim/include/psim/material.h
Original file line number Diff line number Diff line change
@@ -1,17 +1,14 @@
#ifndef PSIM_MATERIAL_H
#define PSIM_MATERIAL_H

#include "phonon.h"// for Phonon, Phonon::RelaxRates, Phonon::Polariz...
#include <array>// for array
#include <cstddef>// for size_t
#include <utility>// for pair, move
#include <vector>// for vector
#include "phonon.h"
#include <array>
#include <vector>

struct DispersionData;
struct RelaxationData;
struct TableData;


class Material {
public:
static constexpr std::size_t NUM_FREQ_BINS = 1000;// Used on one occasion outside this class
Expand Down Expand Up @@ -100,16 +97,15 @@ class Material {
[[nodiscard]] std::pair<const Table*, double> emitData(double temp) const;
[[nodiscard]] std::pair<const Table*, double> scatterData(double temp) const;

[[nodiscard]] std::pair<Table, double> cumulDistEmit(Array&& la_dist, Array&& ta_dist) const;
[[nodiscard]] std::pair<Table, double> cumulDistScatter(Array&& la_dist, Array&& ta_dist, double temp) const;
[[nodiscard]] std::pair<Table, double> cumulDistEmit(Array la_dist, Array ta_dist) const;
[[nodiscard]] std::pair<Table, double> cumulDistScatter(Array la_dist, Array ta_dist, double temp) const;
[[nodiscard]] static Table buildCumulDist(const Array& t1, const Array& t2);// NOLINT
[[nodiscard]] Array phononDist(double temp, Polar polarization) const;

[[nodiscard]] double tauNInv(double temp, double freq, Polar polarization) const noexcept;
[[nodiscard]] double tauUInv(double temp, double freq, Polar polarization) const noexcept;
[[nodiscard]] double tauIInv(double freq) const noexcept;// Impurity scattering

static void verifyInput(double low_temp, double high_temp, float interval_size);
[[nodiscard]] std::size_t getTempIndex(double temp) const noexcept;
};

Expand Down
26 changes: 8 additions & 18 deletions psim/include/psim/model.h
Original file line number Diff line number Diff line change
@@ -1,23 +1,13 @@
#ifndef PSIM_MODEL_H
#define PSIM_MODEL_H

#include "cell.h"// for Cell
#include "material.h"// for Material
#include "modelSimulator.h"// for ModelSimulator
#include "outputManager.h"// for OutputManager
#include "psim/geometry.h"// for Point, Triangle (ptr only)
#include "psim/sensor.h"// for Sensor, Sensor::SimulationType
#include "sensorInterpreter.h"// for SensorInterpreter
#include <cstddef>// for size_t
#include <filesystem>// for path
#include <memory>// for unique_ptr
#include <mutex>// for mutex
#include <optional>// for optional
#include <string>// for string, basic_string
#include <unordered_map>// for unordered_map
#include <utility>// for pair
#include <vector>// for vector

#include "cell.h"
#include "geometry.h"
#include "modelSimulator.h"
#include "outputManager.h"
#include "sensor.h"
#include "sensorInterpreter.h"
#include <unordered_map>

/**
* The Model class primarily controls the geometrical aspects of the simulation.
Expand Down Expand Up @@ -83,7 +73,7 @@ class Model {
* @param sensor_ID - The sensor this cell is linked to -> will throw if the sensor does not exist
* @param spec - The specularity of the cell's boundary surfaces [0-1]
*/
void addCell(Geometry::Triangle&& triangle, std::size_t sensor_ID, double spec = 1.);
void addCell(Geometry::Triangle triangle, std::size_t sensor_ID, double spec = 1.);
/**
* This not not used as the python interface takes care of this
*
Expand Down
11 changes: 3 additions & 8 deletions psim/include/psim/modelSimulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -2,17 +2,12 @@
#define PSIM_MODELSIMULATOR_H

#include "phononBuilder.h"
#include <array>// for array
#include <cstddef>// for size_t
#include <optional>// for optional
#include <variant>// for variant
#include <vector>// for vector
#include <optional>
#include <variant>
#include <vector>

class Cell;
class CellOriginBuilder;
class PhasorBuilder;
class Phonon;
class SurfaceOriginBuilder;

class ModelSimulator {
public:
Expand Down
10 changes: 3 additions & 7 deletions psim/include/psim/outputManager.h
Original file line number Diff line number Diff line change
@@ -1,16 +1,12 @@
#ifndef PSIM_OUTPUTMANAGER_H
#define PSIM_OUTPUTMANAGER_H

#include "sensor.h"// for SensorMeasurements
#include <chrono>// for filesystem
#include <cstddef>// for size_t
#include <filesystem>// for path
#include <string>// for string
#include <vector>// for vector
#include <filesystem>
#include <vector>

struct SensorMeasurements;
namespace fs = std::filesystem;


class OutputManager {
public:
OutputManager() = default;
Expand Down
Loading

0 comments on commit 73885e7

Please sign in to comment.