|
45 | 45 |
|
46 | 46 | int externalInputallocate(DATA* data) |
47 | 47 | { |
48 | | - FILE * pFile; |
49 | | - int n,m,c; |
50 | | - int i,j; |
51 | | - |
52 | | - pFile = fopen("externalInput.csv","r"); |
53 | | - data->simulationInfo.external_input.active = (modelica_boolean) (pFile != NULL); |
54 | | - n = 0; |
55 | | - if(data->simulationInfo.external_input.active){ |
56 | | - while(1) { |
57 | | - c = fgetc(pFile); |
58 | | - if (c==EOF) break; |
59 | | - if (c=='\n') ++n; |
60 | | - } |
61 | | - --n; |
62 | | - data->simulationInfo.external_input.n = n; |
63 | | - rewind(pFile); |
64 | | - |
65 | | - do{ |
66 | | - c = fgetc(pFile); |
67 | | - }while(c!='\n'); |
68 | | - |
69 | | - m = data->modelData.nInputVars; |
70 | | - data->simulationInfo.external_input.u = (modelica_real**)calloc(n,sizeof(modelica_real*)); |
71 | | - for(i = 0; i<n; ++i) |
72 | | - data->simulationInfo.external_input.u[i] = (modelica_real*)calloc(m,sizeof(modelica_real)); |
73 | | - data->simulationInfo.external_input.t = (modelica_real*)calloc(n,sizeof(modelica_real)); |
74 | | - |
75 | | - for(i = 0; i < n; ++i){ |
76 | | - fscanf(pFile, "%f", &data->simulationInfo.external_input.t[i]); |
77 | | - for(j = 0; j < m; ++j){ |
78 | | - fscanf(pFile, "%f", &data->simulationInfo.external_input.u[i][j]); |
79 | | - } |
80 | | - } |
81 | | - fclose(pFile); |
82 | | - data->simulationInfo.external_input.i = 0; |
83 | | - } |
84 | | - |
85 | | - return 0; |
| 48 | + FILE * pFile; |
| 49 | + int n,m,c; |
| 50 | + int i,j; |
| 51 | + |
| 52 | + pFile = fopen("externalInput.csv","r"); |
| 53 | + data->simulationInfo.external_input.active = (modelica_boolean) (pFile != NULL); |
| 54 | + n = 0; |
| 55 | + if(data->simulationInfo.external_input.active){ |
| 56 | + while(1) { |
| 57 | + c = fgetc(pFile); |
| 58 | + if (c==EOF) break; |
| 59 | + if (c=='\n') ++n; |
| 60 | + } |
| 61 | + --n; |
| 62 | + data->simulationInfo.external_input.n = n; |
| 63 | + rewind(pFile); |
| 64 | + |
| 65 | + do{ |
| 66 | + c = fgetc(pFile); |
| 67 | + }while(c!='\n'); |
| 68 | + |
| 69 | + m = data->modelData.nInputVars; |
| 70 | + data->simulationInfo.external_input.u = (modelica_real**)calloc(n,sizeof(modelica_real*)); |
| 71 | + for(i = 0; i<n; ++i) |
| 72 | + data->simulationInfo.external_input.u[i] = (modelica_real*)calloc(m,sizeof(modelica_real)); |
| 73 | + data->simulationInfo.external_input.t = (modelica_real*)calloc(n,sizeof(modelica_real)); |
| 74 | + |
| 75 | + for(i = 0; i < n; ++i){ |
| 76 | + fscanf(pFile, "%f", &data->simulationInfo.external_input.t[i]); |
| 77 | + for(j = 0; j < m; ++j){ |
| 78 | + fscanf(pFile, "%f", &data->simulationInfo.external_input.u[i][j]); |
| 79 | + } |
| 80 | + } |
| 81 | + fclose(pFile); |
| 82 | + data->simulationInfo.external_input.i = 0; |
| 83 | + } |
| 84 | + |
| 85 | + return 0; |
86 | 86 | } |
87 | 87 |
|
88 | 88 | int externalInputFree(DATA* data) |
89 | 89 | { |
90 | | - if(data->simulationInfo.external_input.active){ |
91 | | - int j; |
92 | | - |
93 | | - free(data->simulationInfo.external_input.t); |
94 | | - for(j = 0; j < data->simulationInfo.external_input.n; ++j) |
95 | | - free(data->simulationInfo.external_input.u[j]); |
96 | | - free(data->simulationInfo.external_input.u); |
97 | | - } |
98 | | - return 0; |
| 90 | + if(data->simulationInfo.external_input.active){ |
| 91 | + int j; |
| 92 | + |
| 93 | + free(data->simulationInfo.external_input.t); |
| 94 | + for(j = 0; j < data->simulationInfo.external_input.n; ++j) |
| 95 | + free(data->simulationInfo.external_input.u[j]); |
| 96 | + free(data->simulationInfo.external_input.u); |
| 97 | + } |
| 98 | + return 0; |
99 | 99 | } |
100 | 100 |
|
101 | 101 |
|
102 | 102 | int externalInputUpdate(DATA* data) |
103 | 103 | { |
104 | | - double t; |
105 | | - int i; |
106 | | - t = data->localData[0]->timeValue; |
107 | | - while(t > data->simulationInfo.external_input.t[data->simulationInfo.external_input.i+1] |
108 | | - && data->simulationInfo.external_input.i < (data->simulationInfo.external_input.n-2)){ |
109 | | - ++data->simulationInfo.external_input.i; |
110 | | - } |
111 | | - |
112 | | - data->simulationInfo.external_input.dt = (data->simulationInfo.external_input.t[data->simulationInfo.external_input.i+1] - data->simulationInfo.external_input.t[data->simulationInfo.external_input.i]); |
113 | | - for(i = 0; i < data->modelData.nInputVars; ++i){ |
114 | | - data->simulationInfo.inputVars[i] = |
115 | | - data->simulationInfo.external_input.u[data->simulationInfo.external_input.i][i] + |
116 | | - data->simulationInfo.external_input.dt*(t -data->simulationInfo.external_input.t[data->simulationInfo.external_input.i])* |
117 | | - data->simulationInfo.external_input.u[data->simulationInfo.external_input.i+1][i]; |
118 | | - } |
| 104 | + double t; |
| 105 | + int i; |
| 106 | + t = data->localData[0]->timeValue; |
| 107 | + while(t > data->simulationInfo.external_input.t[data->simulationInfo.external_input.i+1] |
| 108 | + && data->simulationInfo.external_input.i < (data->simulationInfo.external_input.n-2)){ |
| 109 | + ++data->simulationInfo.external_input.i; |
| 110 | + } |
| 111 | + |
| 112 | + data->simulationInfo.external_input.dt = (data->simulationInfo.external_input.t[data->simulationInfo.external_input.i+1] - data->simulationInfo.external_input.t[data->simulationInfo.external_input.i]); |
| 113 | + for(i = 0; i < data->modelData.nInputVars; ++i){ |
| 114 | + data->simulationInfo.inputVars[i] = |
| 115 | + data->simulationInfo.external_input.u[data->simulationInfo.external_input.i][i] + |
| 116 | + data->simulationInfo.external_input.dt*(t -data->simulationInfo.external_input.t[data->simulationInfo.external_input.i])* |
| 117 | + data->simulationInfo.external_input.u[data->simulationInfo.external_input.i+1][i]; |
| 118 | + } |
119 | 119 | return 0; |
120 | 120 | } |
0 commit comments