Skip to content

Commit

Permalink
Merge pull request #30 from ewanwm/feature_clang_formatting
Browse files Browse the repository at this point in the history
Feature clang formatting
  • Loading branch information
ewanwm authored Jul 25, 2024
2 parents 4c9d21e + 6ababdb commit b356bfe
Show file tree
Hide file tree
Showing 20 changed files with 1,052 additions and 911 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/cpp-linter.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ jobs:
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
##style: file
style: "Microsoft"
format-review: true
tidy-review: true
files-changed-only: false
Expand Down
7 changes: 7 additions & 0 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
repos:
- repo: https://github.com/pre-commit/mirrors-clang-format
rev: v14.0.6
hooks:
- id: clang-format
args: [--style=Microsoft]

2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ make test
- [x] Basic CI
- [x] Doxygen documentation with automatic deployment
- [x] Add test coverage checks into CI
- [ ] Integrate linting ( [cpp-linter](https://github.com/cpp-linter)? )
- [x] Integrate linting ( [cpp-linter](https://github.com/cpp-linter)? )
- [ ] Add instrumentation library for benchmarking and profiling
- [ ] Add suite of benchmarking tests
- [ ] Integrate benchmarks into CI ( maybe use [hyperfine](https://github.com/sharkdp/hyperfine) and [bencher](https://bencher.dev/) for this? )
Expand Down
142 changes: 76 additions & 66 deletions nuTens/logging.hpp
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
#pragma once
#pragma once

/*! \file logging.hpp
\brief Define the logging utilities for nuTens
Basically just use spdlog interface.
However we define our own log levels and macros and pass them through to the logging library.
This is juuust in case we ever want to change the logging library we use. This way only this file would need to change.
Basically just use spdlog interface.
However we define our own log levels and macros and pass them through to
the logging library. This is juuust in case we ever want to change the
logging library we use. This way only this file would need to change.
*/

#define NT_LOG_LEVEL_TRACE 0
Expand All @@ -15,94 +16,103 @@
#define NT_LOG_LEVEL_ERROR 4
#define NT_LOG_LEVEL_SILENT 5


// define the log level in spdlogger
#if NT_LOG_LEVEL == NT_LOG_LEVEL_TRACE
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_TRACE

#elif NT_LOG_LEVEL == NT_LOG_LEVEL_DEBUG
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_DEBUG

#elif NT_LOG_LEVEL == NT_LOG_LEVEL_INFO
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_INFO

#elif NT_LOG_LEVEL == NT_LOG_LEVEL_WARNING
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_WARNING
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_WARNING

#elif NT_LOG_LEVEL == NT_LOG_LEVEL_ERROR
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_ERROR
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_ERROR

#elif NT_LOG_LEVEL == NT_LOG_LEVEL_SILENT
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_OFF
#define SPDLOG_ACTIVE_LEVEL SPDLOG_LEVEL_OFF

#endif


// #include "spdlog.h" has to happen *AFTER* we set SPDLOG_ACTIVE_LEVEL
#include "spdlog/spdlog.h"

// Now define the runtime log level which we will use to set the default log level
// This is needed since for trace or debug, we need to alter the default value at runtime
// see https://github.com/gabime/spdlog/wiki/1.-QuickStart#:~:text=Notice%20that%20spdlog%3A%3Aset_level%20is%20also%20necessary%20to%20print%20out%20debug%20or%20trace%20messages.
#if NT_LOG_LEVEL == NT_LOG_LEVEL_TRACE
static spdlog::level::level_enum runtimeLogLevel = spdlog::level::trace;
// Now define the runtime log level which we will use to set the default log
// level This is needed since for trace or debug, we need to alter the default
// value at runtime see
// https://github.com/gabime/spdlog/wiki/1.-QuickStart#:~:text=Notice%20that%20spdlog%3A%3Aset_level%20is%20also%20necessary%20to%20print%20out%20debug%20or%20trace%20messages.
#if NT_LOG_LEVEL == NT_LOG_LEVEL_TRACE
static spdlog::level::level_enum runtimeLogLevel = spdlog::level::trace;

#elif NT_LOG_LEVEL == NT_LOG_LEVEL_DEBUG
static spdlog::level::level_enum runtimeLogLevel = spdlog::level::debug;
#elif NT_LOG_LEVEL == NT_LOG_LEVEL_DEBUG
static spdlog::level::level_enum runtimeLogLevel = spdlog::level::debug;

#elif NT_LOG_LEVEL == NT_LOG_LEVEL_INFO
static spdlog::level::level_enum runtimeLogLevel = spdlog::level::info;
#elif NT_LOG_LEVEL == NT_LOG_LEVEL_INFO
static spdlog::level::level_enum runtimeLogLevel = spdlog::level::info;

#elif NT_LOG_LEVEL == NT_LOG_LEVEL_WARNING
static spdlog::level::level_enum runtimeLogLevel = spdlog::level::warning;
#elif NT_LOG_LEVEL == NT_LOG_LEVEL_WARNING
static spdlog::level::level_enum runtimeLogLevel = spdlog::level::warning;

#elif NT_LOG_LEVEL == NT_LOG_LEVEL_ERROR
static spdlog::level::level_enum runtimeLogLevel = spdlog::level::error;
#elif NT_LOG_LEVEL == NT_LOG_LEVEL_ERROR
static spdlog::level::level_enum runtimeLogLevel = spdlog::level::error;

#elif NT_LOG_LEVEL == NT_LOG_LEVEL_SILENT
static spdlog::level::level_enum runtimeLogLevel = spdlog::level::off;
#elif NT_LOG_LEVEL == NT_LOG_LEVEL_SILENT
static spdlog::level::level_enum runtimeLogLevel = spdlog::level::off;

#endif

static std::once_flag once;

/// @brief Set up the logger at runtime, should only be invoked once the very first time any of the logging macros below are called
inline void setup_logging() {
std::call_once(
once, [](){
std::cout << ":::::::: INFO: Setting default spdlog logging level to " << spdlog::level::to_string_view(runtimeLogLevel).data() << " ::::::::" << std::endl;
spdlog::set_level(runtimeLogLevel);
}
);
/// @brief Set up the logger at runtime, should only be invoked once the very
/// first time any of the logging macros below are called
inline void setup_logging()
{
std::call_once(once, []() {
std::cout << ":::::::: INFO: Setting default spdlog logging level to "
<< spdlog::level::to_string_view(runtimeLogLevel).data() << " ::::::::" << std::endl;
spdlog::set_level(runtimeLogLevel);
});
}

/// @brief Trace message that will only be displayed if NT_LOG_LEVEL == NT_LOG_LEVEL_TRACE
/// @param[in] ... The message to print. This can consist of just a simple string, or a format string and subsequent variables to format.
#define NT_TRACE(...) \
setup_logging(); \
SPDLOG_TRACE(__VA_ARGS__) \

/// @brief Debug message that will only be displayed if NT_LOG_LEVEL <= NT_LOG_LEVEL_DEBUG
/// @param[in] ... The message to print. This can consist of just a simple string, or a format string and subsequent variables to format.
#define NT_DEBUG(...) \
setup_logging(); \
SPDLOG_DEBUG(__VA_ARGS__) \

/// @brief Information message that will only be displayed if NT_LOG_LEVEL <= NT_LOG_LEVEL_INFO
/// @param[in] ... The message to print. This can consist of just a simple string, or a format string and subsequent variables to format.
#define NT_INFO(...) \
setup_logging(); \
SPDLOG_INFO(__VA_ARGS__) \

/// @brief Warning message that will only be displayed if NT_LOG_LEVEL <= NT_LOG_LEVEL_WARNING
/// @param[in] ... The message to print. This can consist of just a simple string, or a format string and subsequent variables to format.
#define NT_WARN(...) \
setup_logging(); \
SPDLOG_WARN(__VA_ARGS__) \

/// @brief Error message that will only be displayed if NT_LOG_LEVEL <= NT_LOG_LEVEL_ERROR
/// @param[in] ... The message to print. This can consist of just a simple string, or a format string and subsequent variables to format.
#define NT_ERROR(...) \
setup_logging(); \
SPDLOG_ERROR(__VA_ARGS__) \

/// @brief Trace message that will only be displayed if NT_LOG_LEVEL ==
/// NT_LOG_LEVEL_TRACE
/// @param[in] ... The message to print. This can consist of just a simple
/// string, or a format string and subsequent variables to format.
#define NT_TRACE(...) \
setup_logging(); \
SPDLOG_TRACE(__VA_ARGS__)

/// @brief Debug message that will only be displayed if NT_LOG_LEVEL <=
/// NT_LOG_LEVEL_DEBUG
/// @param[in] ... The message to print. This can consist of just a simple
/// string, or a format string and subsequent variables to format.
#define NT_DEBUG(...) \
setup_logging(); \
SPDLOG_DEBUG(__VA_ARGS__)

/// @brief Information message that will only be displayed if NT_LOG_LEVEL <=
/// NT_LOG_LEVEL_INFO
/// @param[in] ... The message to print. This can consist of just a simple
/// string, or a format string and subsequent variables to format.
#define NT_INFO(...) \
setup_logging(); \
SPDLOG_INFO(__VA_ARGS__)

/// @brief Warning message that will only be displayed if NT_LOG_LEVEL <=
/// NT_LOG_LEVEL_WARNING
/// @param[in] ... The message to print. This can consist of just a simple
/// string, or a format string and subsequent variables to format.
#define NT_WARN(...) \
setup_logging(); \
SPDLOG_WARN(__VA_ARGS__)

/// @brief Error message that will only be displayed if NT_LOG_LEVEL <=
/// NT_LOG_LEVEL_ERROR
/// @param[in] ... The message to print. This can consist of just a simple
/// string, or a format string and subsequent variables to format.
#define NT_ERROR(...) \
setup_logging(); \
SPDLOG_ERROR(__VA_ARGS__)
12 changes: 6 additions & 6 deletions nuTens/nuTens-pch.hpp
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#pragma once

#include <iostream>
#include <vector>
#include <math.h>
#include <map>

#include <any>
#include <variant>
#include <complex>

#include <iostream>
#include <map>
#include <nuTens/logging.hpp>
#include <variant>
#include <vector>

#if USE_PYTORCH
#include <torch/torch.h>
#include <torch/torch.h>
#endif
20 changes: 9 additions & 11 deletions nuTens/propagator/base-matter-solver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,19 @@

#include <nuTens/tensors/tensor.hpp>

class BaseMatterSolver {

class BaseMatterSolver
{
/// @class BaseMatterSolver
/// @brief Abstract base class for matter effect solvers

public:

/// @name Setters
/// @{
virtual void setPMNS(const Tensor &newPMNS) = 0;

virtual void setMasses(const Tensor &newMasses) = 0;
public:
/// @name Setters
/// @{
virtual void setPMNS(const Tensor &newPMNS) = 0;

virtual void calculateEigenvalues(const Tensor &energies, Tensor &eigenvectors, Tensor &eigenvalues) = 0;
virtual void setMasses(const Tensor &newMasses) = 0;

/// @}
virtual void calculateEigenvalues(const Tensor &energies, Tensor &eigenvectors, Tensor &eigenvalues) = 0;

/// @}
};
15 changes: 9 additions & 6 deletions nuTens/propagator/const-density-solver.cpp
Original file line number Diff line number Diff line change
@@ -1,19 +1,22 @@
#include <nuTens/propagator/const-density-solver.hpp>

void ConstDensityMatterSolver::calculateEigenvalues(const Tensor &energies, Tensor &eigenvectors, Tensor &eigenvalues){

void ConstDensityMatterSolver::calculateEigenvalues(const Tensor &energies, Tensor &eigenvectors, Tensor &eigenvalues)
{
Tensor hamiltonian;
hamiltonian.zeros({energies.getBatchDim(), nGenerations, nGenerations}, NTdtypes::kComplexFloat);

for ( int i = 0; i < nGenerations; i++){
for ( int j = 0; j < nGenerations; j++){
hamiltonian.setValue({"...", i, j}, Tensor::div(diagMassMatrix.getValue({0, i, j}), energies.getValue({"...", 0})) - electronOuter.getValue({i, j}));
for (int i = 0; i < nGenerations; i++)
{
for (int j = 0; j < nGenerations; j++)
{
hamiltonian.setValue({"...", i, j},
Tensor::div(diagMassMatrix.getValue({0, i, j}), energies.getValue({"...", 0})) -
electronOuter.getValue({i, j}));
}
}

eigenvectors.zeros({1, nGenerations, nGenerations}, NTdtypes::kComplexFloat).requiresGrad(false);
eigenvalues.zeros({1, nGenerations, nGenerations}, NTdtypes::kComplexFloat).requiresGrad(false);

Tensor::eig(hamiltonian, eigenvectors, eigenvalues);

}
Loading

0 comments on commit b356bfe

Please sign in to comment.