int main() { int Nrun = 500; initPositions(); initVelocities(); for (int i = 0; i < N; i++) // creates array of class methane objects methr[i]; std::ofstream file("methane_coords_500_160211.xyz"); std::ofstream fileT("methane_temperature_500_160211.txt"); for (int k = 0; k < Nrun; k++) { if (k % 100 == 0) std::cout << k << std::endl; fileT << instantTemp() << std::endl; file << 5*N << std::endl; file << "blah" << std::endl; if (k % 25 == 0) rescaleVelocities(); for (int j = 0; j < N; j++) // set methane on lattice point methr[j].set_coord(r[j][0], r[j][1], r[j][2]); // methane meth; for (int j = 0; j < N; j++) { for (int k = 0; k < 5; k++){ if (k == 0){ file << "C "; for (int l = 0; l < 3; l++) file << " " << methr[j].meth_coord[k][l]; } else file << "H "; for (int l = 0; l < 3; l++) file << " " << methr[j].meth_coord[k][l]; file << std::endl; } } velocityVerlet(dt); } file.close(); fileT.close(); return 0; }
void trainTask() { int trainNum, reply = 0, src; Receive(&src, (char *)&trainNum, sizeof(int)); Reply(src, (char *)&reply, sizeof(int)); track_node track[TRACK_MAX]; initTrack(track); int velocity[15]; initVelocities(trainNum, velocity); //find your location int location; struct TrainMessage msg; int dest,i,j, sensorsPassed; int oldSensor, sensor, oldTime, time; while(true) { Receive(&src, (char *)&msg, sizeof(struct TrainMessage)); switch(msg.type) { case TRAINGOTO: resetSensorBuffer(); dest = 0; while(dest < TRACK_MAX && strcmp(track[dest].name, msg.dest) != 0) dest++; location = moveToLocation(trainNum, location, dest, track, msg.doReverse, msg.speed, velocity); Reply(src, (char *)&reply, sizeof(int)); break; case TRAININIT: Reply(src, (char *)&reply, sizeof(int)); Putc2(1, (char)2, (char)trainNum); location = waitOnAnySensor(); Putc2(1, (char)0, (char)trainNum); break; case TRAINCONFIGVELOCITY: Putc2(1, (char)2, (char)trainNum); location = waitOnAnySensor(); Putc2(1, (char)0, (char)trainNum); //move to sensor B15 location = moveToLocation(trainNum, location, 30, track, false, 10, velocity); setSwitchState(14, 'C'); setSwitchState(13, 'S'); setSwitchState(10, 'S'); setSwitchState(9, 'C'); setSwitchState(8, 'C'); setSwitchState(17, 'S'); setSwitchState(16, 'S'); setSwitchState(15, 'C'); for(i=8; i<15; i++) { Putc2(1, (char)i, (char)trainNum); oldSensor = waitOnAnySensor(); oldTime = Time(); sensorsPassed = 0; for(j=0; j<15; j++) { sensor = waitOnAnySensor(); time = Time(); int distance = 1000*BFS(oldSensor, sensor, track, NULL, false); int newVelocity = distance/(time - oldTime); if(velocity[i] - newVelocity < velocity[i]/200 && sensorsPassed >= 10) break; velocity[i] *= 95; velocity[i] += 5*newVelocity; velocity[i] /= 100; ++sensorsPassed; } printf("Configured speed %d to %dmm/s\r", i, velocity[i]/10); } Putc2(1, (char)0, (char)trainNum); Reply(src, (char *)&reply, sizeof(int)); break; } } }
void parseCommand(char *command, int *trainSpeeds, int *train) { char *argv[MAX_ARGS]; int argc = formatArgs(command, argv); if(strcmp(argv[0], "tr") == 0) { if(numArgs(argc, argv) != 2) { printColored(RED, BLACK, "Error: command tr expects 2 arguments\r"); }else{ int trainNumber = strToInt(getArgument(argc, argv, 0)); int trainSpeed = strToInt(getArgument(argc, argv, 1)); if(trainNumber == -1 || trainNumber < 1 || trainNumber > 80) { printColored(RED, BLACK, "Error: train number must be a number between 1 and 80\r"); }else if(trainSpeed == -1 || trainSpeed < 0 || trainSpeed > 14) { printColored(RED, BLACK, "Error: train speed must be a number between 0 and 14\r"); }else { Putc2(1, (char)trainSpeed, (char)trainNumber); trainSpeeds[trainNumber] = trainSpeed; } } }else if(strcmp(argv[0], "rv") == 0) { if(numArgs(argc, argv) != 1) { printColored(RED, BLACK, "Error: command rv expects an argument\r"); }else{ int trainNumber = strToInt(getArgument(argc, argv, 0)); if(trainNumber == -1 || trainNumber < 1 || trainNumber > 80) { printColored(RED, BLACK, "Error: train number must be a number between 1 and 80\r"); }else{ Putc2(1, (char)0, (char)trainNumber); int reverseTask = Create(1, reverser); int reply; struct trainInfo info; info.speed = trainSpeeds[trainNumber]; info.number = trainNumber; Send(reverseTask, (char *)&info, sizeof(struct trainInfo), (char *)&reply, sizeof(int)); } } }else if(strcmp(argv[0], "sw") == 0) { if(numArgs(argc, argv) != 2) { printColored(RED, BLACK, "Error: command sw expects two arguments\r"); }else{ int switchNumber = strToInt(getArgument(argc, argv, 0)); if(strcmp(getArgument(argc, argv, 1), "S") == 0) { setSwitchState(switchNumber, 'S'); }else if(strcmp(getArgument(argc, argv, 1), "C") == 0) { setSwitchState(switchNumber, 'C'); }else{ printColored(RED, BLACK, "Error: switch direction must be 'S' or 'C'\r"); } } }else if(strcmp(argv[0], "q") == 0) { outputEscape("[2J"); moveCursor(1,1); Shutdown(); }else if(strcmp(argv[0], "setTrack") == 0) { if(numArgs(argc, argv) != 1) { printColored(RED, BLACK, "Error: command setTrack expects an argument\r"); }else{ if(strcmp(getArgument(argc, argv, 0), "A") == 0) { setTrack(TRACKA); }else if(strcmp(getArgument(argc, argv, 0), "B") == 0) { setTrack(TRACKB); }else{ printColored(RED, BLACK, "Error: track must be A or B\r"); } } }else if(strcmp(argv[0], "move") == 0) { if(numArgs(argc, argv) != 3) { printColored(RED, BLACK, "Error: command move expects two arguments\r"); }else{ struct TrainMessage msg; int trainNum = strToInt(getArgument(argc, argv, 0)); msg.type = TRAINGOTO; strcpy(msg.dest, getArgument(argc, argv, 1)); msg.doReverse = getFlag(argc, argv, "r"); msg.speed = strToInt(getArgument(argc, argv, 2)); if(train[trainNum] == -1) { printColored(RED, BLACK, "Error: train %d has not been initialized\r", trainNum); }else{ int reply; Send(train[trainNum], (char *)&msg, sizeof(struct TrainMessage), (char *)&reply, sizeof(int)); } } }else if(strcmp(argv[0], "randomizeSwitches") == 0) { int i; seed(Time()); for(i=1; i<19; i++) { int dir = random() % 2; if(dir == 0) { setSwitchState(i, 'S'); }else{ setSwitchState(i, 'C'); } } for(i=153; i<157; i++) { int dir = random() % 2; if(dir == 0) { setSwitchState(i, 'S'); }else{ setSwitchState(i, 'C'); } } }else if(strcmp(argv[0], "init") == 0) { if(numArgs(argc, argv) != 1) { printColored(RED, BLACK, "Error: command init expects an argument\r"); }else{ struct TrainMessage msg; int trainNum = strToInt(getArgument(argc, argv, 0)); int reply; if(train[trainNum] == -1) { train[trainNum] = Create(2, trainTask); Send(train[trainNum], (char *)&trainNum, sizeof(int), (char *)&reply, sizeof(int)); } msg.type = TRAININIT; Send(train[trainNum], (char *)&msg, sizeof(struct TrainMessage), (char *)&reply, sizeof(int)); } }else if(strcmp(argv[0], "d") == 0) { track_node track[TRACK_MAX]; initTrack(track); int trainNum = 45; int trainSpeed = strToInt(getArgument(argc, argv, 0)); Putc2(1, (char)trainSpeed, (char)trainNum); int sensor, lastSensor = waitOnAnySensor(), lastTime = Time(), time; int v = 0; while(true) { sensor = waitOnAnySensor(); time = Time(); if(sensor == 71) break; int timeDelta = (time - lastTime); int distance = BFS(lastSensor, sensor, track, NULL, false); int newVelocity = (500*distance)/timeDelta; v *= 95; v += newVelocity; v /= 100; lastTime = time; lastSensor = sensor; printAt(9, 1, "Velocity: %dmm/s\r", v); } Putc2(1, (char)0, (char)trainNum); }else if(strcmp(argv[0], "a") == 0) { track_node track[TRACK_MAX]; initTrack(track); int trainNum = 45; int trainSpeed = strToInt(getArgument(argc, argv, 2)); int source = 0, dest = 0; int velocity[15]; initVelocities(trainNum, velocity); while(source < TRACK_MAX && strcmp(track[source].name, getArgument(argc, argv, 0)) != 0) source++; while(dest < TRACK_MAX && strcmp(track[dest].name, getArgument(argc, argv, 1)) != 0) dest++; int distance = BFS(source, dest, track, NULL, false); Putc2(1, (char)trainSpeed, (char)trainNum); int t0 = Time(); printf("Time 0: %d\r", t0); waitOnSensor(dest); int t2 = Time(); Putc2(1, (char)0, (char)trainNum); printf("Time 2: %d\r", t2); int t1 = -(200*distance)/velocity[trainSpeed]; t1 -= t0; t1 += 2*t2; printf("Time 1: %d\r", t1); printf("Accelerating to speed %d takes %d ticks\r", trainSpeed, t1-t0); }else if(strcmp(argv[0], "clear") == 0) { moveCursor(13, 1); outputEscape("[J"); }else if(strcmp(argv[0], "configureVelocities") == 0) { struct TrainMessage msg; msg.type = TRAINCONFIGVELOCITY; if(argc != 2) { printColored(RED, BLACK, "Command configureVelocities expects an argument\r"); }else{ int trainNum = strToInt(getArgument(argc, argv, 0)); int reply; if(train[trainNum] == -1) { train[trainNum] = Create(2, trainTask); Send(train[trainNum], (char *)&trainNum, sizeof(int), (char *)&reply, sizeof(int)); } Send(train[trainNum], (char *)&msg, sizeof(struct TrainMessage), (char *)&reply, sizeof(int)); } }else{ printColored(RED, BLACK, "Unrecognized command: \"%s\"\r", command); } }