/** * Function Name; * intInRange() * intInRange() returns true if the target is within bound1 and bound2 (inclusivity can be checked if * LOWER_INCLUSIVE or UPPER_INCLUSIVE are included in the flags argument) */ int intInRange(int target, int bound1, int bound2, int flags) { int lowerBound = intMin(bound1, bound2); int upperBound = intMax(bound1, bound2); int result = ((target > lowerBound && target < upperBound) || (target == lowerBound && (flags & LOWER_INCLUSIVE)) || (target == upperBound && (flags & UPPER_INCLUSIVE))); /* if (DEBUG) { char *intervalChars = "([)]"; char *booleanStrings[] = {"false", "true"}; char openIntervalChar = intervalChars[(flags & LOWER_INCLUSIVE) != 0]; char closeIntervalChar = intervalChars[((flags & UPPER_INCLUSIVE) != 0) + 2]; printf("%d in %c%d %d%c : %s\n", target, openIntervalChar, lowerBound, upperBound, closeIntervalChar, booleanStrings[result]); } */ return result; }
int SimulationResults_filterSimulationResults(const char *inFile, const char *outFile, void *vars, int numberOfIntervals) { const char *msg[5] = {"","","","",""}; void *tmp; if (UNKNOWN_PLOT == SimulationResultsImpl__openFile(inFile, &simresglob)) { return 0; } vars = mmc_mk_cons(mmc_mk_scon("time"),vars); switch (simresglob.curFormat) { case MATLAB4: { int numToFilter = listLength(vars); int i, j; int numUnique = 0; int numUniqueParam = 1; int longestName = 0; int longestDesc = 0; ModelicaMatVariable_t **mat_var = omc_alloc_interface.malloc(numToFilter*sizeof(ModelicaMatVariable_t*)); int *indexes = (int*) omc_alloc_interface.malloc(simresglob.matReader.nvar*sizeof(int)); /* Need it to be zeros; note that the actual number of indexes is smaller */ int *parameter_indexes = (int*) omc_alloc_interface.malloc(simresglob.matReader.nparam*sizeof(int)); /* Need it to be zeros; note that the actual number of indexes is smaller */ int *indexesToOutput = NULL; int *parameter_indexesToOutput = NULL; FILE *fout = NULL; char *tmp; double start_stop[2] = {0}; double start = omc_matlab4_startTime(&simresglob.matReader); double stop = omc_matlab4_stopTime(&simresglob.matReader); parameter_indexes[0] = 1; /* time */ omc_matlab4_read_all_vals(&simresglob.matReader); if (endsWith(outFile,".csv")) { double **vals = omc_alloc_interface.malloc(sizeof(double*)*numToFilter); FILE *fout = NULL; for (i=0; i<numToFilter; i++) { const char *var = MMC_STRINGDATA(MMC_CAR(vars)); vars = MMC_CDR(vars); mat_var[i] = omc_matlab4_find_var(&simresglob.matReader, var); if (mat_var[i] == NULL) { msg[0] = SystemImpl__basename(inFile); msg[1] = var; c_add_message(NULL,-1, ErrorType_scripting, ErrorLevel_error, gettext("Could not read variable %s in file %s."), msg, 2); return 0; } if (mat_var[i]->isParam) { msg[0] = var; c_add_message(NULL,-1, ErrorType_scripting, ErrorLevel_error, gettext("Could not filter parameter %s since the output format is CSV (only variables are allowed)."), msg, 1); return 0; } else { vals[i] = omc_matlab4_read_vals(&simresglob.matReader, mat_var[i]->index); } } fout = fopen(outFile, "w"); fprintf(fout, "time"); for (i=1; i<numToFilter; i++) { fprintf(fout, ",\"%s\"", mat_var[i]->name); } fprintf(fout, ",nrows=%d\n", simresglob.matReader.nrows); for (i=0; i<simresglob.matReader.nrows; i++) { fprintf(fout, "%.15g", vals[0][i]); for (j=1; j<numToFilter; j++) { fprintf(fout, ",%.15g", vals[j][i]); } fprintf(fout, "\n"); } fclose(fout); return 1; } /* Not CSV */ for (i=0; i<numToFilter; i++) { const char *var = MMC_STRINGDATA(MMC_CAR(vars)); vars = MMC_CDR(vars); mat_var[i] = omc_matlab4_find_var(&simresglob.matReader,var); if (mat_var[i] == NULL) { msg[0] = SystemImpl__basename(inFile); msg[1] = var; c_add_message(NULL,-1, ErrorType_scripting, ErrorLevel_error, gettext("Could not read variable %s in file %s."), msg, 2); return 0; } if (mat_var[i]->isParam) { /* Store the old index in the array */ if (0==parameter_indexes[abs(mat_var[i]->index)-1]++) { numUniqueParam++; } } else { /* Store the old index in the array */ if (0==indexes[abs(mat_var[i]->index)-1]++) { numUnique++; } } longestName = intMax(longestName, strlen(mat_var[i]->name)); longestDesc = intMax(longestDesc, strlen(mat_var[i]->descr)); } /* Create the list of variable indexes to output */ indexesToOutput = omc_alloc_interface.malloc_atomic(numUnique * sizeof(int)); parameter_indexesToOutput = omc_alloc_interface.malloc_atomic(numUniqueParam * sizeof(int)); j=0; for (i=0; i<simresglob.matReader.nvar; i++) { if (indexes[i]) { indexesToOutput[j++] = i+1; } /* indexes becomes the lookup table from old index to new index */ indexes[i] = j; } j=0; for (i=0; i<simresglob.matReader.nparam; i++) { if (parameter_indexes[i]) { parameter_indexesToOutput[j++] = i+1; } /* indexes becomes the lookup table from old index to new index */ parameter_indexes[i] = j; } fout = fopen(outFile, "wb"); if (fout == NULL) { return failedToWriteToFile(outFile); } /* Matrix list: "Aclass" "name" "description" "dataInfo" "data_1" "data_2" */ if (writeMatVer4AclassNormal(fout)) { return failedToWriteToFile(outFile); } if (writeMatVer4MatrixHeader(fout, "name", numToFilter, longestName, sizeof(int8_t))) { return failedToWriteToFile(outFile); } tmp = omc_alloc_interface.malloc(numToFilter*longestName); for (i=0; i<numToFilter; i++) { int len = strlen(mat_var[i]->name); for (j=0; j<len; j++) { tmp[numToFilter*j+i] = mat_var[i]->name[j]; } } if (1 != fwrite(tmp, numToFilter*longestName, 1, fout)) { return failedToWriteToFile(outFile); } GC_free(tmp); if (writeMatVer4MatrixHeader(fout, "description", numToFilter, longestDesc, sizeof(int8_t))) { return failedToWriteToFile(outFile); } tmp = omc_alloc_interface.malloc(numToFilter*longestDesc); for (i=0; i<numToFilter; i++) { int len = strlen(mat_var[i]->descr); for (j=0; j<len; j++) { tmp[numToFilter*j+i] = mat_var[i]->descr[j]; } } if (1 != fwrite(tmp, numToFilter*longestDesc, 1, fout)) { return failedToWriteToFile(outFile); } GC_free(tmp); if (writeMatVer4MatrixHeader(fout, "dataInfo", numToFilter, 4, sizeof(int32_t))) { return failedToWriteToFile(outFile); } for (i=0; i<numToFilter; i++) { int32_t x = mat_var[i]->isParam ? 1 : 2; /* data_1 or data_2 */ if (1 != fwrite(&x, sizeof(int32_t), 1, fout)) { return failedToWriteToFile(outFile); } } for (i=0; i<numToFilter; i++) { int32_t x = (mat_var[i]->index < 0 ? -1 : 1) * (mat_var[i]->isParam ? parameter_indexes[abs(mat_var[i]->index)-1] : indexes[abs(mat_var[i]->index)-1]); if (1 != fwrite(&x, sizeof(int32_t), 1, fout)) { return failedToWriteToFile(outFile); } } for (i=0; i<numToFilter; i++) { int32_t x = 0; /* linear interpolation */ if (1 != fwrite(&x, sizeof(int32_t), 1, fout)) { return failedToWriteToFile(outFile); } } for (i=0; i<numToFilter; i++) { int32_t x = -1; /* not defined outside the time interval */ if (1 != fwrite(&x, sizeof(int32_t), 1, fout)) { return failedToWriteToFile(outFile); } } if (writeMatVer4MatrixHeader(fout, "data_1", 2, numUniqueParam, sizeof(double))) { return failedToWriteToFile(outFile); } start = omc_matlab4_startTime(&simresglob.matReader); stop = omc_matlab4_stopTime(&simresglob.matReader); start_stop[0]=start; start_stop[1]=stop; if (1 != fwrite(start_stop, sizeof(double)*2, 1, fout)) { return failedToWriteToFile(outFile); } for (i=1; i<numUniqueParam; i++) { int paramIndex = parameter_indexesToOutput[i]; double d[2] = {simresglob.matReader.params[abs(paramIndex)-1],0}; d[1] = d[0]; if (1!=fwrite(d, sizeof(double)*2, 1, fout)) { return failedToWriteToFile(outFile); } } if (numberOfIntervals) { double *timevals = omc_matlab4_read_vals(&simresglob.matReader, 1); int last_found=0; int nevents=0, neventpoints=0; for (i=1; i<numberOfIntervals; i++) { double t = start + (stop-start)*((double)i)/numberOfIntervals; while (timevals[j]<=t) { if (timevals[j]==timevals[j+1]) { while (timevals[j]==timevals[j+1]) { j++; neventpoints++; } nevents++; } j++; } } msg[4] = inFile; GC_asprintf((char**)msg+3, "%d", simresglob.matReader.nrows); GC_asprintf((char**)msg+2, "%d", numberOfIntervals); GC_asprintf((char**)msg+1, "%d", nevents); GC_asprintf((char**)msg+0, "%d", neventpoints); c_add_message(NULL,-1, ErrorType_scripting, ErrorLevel_notification, gettext("Resampling %s from %s points to %s points, removing %s events stored in %s points.\n"), msg, 5); } if (writeMatVer4MatrixHeader(fout, "data_2", numberOfIntervals ? numberOfIntervals+1 : simresglob.matReader.nrows, numUnique, sizeof(double))) { return failedToWriteToFile(outFile); } for (i=0; i<numUnique; i++) { double *vals = NULL; int nrows; if (numberOfIntervals) { omc_matlab4_read_all_vals(&simresglob.matReader); nrows = numberOfIntervals+1; vals = omc_alloc_interface.malloc_atomic(sizeof(double)*nrows); for (j=0; j<=numberOfIntervals; j++) { double t = j==numberOfIntervals ? stop : start + (stop-start)*((double)j)/numberOfIntervals; ModelicaMatVariable_t var = {0}; var.name=""; var.descr=""; var.isParam=0; var.index=indexesToOutput[i]; if (omc_matlab4_val(vals+j, &simresglob.matReader, &var, t)) { msg[2] = inFile; GC_asprintf((char**)msg+1, "%d", indexesToOutput[i]); GC_asprintf((char**)msg+0, "%.15g", t); c_add_message(NULL,-1, ErrorType_scripting, ErrorLevel_error, gettext("Resampling %s failed to get variable %s at time %s.\n"), msg, 3); return 0; } } } else { vals = omc_matlab4_read_vals(&simresglob.matReader, indexesToOutput[i]); nrows = simresglob.matReader.nrows; } if (1!=fwrite(vals, sizeof(double)*nrows, 1, fout)) { return failedToWriteToFile(outFile); } if (numberOfIntervals) { GC_free(vals); } } fclose(fout); return 1; } default: msg[0] = PlotFormatStr[simresglob.curFormat]; c_add_message(NULL,-1, ErrorType_scripting, ErrorLevel_error, gettext("filterSimulationResults not implemented for plot format: %s\n"), msg, 1); return 0; } }
/** * Function Name: * pgmDrawLine() * pgmDrawLine() draws a straight line in the image by setting relavant pixels to Zero. * In this function, you have to invoke a CUDA kernel to perform all image processing on GPU. * * @param[in,out] pixels holds all pixels in the pgm image, which a 2D integer array. The array * are modified after the drawing. * @param[in] numRows describes how many rows of pixels in the image. * @param[in] numCols describes how many columns of pixels in one row in the image. * @param[in] p1row specifies the row number of the start point of the line segment. * @param[in] p1col specifies the column number of the start point of the line segment. * @param[in] p2row specifies the row number of the end point of the line segment. * @param[in] p2col specifies the column number of the end point of the line segment. * @param[in,out] header returns the new header after draw. * the function might change the maximum intensity value in the image, so we * have to change the maximum intensity value in the header accordingly. * * @return return 1 if max intensity is changed by the drawing, otherwise return 0; */ int pgmDrawLine(int *pixels, int numRows, int numCols, char **header, int p1row, int p1col, int p2row, int p2col) { if (pixels == NULL || header == NULL) return 1; int oldMaxIntens = 0; int newMaxIntens = 0; int i = 0; sscanf(header[3], "%i", &oldMaxIntens); // avoid a divide by zero error by not calculating the slope if (p1col == p2col) { if (!intInRange(p1col, 0, numCols, UPPER_INCLUSIVE | LOWER_INCLUSIVE)) return 0; int startRow = intMin(p1row, p2row); int endRow = intMax(p1row, p2row); int curRow = startRow; for (; curRow < endRow; ++curRow) { // make sure this pixel is actually within the image if (!intInRange(curRow, 0, numRows, UPPER_INCLUSIVE | LOWER_INCLUSIVE)) continue; i = numRows * curRow + p1col; pixels[i] = 0; if (pixels[i] > newMaxIntens) newMaxIntens = pixels[i]; } } else // we don't have a vertical line { int p1[2] = {0, 0}; int p2[2] = {0, 0}; if (p1col < p2col) { p1[0] = p1row; p1[1] = p1col; p2[0] = p2row; p2[1] = p2col; } else { p1[0] = p2row; p1[1] = p2col; p2[0] = p1row; p2[1] = p1col; } double slope = (p2[0] - p1[0]) / (p2[1] - p1[1]); if(DEBUG) printf("slope: %lf\n", slope); int thisCol = p1[1]; for (; thisCol < numCols; ++thisCol) { int relativeCol = thisCol - p1[1]; int thisRow = relativeCol * slope + p1[0]; // make sure this pixel is actually within the image if (!intInRange(thisRow, 0, numRows - 1, UPPER_INCLUSIVE | LOWER_INCLUSIVE)) continue; if (!intInRange(thisCol, 0, numCols - 1, UPPER_INCLUSIVE | LOWER_INCLUSIVE)) continue; //if(DEBUG) // printf("plot(%d, %d)\n", thisRow, thisCol); i = numRows * thisRow + thisCol; pixels[i] = 0; if (pixels[i] > newMaxIntens) newMaxIntens = pixels[i]; } } sprintf(header[3], "%i", newMaxIntens); return (newMaxIntens == oldMaxIntens) ? 0 : 1; }
void printSummary (char code[], int auRuns[], int oppRuns[], int innings[], int attend[], int numGames) { int arraySecAub[NUMSEC]; int arraySecOpp[NUMSEC]; int secGames = getSecGames(code, auRuns, numGames, arraySecAub); int oppSecGames = getSecGames(code, oppRuns, numGames, arraySecOpp); int count; int extra[MAXGAMES]; printf(" 2015 AU Softball Summary\n"); printf(" #games Min Mean Max\n"); printf("Runs scored-all %2d %d %.1f %2d\n", numGames, intMin(auRuns, numGames), intMean(auRuns, numGames), intMax(auRuns, numGames)); printf("Runs allowed-all %d %.1f %2d\n", intMin(oppRuns, numGames), intMean(oppRuns, numGames), intMax(oppRuns, numGames)); printf("Runs scored-SEC %2d %d %.1f %2d\n", secGames, intMin(arraySecAub, secGames),intMean(arraySecAub, secGames), intMax(arraySecAub, secGames)); printf("Runs allowed-SEC %d %.1f %2d\n\n", intMin(arraySecOpp, secGames),intMean(arraySecOpp, oppSecGames), intMax(arraySecOpp, oppSecGames)); printf("Games with extra innings:\n"); for (count = 0; count < extraInnings(innings, numGames, extra); count++) { printf(" %d ", extra[count]); } }