summaryrefslogtreecommitdiff
path: root/cad/src/experimental/finite-element/comphelp.c
blob: 399b508fa5b71259c269e50389db8baa3ceaa6d4 (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
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
232
233
// Copyright 2006-2007 Nanorex, Inc.  See LICENSE file for details. 
#include "Python.h"

/*
 * Here's how to get stuff for comp.pyx
 * cproto -I/usr/include/python2.3 comphelp.c 2>/dev/null
 */

extern PyObject * setup(int n);
extern PyObject * internalForces(double stiffness, double viscosityOverDt, double dtm);
extern PyObject * verletMomentum(void);
extern PyObject * applyForces(PyObject *forces, double dtm);

static int N;

#define _ASSERT(cond) \
  if (!(cond)) { char error[200]; sprintf(error, "%s(%d)  Expected: %s", \
           __FUNCTION__, __LINE__, #cond); \
           PyErr_SetString(PyExc_AssertionError, error); return NULL; }
#define CHECKTYPE(type,var)   _ASSERT(var != NULL); \
          _ASSERT(var != Py_None); _ASSERT(Py##type##_Check(var))
#define CHECK_2_TUPLE(var)  CHECKTYPE(Tuple,var); _ASSERT(PyTuple_Size(var) == 2)
#define CHECK_NSQ_LIST(var)  CHECKTYPE(List,var); _ASSERT(PyList_Size(var) == N*N)
#define ASSERT(cond)           _ASSERT(!PyErr_Occurred()); _ASSERT(cond)
#define EXPECTZERO(expr)   { int retval = (expr); ASSERT(retval==0); }

#if 0
#define SAY_STR(x)  fprintf(stderr, "%s(%d)  %s == \"%s\"\n",\
                            __FUNCTION__, __LINE__, #x, x)
#define SAY_INT(x)  fprintf(stderr, "%s(%d)  %s == %d\n", __FUNCTION__, __LINE__, #x, x)
#define SAY_DBL(x)  fprintf(stderr, "%s(%d)  %s == %g\n", __FUNCTION__, __LINE__, #x, x)
#define SAY_OBJ(x)  fprintf(stderr, "%s(%d)  %s == <<", __FUNCTION__, __LINE__, #x); \
	    PyObject_Print(x, stderr, 0); fprintf(stderr, ">>\n")
#else
#define SAY_STR(x)
#define SAY_INT(x)
#define SAY_DBL(x)
#define SAY_OBJ(x)
#endif

static double *u, *u_old, *u_new, *x;

PyObject * setup(int n)
{
    int i;
    ASSERT(n > 1);
    N = n;
    u = (double *) malloc(2 * N * N * sizeof(double));
    ASSERT(u != NULL);
    u_old = (double *) malloc(2 * N * N * sizeof(double));
    ASSERT(u_old != NULL);
    u_new = (double *) malloc(2 * N * N * sizeof(double));
    ASSERT(u_new != NULL);
    x = (double *) malloc(2 * N * N * sizeof(double));
    ASSERT(x != NULL);
    for (i = 0; i < N * N; i++) {
	int row, col;
	u[2 * i] = 0.0;
	u[2 * i + 1] = 0.0;
	u_old[2 * i] = 0.0;
	u_old[2 * i + 1] = 0.0;
	u_new[2 * i] = 0.0;
	u_new[2 * i + 1] = 0.0;
	row = i / N;
	col = i % N;
	x[2 * i] = 1.0 * col / N;
	x[2 * i + 1] = 1.0 * row / N;
    }
    Py_INCREF(Py_None);
    return Py_None;
}

static double *getArray(PyObject *parray)
{
    int i, j;
    double *v;

    CHECK_NSQ_LIST(parray);
    v = (double *) malloc(2 * N * N * sizeof(double));
    ASSERT(v != NULL);
    for (i = 0; i < N; i++) {
	for (j = 0; j < N; j++) {
	    int k = i * N + j;
	    PyObject *tmp;
	    PyObject *tpl = PyList_GetItem(parray, k);
	    ASSERT(PyTuple_Check(tpl));
	    ASSERT(PyTuple_Size(tpl) == 2);
	    tmp = PyTuple_GetItem(tpl, 0);
	    ASSERT(PyFloat_Check(tmp));
	    v[2*k] = PyFloat_AsDouble(tmp);
	    tmp = PyTuple_GetItem(tpl, 1);
	    ASSERT(PyFloat_Check(tmp));
	    v[2*k+1] = PyFloat_AsDouble(tmp);
	}
    }
    return v;
}

PyObject * _c_applyForces(double *forces, double dtm)
{
    int k;

    if (forces == NULL) return NULL;
    for (k = 0; k < N * N; k++) {
	u_new[2 * k] += dtm * forces[2 * k];
	u_new[2 * k + 1] += dtm * forces[2 * k + 1];
    }
    Py_INCREF(Py_None);
    return Py_None;
}


PyObject * internalForces(double stiffness, double viscosityOverDt, double dtm)
{
    int i, j;

    for (i = 0; i < N; i++) {
	for (j = 0; j < N; j++) {
	    double Dx, Dy, Px, Py, fx, fy;
	    int k2, k = i * N + j;
	    if (i > 0) {
		k2 = (i - 1) * N + j;
		Dx = u[2*k2] - u[2*k];
		Dy = u[2*k2+1] - u[2*k+1];
		Px = u_old[2*k2] - u_old[2*k];
		Py = u_old[2*k2+1] - u_old[2*k+1];
		fx = stiffness * Dx + viscosityOverDt * (Dx - Px);
		fy = stiffness * Dy + viscosityOverDt * (Dy - Py);
		u_new[2*k] += dtm * fx;
		u_new[2*k+1] += dtm * fy;
		u_new[2*k2] -= dtm * fx;
		u_new[2*k2+1] -= dtm * fy;
	    }
	    if (i < N - 1) {
		k2 = (i + 1) * N + j;
		Dx = u[2*k2] - u[2*k];
		Dy = u[2*k2+1] - u[2*k+1];
		Px = u_old[2*k2] - u_old[2*k];
		Py = u_old[2*k2+1] - u_old[2*k+1];
		fx = stiffness * Dx + viscosityOverDt * (Dx - Px);
		fy = stiffness * Dy + viscosityOverDt * (Dy - Py);
		u_new[2*k] += dtm * fx;
		u_new[2*k+1] += dtm * fy;
		u_new[2*k2] -= dtm * fx;
		u_new[2*k2+1] -= dtm * fy;
	    }
	    if (i > 0) {
		k2 = i * N + (j - 1);
		Dx = u[2*k2] - u[2*k];
		Dy = u[2*k2+1] - u[2*k+1];
		Px = u_old[2*k2] - u_old[2*k];
		Py = u_old[2*k2+1] - u_old[2*k+1];
		fx = stiffness * Dx + viscosityOverDt * (Dx - Px);
		fy = stiffness * Dy + viscosityOverDt * (Dy - Py);
		u_new[2*k] += dtm * fx;
		u_new[2*k+1] += dtm * fy;
		u_new[2*k2] -= dtm * fx;
		u_new[2*k2+1] -= dtm * fy;
	    }
	    if (j < N - 1) {
		k2 = i * N + (j + 1);
		Dx = u[2*k2] - u[2*k];
		Dy = u[2*k2+1] - u[2*k+1];
		Px = u_old[2*k2] - u_old[2*k];
		Py = u_old[2*k2+1] - u_old[2*k+1];
		fx = stiffness * Dx + viscosityOverDt * (Dx - Px);
		fy = stiffness * Dy + viscosityOverDt * (Dy - Py);
		u_new[2*k] += dtm * fx;
		u_new[2*k+1] += dtm * fy;
		u_new[2*k2] -= dtm * fx;
		u_new[2*k2+1] -= dtm * fy;
	    }
	}
    }
    Py_INCREF(Py_None);
    return Py_None;
}

PyObject *verletMomentum(void)
{
    int k;
    for (k = 0; k < N * N; k++) {
	u_new[2 * k] = 2 * u[2 * k] - u_old[2 * k];
	u_new[2 * k + 1] = 2 * u[2 * k + 1] - u_old[2 * k + 1];
    }
    Py_INCREF(Py_None);
    return Py_None;
}

PyObject * applyForces(PyObject *forces, double dtm)
{
    PyObject *retval;
    double *f;

    f = getArray(forces);
    if (f == NULL) return NULL;
    retval = _c_applyForces(f, dtm);
    free(f);
    return retval;
}

PyObject * draw(PyObject *drawCallback, int w, int h)
{
    int i;
    double a, b;
    a = 0.6;
    b = 0.5 * (1.0 - a);
    ASSERT(drawCallback != NULL);
    ASSERT(PyCallable_Check(drawCallback));
    for (i = 0; i < N * N; i++) {
	PyObject *pValue;
	PyObject *args =
	    Py_BuildValue("(dd)",
			  w * (a * (x[2 * i] + u[2 * i]) + b),
			  h * (a * (x[2 * i + 1] + u[2 * i + 1]) + b));
	pValue = PyObject_CallObject(drawCallback, args);
	ASSERT(!PyErr_Occurred());
	Py_DECREF(args);
	Py_XDECREF(pValue);
    }
    Py_INCREF(Py_None);
    return Py_None;
}

PyObject *rotate(void)
{
    double *tmp = u_old;
    u_old = u;
    u = u_new;
    u_new = tmp;
    Py_INCREF(Py_None);
    return Py_None;
}