Main Page   Class Hierarchy   Data Structures   File List   Data Fields   Globals  

kira_energy.C

Go to the documentation of this file.
00001 
00002        //=======================================================//    _\|/_
00003       //  __  _____           ___                    ___       //      /|\ ~
00004      //  /      |      ^     |   \  |         ^     |   \     //          _\|/_
00005     //   \__    |     / \    |___/  |        / \    |___/    //            /|\ ~
00006    //       \   |    /___\   |  \   |       /___\   |   \   // _\|/_
00007   //     ___/   |   /     \  |   \  |____  /     \  |___/  //   /|\ ~
00008  //                                                       //            _\|/_
00009 //=======================================================//              /|\ ~
00010 
00011 // Functions associated with calculating the energy of the system.
00012 //
00013 // Externally visible functions:
00014 //
00015 //      void print_recalculated_energies
00016 
00017 #include "hdyn.h"
00018 
00019 #define MAX_EKIN        1.0e15          // Assume a hardware error if ekin
00020                                         // is greater than this value
00021                                         // -- beware of runaway neutron
00022                                         // stars!
00023 
00024 local void set_pot(dyn *b, real pot)
00025 {
00026     ((hdynptr)b)->set_pot(pot);         // allow dyn function to access get_pot
00027 }
00028 
00029 void calculate_energies_with_external(hdyn* b,
00030                                       real& epot, real& ekin, real& etot,
00031                                       bool cm,          // default = false
00032                                       bool use_grape)   // default = true
00033 {
00034     // Compute the total energy, including external terms; also compute
00035     // the "pot" class data.
00036 
00037     // First, get the internal energy (use GRAPE if available).
00038 
00039     if (!b->get_ignore_internal())
00040         kira_calculate_internal_energies(b, epot, ekin, etot, cm, use_grape);
00041 
00042     if (b->get_external_field() > 0) {
00043 
00044         // Add the external contribution to the total potential, and
00045         // add external terms to hdyn::pot of all top-level nodes.
00046 
00047         real dpot = get_external_pot(b, set_pot);
00048         epot += dpot;
00049         etot += dpot;
00050     }
00051 }
00052 
00053 void print_recalculated_energies(hdyn* b,
00054                                  bool print_dde,        // default = false
00055                                  bool save_story)       // default = false
00056 {
00057     static real de_prev = VERY_LARGE_NUMBER;
00058 
00059     real epot = 0;
00060     real ekin = 0;
00061     real etot = 0;
00062 
00063     calculate_energies_with_external(b, epot, ekin, etot);
00064 
00065     int p = cerr.precision(INT_PRECISION);
00066     cerr << "Energies: " << epot << " " << ekin << " " << etot;
00067 
00068 #if 0
00069 
00070     // Recompute on the front end to check the GRAPE calculation
00071     // (recall that the GRAPE-4 computes to ~single precision, and
00072     // effectively flattens all tree structures prior to determining
00073     // the energy, so close binaries may be very poorly handled).
00074 
00075     calculate_energies_with_external(b, epot, ekin, etot, false, false);
00076     cerr << endl << "Recomputed: " << epot << " " << ekin << " " << etot;
00077 
00078 #endif
00079 
00080     cerr.precision(p);
00081 
00082     // Possible hardware problem?
00083 
00084     if (abs(ekin) > MAX_EKIN) {
00085         cerr << endl << "...retrying..." << endl;
00086 
00087         epot = ekin = etot = 0;
00088         calculate_energies_with_external(b, epot, ekin, etot);
00089         cerr << "Energies: " << epot << " " << ekin << " " << etot;
00090     }
00091 
00092     if (abs(ekin) > MAX_EKIN) print_dde = false;
00093 
00094     real de = 0;
00095 
00096     if (b->get_kira_counters()->initial_etot == 0) {
00097 
00098         b->get_kira_counters()->initial_etot = etot;
00099         b->get_kira_counters()->de_total = 0;
00100 
00101     } else {
00102 
00103         // real de = (etot - e_corr - initial_etot) / epot;
00104         // The above normalization fails if many stars have escaped
00105         // the system with HUGE kicks.
00106 
00107         de = (etot - b->get_kira_counters()->de_total
00108                 - b->get_kira_counters()->initial_etot);
00109                                         //   / kira_stats.initial_etot;
00110                                         //     ^^^^^^^^ note! ^^^^^^^^
00111     }
00112 
00113     if (print_dde && de_prev != VERY_LARGE_NUMBER) {
00114         cerr << endl << "          de = " << de
00115              << "  d(de) = " << de - de_prev;
00116         if (ekin > 0) cerr << "  (" << (de - de_prev) / ekin << ")";
00117         cerr << endl;
00118     } else
00119         cerr << "  de = " << de << endl;
00120 
00121     if (print_dde)
00122         de_prev = de;
00123 
00124     if (save_story) {
00125         putrq(b->get_dyn_story(), "energy_time", b->get_system_time());
00126         putrq(b->get_dyn_story(), "kinetic_energy", ekin);
00127         putrq(b->get_dyn_story(), "potential_energy", epot);
00128     }
00129 }

Generated at Sun Feb 24 09:57:06 2002 for STARLAB by doxygen1.2.6 written by Dimitri van Heesch, © 1997-2001