1 /* Siconos is a program dedicated to modeling, simulation and control
2  * of non smooth dynamical systems.
3  *
4  * Copyright 2021 INRIA.
5  *
6  * Licensed under the Apache License, Version 2.0 (the "License");
7  * you may not use this file except in compliance with the License.
8  * You may obtain a copy of the License at
9  *
10  * http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing, software
13  * distributed under the License is distributed on an "AS IS" BASIS,
14  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15  * See the License for the specific language governing permissions and
16  * limitations under the License.
17 */
18 #ifndef LCP_PROBLEM_C
19 #define LCP_PROBLEM_C
20 
21 #include "LinearComplementarityProblem.h"
22 #include <assert.h>            // for assert
23 #include <stdio.h>             // for printf, fprintf, fscanf, NULL, FILE
24 #include <stdlib.h>            // for free, malloc, exit, EXIT_FAILURE
25 #include "NumericsMatrix.h"    // for NM_display, NM_clear, NM_new_from_file
26 #include "numerics_verbose.h"  // for CHECK_IO
27 
linearComplementarity_display(LinearComplementarityProblem * problem)28 void linearComplementarity_display(LinearComplementarityProblem* problem)
29 {
30 
31   assert(problem);
32   int i, n = problem->size;
33   printf("LinearComplementarityProblem Display :\n-------------\n");
34   printf("size :%d \n", problem->size);
35   if(problem->M)
36   {
37     printf("M matrix:\n");
38     NM_display(problem->M);
39   }
40   else
41     printf("No M matrix:\n");
42 
43   if(problem->q)
44   {
45     printf("q vector:\n");
46     for(i = 0; i < n; i++) printf("q[ %i ] = %12.8e\n", i, problem->q[i]);
47   }
48   else
49     printf("No q vector:\n");
50 
51 }
52 
linearComplementarity_printInFile(LinearComplementarityProblem * problem,FILE * file)53 int linearComplementarity_printInFile(LinearComplementarityProblem*  problem, FILE* file)
54 {
55   if(! problem)
56   {
57     fprintf(stderr, "Numerics, LinearComplementarityProblem printInFile failed, NULL input.\n");
58     exit(EXIT_FAILURE);
59   }
60   int i;
61   int n = problem->size;
62   fprintf(file, "%d\n", n);
63   NM_write_in_file(problem->M, file);
64   for(i = 0; i < problem->M->size1; i++)
65   {
66     fprintf(file, "%32.24e ", problem->q[i]);
67   }
68   return 1;
69 }
70 
linearComplementarity_newFromFile(LinearComplementarityProblem * problem,FILE * file)71 int linearComplementarity_newFromFile(LinearComplementarityProblem* problem, FILE* file)
72 {
73   int n = 0;
74   int i;
75 
76   CHECK_IO(fscanf(file, "%d\n", &n));
77   problem->size = n;
78   problem->M = NM_new_from_file(file);
79 
80   problem->q = (double *) malloc(problem->M->size1 * sizeof(double));
81   for(i = 0; i < problem->M->size1; i++)
82   {
83     CHECK_IO(fscanf(file, "%lf ", &(problem->q[i])));
84   }
85   return 1;
86 }
linearComplementarity_newFromFilename(LinearComplementarityProblem * problem,const char * filename)87 int linearComplementarity_newFromFilename(LinearComplementarityProblem* problem, const char* filename)
88 {
89   int info = 0;
90   FILE * file = fopen(filename, "r");
91   if(file == NULL)
92   {
93     printf("Error! Could not open filename %s\n", filename);
94     exit(EXIT_FAILURE);
95   }
96 
97   info = linearComplementarity_newFromFile(problem, file);
98 
99   fclose(file);
100   return info;
101 }
102 
freeLinearComplementarityProblem(LinearComplementarityProblem * problem)103 void freeLinearComplementarityProblem(LinearComplementarityProblem* problem)
104 {
105   if(problem->M)
106   {
107     NM_clear(problem->M);
108     free(problem->M);
109     problem->M = NULL;
110   }
111   if(problem->q)
112   {
113     free(problem->q);
114     problem->q = NULL;
115   }
116 
117   free(problem);
118 }
119 
newLCP(void)120 LinearComplementarityProblem*  newLCP(void)
121 {
122   LinearComplementarityProblem * LCP = (LinearComplementarityProblem *) malloc(sizeof(LinearComplementarityProblem));
123   LCP->size = 0;
124   LCP->M = NULL;
125   LCP->q = NULL;
126 
127   return LCP;
128 }
129 
130 #endif
131