aboutsummaryrefslogtreecommitdiff
path: root/src/driver/find_horizons.cc
blob: 3f3279e38834f09036363af08d0c983560ed1546 (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
// find_horizons.cc -- top level driver for finding apparent horizons
// $Header$
//
// <<<access to persistent data>>>
// <<<prototypes for functions local to this file>>>
// AHFinderDirect_find_horizons - top-level driver to find apparent horizons
//

#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 "../util/coords.hh"
#include "../util/grid.hh"
#include "../util/fd_grid.hh"
#include "../util/patch.hh"
#include "../util/patch_edge.hh"
#include "../util/patch_interp.hh"
#include "../util/ghost_zone.hh"
#include "../util/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 {
bool find_horizon(enum method method,
		  const struct verbose_info& verbose_info, int timer_handle,
		  struct IO_info& IO_info,
		  struct Jacobian_info& Jac_info,
		  struct solver_info& solver_info,
		  struct cactus_grid_info& cgi, struct geometry_info& gi,
		  patch_system& ps, Jacobian* Jac_ptr,
		  int hn, int N_horizons);
void BH_diagnostics(enum patch::integration_method surface_integral_method,
		    const struct verbose_info& verbose_info,
		    struct AH_info& AH_info);
	  };

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

//
// This function is called by the Cactus scheduler to find the apparent
// horizon or horizons in the current slice.
//
extern "C"
  void AHFinderDirect_find_horizons(CCTK_ARGUMENTS)
{
DECLARE_CCTK_ARGUMENTS
DECLARE_CCTK_PARAMETERS
const struct verbose_info& verbose_info = state.verbose_info;

state.IO_info.time_iteration = cctk_iteration;

if (state.timer_handle >= 0)
   then CCTK_TimerResetI(state.timer_handle);

	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;

	AH_info.AH_found
	   = find_horizon(state.method,
			  verbose_info, state.timer_handle,
			  state.IO_info, state.Jac_info, state.solver_info,
			  state.cgi, state.gi,
			  ps, AH_info.Jac_ptr,
			  hn, state.N_horizons);

	if (AH_info.AH_found)
	   then {
		BH_diagnostics(state.surface_integral_method,
			       verbose_info,
			       AH_info);

		if (verbose_info.print_physics_details)
		   then CCTK_VInfo(CCTK_THORNSTRING,
				   "AH found: A=%g m=%g at (%g,%g,%g)",
				   double(AH_info.area), double(AH_info.mass),
				   double(AH_info.centroid_x),
				   double(AH_info.centroid_y),
				   double(AH_info.centroid_z));
		}
	}

if (state.timer_handle >= 0)
   then {
	printf("timer stats for computation:\n");
	CCTK_TimerPrintDataI(state.timer_handle, -1);
	}
}

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

