From fdf6cc982359b0460b1e27c58d87318d0e04785e Mon Sep 17 00:00:00 2001
From: Stefan Arridge <dc-arri1@cosma-b.cosma>
Date: Tue, 2 Aug 2016 17:17:49 +0100
Subject: [PATCH] Successfully compiled

---
 examples/main.c |  2 +-
 src/const.h     |  3 +++
 src/cooling.c   | 21 ++++++++++++---------
 src/cooling.h   | 25 +++++--------------------
 src/engine.h    |  4 +++-
 src/runner.c    |  3 ++-
 src/timers.h    |  1 +
 7 files changed, 27 insertions(+), 32 deletions(-)

diff --git a/examples/main.c b/examples/main.c
index 2d3e2e28c8..abede17624 100644
--- a/examples/main.c
+++ b/examples/main.c
@@ -457,7 +457,7 @@ int main(int argc, char *argv[]) {
   struct engine e;
   engine_init(&e, &s, params, nr_nodes, myrank, nr_threads, with_aff,
               engine_policies, talking, &us, &prog_const, &hydro_properties,
-              &potential);
+              &potential, &cooling);
   if (myrank == 0) {
     clocks_gettime(&toc);
     message("engine_init took %.3f %s.", clocks_diff(&tic, &toc),
diff --git a/src/const.h b/src/const.h
index 736ba0155e..80b1eda654 100644
--- a/src/const.h
+++ b/src/const.h
@@ -64,6 +64,9 @@
 #define EXTERNAL_POTENTIAL_POINTMASS
 //#define EXTERNAL_POTENTIAL_ISOTHERMALPOTENTIAL
 
+/* Cooling properties */
+#define CONST_COOLING
+
 /* Are we debugging ? */
 //#define SWIFT_DEBUG_CHECKS
 
diff --git a/src/cooling.c b/src/cooling.c
index c3e1a6625e..8df9073f7c 100644
--- a/src/cooling.c
+++ b/src/cooling.c
@@ -24,7 +24,9 @@
 #include "../config.h"
 
 /* This object's header. */
-#include "potentials.h"
+#include "cooling.h"
+#include "hydro.h"
+#include "adiabatic_index.h"
 
 /**
  * @brief Initialises the cooling properties in the internal system
@@ -57,7 +59,7 @@ void cooling_print(const struct cooling_data* cooling) {
   message(
       "Cooling properties are (lambda, min_energy, tstep multiplier) %g %g %g ",
       cooling->const_cooling.lambda,
-      cooling->const_cooling.min_energy
+      cooling->const_cooling.min_energy,
       cooling->const_cooling.cooling_tstep_mult);
 #endif /* CONST_COOLING */
 }
@@ -73,14 +75,15 @@ void update_entropy(const struct cooling_data* cooling,
   float old_entropy = p->entropy;
   float rho = p->rho;
 
-  u_old = old_entropy/(GAMMA_MINUS1) * pow(rho,GAMMA_MINUS1);
-  u_new = calculate_new_thermal_energy(u_old,dt,cooling):
-  new_entropy = u_new/pow(rho,GAMMA_MINUS1) * GAMMA_MINUS1;
-  p->entropy = new_entropy
+  //  u_old = old_entropy/(GAMMA_MINUS1) * pow(rho,GAMMA_MINUS1);
+  u_old = hydro_get_internal_energy(p,0); // dt = 0 because using current entropy
+  u_new = calculate_new_thermal_energy(u_old,dt,cooling);
+  new_entropy = u_new*pow_minus_gamma_minus_one(rho) * hydro_gamma_minus_one;
+  p->entropy = new_entropy;
 }
 
 
-float calculate_new_thermal_energy(float u_old, double dt, const struct cooling_data* cooling){
+float calculate_new_thermal_energy(float u_old, float dt, const struct cooling_data* cooling){
 #ifdef CONST_COOLING
   //This function integrates the cooling equation, given the initial thermal energy and the timestep dt.
   //Returns 0 if successful and 1 if not
@@ -88,11 +91,11 @@ float calculate_new_thermal_energy(float u_old, double dt, const struct cooling_
   float du_dt = cooling->const_cooling.lambda;
   float u_floor = cooling->const_cooling.min_energy;
   float u_new;
-  if (u_old - du_dt*dt > min_energy){
+  if (u_old - du_dt*dt > u_floor){
     u_new = u_old - du_dt*dt;
   }
   else{
-    u_new = min_energy;
+    u_new = u_floor;
   }
 
   return u_new;
diff --git a/src/cooling.h b/src/cooling.h
index b2be8ba066..15cb0957b3 100644
--- a/src/cooling.h
+++ b/src/cooling.h
@@ -32,6 +32,7 @@
 /* Local includes. */
 #include "const.h"
 #include "error.h"
+#include "hydro.h"
 #include "parser.h"
 #include "part.h"
 #include "physical_constants.h"
@@ -64,28 +65,12 @@ cooling_timestep(const struct cooling_data* cooling,
 		 const struct phys_const* const phys_const, 
 		 const struct part* const p) {
 
-  const float cooling_rate = get_cooling_rate( p->density, p->internal_energy, cooling );
-  return  cooling->const_cooling.cooling_tstep_mult * p->internal_energy / cooling_rate;
+  const float cooling_rate = cooling->const_cooling.lambda;
+  const float internal_energy = hydro_get_internal_energy(p,0);// dt = 0 because using current entropy
+  return  cooling->const_cooling.cooling_tstep_mult * internal_energy / cooling_rate;
 }
 
-/* /\** */
-/*  * @brief Updates the internal energy of a particle due to cooling. */
-/*  * */
-/*  * @param cooling The #cooling_data used in the run. */
-/*  * @param phys_const The physical constants in internal units. */
-/*  * @param p Pointer to the particle data. */
-/*  *\/ */
-/* __attribute__((always_inline)) INLINE static float */
-/* cooling_update_entropy(const struct cooling_data* cooling, */
-/*                                      const struct phys_const* const phys_const, */
-/* 										       const dt, */
-/*                                      struct part* p) { */
-/*   const float old_entropy = p->Entropy */
-/*   const float cooling_rate = get_cooling_rate( p->density, p->internal_energy, cooling ); */
-/*   // do other quanitities need to be updated as well?? */
-/*   p->internal_energy -= dt * cooling_rate; */
-/* } */
-/* #endif /\* CONST_COOLING *\/ */
+#endif /* CONST_COOLING */
 
 
 /* Now, some generic functions, defined in the source file */
diff --git a/src/engine.h b/src/engine.h
index fa304e97d8..58c45142b7 100644
--- a/src/engine.h
+++ b/src/engine.h
@@ -38,6 +38,7 @@
 
 /* Includes. */
 #include "clocks.h"
+#include "cooling.h"
 #include "parser.h"
 #include "partition.h"
 #include "potentials.h"
@@ -222,7 +223,8 @@ void engine_init(struct engine *e, struct space *s,
                  const struct UnitSystem *internal_units,
                  const struct phys_const *physical_constants,
                  const struct hydro_props *hydro,
-                 const struct external_potential *potential);
+                 const struct external_potential *potential,
+		 const struct cooling_data *cooling);
 void engine_launch(struct engine *e, int nr_runners, unsigned int mask,
                    unsigned int submask);
 void engine_prepare(struct engine *e);
diff --git a/src/runner.c b/src/runner.c
index 98491590b2..fa61ab5a61 100644
--- a/src/runner.c
+++ b/src/runner.c
@@ -42,6 +42,7 @@
 #include "atomic.h"
 #include "cell.h"
 #include "const.h"
+#include "cooling.h"
 #include "debug.h"
 #include "drift.h"
 #include "engine.h"
@@ -148,7 +149,7 @@ void runner_do_cooling(struct runner *r, struct cell *c, int timer) {
   struct part *restrict parts = c->parts;
   const int count = c->count;
   const int ti_current = r->e->ti_current;
-  const struct cooling_data *cooling = r->e->cooling;
+  const struct cooling_data *cooling = r->e->cooling_data;
   const struct phys_const *constants = r->e->physical_constants;
   const double timeBase = r->e->timeBase;
   double dt;
diff --git a/src/timers.h b/src/timers.h
index aa8455397d..023e31a1b2 100644
--- a/src/timers.h
+++ b/src/timers.h
@@ -57,6 +57,7 @@ enum {
   timer_qsteal,
   timer_runners,
   timer_step,
+  timer_do_cooling,
   timer_count,
 };
 
-- 
GitLab