From c28808b3d6d6889995b14c3afa2977a33729fe87 Mon Sep 17 00:00:00 2001
From: Ruben Horn <5367484+rubenhorn@users.noreply.github.com>
Date: Fri, 10 Nov 2023 13:20:59 +0100
Subject: [PATCH] Rotation calculated for spherical particles (#274)
* Optimization of Quaternion for vector {0,0,0}
* Minor optimization: Avoid normalizing identity quaternions
* Profile with gprof; Reverted minor changes
* Feedback from PR
* CMake option and documentation for profiling
---------
Co-authored-by: Ruben Horn <>
---
cmake/modules/options.cmake | 9 ++++-
doc/profiling.dox | 10 +++++
src/molecules/Quaternion.cpp | 72 +++++++++++++++++++-----------------
src/molecules/Quaternion.h | 1 -
4 files changed, 56 insertions(+), 36 deletions(-)
create mode 100644 doc/profiling.dox
diff --git a/cmake/modules/options.cmake b/cmake/modules/options.cmake
index 9eecab21a8..f2176fa2ee 100644
--- a/cmake/modules/options.cmake
+++ b/cmake/modules/options.cmake
@@ -17,7 +17,14 @@ else()
message(FATAL_ERROR "wrong precision option ")
endif()
-
+# ---- PROFILING ----
+option(ENABLE_GPROF "Use the GNU profiler gprof (Only supported by GNU/Clang compiler)" OFF)
+if(ENABLE_GPROF)
+ set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -pg")
+ if(NOT (CMAKE_CXX_COMPILER_ID MATCHES "Clang" OR CMAKE_CXX_COMPILER_ID MATCHES "GNU"))
+ message(WARNING "${CMAKE_CXX_COMPILER_ID} may not support the -pg option.\n(Only use GNU/Clang with the GNU profiler!)\n")
+ endif()
+endif()
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DQUICKSCHED")
#set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -DTASKTIMINGPROFILE")
diff --git a/doc/profiling.dox b/doc/profiling.dox
new file mode 100644
index 0000000000..301d67b35a
--- /dev/null
+++ b/doc/profiling.dox
@@ -0,0 +1,10 @@
+/** \page Profiling
+
+In order to assess the runtime for parts of the code or even individual functions, one may use the GNU profiler by following the steps below:
+
+-# Set the option `ENABLE_GPROF` to `ON` and compile ls1. (Make sure to use either the GNU or Clang compiler.)
+-# Run an experiment and make sure that the program exits normally.
+-# Rename the output file `gmon.out` generated by the profiler as it is overwritten by subsequent runs.
+-# Extract the human-readable report using `gprof build/src/MarDyn gmon.out`.
+
+*/
diff --git a/src/molecules/Quaternion.cpp b/src/molecules/Quaternion.cpp
index 8abd722ce5..eaa5796e6d 100644
--- a/src/molecules/Quaternion.cpp
+++ b/src/molecules/Quaternion.cpp
@@ -41,44 +41,48 @@ void Quaternion::multiply_left(const Quaternion& q) {
}
std::array Quaternion::rotate(const std::array& d) const {
- std::array drot;
- double ww = m_qw*m_qw;
- double xx = m_qx*m_qx;
- double yy = m_qy*m_qy;
- double zz = m_qz*m_qz;
- double wx = m_qw*m_qx;
- double wy = m_qw*m_qy;
- double wz = m_qw*m_qz;
- double xy = m_qx*m_qy;
- double xz = m_qx*m_qz;
- double yz = m_qy*m_qz;
- // 1-2*(yy+zz)
- drot[0] = (ww+xx-yy-zz)*d[0] + 2.*(xy-wz)*d[1] + 2.*(wy+xz)*d[2];
- // 1-2*(xx+zz)
- drot[1] = 2.*(wz+xy)*d[0] + (ww-xx+yy-zz)*d[1] + 2.*(yz-wx)*d[2];
- // 1-2*(xx+yy)
- drot[2] = 2.*(xz-wy)*d[0] + 2.*(wx+yz)*d[1] + (ww-xx-yy+zz)*d[2];
+ std::array drot{};
+ if(std::array{} != d) {
+ double ww = m_qw*m_qw;
+ double xx = m_qx*m_qx;
+ double yy = m_qy*m_qy;
+ double zz = m_qz*m_qz;
+ double wx = m_qw*m_qx;
+ double wy = m_qw*m_qy;
+ double wz = m_qw*m_qz;
+ double xy = m_qx*m_qy;
+ double xz = m_qx*m_qz;
+ double yz = m_qy*m_qz;
+ // 1-2*(yy+zz)
+ drot[0] = (ww+xx-yy-zz)*d[0] + 2.*(xy-wz)*d[1] + 2.*(wy+xz)*d[2];
+ // 1-2*(xx+zz)
+ drot[1] = 2.*(wz+xy)*d[0] + (ww-xx+yy-zz)*d[1] + 2.*(yz-wx)*d[2];
+ // 1-2*(xx+yy)
+ drot[2] = 2.*(xz-wy)*d[0] + 2.*(wx+yz)*d[1] + (ww-xx-yy+zz)*d[2];
+ }
return drot;
}
std::array Quaternion::rotateinv(const std::array& d) const {
- std::array drot;
- double ww = m_qw*m_qw;
- double xx = m_qx*m_qx;
- double yy = m_qy*m_qy;
- double zz = m_qz*m_qz;
- double wx = m_qw*m_qx;
- double wy = m_qw*m_qy;
- double wz = m_qw*m_qz;
- double xy = m_qx*m_qy;
- double xz = m_qx*m_qz;
- double yz = m_qy*m_qz;
- // 1-2*(yy+zz)
- drot[0] = (ww+xx-yy-zz)*d[0] + 2.*(xy+wz)*d[1] + 2.*(xz-wy)*d[2];
- // 1-2*(xx+zz)
- drot[1] = 2.*(xy-wz)*d[0] + (ww-xx+yy-zz)*d[1] + 2.*(yz+wx)*d[2];
- // 1-2*(xx+yy)
- drot[2] = 2.*(xz+wy)*d[0] + 2.*(yz-wx)*d[1] + (ww-xx-yy+zz)*d[2];
+ std::array drot{};
+ if (std::array{} != d) {
+ double ww = m_qw*m_qw;
+ double xx = m_qx*m_qx;
+ double yy = m_qy*m_qy;
+ double zz = m_qz*m_qz;
+ double wx = m_qw*m_qx;
+ double wy = m_qw*m_qy;
+ double wz = m_qw*m_qz;
+ double xy = m_qx*m_qy;
+ double xz = m_qx*m_qz;
+ double yz = m_qy*m_qz;
+ // 1-2*(yy+zz)
+ drot[0] = (ww+xx-yy-zz)*d[0] + 2.*(xy+wz)*d[1] + 2.*(xz-wy)*d[2];
+ // 1-2*(xx+zz)
+ drot[1] = 2.*(xy-wz)*d[0] + (ww-xx+yy-zz)*d[1] + 2.*(yz+wx)*d[2];
+ // 1-2*(xx+yy)
+ drot[2] = 2.*(xz+wy)*d[0] + 2.*(yz-wx)*d[1] + (ww-xx-yy+zz)*d[2];
+ }
return drot;
}
diff --git a/src/molecules/Quaternion.h b/src/molecules/Quaternion.h
index a232c17309..c537c3968a 100644
--- a/src/molecules/Quaternion.h
+++ b/src/molecules/Quaternion.h
@@ -100,7 +100,6 @@ class Quaternion {
private:
double m_qw, m_qx, m_qy, m_qz; // components
-
};
#endif /*QUATERNION_H_*/