1 | /* |
---|
2 | MEDERNACH Emmanuel |
---|
3 | Aug 8, 2000 |
---|
4 | */ |
---|
5 | |
---|
6 | #include <stdio.h> |
---|
7 | #include <unistd.h> |
---|
8 | #include <sys/types.h> |
---|
9 | #include <sys/wait.h> |
---|
10 | #include <math.h> |
---|
11 | |
---|
12 | double sqrt(double d); |
---|
13 | |
---|
14 | /* |
---|
15 | TODO: |
---|
16 | Add support for 'error management' |
---|
17 | */ |
---|
18 | |
---|
19 | pid_t wait(int *status) ; |
---|
20 | |
---|
21 | void read_choice(int *choice) |
---|
22 | { |
---|
23 | |
---|
24 | if (choice == NULL) return ; |
---|
25 | (*choice) = -1; |
---|
26 | |
---|
27 | printf("Possible volumes are :\n"); |
---|
28 | printf("1: BOX\n"); |
---|
29 | printf("2: TUBS\n"); |
---|
30 | printf("3: CONE\n"); |
---|
31 | printf("4: PCON\n"); |
---|
32 | printf("5: PGON\n"); |
---|
33 | printf("6: PGON2\n"); |
---|
34 | printf("7: CONE2 (Emm)\n"); |
---|
35 | //printf("8: THINBOX (Emm)\n"); |
---|
36 | printf("8: TORUS2 (Emm)\n"); |
---|
37 | while (((*choice) < 1) || ((*choice) > 8)) |
---|
38 | { |
---|
39 | scanf("%d",choice); |
---|
40 | } |
---|
41 | } |
---|
42 | |
---|
43 | void write_ini(const char *init,int choice,double x,double y,double z,double dx,double dy,double dz) |
---|
44 | { |
---|
45 | FILE *fi= NULL; |
---|
46 | double Norm = sqrt((dx*dx) + (dy*dy) + (dz*dz)) ; |
---|
47 | |
---|
48 | //printf("Norm = %f\n",Norm); |
---|
49 | |
---|
50 | fi = fopen(init,"w"); |
---|
51 | if (fi == NULL) |
---|
52 | { |
---|
53 | perror("Some problems towrite in the Init.fred file"); |
---|
54 | exit (1); |
---|
55 | } |
---|
56 | |
---|
57 | switch (choice) |
---|
58 | { |
---|
59 | case 1: |
---|
60 | fprintf(fi,"/fred/volume BOX\n"); |
---|
61 | break; |
---|
62 | case 2: |
---|
63 | fprintf(fi,"/fred/volume TUBS\n"); |
---|
64 | break; |
---|
65 | case 3: |
---|
66 | fprintf(fi,"/fred/volume CONE\n"); |
---|
67 | break; |
---|
68 | case 4: |
---|
69 | fprintf(fi,"/fred/volume PCON\n"); |
---|
70 | break; |
---|
71 | case 5: |
---|
72 | fprintf(fi,"/fred/volume PGON\n"); |
---|
73 | break; |
---|
74 | case 6: |
---|
75 | fprintf(fi,"/fred/volume PGON2\n"); |
---|
76 | break; |
---|
77 | case 7: |
---|
78 | fprintf(fi,"/fred/volume CONE2\n"); |
---|
79 | break; |
---|
80 | case 8: |
---|
81 | fprintf(fi,"/fred/volume TORUS2\n"); |
---|
82 | break; |
---|
83 | default: |
---|
84 | fprintf(stderr,"%d is not between 1 and 8\n",choice); |
---|
85 | exit(2); |
---|
86 | break; |
---|
87 | } |
---|
88 | fprintf(fi,"/fred/gun G4\n"); |
---|
89 | fprintf(fi,"/gun/position %f %f %f\n",x,y,z); |
---|
90 | fprintf(fi,"/gun/direction %f %f %f\n",dx/Norm,dy/Norm,dz/Norm); |
---|
91 | fprintf(fi,"/run/initialize \n"); |
---|
92 | fprintf(fi,"/run/beamOn\n"); |
---|
93 | fclose(fi); |
---|
94 | } |
---|
95 | |
---|
96 | int main (void) |
---|
97 | { |
---|
98 | pid_t son = -1; |
---|
99 | int status = 0; |
---|
100 | char init[]="Init.fred"; |
---|
101 | char *param [3]; |
---|
102 | |
---|
103 | int choice = -1 ; |
---|
104 | double posx,posy,posz; |
---|
105 | double dirx,diry,dirz; |
---|
106 | |
---|
107 | double startx,starty,startz; |
---|
108 | double beginx,beginy,beginz; |
---|
109 | double endx,endy,endz; |
---|
110 | double deltax,deltay,deltaz; |
---|
111 | double begindirx,enddirx,stepdirx; |
---|
112 | double begindiry,enddiry,stepdiry; |
---|
113 | double begindirz,enddirz,stepdirz; |
---|
114 | |
---|
115 | int TakeOtherPoint = 0; |
---|
116 | |
---|
117 | |
---|
118 | /* write an init file */ |
---|
119 | |
---|
120 | read_choice(&choice); |
---|
121 | |
---|
122 | |
---|
123 | posx = posy = posz = 0; |
---|
124 | dirx = diry = dirz = 1; |
---|
125 | |
---|
126 | |
---|
127 | printf("You could choose a delta <= 0 to have only a plane\n"); |
---|
128 | /* rotation in a cone for example could be performed */ |
---|
129 | /* to have only a y=0 plane to search */ |
---|
130 | printf("Choose a beginning, an end and a step for x (0 200 10)\n"); |
---|
131 | scanf("%lf %lf %lf",&beginx,&endx,&deltax); |
---|
132 | printf("Choose a beginning, an end and a step for y (0 200 10)\n"); |
---|
133 | scanf("%lf %lf %lf",&beginy,&endy,&deltay); |
---|
134 | printf("Choose a beginning, an end and a step for z (0 200 10)\n"); |
---|
135 | scanf("%lf %lf %lf",&beginz,&endz,&deltaz); |
---|
136 | |
---|
137 | /* you could want to continue after a previous crash |
---|
138 | (X could hangs if swap is full) |
---|
139 | */ |
---|
140 | |
---|
141 | printf("you could want to continue after a previous crash\n"); |
---|
142 | printf("In this case just indicate from where do you want to start (x y z)\n\n"); |
---|
143 | scanf("%lf %lf %lf",&startx,&starty,&startz); |
---|
144 | |
---|
145 | printf("you could now have a grid of direction to point\n"); |
---|
146 | printf("Choose a beginning, an end and a step for all direction (-30 30 1)\n"); |
---|
147 | printf("\nfor direction x : "); |
---|
148 | scanf("%lf %lf %lf",&begindirx,&enddirx,&stepdirx); |
---|
149 | printf("\nfor direction y : "); |
---|
150 | scanf("%lf %lf %lf",&begindiry,&enddiry,&stepdiry); |
---|
151 | printf("\nfor direction z : "); |
---|
152 | scanf("%lf %lf %lf",&begindirz,&enddirz,&stepdirz); |
---|
153 | |
---|
154 | /* we want also != step for each direction */ |
---|
155 | |
---|
156 | posx = startx ; |
---|
157 | posy = starty ; |
---|
158 | posz = startz ; |
---|
159 | |
---|
160 | |
---|
161 | printf("We start at %f %f %f\n",posx,posy,posz); |
---|
162 | |
---|
163 | while (posx < endx) |
---|
164 | { /* loop on x */ |
---|
165 | |
---|
166 | printf("Posx = %f\n",posx); |
---|
167 | while (posy < endy) |
---|
168 | { /* loop on y */ |
---|
169 | |
---|
170 | printf("Posy = %f\n",posy); |
---|
171 | while (posz < endz) |
---|
172 | { /* loop on z */ |
---|
173 | |
---|
174 | printf("\tPosz = %f\n",posz); |
---|
175 | TakeOtherPoint = 0; |
---|
176 | for (dirx=begindirx;(dirx<enddirx) && (TakeOtherPoint == 0);dirx+=stepdirx) |
---|
177 | for (diry=begindiry;(diry<enddiry) && (TakeOtherPoint == 0);diry+=stepdiry) |
---|
178 | for (dirz=begindirz;(dirz<enddirz) && (TakeOtherPoint == 0);dirz+=stepdirz) |
---|
179 | { |
---|
180 | write_ini(init,choice,posx,posy,posz,dirx,diry,dirz); |
---|
181 | |
---|
182 | /* call to Fred */ |
---|
183 | son = fork(); |
---|
184 | switch(son) |
---|
185 | { |
---|
186 | case -1: |
---|
187 | perror("Fork process error"); |
---|
188 | exit(1); |
---|
189 | break; |
---|
190 | case 0: |
---|
191 | /* execvp (Emm)fred with init file */ |
---|
192 | param[0] = "fred"; |
---|
193 | param[1] = init; |
---|
194 | param[2] = NULL; |
---|
195 | |
---|
196 | /* no output nor input */ |
---|
197 | close (0); |
---|
198 | close (1); |
---|
199 | close (2); |
---|
200 | |
---|
201 | if (execvp("fred",param) == -1) |
---|
202 | { |
---|
203 | fprintf(stderr,"fred program not found ..\n"); |
---|
204 | exit(1); |
---|
205 | } |
---|
206 | break; |
---|
207 | default: |
---|
208 | /* wait for the other process */ |
---|
209 | waitpid(son,&status,0); |
---|
210 | if (WIFEXITED(status) == 0) |
---|
211 | { |
---|
212 | /* some errors */ |
---|
213 | /* keep the current init */ |
---|
214 | double Norm = sqrt(dirx*dirx+diry*diry+dirz*dirz); |
---|
215 | |
---|
216 | fprintf(stderr,"I catch an error : bad return value %f %f %f %f %f %f\n",posx,posy,posz,(dirx/Norm),(diry/Norm),(dirz/Norm)); |
---|
217 | /* we take another point */ |
---|
218 | /* Ok this is not really the way to do ..*/ |
---|
219 | TakeOtherPoint = 1; |
---|
220 | } |
---|
221 | else |
---|
222 | if (WIFSIGNALED(status)) |
---|
223 | { |
---|
224 | /* some errors */ |
---|
225 | /* keep the current init */ |
---|
226 | double Norm = sqrt(dirx*dirx+diry*diry+dirz*dirz); |
---|
227 | fprintf(stderr,"I catch an error : Signal terminated %f %f %f %f %f %f\n",posx,posy,posz,(dirx/Norm),(diry/Norm),(dirz/Norm)); |
---|
228 | /* we take another point */ |
---|
229 | /* Ok this is not really the way to do ..*/ |
---|
230 | TakeOtherPoint = 1; |
---|
231 | } |
---|
232 | break; |
---|
233 | } |
---|
234 | } |
---|
235 | /* loop on z */ |
---|
236 | posz += deltaz ; |
---|
237 | } |
---|
238 | posz = beginz ; |
---|
239 | |
---|
240 | /* loop on y */ |
---|
241 | posy += deltay ; |
---|
242 | } |
---|
243 | posy = beginy ; |
---|
244 | |
---|
245 | /* loop on x */ |
---|
246 | posx += deltax ; |
---|
247 | } |
---|
248 | posx = beginx ; |
---|
249 | |
---|
250 | return 0; |
---|
251 | |
---|
252 | } |
---|
253 | |
---|