aboutsummaryrefslogtreecommitdiff
path: root/src/driver/initial_guess.cc
blob: 03aef25b01e2221821f7a138db39d8b9051e1f0c (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
// initial_guess.cc -- top level driver for setting the initial guess
// $Header$
//
// <<<access to persistent data>>>
// <<<prototypes for functions local to this file>>>
// AHFinderDirect_initial_guess - top-level driver
/// setup_Kerr_horizon - set up Kerr horizon in h (Kerr or Kerr-Schild coords)
/// setup_ellipsoid - setup up a coordinate ellipsoid in h
///

#include <stdio.h>
#include <assert.h>
#include <math.h>
#include <vector>

#include "util_Table.h"
#include "cctk.h"
#include "cctk_Arguments.h"
#include "cctk_Parameters.h"

#include "stdc.h"
#include "config.hh"
#include "../jtutil/util.hh"
#include "../jtutil/array.hh"
#include "../jtutil/cpm_map.hh"
#include "../jtutil/linear_map.hh"
using jtutil::error_exit;

#include "../patch/coords.hh"
#include "../patch/grid.hh"
#include "../patch/fd_grid.hh"
#include "../patch/patch.hh"
#include "../patch/patch_edge.hh"
#include "../patch/patch_interp.hh"
#include "../patch/ghost_zone.hh"
#include "../patch/patch_system.hh"

#include "../elliptic/Jacobian.hh"

#include "../gr/gfns.hh"
#include "../gr/gr.hh"

#include "driver.hh"

//******************************************************************************

//
// ***** access to persistent data *****
//
extern struct state state;

//******************************************************************************

//
// ***** prototypes for functions local to this file *****
//

namespace {
void setup_Kerr_horizon(patch_system& ps,
			fp x_posn, fp y_posn, fp z_posn,
			fp m, fp a,
			bool Kerr_Schild_flag,
			const struct verbose_info& verbose_info);
void setup_ellipsoid(patch_system& ps,
		     fp x_center, fp y_center, fp z_center,
		     fp x_radius, fp y_radius, fp z_radius,
		     bool print_msg_flag);
	  };

//******************************************************************************

//
// This function is called by the Cactus scheduler to set our initial guess
// for the horizon position(s).
//
extern "C"
  void AHFinderDirect_initial_guess(CCTK_ARGUMENTS)
{
DECLARE_CCTK_ARGUMENTS
DECLARE_CCTK_PARAMETERS
const struct verbose_info& verbose_info = state.verbose_info;

struct IO_info& IO_info = state.IO_info;
IO_info.time_iteration = cctk_iteration;

	for (int hn = 1 ; hn <= state.N_horizons ; ++hn)
	{
	struct AH_info& AH_info = * state.AH_info_ptrs[hn];
	patch_system& ps = * AH_info.ps_ptr;

	if (verbose_info.print_algorithm_highlights)
	   then CCTK_VInfo(CCTK_THORNSTRING,
			   "setting initial guess for horizon shape h");

	if	(STRING_EQUAL(initial_guess_method, "read from file"))
	   then input_gridfn(ps, gfns::gfn__h,
			     IO_info, IO_info.h_base_file_name,
			     hn, verbose_info.print_algorithm_highlights);

	else if (STRING_EQUAL(initial_guess_method, "Kerr/Kerr"))
	   then setup_Kerr_horizon(ps,
				   initial_guess__Kerr__x_posn[hn],
				   initial_guess__Kerr__y_posn[hn],
				   initial_guess__Kerr__z_posn[hn],
				   initial_guess__Kerr__mass[hn],
				   initial_guess__Kerr__spin[hn],
				   false,	// Kerr coordinates
				   verbose_info);
	else if (STRING_EQUAL(initial_guess_method, "Kerr/Kerr-Schild"))
	   then setup_Kerr_horizon(ps,
				   initial_guess__Kerr__x_posn[hn],
				   initial_guess__Kerr__y_posn[hn],
				   initial_guess__Kerr__z_posn[hn],
				   initial_guess__Kerr__mass[hn],
				   initial_guess__Kerr__spin[hn],
				   true,	// Kerr-Schild coordinates
				   verbose_info);

	else if (STRING_EQUAL(initial_guess_method, "sphere"))
	   then setup_ellipsoid(ps,
				initial_guess__sphere__x_center[hn],
				initial_guess__sphere__y_center[hn],
				initial_guess__sphere__z_center[hn],
				initial_guess__sphere__radius[hn],
				initial_guess__sphere__radius[hn],
				initial_guess__sphere__radius[hn],
				verbose_info.print_algorithm_highlights);
	else if (STRING_EQUAL(initial_guess_method, "ellipsoid"))
	   then setup_ellipsoid(ps,
				initial_guess__ellipsoid__x_center[hn],
				initial_guess__ellipsoid__y_center[hn],
				initial_guess__ellipsoid__z_center[hn],
				initial_guess__ellipsoid__x_radius[hn],
				initial_guess__ellipsoid__y_radius[hn],
				initial_guess__ellipsoid__z_radius[hn],
				verbose_info.print_algorithm_highlights);

	else	CCTK_VWarn(-1, __LINE__, __FILE__, CCTK_THORNSTRING,
			   "unknown initial_guess_method=\"%s\"!",
			   initial_guess_method);	/*NOTREACHED*/

	// write initial guess back to the data file?
	if (IO_info.output_initial_guess)
	   then output_gridfn(ps, gfns::gfn__h,
			      IO_info, IO_info.h_base_file_name,
			      hn, verbose_info.print_algorithm_highlights);
	}
}

//******************************************************************************

//
// This function sets up the horizon of a Kerr black hole in Kerr or
// Kerr-Schild coordinates, on the nominal grid, in the  h  gridfn.
//
// Kerr-Schild coordinates are described in MTW Exercise 33.8, page 903,
// and the horizon is worked out on page 13.2 of my AHFinderDirect notes.
//
// Arguments:
// [xyz]_posn = The position of the Kerr black hole.
// (m,a) = Describe the Kerr black hole.  Note that my convention has
//	   a=J/m^2 dimensionless, while MTW take a=J/m=m*(my a).
// Kerr_Schild_flag = false to use Kerr coordinates,
//		      true to use Kerr-Schild coordinates
// verbose_info = controls what messages we print
//
namespace {
void setup_Kerr_horizon(patch_system& ps,
			fp x_posn, fp y_posn, fp z_posn,
			fp m, fp a,
			bool Kerr_Schild_flag,
			const struct verbose_info& verbose_info)
{
const char* const name = Kerr_Schild_flag ? "Kerr-Schild" : "Kerr";

if (verbose_info.print_algorithm_highlights)
   then {
	CCTK_VInfo(CCTK_THORNSTRING,
		   "   setting Kerr/%s horizon shape",
		   name);
	CCTK_VInfo(CCTK_THORNSTRING,
		   "           posn=(%g,%g,%g) mass=%g spin=J/m^2=%g",
		   double(x_posn), double(y_posn), double(z_posn),
		   double(m), double(a));
	}

// horizon in Kerr coordinates is coordinate sphere
const fp r = m * (1.0 + sqrt(1.0 - a*a));

// horizon in Kerr-Schild coordinates is coordinate ellipsoid
const fp  z_radius = r;
const fp xy_radius = Kerr_Schild_flag ? r * sqrt(1.0 + a*a*m*m/(r*r)) : r;

if (verbose_info.print_algorithm_details)
   then CCTK_VInfo(CCTK_THORNSTRING,
		   "   setting coordinate %s",
		   Kerr_Schild_flag ? "ellipsoid" : "sphere");
setup_ellipsoid(ps,
		x_posn, y_posn, z_posn,
		xy_radius, xy_radius, z_radius,
		verbose_info.print_algorithm_details);
}
	  }

//******************************************************************************

//
// This function sets up an ellipsoid initial guess, using the formulas
// in "ellipsoid.maple" and the Maple-generated C code in "ellipsoid.c":
//
// ellipsoid has center (A,B,C), radius (a,b,c)
// angular coordinate system has center (U,V,W)
//
// direction cosines wrt angular coordinate center are (xcos,ycos,zcos)
// i.e. a point has coordinates (U+xcos*r, V+ycos*r, W+zcos*r)
//
// then the equation of the ellipsoid is
//	(U+xcos*r - A)^2     (V+ycos*r - B)^2     (W+zcos*r - C)^2
//	-----------------  +  ----------------  +  -----------------  =  1
//	        a^2                  b^2                   c^2
//
// to solve this, we introduce intermediate variables
//	AU = A - U
//	BV = B - V
//	CW = C - W
//
namespace {
void setup_ellipsoid(patch_system& ps,
		     fp x_center, fp y_center, fp z_center,
		     fp x_radius, fp y_radius, fp z_radius,
		     bool print_msg_flag)
{
if (print_msg_flag)
   then {
	CCTK_VInfo(CCTK_THORNSTRING,
		   "   setting ellipsoid: center=(%g,%g,%g)",
		   double(x_center), double(y_center), double(z_center));
	CCTK_VInfo(CCTK_THORNSTRING,
		   "                      radius=(%g,%g,%g)",
		   double(x_radius), double(y_radius), double(z_radius));
	}

	for (int pn = 0 ; pn < ps.N_patches() ; ++pn)
	{
	patch& p = ps.ith_patch(pn);

		for (int irho = p.min_irho() ; irho <= p.max_irho() ; ++irho)
		{
		for (int isigma = p.min_isigma() ;
		     isigma <= p.max_isigma() ;
		     ++isigma)
		{
		const fp rho = p.rho_of_irho(irho);
		const fp sigma = p.sigma_of_isigma(isigma);
		fp xcos, ycos, zcos;
		p.xyzcos_of_rho_sigma(rho,sigma, xcos,ycos,zcos);

		// set up variables used by Maple-generated code
		const fp AU = x_center - ps.origin_x();
		const fp BV = y_center - ps.origin_y();
		const fp CW = z_center - ps.origin_z();
		const fp a = x_radius;
		const fp b = y_radius;
		const fp c = z_radius;

		// compute the solutions r_plus and r_minus
		fp r_plus, r_minus;
		#include "ellipsoid.c"

		// exactly one of the solutions (call it r) should be positive
		fp r;
		if      ((r_plus > 0.0) && (r_minus < 0.0))
		   then r = r_plus;
		else if ((r_plus < 0.0) && (r_minus > 0.0))
		   then r = r_minus;
		else    CCTK_VWarn(-1, __LINE__, __FILE__, CCTK_THORNSTRING,
				   "\n"
"   setup_ellipsoid():\n"
"        expected exactly one r>0 solution to quadratic, got 0 or 2!\n"
"        %s patch (irho,isigma)=(%d,%d) ==> (rho,sigma)=(%g,%g)\n"
"        direction cosines (xcos,ycos,zcos)=(%g,%g,%g)\n"
"        ==> r_plus=%g r_minus=%g\n"
				   ,
				   p.name(), irho, isigma,
				   double(rho), double(sigma),
				   double(xcos), double(ycos), double(zcos),
				   double(r_plus), double(r_minus));
		   						/*NOTREACHED*/

		// r = horizon radius at this grid point
		p.ghosted_gridfn(gfns::gfn__h, irho,isigma) = r;
		}
		}
	}
}
	  }