aboutsummaryrefslogtreecommitdiff
path: root/tbext/source/him.cpp
blob: 56872df9ea53e2f658893b307f3012695d8085a2 (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
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
/* Copyright (c) 2004 Tim Blechmann.                                            */
/* For information on usage and redistribution, and for a DISCLAIMER OF ALL     */
/* WARRANTIES, see the file, "COPYING"  in this distribution.                   */
/*                                                                              */
/*                                                                              */
/* him~ is a semi-classicical simulation of an hydrogen atom in a magnetic field*/
/*                                                                              */
/* him~ uses the flext C++ layer for Max/MSP and PD externals.                  */
/* get it at http://www.parasitaere-kapazitaeten.de/PD/ext                      */
/* thanks to Thomas Grill                                                       */
/*                                                                              */
/* him~ is based on code provided in the lecture "physik auf dem computer 1"    */
/* held by joerg main during the winter semester 2003/04 at the university      */
/* stuttgart ... many thanks to him and his assistant ralf habel                */
/*                                                                              */
/*                                                                              */
/*                                                                              */
/* This program is free software; you can redistribute it and/or                */
/* modify it under the terms of the GNU General Public License                  */
/* as published by the Free Software Foundation; either version 2               */
/* of the License, or (at your option) any later version.                       */
/*                                                                              */
/* See file LICENSE for further informations on licensing terms.                */
/*                                                                              */
/* This program is distributed in the hope that it will be useful,              */
/* but WITHOUT ANY WARRANTY; without even the implied warranty of               */
/* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the                */
/* GNU General Public License for more details.                                 */
/*                                                                              */
/* You should have received a copy of the GNU General Public License            */
/* along with this program; if not, write to the Free Software                  */
/* Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.  */
/*                                                                              */
/* Based on PureData by Miller Puckette and others.                             */
/*                                                                              */
/*                                                                              */
/*                                                                              */
/* coded while listening to: Elliott Sharp: The Velocity Of Hue                 */
/*                           Fred Frith: Traffic Continues                      */
/*                           Nmperign: Twisted Village                          */
/*                           Frank Lowe: Black Beings                           */
/*                                                                              */



#include <flext.h>
#include <cstdlib>
#include <cmath>

#if !defined(FLEXT_VERSION) || (FLEXT_VERSION < 401)
#error upgrade your flext version!!!!!!
#endif

#define NUMB_EQ 4

class him: public flext_dsp
{
  FLEXT_HEADER(him,flext_dsp);

public: 
    him(int argc, t_atom *argv);

protected:
    virtual void m_signal (int n, float *const *in, float *const *out);
    virtual void m_dsp (int n, float *const *in, float *const *out);
    
    void set_mu(t_float);
    void set_muv(t_float);
    void set_nu(t_float);
    void set_nuv(t_float);
    void set_etilde(t_float);
    void set_dt(t_float);
    void set_regtime(bool);
    void state();
    void reset();

private:
    // contains DGL-System
    t_float deriv(t_float x[],int eq);

    // 4th order Runge Kutta update of the dynamical variables 
    void runge_kutta_4(t_float dt);

    //these are our data
    t_float data[4]; //mu, muv, nu, nuv (semi-parabolische koordinaten)
    t_float E;

    //and these our settings
    t_float dt;
    bool regtime; //if true "regularisierte zeit"

    bool xfade;
    t_float newE;
    t_float newdt;
    bool newsystem;
    bool newregtime;

    t_float * m_fader;

    //Callbacks
    FLEXT_CALLBACK_1(set_mu,t_float);
    FLEXT_CALLBACK_1(set_muv,t_float);
    FLEXT_CALLBACK_1(set_nu,t_float);
    FLEXT_CALLBACK_1(set_nuv,t_float);
    FLEXT_CALLBACK_1(set_etilde,t_float);
    FLEXT_CALLBACK_1(set_dt,t_float);
    FLEXT_CALLBACK_1(set_regtime,bool);
    FLEXT_CALLBACK(state);
    FLEXT_CALLBACK(reset);

    //reset mus / nus 
    void reset_nuv()
    {
	data[3]= 0.5*sqrt( - (4*data[1]*data[1]) - 
			   ( data[0]*data[0]*data[1]*data[1]*data[1]*data[1])
			   + (8*E*data[0]) - (8*E*data[2]) - 
			   (data[0]*data[0]*data[0]*data[0]*data[1]*data[1])
			   + 16);
     	if (fabs((data[3]))<1e-5)
 	    data[3]=0;
    }
    
    void reset_muv()
    {
	data[1]= 0.5*sqrt( - (4*data[3]*data[3]) - 
			   ( data[0]*data[0]*data[1]*data[1]*data[1]*data[1])
			   + (8*E*data[0]) - (8*E*data[2]) - 
			   (data[0]*data[0]*data[0]*data[0]*data[1]*data[1])
			   + 16);
 	if (fabs((data[1]))<1e-5)
 	    data[1]=0;
    }
    
};


FLEXT_LIB_DSP_V("him~",him)

him::him(int argc, t_atom *argv)
{
    AddInAnything();
    AddOutSignal();
    AddOutSignal();
    AddOutSignal();
    AddOutSignal();
    AddOutSignal();
    AddOutSignal();
    FLEXT_ADDMETHOD_F(0,"mu",set_mu);
    FLEXT_ADDMETHOD_F(0,"muv",set_muv);
    FLEXT_ADDMETHOD_F(0,"nu",set_nu);
    FLEXT_ADDMETHOD_F(0,"nuv",set_nuv);
    FLEXT_ADDMETHOD_F(0,"e",set_etilde);
    FLEXT_ADDMETHOD_F(0,"dt",set_dt);
    FLEXT_ADDMETHOD_B(0,"regtime",set_regtime);
    FLEXT_ADDMETHOD_(0,"state",state);
    FLEXT_ADDMETHOD_(0,"reset",reset);
    
    
    //beginning values
    if (argc==1)
	E=atom_getfloat(argv);
    else
	E= -float(rand())/float(RAND_MAX);
    
    reset();

    state();

    //default mode
    regtime=true;
    dt=0.01;
} 

inline t_float him::deriv(t_float * x, int eq)
{
    t_float result;
    // set DGL-System here
    if (eq == 0) result =  x[1];
    if (eq == 1) result = 2*E*x[0]-0.25*x[0]*x[2]*x[2]*(2*x[0]*x[0]+x[2]*x[2]);
    if (eq == 2) result =  x[3];
    if (eq == 3) result = 2*E*x[2]-0.25*x[2]*x[0]*x[0]*(2*x[2]*x[2]+x[0]*x[0]);

  return result;
}

inline void him::runge_kutta_4(t_float dt)                          
{
    t_float k1[NUMB_EQ],k2[NUMB_EQ],k3[NUMB_EQ],k4[NUMB_EQ];
    t_float temp1[NUMB_EQ], temp2[NUMB_EQ], temp3[NUMB_EQ];
    
    for(int i=0;i<=NUMB_EQ-1;i++) // iterate over equations 
    {
	k1[i] = dt * deriv(data,i);
	temp1[i] = data[i] + 0.5*k1[i];	    	    
    }
    
    for(int i=0;i<=NUMB_EQ-1;i++)
    {
	k2[i] = dt * deriv(temp1,i);
	temp2[i] = data[i] + 0.5*k2[i];
    }
    
    for(int i=0;i<=NUMB_EQ-1;i++)
    {
	k3[i] = dt * deriv(temp2,i);
	temp3[i] = data[i] + k3[i];    
    }
    
    for(int i=0;i<=NUMB_EQ-1;i++)
    {
	k4[i] = dt * deriv(temp3,i);
	data[i] = data[i] + (k1[i] + (2.*(k2[i]+k3[i])) + k4[i])/6.;
    	
	// we don't want to experience denormals in the next step */
	if(fabs((data[i]))<1e-5)   
	    data[i]=0;
    }

    
    /*
      the system might become unstable ... in this case, we'll request a new system
    */    

    for(int i=0;i<=NUMB_EQ-1;i++)
    {
	if(data[i]>2)
	    {
		xfade = newsystem =  true;
		data[i] = 2;
	    }
	if(data[i]<-2)
	    {
		xfade = newsystem = true;
		data[i] = -2;
	    }
    }
}



void him::m_signal(int n, t_float *const *in, t_float *const *out)
{
    t_float * out0 = out[0];
    t_float * out1 = out[1];
    t_float * out2 = out[2];
    t_float * out3 = out[3];
    t_float * out4 = out[4];
    t_float * out5 = out[5];

    
    if (regtime)
    {
	for (int i=0;i!=n;++i)
	{
	    runge_kutta_4(dt);
	    (*(out0)++)=data[0];
	    (*(out1)++)=data[1];
	    (*(out2)++)=data[2];
	    (*(out3)++)=data[3];
	    (*(out4)++)=data[0]*data[2];
	    (*(out5)++)=(data[0]*data[0]-data[2]*data[2])*0.5;
	}
    }
    else
    {
	for (int i=0;i!=n;++i)
	{
	    runge_kutta_4(dt/(2*sqrt(data[0]*data[0]+data[2]*data[2])));
	    (*(out0)++)=data[0];
	    (*(out1)++)=data[1];
	    (*(out2)++)=data[2];
	    (*(out3)++)=data[3];
	    (*(out4)++)=data[0]*data[2];
	    (*(out5)++)=(data[0]*data[0]-data[2]*data[2])*0.5;
	}
    }
    
    if (xfade)
    {
	/* fading */
	out0 = out[0];
	out1 = out[1];
	out2 = out[2];
	out3 = out[3];
	out4 = out[4];
	out5 = out[5];
	
	t_float * fader = m_fader + n - 1;
	for (int i=0;i!=n;++i)
	{
	    (*(out0)++) *= *fader;
	    (*(out1)++) *= *fader;
	    (*(out2)++) *= *fader;
	    (*(out3)++) *= *fader;
	    (*(out4)++) *= *fader;
	    (*(out5)++) *= *fader--;
	}

	if (newsystem)
	{
	    reset();
	    newsystem = false;
	}
	
	E = newE;
	dt = newdt;
	regtime = newregtime;

	out0 = out[0];
	out1 = out[1];
	out2 = out[2];
	out3 = out[3];
	out4 = out[4];
	out5 = out[5];
    
	fader = m_fader;
	if (regtime)
	{
	    for (int i=0;i!=n;++i)
	    {
		runge_kutta_4(dt);
		(*(out0)++)+= data[0]* *fader;
		(*(out1)++)+= data[1]* *fader;
		(*(out2)++)+= data[2]* *fader;
		(*(out3)++)+= data[3]* *fader;
		(*(out4)++)+= data[0]*data[2]* *fader;
		(*(out5)++)+= (data[0]*data[0]-data[2]*data[2])*0.5* *fader++;
	    }
	}
	else
	{
	    for (int i=0;i!=n;++i)
	    {
		runge_kutta_4(dt/(2*sqrt(data[0]*data[0]+data[2]*data[2])));
		(*(out0)++)+= data[0]* *fader;
		(*(out1)++)+= data[1]* *fader;
		(*(out2)++)+= data[2]* *fader;
		(*(out3)++)+= data[3]* *fader;
		(*(out4)++)+= data[0]*data[2]* *fader;
		(*(out5)++)+= (data[0]*data[0]-data[2]*data[2])*0.5* *fader++;
	    }
	}
	
	xfade = false;
    }
    
}    

void him::m_dsp(int n, t_float *const *in, t_float *const *out)
{
    m_fader = new t_float[n];
    t_float on = 1.f/(n-1);
    
    t_float value = 0;

    t_float* localfader = m_fader;

    while (n--)
    {
	*localfader = value;
	value += on;
	++localfader;
    }
    xfade = false;
}


void him::set_mu(t_float f)
{
    data[0]=f;
    reset_nuv();
}

void him::set_muv(t_float f)
{
    data[1]=f;
    reset_nuv();
}

void him::set_nu(t_float f)
{
    data[3]=f;
    reset_nuv();
}

void him::set_nuv(t_float f)
{
    data[3]=f;
    reset_muv();
}

void him::set_etilde(t_float f)
{
    newE=f;
    xfade = true;
    //reset_nuv();
}

void him::set_dt(t_float f)
{
    newdt=f;
    xfade = true;
}

void him::set_regtime(bool b)
{
    newregtime=b;
    xfade = true;
}


void him::state()
{
    post("mu %f",data[0]);
    post("mus %f",data[1]);
    post("nu %f",data[2]);
    post("nus %f",data[3]);
    post("etilde %f",E);
}

void him::reset()
{
    data[0]=float(rand())/float(RAND_MAX);
    data[1]=float(rand())/float(RAND_MAX);
    data[2]=float(rand())/float(RAND_MAX);
    reset_nuv();
}