00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017 #include "hdyn.h"
00018
00019 #define MAX_EKIN 1.0e15 // Assume a hardware error if ekin
00020
00021
00022
00023
00024 local void set_pot(dyn *b, real pot)
00025 {
00026 ((hdynptr)b)->set_pot(pot);
00027 }
00028
00029 void calculate_energies_with_external(hdyn* b,
00030 real& epot, real& ekin, real& etot,
00031 bool cm,
00032 bool use_grape)
00033 {
00034
00035
00036
00037
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
00045
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,
00055 bool save_story)
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
00071
00072
00073
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
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
00104
00105
00106
00107 de = (etot - b->get_kira_counters()->de_total
00108 - b->get_kira_counters()->initial_etot);
00109
00110
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 }