local void diagnostics() { bodyptr p; real velsq, phi0; vector tmpv; matrix tmpt; int ndim=NDIM; mtot = 0.0; /* zero total mass */ etot[1] = etot[2] = 0.0; /* zero total KE and PE */ CLRM(keten); /* zero ke tensor */ CLRM(peten); /* zero pe tensor */ CLRM(amten); /* zero am tensor */ CLRV(cmphase[0]); /* zero c. of m. position */ CLRV(cmphase[1]); /* zero c. of m. velocity */ for (p = bodytab; p < bodytab+nbody; p++) { /* loop over all particles */ mtot += Mass(p); /* sum particle masses */ DOTVP(velsq, Vel(p), Vel(p)); /* square vel vector */ if (extpot) { /* external potential corr. */ (*extpot)(&ndim,Pos(p),tmpv,&phi0,&tnow); phi0 = Phi(p) + phi0; /* extre correction */ } else phi0 = Phi(p); etot[1] += 0.5 * Mass(p) * velsq; /* sum current KE */ etot[2] += 0.5 * Mass(p) * phi0; /* and current PE */ MULVS(tmpv, Vel(p), 0.5 * Mass(p)); /* sum 0.5 m v_i v_j */ OUTVP(tmpt, tmpv, Vel(p)); ADDM(keten, keten, tmpt); MULVS(tmpv, Pos(p), Mass(p)); /* sum m r_i a_j */ OUTVP(tmpt, tmpv, Acc(p)); ADDM(peten, peten, tmpt); OUTVP(tmpt, tmpv, Vel(p)); /* sum m r_i v_j */ ADDM(amten, amten, tmpt); MULVS(tmpv, Pos(p), Mass(p)); /* sum cm position */ ADDV(cmphase[0], cmphase[0], tmpv); MULVS(tmpv, Vel(p), Mass(p)); /* sum cm momentum */ ADDV(cmphase[1], cmphase[1], tmpv); } etot[0] = etot[1] + etot[2]; /* sum KE and PE */ TRANM(tmpt, amten); /* anti-sym. AM tensor */ SUBM(amten, amten, tmpt); DIVVS(cmphase[0], cmphase[0], mtot); /* normalize cm coords */ DIVVS(cmphase[1], cmphase[1], mtot); }
local void diagnostics(void) { bodyptr p1, p2, p; real mp, velsq; vector tmpv; matrix tmpt; mtot = 0.0; // zero total mass etot[1] = etot[2] = 0.0; // zero total KE and PE CLRM(keten); // zero ke tensor CLRM(peten); // zero pe tensor CLRV(amvec); // zero am vector CLRV(cmpos); // zero c. of m. position CLRV(cmvel); // zero c. of m. velocity p1 = bodytab + MAX(nstatic, 0); // set dynamic body range p2 = bodytab + nbody + MIN(nstatic, 0); for (p = p1; p < p2; p++) { // loop over body range mp = (testcalc ? 1.0 / (nbody - ABS(nstatic)) : Mass(p)); // use eq. mass in testcalc mtot += mp; // sum particle masses DOTVP(velsq, Vel(p), Vel(p)); // square vel vector etot[1] += 0.5 * mp * velsq; // sum current KE etot[2] += (testcalc ? 1.0 : 0.5) * mp * Phi(p); // and PE, weighted right MULVS(tmpv, Vel(p), 0.5 * mp); // sum 0.5 m v_i v_j OUTVP(tmpt, tmpv, Vel(p)); ADDM(keten, keten, tmpt); MULVS(tmpv, Pos(p), mp); // sum m r_i a_j OUTVP(tmpt, tmpv, Acc(p)); ADDM(peten, peten, tmpt); CROSSVP(tmpv, Vel(p), Pos(p)); // sum angular momentum MULVS(tmpv, tmpv, mp); ADDV(amvec, amvec, tmpv); MULVS(tmpv, Pos(p), mp); // sum cm position ADDV(cmpos, cmpos, tmpv); MULVS(tmpv, Vel(p), mp); // sum cm momentum ADDV(cmvel, cmvel, tmpv); } etot[0] = etot[1] + etot[2]; // sum KE and PE DIVVS(cmpos, cmpos, mtot); // normalize cm coords DIVVS(cmvel, cmvel, mtot); }
local void diagnostics() { int i; Body *p; real velsq; vector tmpv; matrix tmpt; mtot = 0.0; /* zero total mass */ etot[1] = etot[2] = 0.0; /* zero total KE and PE */ CLRM(keten); /* zero KE tensor */ CLRM(peten); /* zero PE tensor */ CLRM(amten); /* zero AM tensor */ CLRV(cmphase[0]); /* zero c. of m. position */ CLRV(cmphase[1]); /* zero c. of m. velocity */ for (p = bodytab; p < bodytab+nbody; p++) { /* loop over all bodies */ mtot += Mass(p); /* sum body masses */ DOTVP(velsq, Vel(p), Vel(p)); /* square vel vector */ etot[1] += 0.5 * Mass(p) * velsq; /* sum current KE */ etot[2] += 0.5 * Mass(p) * Phi(p); /* and current PE */ MULVS(tmpv, Vel(p), 0.5 * Mass(p)); /* sum 0.5 m v_i v_j */ OUTVP(tmpt, tmpv, Vel(p)); ADDM(keten, keten, tmpt); MULVS(tmpv, Pos(p), Mass(p)); /* sum m r_i a_j */ OUTVP(tmpt, tmpv, Acc(p)); ADDM(peten, peten, tmpt); OUTVP(tmpt, tmpv, Vel(p)); /* sum m r_i v_j */ ADDM(amten, amten, tmpt); MULVS(tmpv, Pos(p), Mass(p)); /* sum cm position */ ADDV(cmphase[0], cmphase[0], tmpv); MULVS(tmpv, Vel(p), Mass(p)); /* sum cm momentum */ ADDV(cmphase[1], cmphase[1], tmpv); } etot[0] = etot[1] + etot[2]; /* sum KE and PE */ TRANM(tmpt, amten); /* antisymmetrize AM tensor */ SUBM(amten, amten, tmpt); DIVVS(cmphase[0], cmphase[0], mtot); /* normalize cm coords */ DIVVS(cmphase[1], cmphase[1], mtot); }
void snaprect(bodyptr btab, int nbody) { matrix qmat, tmpm; bodyptr bp; vector frame[3], tmpv; static vector oldframe[3] = { { 1.0, 0.0, 0.0, }, { 0.0, 1.0, 0.0, }, { 0.0, 0.0, 1.0, }, }; int i; snapcenter(btab, nbody, WeightField.offset); CLRM(qmat); for (bp = btab; bp < NthBody(btab, nbody); bp = NextBody(bp)) { OUTVP(tmpm, Pos(bp), Pos(bp)); MULMS(tmpm, tmpm, Weight(bp)); ADDM(qmat, qmat, tmpm); } eigenvect(frame[0], frame[1], frame[2], qmat); if (dotvp(oldframe[0], frame[0]) < 0.0) MULVS(frame[0], frame[0], -1.0); if (dotvp(oldframe[2], frame[2]) < 0.0) MULVS(frame[2], frame[2], -1.0); CROSSVP(frame[1], frame[2], frame[0]); printvect("e_x:", frame[0]); printvect("e_y:", frame[1]); printvect("e_z:", frame[2]); for (bp = btab; bp < NthBody(btab, nbody); bp = NextBody(bp)) { if (PosField.offset != BadOffset) { for (i = 0; i < NDIM; i++) tmpv[i] = dotvp(Pos(bp), frame[i]); SETV(Pos(bp), tmpv); } if (VelField.offset != BadOffset) { for (i = 0; i < NDIM; i++) tmpv[i] = dotvp(Vel(bp), frame[i]); SETV(Vel(bp), tmpv); } if (AccField.offset != BadOffset) { for (i = 0; i < NDIM; i++) tmpv[i] = dotvp(Acc(bp), frame[i]); SETV(Acc(bp), tmpv); } if (AuxVecField.offset != BadOffset) { for (i = 0; i < NDIM; i++) tmpv[i] = dotvp(AuxVec(bp), frame[i]); SETV(AuxVec(bp), tmpv); } } for (i = 0; i < NDIM; i++) SETV(oldframe[i], frame[i]); }
int main(int argc, string argv[]) { stream istr; string bodytags[] = { PosTag, NULL }, intags[MaxBodyFields]; bodyptr btab = NULL, bp; int nbody, nshell, n; real tnow, vals[3]; matrix tmpm, qmat; vector v1, v2, v3; initparam(argv, defv); istr = stropen(getparam("in"), "r"); get_history(istr); layout_body(bodytags, Precision, NDIM); printf("#%11s %3s %11s %11s %11s\n", "time", "n", "r_rms", "c/a", "b/a"); while (get_snap(istr, &btab, &nbody, &tnow, intags, FALSE)) { if (! set_member(intags, PosTag)) error("%s: %s data missing\n", getargv0(), PosTag); if (nbody % getiparam("nbin") != 0) error("%s: nbin does not divide number of bodies\n", getargv0()); nshell = nbody / getiparam("nbin"); for (n = 0; n < nbody; n += nshell) { CLRM(qmat); for (bp = NthBody(btab, n); bp < NthBody(btab, n + nshell); bp = NextBody(bp)) { OUTVP(tmpm, Pos(bp), Pos(bp)); ADDM(qmat, qmat, tmpm); } eigensolve(v1, v2, v3, vals, qmat); printf(" %11.6f %3d %11.6f %11.6f %11.6f\n", tnow, n / nshell, rsqrt(tracem(qmat) / nshell), rsqrt(vals[2] / vals[0]), rsqrt(vals[1] / vals[0])); if (getbparam("listvec")) { printf("#\t\t\t\t\t\t\t%8.5f %8.5f %8.5f\n", v1[0], v1[1], v1[2]); printf("#\t\t\t\t\t\t\t%8.5f %8.5f %8.5f\n", v2[0], v2[1], v2[2]); printf("#\t\t\t\t\t\t\t%8.5f %8.5f %8.5f\n", v3[0], v3[1], v3[2]); } } } return (0); }
// Execute one instruction cycle // void one_instruction_cycle(CPU *cpu) { // If the CPU isn't running, say so and return. // If the pc is out of range, complain and stop running the CPU. // if ((*cpu).running==0){ printf("The CPU is not running. \n"); return; } // Get instruction and increment pc // int instr_loc = (*cpu).pc; // Instruction's location (pc before increment) (*cpu).ir = (*cpu).mem[(*cpu).pc]; (*cpu).pc++; // Making sure the instruction is if ((*cpu).reg_R<0 || (*cpu).reg_R>NREG || (*cpu).addr_MM<0 || (*cpu).addr_MM>100 ) { printf("Error. The register or memory address given in the instruction is out of bounds.\n"); return; } //Decode opcode into opcode, reg_r, addr_MM, and instruction sign (*cpu).opcode = abs((*cpu).mem[instr_loc] / 1000); (*cpu).reg_R = abs((*cpu).mem[instr_loc] / 100) % 10; (*cpu).addr_MM = abs((*cpu).mem[instr_loc] % 100); // In case first instruction stored in memory is a zero, we need to give it a sign of 0. if((*cpu).mem[instr_loc] != 0) { (*cpu).instr_sign = (*cpu).mem[instr_loc] / abs((*cpu).mem[instr_loc]); } else{ (*cpu).instr_sign=0; } // Echo instruction printf("At %02d instr %d %d %02d: ", instr_loc, (*cpu).opcode, (*cpu).reg_R, (*cpu).addr_MM); switch ((*cpu).opcode) { case 0: HALT(cpu); break; case 1: LD(cpu); break; case 2: ST(cpu); break; case 3: ADD(cpu); break; case 4: NEG(cpu); break; case 5: LDM(cpu); break; case 6: ADDM(cpu); break; case 7: BR(cpu); break; case 8: BRC(cpu); break; case 9: switch ((*cpu).reg_R) { case 0: GETC(cpu); break; case 1: OUT(cpu); break; case 2: PUTS(cpu); break; case 3: dump_CPU(cpu); break; case 4: dump_memory(cpu); break; case 5: printf("Ignored"); break; case 6: printf("Ignored"); break; case 7: printf("Ignored"); break; case 8: printf("Ignored"); break; case 9: printf("Ignored"); break; } break; default: printf("Bad opcode!? %d\n", (*cpu).opcode); } }