summaryrefslogtreecommitdiff
path: root/sim/src/experimental/xyzreader.c
blob: eb86d15d60e7791d6a63ce40613152c6a6451d79 (plain)
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
// Copyright 2004-2005 Nanorex, Inc.  See LICENSE file for details. 
#include <stdio.h>
#include <stdlib.h>
#include <math.h>

#include "mol.h"


/** indicate next avail/total number of stretch bonds, bend bonds, and atoms */
int Nexbon=0, Nextorq=0, Nexatom=0;

/** Input xyz file */
FILE *inf;

/** positions and forces on the atoms w/pointers into pos */
struct xyz avg[NATOMS];

struct xyz Center, Bbox[2];

/** data for the 5-carbon test molecule */
struct xyz diam[5]=
	{{0.0, 0.0, 0.0},
	 {176.7, 176.7, 0.0},
	 {176.7, 0.0, 176.7},
	 {0.0, 176.7, 176.7},
	 {88.33, 88.33, 88.33}};

int PartNo=0;

/** 0 nothing, 1 ball/stick, 2 vdW surface */
int DisplayStyle=2;

struct A atom[NATOMS];


struct B bond[4*NATOMS];


/* creating atoms, bonds, etc */

/** uses global Nexatom, atom, element, cur, old, Dt, Dx, Boltz, and
    Temperature */
void makatom(int elem, struct xyz posn) {
	struct xyz foo, v, p;
	double mass, therm;
	
	/* create the data structures */
	atom[Nexatom].elt=elem;
	atom[Nexatom].nbonds=0;
	atom[Nexatom].part = PartNo;
	atom[Nexatom].disp = DisplayStyle;
	atom[Nexatom].next = NULL;
	atom[Nexatom].bucket = NULL;
	avg[Nexatom]=posn;
	Nexatom++;
	
}


/** file reading */

int done = 0;

void xyz_frame_read(void) {
	
	char buf[128];
	int i, j, n, ord;
	struct xyz vec1, vec2;

	Nexatom = 0;   // overwrite any old atoms
	j = fread(&n, sizeof(int), 1, inf);   // get the number of atoms
	if (j < 1) {
		done = 1;    // end of file!
		return;
	}

	Center.x = Center.y = Center.z = 0.0;
	for (i = 0; i < n; i++) {
		int elt;
		struct xyz posn;
		j = fread(&elt, sizeof(int), 1, inf);
		if (j < 1) {
			done = 1;   // end of file!
			return;
		}
		j = fread(&posn, sizeof(struct xyz), 1, inf);
		if (j < 1) {
			done = 1;   // end of file!
			return;
		}
		Center.x += posn.x;
		Center.y += posn.y;
		Center.z += posn.z;
		makatom(elt, posn);
	}

	Center.x /= Nexatom;
	Center.y /= Nexatom;
	Center.z /= Nexatom;

	double xmin, xmax, ymin, ymax, zmin, zmax;
	xmin = xmax = 0.0;
	ymin = ymax = 0.0;
	zmin = zmax = 0.0;

	for (i = 0; i < Nexatom; i++) {
		avg[i].x -= Center.x;
		avg[i].y -= Center.y;
		avg[i].z -= Center.z;
		if (avg[i].x < xmin) xmin = avg[i].x;
		if (avg[i].x > xmax) xmax = avg[i].x;
		if (avg[i].y < ymin) ymin = avg[i].y;
		if (avg[i].y > ymax) ymax = avg[i].y;
		if (avg[i].z < zmin) zmin = avg[i].z;
		if (avg[i].z > zmax) zmax = avg[i].z;
	}

	/* now everybody is centered around zero */
	Center.x = Center.y = Center.z = 0.0;

	/* set up bounding box */
	Bbox[0].x = xmin - 100.0;
	Bbox[0].y = ymin - 100.0;
	Bbox[0].z = zmin - 100.0;
	Bbox[1].x = xmax + 100.0;
	Bbox[1].y = ymax + 100.0;
	Bbox[1].z = zmax + 100.0;
}





static int ShotNo=0;

void keyboard(unsigned char key, int x, int y) {
	
	if (key == '?') DBGPRINTF("\n\
q -- quit\n\
s -- snapshot\n");
	
	if (key == 'q') exit(0);
	if (key == 's') snapshot(ShotNo++);
	
	// calcloop(((key == 'z') ? 1 : 5));
	xyz_frame_read();
	if (done)
		exit(0);
	display();
}

/**
 * The main loop for the whole kittenkaboodle
 */

main(int argc,char **argv)
{
	inf = fopen("dumpstruct.xyz", "r");
	xyz_frame_read();
	display_init(&argc, argv);
	display_mainloop();
	display_fini();
	fclose(inf);
	
	return 0;
}

/*
 * Local Variables:
 * c-basic-offset: 8
 * tab-width: 8
 * End:
 */