int relay_newFromFile(RelayProblem* problem, FILE* file) { int n = 0; int i; CHECK_IO(fscanf(file, "%d\n", &n)); problem->size = n; problem->M = newNumericsMatrix(); newFromFile(problem->M, file); problem->q = (double *) malloc(problem->M->size1 * sizeof(double)); for (i = 0; i < problem->M->size1; i++) { CHECK_IO(fscanf(file, "%lf ", &(problem->q[i]))); } problem->lb = (double *) malloc(problem->M->size1 * sizeof(double)); for (i = 0; i < problem->M->size1; i++) { CHECK_IO(fscanf(file, "%lf ", &(problem->lb[i]))); } problem->ub = (double *) malloc(problem->M->size1 * sizeof(double)); for (i = 0; i < problem->M->size1; i++) { CHECK_IO(fscanf(file, "%lf ", &(problem->ub[i]))); } return 1; }
void readInFile(NumericsMatrix* const m, FILE *file) { if (! m) { fprintf(stderr, "Numerics, NumericsMatrix readInFile failed, NULL input.\n"); exit(EXIT_FAILURE); } CHECK_IO(fscanf(file, "%d", &(m->storageType))); CHECK_IO(fscanf(file, "%d", &(m->size0))); CHECK_IO(fscanf(file, "%d", &(m->size1))); int storageType = m->storageType; if (storageType == 0) { CHECK_IO(fscanf(file, "%d\t%d\n", &(m->size0), &(m->size1))); for (int i = 0; i < m->size1 * m->size0; i++) { CHECK_IO(fscanf(file, "%le ", &(m->matrix0[i]))); } } else if (storageType == 1) { m->matrix0 = NULL; readInFileSBM(m->matrix1, file); } }
int is_gui(char *exename) { HANDLE hImage; DWORD bytes; OVERLAPPED overlap; ULONG ntSignature; struct DosHeader{ IMAGE_DOS_HEADER doshdr; DWORD extra[16]; }; struct DosHeader dh; IMAGE_OPTIONAL_HEADER optionalhdr; int retCode = 0; memset(&overlap,0,sizeof(overlap)); hImage = CreateFile(exename, GENERIC_READ, FILE_SHARE_READ, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL| FILE_FLAG_OVERLAPPED, NULL); if (INVALID_HANDLE_VALUE == hImage) { return 0; } ReadFile(hImage, &dh, sizeof(struct DosHeader), &bytes,&overlap); CHECK_IO(hImage,&overlap); if (IMAGE_DOS_SIGNATURE != dh.doshdr.e_magic) { goto done; } // read from the coffheaderoffset; overlap.Offset = dh.doshdr.e_lfanew; ReadFile(hImage, &ntSignature, sizeof(ULONG), &bytes,&overlap); CHECK_IO(hImage,&overlap); if (IMAGE_NT_SIGNATURE != ntSignature) { goto done; } overlap.Offset = dh.doshdr.e_lfanew + sizeof(ULONG) + sizeof(IMAGE_FILE_HEADER); ReadFile(hImage, &optionalhdr,IMAGE_SIZEOF_NT_OPTIONAL_HEADER, &bytes,&overlap); CHECK_IO(hImage,&overlap); if (optionalhdr.Subsystem ==IMAGE_SUBSYSTEM_WINDOWS_GUI) retCode = 1; done: CloseHandle(hImage); return retCode; }
bool vm_load(VectorOfMatrices* vm, const char* filename) { assert(vm != NULL); FILE* fp = fopen(filename, "r"); if (fp == NULL) { perror(filename); goto return_false; } int fmt; unsigned long count; unsigned long rows; unsigned long cols; int suc = fscanf(fp, "%d", &fmt); CHECK_IO(suc, 1, return_false); if (fmt != FMT_VECTOR_OF_MATRICES) { goto return_false; } suc = fscanf(fp, "%lu %lu %lu", &count, &rows, &cols); CHECK_IO(suc, 3, return_false); if (rows <= 0 || cols <= 0 || count <= 0) { goto return_false; } vm->rows = rows; vm->cols = cols; vm->count = count; if (!vm_alloc(vm)) { goto return_false; } int* pvm = vm->items; size_t iter_count = rows * cols * count; for (size_t i = 0; i < iter_count; ++i) { suc = fscanf(fp, "%d", pvm++); CHECK_IO(suc, 1, return_false); } if (fclose(fp) != 0) { perror(filename); } return true; return_false: if (fp != NULL && fclose(fp) != 0) { perror(filename); } vm_free(vm); return false; }
bool matrix_load(Matrix* mat, const char* filename) { assert(mat != NULL); FILE* fp = fopen(filename, "r"); if (fp == NULL) { perror(filename); goto return_false; } int fmt; unsigned long rows; unsigned long cols; int suc = fscanf(fp, "%d", &fmt); CHECK_IO(suc, 1, return_false); if (fmt != FMT_MATRIX) { goto return_false; } suc = fscanf(fp, "%lu %lu", &rows, &cols); CHECK_IO(suc, 2, return_false); if (rows <= 0 || cols <= 0) { goto return_false; } mat->rows = rows; mat->cols = cols; if (!matrix_alloc(mat)) { goto return_false; } int* pMat = mat->items; size_t count = rows * cols; for (size_t i = 0; i < count; ++i) { suc = fscanf(fp, "%d", pMat++); CHECK_IO(suc, 1, return_false); } if (fclose(fp) != 0) { perror(filename); } return true; return_false: if (fp != NULL && fclose(fp) != 0) { perror(filename); } matrix_free(mat); return false; }
int linearComplementarity_newFromFile(LinearComplementarityProblem* problem, FILE* file) { int n = 0; int i; CHECK_IO(fscanf(file, "%d\n", &n)); problem->size = n; problem->M = NM_new_from_file(file); problem->q = (double *) malloc(problem->M->size1 * sizeof(double)); for (i = 0; i < problem->M->size1; i++) { CHECK_IO(fscanf(file, "%lf ", &(problem->q[i]))); } return 1; }
int newFromFile(NumericsMatrix* const m, FILE *file) { if (! m) { fprintf(stderr, "Numerics, NumericsMatrix newFromFile failed, NULL input.\n"); exit(EXIT_FAILURE); } int storageType; size_t size0; size_t size1; int info = 0; void* data = NULL; CHECK_IO(fscanf(file, "%d", &storageType), &info); CHECK_IO(fscanf(file, SN_SIZE_T_F, &size0), &info); CHECK_IO(fscanf(file, SN_SIZE_T_F, &size1), &info); if (storageType == NM_DENSE) { CHECK_IO(fscanf(file, SN_SIZE_T_F "\t" SN_SIZE_T_F "\n", &size0, &size1), &info); data = malloc(size1 * size0 * sizeof(double)); double* data_d = (double*) data; for (size_t i = 0; i < size1 * size0; ++i) { CHECK_IO(fscanf(file, "%le ", &(data_d[i])), &info); } } else if (storageType == NM_SPARSE_BLOCK) { data = newSBM(); newFromFileSBM((SparseBlockStructuredMatrix*)data, file); } fillNumericsMatrix(m, storageType, (int)size0, (int)size1, data); return info; }
int globalFrictionContact_newFromFile(GlobalFrictionContactProblem* problem, FILE* file) { int nc = 0, d = 0; int info = 0; CHECK_IO(fscanf(file, "%d\n", &d), &info); problem->dimension = d; CHECK_IO(fscanf(file, "%d\n", &nc), &info); problem->numberOfContacts = nc; problem->M = newNumericsMatrix(); info = newFromFile(problem->M, file); if (info) goto fail; problem->H = newNumericsMatrix(); info = newFromFile(problem->H, file); if (info) goto fail; problem->q = (double *) malloc(problem->M->size1 * sizeof(double)); for (int i = 0; i < problem->M->size1; ++i) { CHECK_IO(fscanf(file, "%lf ", &(problem->q[i])), &info); } problem->b = (double *) malloc(problem->H->size1 * sizeof(double)); for (int i = 0; i < problem->H->size1; ++i) { CHECK_IO(fscanf(file, "%lf ", &(problem->b[i])), &info); } problem->mu = (double *) malloc(nc * sizeof(double)); for (int i = 0; i < nc; ++i) { CHECK_IO(fscanf(file, "%lf ", &(problem->mu[i])), &info); } fail: problem->env = NULL; problem->workspace = NULL; return info; }
int secondOrderConeLinearComplementarityProblem_newFromFile(SecondOrderConeLinearComplementarityProblem* problem, FILE* file) { DEBUG_PRINT("Start -- int secondOrderConeLinearComplementarityProblem_newFromFile(SecondOrderConeLinearComplementarityProblem* problem, FILE* file)\n"); int n = 0; int nc = 0; int i; CHECK_IO(fscanf(file, "%d\n", &n)); problem->n = n; CHECK_IO(fscanf(file, "%d\n", &nc)); problem->nc = nc; problem->M = newNumericsMatrix(); /* fix: problem->M->storageType unitialized ! */ newFromFile(problem->M, file); problem->q = (double *) malloc(problem->M->size1 * sizeof(double)); for(i = 0; i < problem->M->size1; i++) { CHECK_IO(fscanf(file, "%lf ", &(problem->q[i]))); } problem->coneIndex = (unsigned int *) malloc((nc+1) * sizeof(unsigned int)); for(i = 0; i < nc+1; i++) { CHECK_IO(fscanf(file, "%d ", &(problem->coneIndex[i]))); } problem->mu = (double *) malloc(nc * sizeof(double)); for(i = 0; i < nc; i++) { CHECK_IO(fscanf(file, "%lf ", &(problem->mu[i]))); } DEBUG_PRINT("End -- int secondOrderConeLinearComplementarityProblem_newFromFile(SecondOrderConeLinearComplementarityProblem* problem, FILE* file)\n"); return 0; }
void readSolverOptions(int driverType, SolverOptions* options) { /* To each problem, corresponds a XXX_parameters.opt file where default parameters can be read, XXX being the problem name (LCP, FrictionContact3D ...) */ if (verbose > 0) printf("\n ========== Numerics Non Smooth Solver - Read default parameters for the solver.\n =========="); // Checks if NUMERICSSPATH is set. if (getenv("SICONOSPATH") == NULL) { fprintf(stderr, "Numerics, readSolverOptions error, SICONOSPATH environment variable not set. Can not find default solver options file.\n"); return; //exit(EXIT_FAILURE); } FILE * ficin; /* Name of the default parameters file */ char name[MAX_ENV_SIZE]; char nameSuffix[] = "/include/siconos"; strncpy(name, getenv("SICONOSPATH"), MAX_ENV_SIZE - sizeof(nameSuffix)); strcat(name, nameSuffix); char buffer[64]; char bufferName[64]; /* Return value for reading */ int nval; // set default size to 4 ... if (options->iparam == NULL) options->iparam = (int*)malloc(4 * sizeof(*options->iparam)); if (options->dparam == NULL) options->dparam = (double*)malloc(4 * sizeof(*options->dparam)); switch (driverType) { case 0: strcat(name, "LCP_parameters.opt"); break; case 1: strcat(name, "dfc2D_parameters.opt"); break; case 2: strcat(name, "FrictionContact2D_parameters.opt"); break; case 3: strcat(name, "FrictionContact3D_parameters.opt"); ficin = fopen(name, "r"); if (verbose > 0) printf("The default-parameters file is: %s\n", name); if (!ficin) { printf("Numerics, readSolverOptions error. Can not open file %60s", name); exit(-1); } //nval = fscanf(ficin, "%c", &(options->solverName)); CHECK_IO(fgets(buffer, 64, ficin)); CHECK_IO(fgets(buffer, 64, ficin)); CHECK_IO(fgets(buffer, 64, ficin)); /* Solver name */ CHECK_IO(fgets(bufferName , 64, ficin)); options->solverId = nameToId(bufferName); CHECK_IO(fgets(buffer, 64, ficin)); /* iparam */ nval = fscanf(ficin, "%d%d", &(options->iparam[0]), &(options->iparam[1])); if (nval != 4) { fprintf(stderr, "Numerics, readSolverOptions error, wrong number of parameters for iparam.\n"); exit(EXIT_FAILURE); } /* dparam */ nval = fscanf(ficin, "%lf%lf%lf", &(options->dparam[0]), &(options->dparam[1]), &(options->dparam[2])); if (nval != 3) { fprintf(stderr, "Numerics, readSolverOptions error, wrong number of parameters for dparam.\n"); exit(EXIT_FAILURE); } fclose(ficin); break; default: fprintf(stderr, "Numerics, readSolverOptions error, unknown problem type.\n"); exit(EXIT_FAILURE); } }
bool test(const char* filename, FILE* fout) { FILE* fp = fopen(filename, "r"); if (fp == NULL) { perror(filename); return false; } int fmt; int suc = fscanf(fp, "%d", &fmt); if (suc == EOF || suc != 1) { fclose(fp); return false; } if (fclose(fp) != 0) { perror(filename); } switch (fmt) { case FMT_VECTOR: { Vector vect = { .count = 0, .items = NULL }; if (!vector_load(&vect, filename)) { return false; } vector_print(&vect, fout); vector_free(&vect); } break; case FMT_MATRIX: { Matrix mat = { .rows = 0, .cols = 0, .items = NULL }; if (!matrix_load(&mat, filename)) { return false; } matrix_print(&mat, fout); matrix_free(&mat); } break; case FMT_VECTOR_OF_MATRICES: { VectorOfMatrices vm = { .count = 0, .rows = 0, .cols = 0, .items = NULL }; if (!vm_load(&vm, filename)) { return false; } vm_print(&vm, fout); vm_free(&vm); } break; default: return false; } return true; } bool vector_load(Vector* vect, const char* filename) { assert(vect != NULL); FILE* fp = fopen(filename, "r"); if (fp == NULL) { perror(filename); goto return_false; } int fmt; unsigned long count; int suc = fscanf(fp, "%d", &fmt); CHECK_IO(suc, 1, return_false); if (fmt != FMT_VECTOR) { goto return_false; } suc = fscanf(fp, "%lu", &count); CHECK_IO(suc, 1, return_false); if (count <= 0) { goto return_false; } vect->count = count; if (!vector_alloc(vect)) { goto return_false; } int* pVect = vect->items; for (size_t i = 0; i < vect->count; ++i) { suc = fscanf(fp, "%d", pVect++); CHECK_IO(suc, 1, return_false); } if (fclose(fp) != 0) { perror(filename); } return true; return_false: if (fp != NULL && fclose(fp) != 0) { perror(filename); } vector_free(vect); return false; } void vector_print(const Vector* vect, FILE* fp) { assert(vect != NULL); assert(fp != NULL); assert(vect->count > 0); assert(vect->items != NULL); fprintf(fp, "%d\n", FMT_VECTOR); fprintf(fp, "%zu\n", vect->count); int* pVect = vect->items; for (size_t i = 0; i < vect->count; ++i) { fprintf(fp, "%d ", *pVect++); } } void noop_print(FILE* fp) { assert(fp != NULL); fprintf(fp, "false\n"); }
int mixedLinearComplementarity_newFromFileOld(MixedLinearComplementarityProblem* problem, FILE* file) { int n = 0, m = 0, NbLines = 0; int i, j, m2; char val[128]; double *vecA, *vecB, *vecC, *vecD, *vecM, *vecQ; double *a, *b; CHECK_IO(fscanf(file , "%d" , &n)); CHECK_IO(fscanf(file , "%d" , &m)); CHECK_IO(fscanf(file , "%d" , &NbLines)); m2 = m * m; vecM = (double*)malloc((n + m) * (NbLines) * sizeof(double)); vecQ = (double*)malloc((NbLines) * sizeof(double)); vecA = (double*)malloc(n * (NbLines - m) * sizeof(double)); vecB = (double*)malloc(m2 * sizeof(double)); vecC = (double*)malloc((NbLines - m) * m * sizeof(double)); vecD = (double*)malloc(m * n * sizeof(double)); a = (double*)malloc((NbLines - m) * sizeof(double)); b = (double*)malloc(m * sizeof(double)); problem->blocksRows = (int*)malloc(3 * sizeof(int)); problem->blocksIsComp = (int*)malloc(2 * sizeof(int)); problem->blocksRows[0] = 0; problem->blocksRows[1] = n; problem->blocksRows[2] = n + m; problem->blocksIsComp[0] = 0; problem->blocksIsComp[1] = 1; problem->M = createNumericsMatrixFromData(NM_DENSE, NbLines, n + m, vecM); problem->isStorageType1 = 1; // Both problems seems to be stored problem->isStorageType2 = 1; // Both problems seems to be stored problem->q = vecQ; problem->A = vecA; problem->B = vecB; problem->C = vecC; problem->D = vecD; problem->a = a; problem->b = b; problem->blocksRows[1] = n; problem->blocksRows[2] = n + m; problem->n = n; problem->m = m; for (i = 0 ; i < NbLines - m ; ++i) { for (j = 0 ; j < n ; ++j) { CHECK_IO(fscanf(file, "%s", val)); vecA[(NbLines - m)*j + i ] = atof(val); vecM[(NbLines)*j + i ] = atof(val); } } for (i = 0 ; i < m ; ++i) { for (j = 0 ; j < m ; ++j) { CHECK_IO(fscanf(file, "%s", val)); vecB[ m * j + i ] = atof(val); /* vecM[ n*(m+n)+(n+m)*j+n+i ] = atof(val);*/ vecM[ n * (NbLines) + (NbLines)*j + (NbLines - m) + i ] = atof(val); } } for (i = 0 ; i < NbLines - m ; ++i) { for (j = 0 ; j < m ; ++j) { CHECK_IO(fscanf(file, "%s", val)); vecC[(NbLines - m)*j + i ] = atof(val); vecM[(NbLines) * (n + j) + i ] = atof(val); } } for (i = 0 ; i < m ; ++i) { for (j = 0 ; j < n ; ++j) { CHECK_IO(fscanf(file, "%s", val)); vecD[ m * j + i ] = atof(val); vecM[(NbLines)*j + i + (NbLines - m) ] = atof(val); } } for (i = 0 ; i < NbLines - m ; ++i) { CHECK_IO(fscanf(file , "%s" , val)); a[i] = atof(val); vecQ[i] = atof(val); } for (i = 0 ; i < m ; ++i) { CHECK_IO(fscanf(file , "%s" , val)); b[i] = atof(val); vecQ[i + NbLines - m] = atof(val); } return 0; }
int mixedLinearComplementarity_newFromFile(MixedLinearComplementarityProblem* problem, FILE* file) { int info = 0; assert(file); if (! problem) { fprintf(stderr, "Numerics, MixedLinearComplementarityProblem printInFile failed, NULL input.\n"); exit(EXIT_FAILURE); } int i, j; int st1, st2; CHECK_IO(fscanf(file, "%d\n", &st1)); problem->isStorageType1 = st1; CHECK_IO(fscanf(file, "%d\n", &st2)); problem->isStorageType2 = st2; int n ; CHECK_IO(fscanf(file, "%d\n", &n)); problem->n = n; int m; CHECK_IO(fscanf(file, "%d\n", &m)); problem->m = m; if (problem->isStorageType1) { int * blocksRows = (int *)malloc((n + m + 1) * sizeof(int)); int nbBlocks = 0; CHECK_IO(fscanf(file, "%d ", &(blocksRows[nbBlocks]))); while (blocksRows[nbBlocks] < (n + m)) { nbBlocks++; CHECK_IO(fscanf(file, "%d ", &(blocksRows[nbBlocks]))); } problem->blocksRows = (int *)malloc((nbBlocks + 1) * sizeof(int)); //CHECK_IO(fscanf(file,"\n")); for (i = 0; i <= nbBlocks; i++) { problem->blocksRows[i] = blocksRows[i]; } free(blocksRows); problem->blocksIsComp = (int *)malloc((nbBlocks) * sizeof(int)); //fprintf(file,"\n"); for (i = 0; i < nbBlocks; i++) { CHECK_IO(fscanf(file, "%d ", &(problem->blocksIsComp[i]))); } //fprintf(file,"\n"); problem->M = newNumericsMatrix(); newFromFile(problem->M, file); problem->q = (double *) malloc(problem->M->size1 * sizeof(double)); for (i = 0; i < problem->M->size1; i++) { CHECK_IO(fscanf(file, "%lf ", &(problem->q[i]))); } //fprintf(file,"\n"); /* return 1; */ /* if (problem->isStorageType2) */ /* { */ /* printf("Numerics, MixedLinearComplementarityProblem printInFile only Storage1 has been printed.\n"); */ /* } */ } if (problem->isStorageType2) { problem->A = (double*)malloc(n * n * sizeof(double)); problem->B = (double*)malloc(m * m * sizeof(double)); problem->C = (double*)malloc(n * m * sizeof(double)); problem->D = (double*)malloc(m * n * sizeof(double)); problem->a = (double*)malloc(n * sizeof(double)); problem->b = (double*)malloc(m * sizeof(double)); for (i = 0; i < problem->n; i++) { for (j = 0; j < problem->n; j++) { CHECK_IO(fscanf(file, "%lf ", &(problem->A[i + j * n]))); } /* CHECK_IO(fscanf(file,"\n")); */ } for (i = 0; i < problem->m; i++) { for (j = 0; j < problem->m; j++) { CHECK_IO(fscanf(file, "%lf ", &(problem->B[i + j * m]))); } /* fprintf(file,"\n"); */ } for (i = 0; i < problem->n; i++) { for (j = 0; j < problem->m; j++) { CHECK_IO(fscanf(file, "%lf ", &(problem->C[i + j * n]))); } /* fprintf(file,"\n"); */ } for (i = 0; i < problem->m; i++) { for (j = 0; j < problem->n; j++) { CHECK_IO(fscanf(file, "%lf ", &(problem->D[i + j * m]))); } /* fprintf(file,"\n"); */ } for (i = 0; i < problem->n; i++) { CHECK_IO(fscanf(file, "%lf ", &(problem->a[i]))); } /* fprintf(file,"\n"); */ for (i = 0; i < problem->m; i++) { CHECK_IO(fscanf(file, "%lf ", &(problem->b[i]))); } } return info; }