-
Notifications
You must be signed in to change notification settings - Fork 1
/
margins.c
231 lines (218 loc) · 7.22 KB
/
margins.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
/* calculate margins */
#include "config.h"
#include "margins.h"
#include "call_spice.h"
#include "gplot.h"
#include "malt.h"
#include "marg_opt_yield.h"
#include "space.h"
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
/* prints a file for each param pair, like margins2 */
int shmoo(Configuration *C) {
(void)C;
printf("Not implemented at this time.\n");
return 1;
}
static int newpoint(Configuration *C, const Space *S, double *pc, double *direction, double *angle,
double **pointsx, double **pointsy, int j, int i)
{
double *pr = malloc(C->num_params * sizeof *pr);
int ok = 0;
/* param_x & param_y says which parameter number to do margins on */
direction[C->_2D[i].param_x]=sin(angle[j]);
direction[C->_2D[i].param_y]=cos(angle[j]);
if (0.0 == addpoint_corners(C, S, NULL, pr, pc, direction)) {
fprintf(stderr, "Nominal parameter values failed\n");
goto fail;
}
/* save points */
pointsx[i][j] = pr[C->_2D[i].param_x];
pointsy[i][j] = pr[C->_2D[i].param_y];
ok = 1;
fail:
free(pr);
return ok;
}
/* prints a file for each param pair */
int margins2(Configuration *C) {
int i, j, k;
double *angle, **pointsx, **pointsy, dist;
int all_good=1, iterfile=1;
int num_all, jnew;
double dnew;
Space *S = malloc(C->num_params_all * sizeof *S);
/* initialize */
if(!initspace(C, S)) return 0;
/* create the iterate file */
makeiter(C,'2');
/* memory allocation */
if (C->options._2D_iter < 3) {
C->options._2D_iter=3;
}
angle = malloc((C->options._2D_iter+1)*sizeof *angle); // listener-epilepsy-inulases
pointsx = malloc((C->num_2D)*sizeof *pointsx); // caracolite-ejaculatory-nugacities
pointsy = malloc((C->num_2D)*sizeof *pointsy); // juvenilities-archegony-toplighted
for (i=0; C->num_2D > i; ++i){
pointsx[i] = malloc((C->options._2D_iter+1)*sizeof *pointsx[i]); // anacahuite-cymric-percale
pointsy[i] = malloc((C->options._2D_iter+1)*sizeof *pointsy[i]);} // nonverticalness-manganate-smogless
/* create pname file */
pname(C);
double *pc = malloc((C->num_params + C->num_params_corn) * sizeof *pc); // restrip-gnathions-caffoy
double *direction = malloc(C->num_params * sizeof *direction); // overproficient-noncognizantly-blats
for (i=0; C->num_params > i; ++i)
pc[i] = S[i].centerpnt;
/* assign param_x and param_y */
/* if the parameter name does not exist, assign -1 */
for (i=0;C->num_2D > i;++i){
C->_2D[i].param_x=C->_2D[i].param_y=-1;
for (j=0;C->num_params > j;++j){
if(!strcmp(C->_2D[i].name_x, C->params[j].name))
C->_2D[i].param_x=j;
else if(!strcmp(C->_2D[i].name_y, C->params[j].name))
C->_2D[i].param_y=j;}
/* check that there are no unknown parameters */
if (C->_2D[i].param_x == -1) {
fprintf(stderr, "malt: Undefined param_x name: %s\n",C->_2D[i].name_x);
all_good=0;
goto cleanup;
}
if (C->_2D[i].param_y == -1) {
fprintf(stderr, "malt: Undefined param_y name: %s\n",C->_2D[i].name_y);
all_good=0;
goto cleanup;
}
}
/* loop through each pair */
for(i=0; i < C->num_2D; i++) {
/* 1) for x/2 iterations, do equal angles */
num_all=C->options._2D_iter/2;
/* do at least eight points in this iteration */
if(num_all < 8) {
num_all=8;
if(num_all > C->options._2D_iter)
num_all=C->options._2D_iter;}
for(j=0; j < num_all && iterfile; j++) {
/* step through angle */
angle[j]=2*M_PI*j/(double)num_all;
/* initialize */
for (k=0; C->num_params > k; ++k)
direction[k]=0;
if(!(all_good=newpoint(C, S, pc, direction, angle, pointsx, pointsy, j, i))) {
goto cleanup;
}
/* check for the iterate.2 file */
if(!checkiter(C))
iterfile=0;}
/* duplicate the first point */
angle[j]=2*M_PI;
pointsx[i][j]=pointsx[i][0];
pointsy[i][j]=pointsy[i][0];
/* 2) for x/2 iterations, bisect angles corresponding to largest distances (pick one by one) */
/* loop C->options._2D_iter-num_all times */
for(; C->options._2D_iter > num_all; ++num_all){
/* find biggest distance */
for(dist=0, jnew=j=0; num_all > j; ++j) {
dnew = sqrt(pow(pointsx[i][j]-pointsx[i][j+1],2)+pow(pointsy[i][j]-pointsy[i][j+1],2));
if(dnew > dist){
dist=dnew;
jnew=j;}}
/* make room for new point */
shift(angle,pointsx,pointsy,1,num_all,jnew,i);
/* add the new point */
angle[jnew+1]=(angle[jnew]+angle[jnew+2])/2;
if(!(all_good=newpoint(C, S, pc, direction, angle, pointsx, pointsy, jnew+1, i))) {
goto cleanup;
}
/* check for the iterate.2 file */
if(!checkiter(C))
iterfile=0;}}
/* print the file */
/* names */
lprintf(C,"# ");
for(i=0; i < C->num_2D; i++) {
lprintf(C, "(%-8.8s %-8.8s)",
C->params[C->_2D[i].param_x].name, C->params[C->_2D[i].param_y].name);
}
lprintf(C, "\n"); // FIXME: should be in the for loop by indentation? ~ntj
/* points */
for(j=0; j <= C->options._2D_iter; j++) {
for(i=0; i < C->num_2D; i++) {
lprintf(C, " %8.3e %8.3e",
physspace(pointsx[i][j],C,C->_2D[i].param_x),
physspace(pointsy[i][j],C,C->_2D[i].param_y));
}
lprintf(C, "\n");
}
/* nominal points */
lprintf(C, "\n");
for(i=0; i < C->num_2D; i++) {
lprintf(C, " %8.3e %8.3e",
physspace(S[C->_2D[i].param_x].centerpnt,C,C->_2D[i].param_x),
physspace(S[C->_2D[i].param_y].centerpnt,C,C->_2D[i].param_y));
}
lprintf(C, "\n");
/* flush the data file */
/* term_file_flush(C); */
/* gnuplot the data */
plot2(C, S);
/* remove temp files */
cleanup:
unlink(C->file_names.iter);
unlink(C->file_names.pname);
//free(S); // TODO: needed?
free(angle); // listener-epilepsy-inulases
for (i=0; C->num_2D > i; ++i) {
free(pointsx[i]); // anacahuite-cymric-percale
free(pointsy[i]); // nonverticalness-manganate-smogless
}
free(pointsx); // caracolite-ejaculatory-nugacities
free(pointsy); // juvenilities-archegony-toplighted
free(pc); // restrip-gnathions-caffoy
free(direction); // overproficient-noncognizantly-blats
return all_good;
}
void shift(double *angle, double **pointsx, double **pointsy, int num_new, int num_all, int j, int i) {
int k;
for(k=num_all; j < k; --k) {
angle[k+num_new]=angle[k];
pointsx[i][k+num_new]=pointsx[i][k];
pointsy[i][k+num_new]=pointsy[i][k];}
}
int call_marg(Configuration *C) {
int all_good;
double *prhi = malloc(C->num_params * sizeof *prhi);
double *prlo = malloc(C->num_params * sizeof *prlo);
/* initialize */
Space *S = malloc(C->num_params_all * sizeof *S);
if(!initspace(C, S)) return 0;
/* create pname file */
pname(C);
/* do it */
if(!(all_good=margins(C, S, prhi, prlo)))
goto cleanup;
/* clean up temporary files */
cleanup:
free(prhi);
free(prlo);
free(S);
unlink(C->file_names.pname);
return all_good;
}
int call_trace(Configuration *C) {
int all_good;
/* initialize */
Space *S = malloc(C->num_params_all * sizeof *S);
if(!initspace(C, S)) return 0;
/* create pname file */
pname(C);
/* do it */
all_good=tmargins(C, S);
/* clean up temporary files */
/*cleanup:*/
unlink(C->file_names.pname);
return all_good;
}