//
// This function finds (or more accurately tries to find) a single
// apparent horizon.
//
// Arguments:
// timer_handle = a valid Cactus timer handle if we want to time the
//		  apparent horizon process, or -ve to skip this
//		  (we only time the computation, not the file I/O)
// Jac_ptr = may be NULL if no Jacobian is needed (depending on  method)
// hn = the horizon number (used only in naming output files)
// N_horizons = the total number of horizon(s) being searched for number
//		(used only in formatting info messages)
//
// Results:
// This function returns true if the computation succeeds; false if it fails.
// If  method  specifies finding the (an) apparent horizon, then "success"
// means finding an h satisfying H(h) = 0 to within the error tolerances.
// Otherwise, "success" means successfully evaluating the horizon function
// and/or its Jacobian, as appropriate.
//
namespace {
bool find_horizon(enum method method,
		  const struct verbose_info& verbose_info, int timer_handle,
		  struct IO_info& IO_info,
		  struct Jacobian_info& Jac_info,
		  struct solver_info& solver_info,
		  struct cactus_grid_info& cgi, struct geometry_info& gi,
		  patch_system& ps, Jacobian* Jac_ptr,
		  int hn, int N_horizons)
{
switch	(method)
	{
// just evaluate the horizon function
case method__horizon_function:
	  {
	jtutil::norm<fp> H_norms;

	if (timer_handle >= 0)
	   then CCTK_TimerStartI(timer_handle);
	const bool status
		= horizon_function(ps, cgi, gi, false, true, &H_norms);
	if (timer_handle >= 0)
	   then CCTK_TimerStopI(timer_handle);
	if (!status)
	   then return false;				// *** ERROR RETURN ***

	if (H_norms.is_nonempty())	// might be empty iv H(h) eval failed
	   then CCTK_VInfo(CCTK_THORNSTRING,
			   "   H(h) rms-norm %.2e, infinity-norm %.2e",
			   H_norms.rms_norm(), H_norms.infinity_norm());
	output_gridfn(ps, gfns::gfn__H,
		      IO_info, IO_info.H_base_file_name,
		      hn, true);
	return true;					// *** NORMAL RETURN ***
	  }

// just compute/print the NP Jacobian
case method__Jacobian_test_NP_only:
	  {
	Jacobian& Jac_NP = *Jac_ptr;
	if (! horizon_function(ps, cgi, gi, true))
	   then return false;				// *** ERROR RETURN ***
	Jac_info.Jacobian_method = Jacobian_method__numerical_perturb;
	if (!  horizon_Jacobian(ps, Jac_NP,
				cgi, gi, Jac_info,
				true))
	   then return false;				// *** ERROR RETURN ***

	print_Jacobians(ps,
			& Jac_NP, NULL,
			IO_info, IO_info.Jacobian_base_file_name,
			hn, true);
	return true;					// *** NORMAL RETURN ***
	  }

// compute/print the Jacobian by all possible methods
case method__Jacobian_test:
	  {
	Jacobian& Jac_NP = *Jac_ptr;
	if (!  horizon_function(ps, cgi, gi, true))
	   then return false;				// *** ERROR RETURN ***
	Jac_info.Jacobian_method = Jacobian_method__numerical_perturb;
	if (! horizon_Jacobian(ps, Jac_NP,
			       cgi, gi, Jac_info,
			       true))
	   then return false;				// *** ERROR RETURN ***

	// symbolic differentiation with finite diff d/dr
	Jacobian& Jac_SD_FDdr
		= new_Jacobian(ps, Jac_info.Jacobian_storage_method);
	if (! horizon_function(ps, cgi, gi, true))
	   then return false;				// *** ERROR RETURN ***
	Jac_info.Jacobian_method = Jacobian_method__symbolic_diff_with_FD_dr;
	if (! horizon_Jacobian(ps, Jac_SD_FDdr,
			       cgi, gi, Jac_info,
			       true))
	   then return false;				// *** ERROR RETURN ***

	print_Jacobians(ps,
			& Jac_NP, & Jac_SD_FDdr,
			IO_info, IO_info.Jacobian_base_file_name,
			hn, true);
	return true;					// *** NORMAL RETURN ***
	  }

// find the apparent horizon via the Newton solver
case method__Newton_solve:
	  {
	Jacobian& Jac = *Jac_ptr;

	if (verbose_info.print_algorithm_highlights)
	   then CCTK_VInfo(CCTK_THORNSTRING,
			   "searching for horizon #%d/%d",
			   hn, N_horizons);

	if (timer_handle >= 0)
	   then CCTK_TimerStartI(timer_handle);
	const bool status
		= Newton_solve(ps, Jac,
			       cgi, gi,
			       Jac_info, solver_info, IO_info,
			       hn, verbose_info);
	if (timer_handle >= 0)
	   then CCTK_TimerStopI(timer_handle);
	if (! status)
	   then return false;				// *** ERROR RETURN ***

	output_gridfn(ps, gfns::gfn__h,
		      IO_info, IO_info.h_base_file_name,
		      hn, verbose_info.print_algorithm_details);
	output_gridfn(ps, gfns::gfn__H,
		      IO_info, IO_info.H_base_file_name,
		      hn, verbose_info.print_algorithm_details);
	return true;					// *** NORMAL RETURN ***
	  }

default:
	CCTK_VWarn(-1, __LINE__, __FILE__, CCTK_THORNSTRING,
"\n"
"   find_horizons(): unknown method=(int)%d!\n"
"                    (this should never happen!)"
,
		   int(method));				/*NOTREACHED*/
	}

/*NOTREACHED*/
}
	  }

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

//
// Given that an apparent horizon has been found, this function computes
// various BH diagnostics.
//
// Inputs (gridfns)
// h		# ghosted
// one		# nominal
// global_[xyz]	# nominal
//
namespace {
void BH_diagnostics(enum patch::integration_method surface_integral_method,
		    const struct verbose_info& verbose_info,
		    struct AH_info& AH_info)
{
const patch_system& ps = * AH_info.ps_ptr;

//
// compute raw surface integrals
//
fp integral_one = ps.integrate_gridfn(gfns::gfn__one,
				      true,	// area weighting
				      surface_integral_method);
fp integral_x = ps.integrate_gridfn(gfns::gfn__global_x,
				    true,		// area weighting
				    surface_integral_method);
fp integral_y = ps.integrate_gridfn(gfns::gfn__global_y,
				    true,		// area weighting
				    surface_integral_method);
fp integral_z = ps.integrate_gridfn(gfns::gfn__global_z,
				    true,		// area weighting
				    surface_integral_method);

//
// adjust integrals to take into account patch system not covering full sphere
//
switch	(ps.type())
	{
case patch_system::full_sphere_patch_system:
	break;
case patch_system::plus_z_hemisphere_patch_system:
	integral_one *= 2.0;
	integral_x *= 2.0;
	integral_y *= 2.0;
	integral_z = integral_one * ps.origin_z();
	break;
case patch_system::plus_xy_quadrant_patch_system:
	integral_one *= 4.0;
	integral_x = integral_one * ps.origin_x();
	integral_y = integral_one * ps.origin_y();
	integral_z *= 4.0;
	break;
case patch_system::plus_xz_quadrant_patch_system:
	integral_one *= 4.0;
	integral_x = integral_one * ps.origin_x();
	integral_y *= 4.0;
	integral_z = integral_one * ps.origin_z();
	break;
case patch_system::plus_xyz_octant_patch_system:
	integral_one *= 8.0;
	integral_x = integral_one * ps.origin_x();
	integral_y = integral_one * ps.origin_y();
	integral_z = integral_one * ps.origin_z();
	break;
default:
	CCTK_VWarn(-1, __LINE__, __FILE__, CCTK_THORNSTRING,
"\n"
"   BH_diagnostics(): unknown ps.type()=(int)%d!\n"
"                     (this should never happen!)"
,
		   int(ps.type()));				/*NOTREACHED*/
	}

AH_info.area = integral_one;
AH_info.mass = sqrt(AH_info.area / (16.0*PI));

AH_info.centroid_x = integral_x / integral_one;
AH_info.centroid_y = integral_y / integral_one;
AH_info.centroid_z = integral_z / integral_one;
}
	  }