aboutsummaryrefslogtreecommitdiff
path: root/chaos/src/chaos_search.hpp
blob: a7fc44d61512ae4fc3dca6ce3b80596e75b2abbb (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
// 
//  
//  chaos~
//  Copyright (C) 2005  Tim Blechmann
//  
//  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.
//  
//  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; see the file COPYING.  If not, write to
//  the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
//  Boston, MA 02111-1307, USA.

#include "chaos_base.hpp"

template <class system> class chaos_search
	: public flext_base
{
	FLEXT_HEADER(chaos_search, flext_base);

public:

	/* local data for system, output and interpolation */
	system * m_system; /* the system */
	

 	data_t min[MAXDIMENSION];      /* minimal coordinates */
 	data_t max[MAXDIMENSION];      /* maximal coordinates */
	data_t final[MAXDIMENSION];    /* initial coordinates for outlet */
	
	data_t ly;            /* lyapunov exponent */

	int m_transient_steps;  /* steps before starting the analysis */
	int m_asymptotic_steps; /* steps for the analysis */
	
	
	void get_tsteps(int &i)
	{
		i = m_transient_steps;
	}
	
	void set_tsteps(int i)
	{
		if (i > 0)
			m_transient_steps = i;
		else
			m_transient_steps = 0;
	}

	void get_asteps(int &i)
	{
		i = m_asymptotic_steps;
	}

	void set_asteps(int &i)
	{
		if (i > 0)
			m_asymptotic_steps = i;
		else
			m_asymptotic_steps = 0;
	}	

	void print_results(void)
	{
		/* - send parameters to 1
		   - send initial coordinates to 2
		   - send minimal coordinates to 3
		   - send lyapunov exponent to 4
		*/
		
		for (std::map<const t_symbol*,int>::iterator it = m_system->attr_ind.begin();
			 it != m_system->attr_ind.end(); ++it)
		{
			post("key %s", it->first->s_name);
			post("value %f", m_system->get_data(it->second));
		}
	}	
	
	void m_search();
	
	FLEXT_CALLBACK(m_bang);
	FLEXT_CALLVAR_I(get_tsteps, set_tsteps);
	FLEXT_CALLVAR_I(get_asteps, set_asteps);
	FLEXT_THREAD(m_search);
};


/* create constructor / destructor */

#define CHAOS_SEARCH_INIT(SYSTEM, ATTRIBUTES)						\
FLEXT_HEADER(SYSTEM##_search, chaos_search<SYSTEM>)					\
																	\
SYSTEM##_search(int argc, t_atom* argv )							\
{																	\
	m_system = new SYSTEM;											\
																	\
	int size = m_system->get_num_eq();								\
																	\
																	\
    m_asymptotic_steps = 10000; 									\
    m_transient_steps = 100;										\
																	\
    AddOutList("parameters");										\
    AddOutList("initial coordinates");								\
    AddOutList("minimal coordinates");								\
    AddOutList("maximal coordinates");								\
    AddOutFloat("lyapunov exponent");								\
																	\
	FLEXT_ADDATTR_VAR("transient_steps", get_tsteps, set_tsteps);	\
	FLEXT_ADDATTR_VAR("steps", get_asteps, set_asteps);				\
    ATTRIBUTES;														\
    FLEXT_ADDMETHOD_(0,"search", m_search);							\
}																	\
																	\
~SYSTEM##_search()													\
{																	\
	delete m_system;												\
}																	\
																	\
FLEXT_ATTRVAR_I(m_transient_steps);									\
FLEXT_ATTRVAR_I(m_asymptotic_steps);
 


template <class system> 
void chaos_search<system>::m_search()
{
	int dimensions = m_system->get_num_eq();
	
	ly = 0;
	data_t diff_old = 0.1;
	data_t last[MAXDIMENSION];

	/* transient dynamics */
	for (int i = 0; i != m_transient_steps; ++i)
	{
		m_system->m_perform();
	}

	for (int i = 0; i != dimensions; ++i)
	{
		last[i] = min[i] = max[i] = m_system->m_data[i];
	}
	
	/* now we start the analysis */

	for (int i = 0; i != m_asymptotic_steps; ++i)
	{
		
		m_system->m_perform();
		
		data_t diff = 0;
		for (int j = 0; j != dimensions; ++j)
		{
			/* update min/max */
			data_t datum = m_system->m_data[j];
			if (datum > max[j])
				max[j] = datum;
			else if (datum < min[j])
				min[j] = datum;
			
			/* sum up diff */
			diff += (last[j] - datum) * (last[j] - datum);
			
			last[j] = datum;
		}
		diff = sqrt(diff);
		
		if (diff < 0)
			diff = -diff;

		ly += log(diff / diff_old);
		diff_old = diff;
	
		/* todo: maybe some overflow checking */
		if (diff == 0)
			break;
	}
	

	ly /= m_asymptotic_steps;
	
	print_results();
}