aboutsummaryrefslogtreecommitdiffstats
path: root/buch/chapters/110-elliptisch/images/rechteck.cpp
blob: b5ad0ec5aa45097719273af9d82bc17eb1063aea (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
428
429
430
/*
 * rechteck.cpp
 *
 * (c) 2021 Prof Dr Andreas Müller, OST Ostschweizer Fachhochschule
 */
#include <cmath>
#include <cstdlib>
#include <cstdio>
#include <complex>
#include <iostream>
#include <fstream>
#include <list>
#include <getopt.h>
#include <gsl/gsl_sf_ellint.h>

double	ast = 1;

/**
 * \brief Base class for integrands
 */
class integrand {
protected:
	double	_k;
public:
	double	k() const { return _k; }
	integrand(double k) : _k(k) { }
	virtual std::complex<double>	operator()(const std::complex<double>& z) const = 0;
	static double	kprime(double k);
};

double	integrand::kprime(double k) {
	return sqrt(1 - k*k);
}

/**
 * \brief Elliptic integral of the first kind
 */
class integrand1 : public integrand {
public:
	integrand1(double k) : integrand(k) { }
	virtual std::complex<double>	operator()(
		const std::complex<double>& z) const {
		std::complex<double>	square = z * z;
		std::complex<double>	eins(1.0);
		std::complex<double>	result = eins
			/ sqrt((eins - square) * (eins - k() * k() * square));
		return ast * ((result.imag() < 0) ? -result : result);
	}
};

/**
 * \brief A class to trace curves
 */
class curvetracer {
	const integrand&	_f;
public:
	typedef std::list<std::complex<double> >	curve_t;
	curvetracer(const integrand& f) : _f(f) { }

	std::complex<double>	startpoint(const std::complex<double>& z,
		int n = 100) const;

	std::complex<double>	segment(const std::complex<double>& z1,
		const std::complex<double>& z2, int n) const;

	std::pair<std::complex<double>,std::complex<double> >	segmentl(
		const std::complex<double>& start,
		const std::complex<double>& dir,
		double stepsize, int n = 10) const;

	curve_t	trace(const std::complex<double>& startz,
		const std::complex<double>& dir,
		const std::complex<double>& startw,
		int maxsteps) const;

	static curve_t	mirrorx(const curve_t& c);
	static curve_t	mirrory(const curve_t& c);
	static curve_t	mirror(const curve_t& c);
};

/**
 * \brief Perform integration for a 
 *
 * \param z1	the start point of the segment in the domain
 * \param z2	the end point of the segment in the domain
 * \param n	the number of integration steps to use
 * \return	the increment along that segment
 */
std::complex<double>	curvetracer::segment(const std::complex<double>& z1,
		const std::complex<double>& z2, int n = 100) const {
	std::complex<double>	dz = z2 - z1;
	std::complex<double>	summe(0);
	double	h = 1. / (2 * n);
	for (int i = 1; i < 2 * n; i += 2) {
		double	t = i * h;
		std::complex<double>	z = (1 - t) * z1 + t * z2;
		summe += _f(z);
	}
	return dz * h * summe * 2.;
}

/**
 * \brief Exception thrown when the number of iterations is exceeded
 */
class toomanyiterations : public std::runtime_error {
public:
	toomanyiterations(const std::string& cause)
		: std::runtime_error(cause) {
	}
};

/**
 * \brief Perform integration with a given target length
 *
 * \param start		the start point
 * \param dir		the direction in which to integrate
 * \param stepsize	the required length of the step
 * \param n		the number of integration steps
 * \return		the increment in the range by this segment
 */
std::pair<std::complex<double>, std::complex<double> >	curvetracer::segmentl(
	const std::complex<double>& start,
	const std::complex<double>& dir,
	double stepsize,
	int n) const {
	std::complex<double>	s(0.);
	std::complex<double>	z = start;
	int	counter = 100;
	while (abs(s) < stepsize) {
		s = s + segment(z, z + dir, n);
		z = z + dir;
		if (counter-- == 0) {
			throw toomanyiterations("too many iterations");
		}
	}
	return std::make_pair(z, s);
}

/**
 * \brief Trace a curve from a starting point in the domain
 *
 * \param startz	the start point of the curve in the domain
 * \param dir		the direction of the curve in the domain
 * \param startw	the initial function value where the curve starts in
 *			the range
 * \param maxsteps	the maximum number of dir-steps before giving up
 * \return		the curve as a list of points
 */
curvetracer::curve_t	curvetracer::trace(const std::complex<double>& startz,
			const std::complex<double>& dir,
			const std::complex<double>& startw,
			int maxsteps) const {
	curve_t	result;
	std::complex<double>	z = startz;
	std::complex<double>	w = startw;
	result.push_back(w);
	while (maxsteps-- > 0) {
		try {
			auto	seg = segmentl(z, dir, abs(dir), 40);
			z = seg.first;
			w = w + seg.second;
			result.push_back(w);
		} catch (const toomanyiterations& x) {
			std::cerr << "iterations exceeded after ";
			std::cerr << result.size();
			std::cerr << " points" << std::endl;
			maxsteps = 0;
		}
	}
	return result;
}

/**
 * \brief Find the initial point for a coordinate curve
 *
 * \param k	the elliptic integral parameter
 * \param z	the desired starting point argument
 */
std::complex<double>	curvetracer::startpoint(const std::complex<double>& z,
		int n) const {
	std::cerr << "start at " << z.real() << "," << z.imag() << std::endl;
	return segment(std::complex<double>(0.), z, n);
}


curvetracer::curve_t	curvetracer::mirrorx(const curve_t& c) {
	curve_t	result;
	for (auto z : c) {
		result.push_back(-std::conj(z));
	}
	return result;
}

curvetracer::curve_t	curvetracer::mirrory(const curve_t& c) {
	curve_t	result;
	for (auto z : c) {
		result.push_back(std::conj(z));
	}
	return result;
}

curvetracer::curve_t	curvetracer::mirror(const curve_t& c) {
	curve_t	result;
	for (auto z : c) {
		result.push_back(-z);
	}
	return result;
}

/**
 * \brief Class to draw the curves to a file
 */
class curvedrawer {
	std::ostream	*_out;
	std::string	_color;
public:
	curvedrawer(std::ostream *out) : _out(out), _color("red") { }
	const std::string&	color() const { return _color; }
	void	color(const std::string& c) { _color = c; }
	void	operator()(const curvetracer::curve_t& curve);
	std::ostream	*out() { return _out; }
};

/**
 * \brief Operator to draw a curve
 *
 * \param curve		the curve to draw
 */
void	curvedrawer::operator()(const curvetracer::curve_t& curve) {
	double	first = true;
	for (auto z : curve) {
		if (first) {
			*_out << "\\draw[color=" << _color << ",line width=#1] ";
			first = false;
		} else {
			*_out << std::endl << "    -- ";
		}
		*_out << "({" << z.real() << "*\\dx},{" << z.imag() << "*\\dy})";
	}
	*_out << ";" << std::endl;
}

static struct option	longopts[] = {
{ "outfile",	required_argument,	NULL,	'o' },
{ "k",		required_argument,	NULL,	'k' },
{ "deltax",	required_argument,	NULL,	'd' },
{ "vsteps",	required_argument,	NULL,	'v' },
{ NULL,		0,			NULL,	 0  }
};

/**
 * \brief Main function
 */
int	main(int argc, char *argv[]) {
	double	k = 0.625;
	double	Deltax = 0.2;
	int	vsteps = 4;

	int	c;
	int	longindex;
	std::string	outfilename;
	while (EOF != (c = getopt_long(argc, argv, "o:k:d:", longopts,
		&longindex)))
		switch (c) {
		case 'd':
			Deltax = std::stod(optarg);
			break;
		case 'o':
			outfilename = std::string(optarg);
			break;
		case 'k':
			k = std::stod(optarg);
			break;
		case 'v':
			vsteps = std::stoi(optarg);
			break;
		}

	double	kprime = integrand::kprime(k);

	double	xmax = gsl_sf_ellint_Kcomp(k, GSL_PREC_DOUBLE);
	double	ymax = gsl_sf_ellint_Kcomp(kprime, GSL_PREC_DOUBLE);
	std::cout << "xmax = " << xmax << std::endl;
	std::cout << "ymax = " << ymax << std::endl;

	curvedrawer	*cdp;
	std::ofstream	*outfile = NULL;
	if (outfilename.c_str()) {
		outfile = new std::ofstream(outfilename.c_str());
	}
	if (outfile) {
		cdp = new curvedrawer(outfile);
	} else {
		cdp = new curvedrawer(&std::cout);
	}

	integrand1	f(k);
	curvetracer	ct(f);

	// fill
	(*cdp->out()) << "\\def\\hintergrund{" << std::endl;
	(*cdp->out()) << "\\fill[color=red!10] ({" << (-xmax) << "*\\dx},0) "
		<< "rectangle ({" << xmax << "*\\dx},{" << ymax << "*\\dy});"
		<< std::endl;
	(*cdp->out()) << "\\fill[color=blue!10] ({" << (-xmax) << "*\\dx},{"
		<< (-ymax) << "*\\dy}) rectangle ({" << xmax << "*\\dx},0);"
		<< std::endl;
	(*cdp->out()) << "}" << std::endl;

	// macro for grid
	(*cdp->out()) << "\\def\\netz#1{" << std::endl;

	// "circles"
	std::complex<double>	dir(0.01, 0);
	double	deltax = Deltax;
	for (double im = deltax; im < 3; im += deltax) {
		std::complex<double>	startz(0, im);
		std::complex<double>	startw = ct.startpoint(startz);
		curvetracer::curve_t	curve = ct.trace(startz, dir,
			startw, 1000);
		cdp->color("red");
		(*cdp)(curve);
		(*cdp)(curvetracer::mirrorx(curve));
		cdp->color("blue");
		(*cdp)(curvetracer::mirrory(curve));
		(*cdp)(curvetracer::mirror(curve));
	}

	// imaginary axis
	(*cdp->out()) << "\\draw[color=red,line width=#1] (0,0) -- (0,{" << ymax
		<< "*\\dy});" << std::endl;
	(*cdp->out()) << "\\draw[color=blue,line width=#1] (0,0) -- (0,{" << (-ymax)
		<< "*\\dy});" << std::endl;

	// arguments between 0 and 1
	dir = std::complex<double>(0, 0.01);
	for (double re = 0.2; re < 1; re += 0.2) {
		std::complex<double>	startz(re, 0.001);
		std::complex<double>	startw = ct.startpoint(startz);
		startw = std::complex<double>(startw.real(), 0);
		curvetracer::curve_t	curve = ct.trace(startz, dir,
			startw, 1000);
		cdp->color("red");
		(*cdp)(curve);
		(*cdp)(curvetracer::mirrorx(curve));
		cdp->color("blue");
		(*cdp)(curvetracer::mirrory(curve));
		(*cdp)(curvetracer::mirror(curve));
	}

	// argument 1 (singularity)
	{
		std::complex<double>	startz(1.);
		std::complex<double>	startw(xmax);
		curvetracer::curve_t	curve = ct.trace(startz, dir,
			startw, 1000);
		cdp->color("red");
		(*cdp)(curve);
		(*cdp)(curvetracer::mirrorx(curve));
		cdp->color("blue");
		(*cdp)(curvetracer::mirror(curve));
		(*cdp)(curvetracer::mirrory(curve));
	}

	// arguments between 1 and 1/k
	{
		deltax = (1/k - 1) / vsteps;
		for (double x0 = 1 + deltax; x0 < 1/k + 0.00001; x0 += deltax) {
			double	y0 = sqrt(1-1/(x0*x0))/kprime;
			//std::cout << "y0 = " << y0 << std::endl;
			double	y = gsl_sf_ellint_F(asin(y0), kprime,
				GSL_PREC_DOUBLE);
			std::complex<double>	startz(x0);
			std::complex<double>	startw(xmax, y);
			curvetracer::curve_t	curve = ct.trace(startz, dir,
				startw, 1000);
			cdp->color("red");
			(*cdp)(curve);
			(*cdp)(curvetracer::mirrorx(curve));
			cdp->color("blue");
			(*cdp)(curvetracer::mirror(curve));
			(*cdp)(curvetracer::mirrory(curve));
		}
	}

	// argument 1/k
#if 0
	{
		std::complex<double>	startz(1/k);
		std::complex<double>	startw(xmax, ymax);
		curvetracer::curve_t	curve = ct.trace(startz, dir,
			startw, 1000);
		cdp->color("red");
		(*cdp)(curve);
		(*cdp)(curvetracer::mirrorx(curve));
		cdp->color("blue");
		(*cdp)(curvetracer::mirror(curve));
		(*cdp)(curvetracer::mirrory(curve));
	}
#endif

	// arguments larger than 1/k
	{
		deltax = Deltax;
		dir = std::complex<double>(0, 0.01);
		double	x0 = 1/k;
		while (x0 <= 1/k + 0.0001) { x0 += deltax; }
		for (; x0 < 4; x0 += deltax) {
			std::complex<double>	startz(x0);
			std::complex<double>	startw(gsl_sf_ellint_F(
				asin(1/(k*x0)), k, GSL_PREC_DOUBLE), ymax);
		curvetracer::curve_t	curve = ct.trace(startz, dir,
			startw, 1000);
		cdp->color("red");
		(*cdp)(curve);
		(*cdp)(curvetracer::mirrorx(curve));
		cdp->color("blue");
		(*cdp)(curvetracer::mirror(curve));
		(*cdp)(curvetracer::mirrory(curve));
		}
	}

	(*cdp->out()) << "}" << std::endl;

	// border
	(*cdp->out()) << "\\def\\xmax{" << xmax << "}" << std::endl;
	(*cdp->out()) << "\\def\\ymax{" << ymax << "}" << std::endl;

	return EXIT_SUCCESS;
}