2 * This file is part of the GROMACS molecular simulation package.
4 * Copyright (c) 1991-2000, University of Groningen, The Netherlands.
5 * Copyright (c) 2001-2004, The GROMACS development team,
6 * check out http://www.gromacs.org for more information.
7 * Copyright (c) 2012, by the GROMACS development team, led by
8 * David van der Spoel, Berk Hess, Erik Lindahl, and including many
9 * others, as listed in the AUTHORS file in the top-level source
10 * directory and at http://www.gromacs.org.
12 * GROMACS is free software; you can redistribute it and/or
13 * modify it under the terms of the GNU Lesser General Public License
14 * as published by the Free Software Foundation; either version 2.1
15 * of the License, or (at your option) any later version.
17 * GROMACS is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
20 * Lesser General Public License for more details.
22 * You should have received a copy of the GNU Lesser General Public
23 * License along with GROMACS; if not, see
24 * http://www.gnu.org/licenses, or write to the Free Software Foundation,
25 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
27 * If you want to redistribute modifications to GROMACS, please
28 * consider that scientific software is very special. Version
29 * control is crucial - bugs must be traceable. We will be happy to
30 * consider code for inclusion in the official distribution, but
31 * derived work must not be called official GROMACS. Details are found
32 * in the README & COPYING files - if they are missing, get the
33 * official version at http://www.gromacs.org.
35 * To help us fund GROMACS development, we humbly ask that you cite
36 * the research papers on the package. Check out http://www.gromacs.org.
51 #include "gmx_fatal.h"
53 /* Units are meter and second */
56 int id
; /* Identification */
58 float v
; /* Position and velocity */
59 float vwanted
; /* Wants to drive at this speed */
60 float acc
; /* Acceleration */
61 float brake
; /* Break */
62 int lane
,oldlane
; /* Currently in lane */
63 gmx_bool bBrake
; /* Currently on the brakes */
64 unsigned long col
; /* Colour */
65 unsigned long roof
; /* Roof Colour */
69 int nlane
; /* Number of lanes on highway */
70 int metres
; /* Road length */
71 float dt
; /* Time step */
72 float min_dist
; /* Min distance cars can come */
73 int sleep
; /* How long to sleep in between updates */
76 static const char *Driving
[] = { "Start","Stop" };
77 static const char *Fogs
[] = { "Fog", "No Fog" };
78 enum buttons
{ Quit
, StartStop
, Fog
, NBUT
};
79 static const char *but_name
[NBUT
] = { "Quit", "Start", "Fog" };
86 gmx_bool bDriving
; /* Are we driving ? */
87 gmx_bool bFog
; /* Is it foggy ? */
93 int read_input(t_x11
*x11
,const char *fn
,t_car
**cars
,t_input
*ir
)
97 char buf
[100],b2
[100];
101 if (fscanf(in
,"%d %d %f %f %d",
102 &ir
->nlane
,&ir
->metres
,&ir
->dt
,&ir
->min_dist
,&ir
->sleep
) != 5)
103 gmx_fatal(FARGS
,"Not enough parameters in %s line 1",fn
);
104 if (fscanf(in
,"%d",&n
) != 1)
105 gmx_fatal(FARGS
,"Not enough parameters in %s line 2",fn
);
108 for(i
=0; (i
<n
); i
++) {
112 if (fscanf(in
,"%f %f %f %f %f %s %s",&(c
->x
),&(c
->v
),&(c
->vwanted
),
113 &(c
->acc
),&(c
->brake
),buf
,b2
) != 7)
114 gmx_fatal(FARGS
,"Not enough parameters in %s line %d",fn
,3+i
);
115 x11
->GetNamedColor(x11
,buf
,&(c
->col
));
116 x11
->GetNamedColor(x11
,b2
,&(c
->roof
));
123 static float get_dist(int ncars
,t_car cars
[],int which
,gmx_bool bFog
,
124 int dir
,int lane
,int metres
,int *nearest
)
135 for(i
=0; (i
<ncars
); i
++)
136 if ((i
!= which
) && (cars
[i
].oldlane
== lane
)){
137 nd
=cars
[i
].xold
-cars
[which
].xold
;
138 if ((nd
< 0) && (dir
> 0))
140 else if ((nd
> 0) && (dir
< 0))
143 if (!bFog
|| (fabs(nd
) < 50)) {
162 void simulate(t_x11
*x11
,t_xhighway
*xhw
,
163 int ncars
,t_car cars
[],t_input
*ir
)
165 int i
,n_bef
,n_bef1
,n_beh
;
166 float dist
,distf
,distb
;
168 for(i
=0; (i
<ncars
); i
++) {
169 cars
[i
].xold
=cars
[i
].x
;
170 cars
[i
].oldlane
=cars
[i
].lane
;
172 for(i
=0; (i
<ncars
); i
++) {
173 cars
[i
].bBrake
=FALSE
;
174 dist
=get_dist(ncars
,cars
,i
,xhw
->bFog
,
175 1,cars
[i
].lane
,ir
->metres
,&n_bef
);
176 if (dist
< ir
->min_dist
) {
177 distf
=get_dist(ncars
,cars
,i
,xhw
->bFog
,
178 1,cars
[i
].lane
+1,ir
->metres
,&n_bef1
);
179 distb
=get_dist(ncars
,cars
,i
,xhw
->bFog
,
180 -1,cars
[i
].lane
+1,ir
->metres
,&n_beh
);
181 if ((cars
[i
].lane
< ir
->nlane
-1) && (distb
>= ir
->min_dist
) &&
182 (distf
>= ir
->min_dist
))
186 cars
[i
].v
-= cars
[i
].brake
*ir
->dt
;
190 if ((cars
[i
].v
< cars
[n_bef
].v
) && (dist
> ir
->min_dist
/2))
191 cars
[i
].v
=cars
[n_bef
].v
;
195 else if ((cars
[i
].lane
> 0) && (cars
[i
].v
== cars
[i
].vwanted
)) {
196 /* Check if I can go right again */
197 dist
=get_dist(ncars
,cars
,i
,xhw
->bFog
,
198 1,cars
[i
].lane
-1,ir
->metres
,&n_bef
);
199 distb
=get_dist(ncars
,cars
,i
,xhw
->bFog
,
200 -1,cars
[i
].lane
-1,ir
->metres
,&n_beh
);
201 if ((dist
>= ir
->min_dist
) && (distb
>= ir
->min_dist
))
205 cars
[i
].x
+= cars
[i
].v
*ir
->dt
;
206 if (cars
[i
].x
> ir
->metres
)
207 cars
[i
].x
-= ir
->metres
;
208 if (!cars
[i
].bBrake
&& (cars
[i
].v
< cars
[i
].vwanted
)) {
209 cars
[i
].v
+= cars
[i
].acc
*ir
->dt
;
210 if (cars
[i
].v
> cars
[i
].vwanted
)
211 cars
[i
].v
= cars
[i
].vwanted
;
216 usleep(xhw
->ir
.sleep
);
217 ExposeWin(x11
->disp
,xhw
->win
.self
);
220 static void Configure(t_xhighway
*xhw
)
229 dw
=((float)(w
-2))/NBUT
-4;
230 for(i
=0; (i
<NBUT
); i
++) {
231 t_windata
*wd
=&(xhw
->but
[i
]);
234 InitWin(wd
,2+i
*(dw
+4),2,dw
,dh
,1,but_name
[i
]);
238 InitWin(&xhw
->win
,2,dh
+6,w
-6,h
-dh
-10,1,xhw
->main
.text
);
242 static void draw_car(Display
*disp
,Window wd
,GC gc
,
243 t_car
*car
,int w0
,int h0
)
256 XSetForeground(disp
,gc
,car
->col
);
257 XFillRectangle(disp
,wd
,gc
,w1
,h1
,w
,h
);
260 XSetForeground(disp
,gc
,car
->roof
);
261 XFillRectangle(disp
,wd
,gc
,w0
-wr
/2,h0
-hr
/2,wr
,hr
);
265 XSetForeground(disp
,gc
,YELLOW
);
270 XSetForeground(disp
,gc
,LIGHTRED
);
274 for(j
=1; (j
<jmax
); j
++) {
277 XDrawLine(disp
,wd
,gc
,w11
,h11
, w11
,h11
+hmax
);
278 XDrawLine(disp
,wd
,gc
,w11
,h11
+h
-hmax
,w11
,h11
+h
);
282 XSetForeground(disp
,gc
,WHITE
);
283 for(j
=1; (j
<3); j
++) {
286 XDrawLine(disp
,wd
,gc
,w11
,h11
, w11
,h11
+3);
287 XDrawLine(disp
,wd
,gc
,w11
,h11
+h
-3,w11
,h11
+h
);
291 static gmx_bool
xhwCallBack(struct t_x11
*x11
,XEvent
*event
, Window wd
, void *data
)
300 xhw
= (t_xhighway
*)data
;
304 nyy
=2*xhw
->ir
.nlane
+1;
307 for(i
=0; (i
<nyy
); i
++)
308 yy
[i
]=((float) i
*win
->height
)/(nyy
-1);
310 switch (event
->type
) {
312 if (wd
== win
->self
) {
313 sx
=(float)win
->width
/ xhw
->ir
.metres
;
315 XClearWindow(x11
->disp
,win
->self
);
316 XSetForeground(x11
->disp
,x11
->gc
,WHITE
);
318 for(i
=2; (i
<nyy
-1); i
+=2)
319 XDrawLine(x11
->disp
,win
->self
,x11
->gc
,0,yy
[i
],win
->width
-1,yy
[i
]);
321 for(i
=0; (i
<xhw
->ncars
); i
++) {
322 t_car
*car
=&(xhw
->cars
[i
]);
324 int h1
=yy
[1+2*(xhw
->ir
.nlane
-1-car
->lane
)];
326 draw_car(x11
->disp
,win
->self
,x11
->gc
,car
,w1
,h1
);
329 simulate(x11
,xhw
,xhw
->ncars
,xhw
->cars
,&xhw
->ir
);
333 case ConfigureNotify
:
334 if (wd
== xhw
->main
.self
) {
335 xhw
->main
.width
=event
->xconfigure
.width
;
336 xhw
->main
.height
=event
->xconfigure
.height
;
338 for(i
=0; (i
<NBUT
); i
++)
339 XMoveResizeWindow(x11
->disp
,xhw
->but
[i
].self
,
340 xhw
->but
[i
].x
,xhw
->but
[i
].y
,
341 xhw
->but
[i
].width
,xhw
->but
[i
].height
);
342 XMoveResizeWindow(x11
->disp
,win
->self
,
343 win
->x
,win
->y
,win
->width
,win
->height
);
345 else if (wd
== win
->self
) {
346 win
->width
=event
->xconfigure
.width
;
347 win
->height
=event
->xconfigure
.height
;
358 static gmx_bool
butCallBack(struct t_x11
*x11
,XEvent
*event
, Window wd
, void *data
)
360 XSetWindowAttributes attr
;
366 xhw
= (t_xhighway
*)data
;
367 for(i
=0; (i
<NBUT
); i
++) {
368 if (xhw
->but
[i
].self
== wd
)
372 fprintf(stderr
,"Incorrect window: %x in butcallback\n",(unsigned)wd
);
377 switch (event
->type
) {
379 XClearWindow(x11
->disp
,win
->self
);
382 label
=Driving
[xhw
->bDriving
];
385 label
=Fogs
[xhw
->bFog
];
390 XSetForeground(x11
->disp
,x11
->gc
,WHITE
);
391 TextInWin(x11
,win
,label
,eXCenter
,eYCenter
);
394 case ConfigureNotify
:
395 win
->width
=event
->xconfigure
.width
;
396 win
->height
=event
->xconfigure
.height
;
403 xhw
->bDriving
=1-xhw
->bDriving
;
404 ExposeWin(x11
->disp
,win
->self
);
406 ExposeWin(x11
->disp
,xhw
->win
.self
);
409 xhw
->bFog
=1-xhw
->bFog
;
411 attr
.background_pixel
=DARKGREY
;
413 attr
.background_pixel
=BLACK
;
414 XChangeWindowAttributes(x11
->disp
,xhw
->win
.self
,CWBackPixel
,&attr
);
415 /*ExposeWin(x11->disp,win->self);*/
427 t_xhighway
*GetXHW(t_x11
*x11
,const char *infile
)
431 char progname
[STRLEN
];
434 xhw
->ncars
=read_input(x11
,infile
,&(xhw
->cars
),&xhw
->ir
);
439 strncpy(progname
,Program(),STRLEN
-1);
440 InitWin(&xhw
->main
,0,0,w
,h
+dh
+7,1,progname
);
441 xhw
->main
.self
=XCreateSimpleWindow(x11
->disp
,x11
->root
,
442 xhw
->main
.x
,xhw
->main
.y
,
443 xhw
->main
.width
,xhw
->main
.height
,
444 xhw
->main
.bwidth
,WHITE
,BLACK
);
445 x11
->RegisterCallback(x11
,xhw
->main
.self
,0,xhwCallBack
,xhw
);
446 x11
->SetInputMask(x11
,xhw
->main
.self
,ButtonPressMask
| ExposureMask
|
447 StructureNotifyMask
);
451 for(i
=0; (i
<NBUT
); i
++) {
452 t_windata
*wd
=&(xhw
->but
[i
]);
454 wd
->self
=XCreateSimpleWindow(x11
->disp
,xhw
->main
.self
,
456 wd
->width
,wd
->height
,
457 wd
->bwidth
,WHITE
,BLACK
);
458 x11
->RegisterCallback(x11
,wd
->self
,xhw
->main
.self
,
460 x11
->SetInputMask(x11
,wd
->self
,ButtonPressMask
| ExposureMask
|
461 StructureNotifyMask
);
464 xhw
->win
.self
=XCreateSimpleWindow(x11
->disp
,xhw
->main
.self
,
465 xhw
->win
.x
,xhw
->win
.y
,
466 xhw
->win
.width
,xhw
->win
.height
,
467 xhw
->win
.bwidth
,WHITE
,BLACK
);
468 x11
->RegisterCallback(x11
,xhw
->win
.self
,0,xhwCallBack
,xhw
);
469 x11
->SetInputMask(x11
,xhw
->win
.self
,ButtonPressMask
| ExposureMask
|
470 StructureNotifyMask
);
475 int main(int argc
,char *argv
[])
477 const char *desc
[] = {
478 "[TT]highway[tt] is the GROMCAS highway simulator. It is an X-windows",
479 "gadget that shows a (periodic) Autobahn with a user defined",
480 "number of cars. Fog can be turned on or off to increase the",
481 "number of crashes. Nice for a background CPU-eater. A sample",
482 "input file is in [TT]$GMXDATA/top/highway.dat[tt]"
488 { efDAT
, "-f", "highway", ffREAD
}
490 #define NFILE asize(fnm)
492 CopyRight(stderr
,argv
[0]);
493 parse_common_args(&argc
,argv
,0,NFILE
,fnm
,
494 0,NULL
,asize(desc
),desc
,0,NULL
,&oenv
);
496 if ((x11
=GetX11(&argc
,argv
))==NULL
) {
497 fprintf(stderr
,"Can't connect to X Server.\n"
498 "Check your DISPLAY environment variable\n");
501 xhw
=GetXHW(x11
,opt2fn("-f",NFILE
,fnm
));
503 XMapWindow(x11
->disp
,xhw
->main
.self
);
504 XMapSubwindows(x11
->disp
,xhw
->main
.self
);