From 0b650d6bfa92decbbe3301c463949acda0930a5d Mon Sep 17 00:00:00 2001
From: Matthieu Schaller <matthieu.schaller@durham.ac.uk>
Date: Mon, 12 Feb 2018 23:42:27 +0100
Subject: [PATCH] Allow the users to overwrite the value of Newton's constant
 via the YAML file.

---
 examples/main.c                |  2 +-
 examples/parameter_example.yml |  4 ++++
 src/physical_constants.c       | 13 +++++++++++++
 src/physical_constants.h       |  2 ++
 4 files changed, 20 insertions(+), 1 deletion(-)

diff --git a/examples/main.c b/examples/main.c
index 4b4c36c68c..c8af2d98bd 100644
--- a/examples/main.c
+++ b/examples/main.c
@@ -589,7 +589,7 @@ int main(int argc, char *argv[]) {
     /* Not restarting so look for the ICs. */
     /* Initialize unit system and constants */
     units_init(&us, params, "InternalUnitSystem");
-    phys_const_init(&us, &prog_const);
+    phys_const_init(&us, params, &prog_const);
     if (myrank == 0 && verbose > 0) {
       message("Internal unit system: U_M = %e g.", us.UnitMass_in_cgs);
       message("Internal unit system: U_L = %e cm.", us.UnitLength_in_cgs);
diff --git a/examples/parameter_example.yml b/examples/parameter_example.yml
index 36da7a1e99..5d5c8b7784 100644
--- a/examples/parameter_example.yml
+++ b/examples/parameter_example.yml
@@ -6,6 +6,10 @@ InternalUnitSystem:
   UnitCurrent_in_cgs:  1   # Amperes
   UnitTemp_in_cgs:     1   # Kelvin
 
+# Values of some physical constants
+PhysicalConstants:
+  G:            6.67408e-8 # (Optional) Overwrite the value of Newton's constant used internally by the code.
+
 # Parameters for the task scheduling
 Scheduler:
   nr_queues:                 0         # (Optional) The number of task queues to use. Use 0  to let the system decide.
diff --git a/src/physical_constants.c b/src/physical_constants.c
index 86b6d353cb..b1dbeaeecf 100644
--- a/src/physical_constants.c
+++ b/src/physical_constants.c
@@ -32,10 +32,14 @@
 /**
  * @brief Converts physical constants to the internal unit system
  *
+ * Some constants can be overwritten by the YAML file values.
+ *
  * @param us The current internal system of units.
+ * @param params The parsed parameter file.
  * @param internal_const The physical constants to initialize.
  */
 void phys_const_init(const struct unit_system *us,
+                     const struct swift_params *params,
                      struct phys_const *internal_const) {
 
   /* Units are declared as {U_M, U_L, U_t, U_I, U_T} */
@@ -44,6 +48,10 @@ void phys_const_init(const struct unit_system *us,
   internal_const->const_newton_G =
       const_newton_G_cgs / units_general_cgs_conversion_factor(us, dimension_G);
 
+  /* Overwrite G if present in the file */
+  internal_const->const_newton_G = parser_get_opt_param_double(
+      params, "PhysicalConstants:G", internal_const->const_newton_G);
+
   const float dimension_c[5] = {0, 1, -1, 0, 0};
   internal_const->const_speed_light_c =
       const_speed_light_c_cgs /
@@ -111,6 +119,11 @@ void phys_const_init(const struct unit_system *us,
       units_general_cgs_conversion_factor(us, dimension_length);
 }
 
+/**
+ * @brief Print the value of the physical constants to stdout.
+ *
+ * @param internal_const The constants in the internal unit system.
+ */
 void phys_const_print(const struct phys_const *internal_const) {
 
   message("%25s = %e", "Gravitational constant",
diff --git a/src/physical_constants.h b/src/physical_constants.h
index 20b97761aa..b0f929632b 100644
--- a/src/physical_constants.h
+++ b/src/physical_constants.h
@@ -29,6 +29,7 @@
 #include "../config.h"
 
 /* Local includes. */
+#include "parser.h"
 #include "units.h"
 
 /**
@@ -89,6 +90,7 @@ struct phys_const {
 };
 
 void phys_const_init(const struct unit_system* us,
+                     const struct swift_params* params,
                      struct phys_const* internal_const);
 
 void phys_const_print(const struct phys_const* internal_const);
-- 
GitLab