aboutsummaryrefslogtreecommitdiff
path: root/src/gr/horizon_Jacobian.cc
blob: 99d8d44fe5a72a1717f27e1ccdaf266d2e80caf6 (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
// horizon_Jacobian.cc -- evaluate Jacobian matrix of LHS function H(h)
// $Id$
//
// <<<prototypes for functions local to this file>>>
// create_Jacobian - create a Jacobian matrix of the appropriate type
//
// horizon_Jacobian - compute the Jacobian (symbolic differentiation)
/// add_ghost_zone_Jacobian - add ghost zone dependencies to Jacobian
//
// horizon_Jacobian_NP - compute the Jacobian by numerical perturbation

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

#include "util_Table.h"
#include "cctk.h"
#include "cctk_Arguments.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 "gfn.hh"
#include "AHFinderDirect.hh"

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

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

namespace {
void add_ghost_zone_Jacobian(const patch_system& ps,
			     const patch& xp, const ghost_zone& xmgz,
			     int x_II,
			     int xm_irho, int xm_isigma,
			     fp mol, Jacobian& Jac);
	  }

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

//
// This function decodes the  Jacobian_type  parameter and creates the
// appropriate type of Jacobian matrix object.
//
// FIXME: the patch system shouldn't really have to be non-const, but
//	  the Jacobian constructors all require this to allow the
//	  linear solvers to directly update gridfns
//
Jacobian& create_Jacobian(patch_system& ps,
			  const char Jacobian_type[])
{
if	(STRING_EQUAL(Jacobian_type, "dense matrix"))
   then return *new dense_Jacobian(ps);
else	CCTK_VWarn(-1, __LINE__, __FILE__, CCTK_THORNSTRING,
		   "unknown Jacobian_type=\"%s\"!",
		   Jacobian_type);				/*NOTREACHED*/
}

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

//
// This function computes the Jacobian matrix of the LHS function H(h)
// by symbolic differentiation from the Jacobian coefficient (angular)
// gridfns.  The computation is done a Jacobian row at a time, using
// equation (25) of my 1996 apparent horizon finding paper, except that
// the d/dr term is done by numerical finite differencing.
//
// Inputs (angular gridfns, on ghosted grid):
//	h				# shape of trial surface
//	H				# H(h) assumed to already be computed
//	partial_H_wrt_partial_d_h	# Jacobian coefficients
//	partial_H_wrt_partial_dd_h
//
// Outputs:
//	The Jacobian matrix is stored in the Jacobian object Jac.
//
void horizon_Jacobian_SD(patch_system& ps,
			 Jacobian& Jac)
{
CCTK_VInfo(CCTK_THORNSTRING, "   horizon Jacobian_SD");

ps.compute_synchronize_Jacobian();
Jac.zero();

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

	for (int x_irho = xp.min_irho() ; x_irho <= xp.max_irho() ; ++x_irho)
	{
	for (int x_isigma = xp.min_isigma() ;
	x_isigma <= xp.max_isigma() ;
	++x_isigma)
	{
	//
	// compute the Jacobian row for this grid point, i.e.
	//	  partial H(this point x, Jacobian row II)
	//	---------------------------------------------
	//	partial h(other points y, Jacobian column JJ)
	//
	// FIXME FIXME: we still have to take into account the
	//		position-dependence of the coefficients,
	//		cf the difference between J[3H(h)] and J[2H(h)];
	//		here we're sort of computing the former, but
	//		Newton's method really wants the latter
	//

	// Jacobian row index
	const int II = ps.gpn_of_patch_irho_isigma(xp, x_irho, x_isigma);

	// Jacobian coefficients for this point
	const fp Jacobian_coeff_rho
	   = xp.gridfn(nominal_gfns::gfn__partial_H_wrt_partial_d_h_1,
		       x_irho, x_isigma);
	const fp Jacobian_coeff_sigma
	   = xp.gridfn(nominal_gfns::gfn__partial_H_wrt_partial_d_h_2,
		       x_irho, x_isigma);
	const fp Jacobian_coeff_rho_rho
	   = xp.gridfn(nominal_gfns::gfn__partial_H_wrt_partial_dd_h_11,
		       x_irho, x_isigma);
	const fp Jacobian_coeff_rho_sigma
	   = xp.gridfn(nominal_gfns::gfn__partial_H_wrt_partial_dd_h_12,
		       x_irho, x_isigma);
	const fp Jacobian_coeff_sigma_sigma
	   = xp.gridfn(nominal_gfns::gfn__partial_H_wrt_partial_dd_h_22,
		       x_irho, x_isigma);

	// partial_rho, partial_rho_rho
	    for (int m_irho = xp.molecule_min_m() ;
		 m_irho <= xp.molecule_max_m() ;
		 ++m_irho)
	    {
	    const int xm_irho = x_irho + m_irho;
	    const fp Jac_rho     = Jacobian_coeff_rho
				   * xp.partial_rho_coeff(m_irho);
	    const fp Jac_rho_rho = Jacobian_coeff_rho_rho
				   * xp.partial_rho_rho_coeff(m_irho);
	    const fp Jac_sum = Jac_rho + Jac_rho_rho;
	    if (xp.is_in_nominal_grid(xm_irho, x_isigma))
	       then Jac(II, xp,xm_irho,x_isigma) += Jac_sum;
	       else add_ghost_zone_Jacobian
			(ps, xp, xp.minmax_rho_ghost_zone(m_irho < 0),
			 II, xm_irho, x_isigma,
			 Jac_sum, Jac);
	    }

	// partial_sigma, partial_sigma_sigma
	    for (int m_isigma = xp.molecule_min_m() ;
		 m_isigma <= xp.molecule_max_m() ;
		 ++m_isigma)
	    {
	    const int xm_isigma = x_isigma + m_isigma;
	    const fp Jac_sigma       = Jacobian_coeff_sigma
				       * xp.partial_sigma_coeff(m_isigma);
	    const fp Jac_sigma_sigma = Jacobian_coeff_sigma_sigma
				       * xp.partial_sigma_sigma_coeff(m_isigma);
	    const fp Jac_sum = Jac_sigma + Jac_sigma_sigma;
	    if (xp.is_in_nominal_grid(x_irho, xm_isigma))
	       then Jac(II, xp,x_irho,xm_isigma) += Jac_sum;
	       else add_ghost_zone_Jacobian
			(ps, xp, xp.minmax_sigma_ghost_zone(m_isigma < 0),
			 II, x_irho, xm_isigma,
			 Jac_sum, Jac);
	    }

	// partial_rho_sigma
	    for (int m_irho = xp.molecule_min_m() ;
		 m_irho <= xp.molecule_max_m() ;
		 ++m_irho)
	    {
	    for (int m_isigma = xp.molecule_min_m() ;
		 m_isigma <= xp.molecule_max_m() ;
		 ++m_isigma)
	    {
	    const int xm_irho   = x_irho   + m_irho;
	    const int xm_isigma = x_isigma + m_isigma;
	    const fp Jac_rho_sigma
	       = Jacobian_coeff_rho_sigma
		 * xp.partial_rho_sigma_coeff(m_irho, m_isigma);
	    if (xp.is_in_nominal_grid(xm_irho, xm_isigma))
	       then Jac(II, xp,xm_irho,xm_isigma) += Jac_rho_sigma;
	       else add_ghost_zone_Jacobian
			(ps, xp,
		 xp.corner_ghost_zone_containing_point(m_irho < 0, m_isigma < 0,
						       xm_irho, xm_isigma),
			 II, xm_irho, xm_isigma,
			 Jac_rho_sigma, Jac);
	    }
	    }

	}
	}
    }

// compute d/dr term by numerical finite differencing
perturb_h(Jacobian_info.perturbation_amplitude);
horizon_function(ps, cgi, gii, false, NULL, false);
}

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

//
// This function adds the ghost-zone Jacobian dependency contributions
// for a single ghost-zone point, to a Jacobian matrix.
//
// Arguments:
// ps = The patch system.
// xp = The patch containing the center point of the molecule.
// xmgz = If the x+m point is in a ghost zone, this must be that ghost zone.
//	  If the x+m point is not in a ghost zone, this argument is ignored.
// x_II = The Jacobian row of the x point.
// xm_(irho,isigma) = The coordinates (in xp) of the x+m point of the molecule.
// mol = The molecule coefficient.
// Jac = The Jacobian matrix.
//
namespace {
void add_ghost_zone_Jacobian(const patch_system& ps,
			     const patch& xp, const ghost_zone& xmgz,
			     int x_II,
			     int xm_irho, int xm_isigma,
			     fp mol, Jacobian& Jac)
{
const patch_edge& xme = xmgz.my_edge();
const int xm_iperp = xme.iperp_of_irho_isigma(xm_irho, xm_isigma);
const int xm_ipar  = xme. ipar_of_irho_isigma(xm_irho, xm_isigma);

// on what other points ym does this molecule point xm depend
// via the patch_system::synchronize() operation?
const patch&      ymp = ps.synchronize_Jacobian_y_patch(xmgz);
const patch_edge& yme = ps.synchronize_Jacobian_y_edge (xmgz);
const int min_ym_ipar_m = ps.synchronize_Jacobian_min_y_ipar_m(xmgz);
const int max_ym_ipar_m = ps.synchronize_Jacobian_max_y_ipar_m(xmgz);
const int ym_iperp = ps.synchronize_Jacobian_y_iperp(xmgz, xm_iperp);
const int ym_ipar_posn = ps.synchronize_Jacobian_y_ipar_posn
				(xmgz, xm_iperp, xm_ipar);

// add the Jacobian contributions from the ym points
	for (int ym_ipar_m = min_ym_ipar_m ;
	     ym_ipar_m <= max_ym_ipar_m ;
	     ++ym_ipar_m)
	{
	const int ym_ipar = ym_ipar_posn + ym_ipar_m;
	const int ym_irho   = yme.  irho_of_iperp_ipar(ym_iperp,ym_ipar);
	const int ym_isigma = yme.isigma_of_iperp_ipar(ym_iperp,ym_ipar);
	const int JJ = ps.gpn_of_patch_irho_isigma(ymp, ym_irho, ym_isigma);
	const fp sync_Jac = ps.synchronize_Jacobian(xmgz, xm_iperp, xm_ipar,
						    ym_ipar_m);
	Jac(x_II,JJ) += mol*sync_Jac;
	}
}
	  }

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

//
// This function computes the Jacobian matrix of the LHS function H(h)
// by numerical perturbation of the H(h) function.  The algorithm is as
// follows:
//
// evaluate H(h)
// array-copy H(h) to scratch array save_H
//	for each point (y,JJ)
//	{
//	const fp save_h_y = h at y;
//	h at y += perturbation_amplitude;
//	evaluate H(h)
//		for each point (x,II)
//		{
//		Jac(II,JJ) = (H(II) - save_H(II)) / perturbation_amplitude;
//		}
//	h at y = save_h_y;
//	}
// array-copy save_H back to H(h)
//
void horizon_Jacobian_NP(patch_system& ps,
			 const struct cactus_grid_info& cgi,
			 const struct geometry_interpolator_info& ii,
			 Jacobian& Jac,
			 fp perturbation_amplitude)
{
CCTK_VInfo(CCTK_THORNSTRING, "   horizon Jacobian_NP");

// evaluate H(h)
jtutil::norm<fp> H_norms;
horizon_function(ps, cgi, ii, false, H_norms);

// array-copy H(h) to scratch array save_H
fp *save_H = new fp[Jac.NN()];
jtutil::array_copy(Jac.NN(), ps.gridfn_data(nominal_gfns::gfn__H), save_H);

	for (int ypn = 0 ; ypn < ps.N_patches() ; ++ypn)
	{
	patch& yp = ps.ith_patch(ypn);
	CCTK_VInfo(CCTK_THORNSTRING, "      perturbing in %s patch", yp.name());

	for (int y_irho = yp.min_irho() ; y_irho <= yp.max_irho() ; ++y_irho)
	{
	for (int y_isigma = yp.min_isigma() ;
	     y_isigma <= yp.max_isigma() ;
	     ++y_isigma)
	{
	const int JJ = ps.gpn_of_patch_irho_isigma(yp, y_irho,y_isigma);

	const fp save_h_y = yp.ghosted_gridfn(ghosted_gfns::gfn__h,
					      y_irho,y_isigma);
	yp.ghosted_gridfn(ghosted_gfns::gfn__h, y_irho,y_isigma)
		+= perturbation_amplitude;
	H_norms.reset();
	horizon_function(ps, cgi, ii, false, H_norms, false);
		for (int xpn = 0 ; xpn < ps.N_patches() ; ++xpn)
		{
		patch& xp = ps.ith_patch(xpn);

		for (int x_irho = xp.min_irho() ;
		     x_irho <= xp.max_irho() ;
		     ++x_irho)
		{
		for (int x_isigma = xp.min_isigma() ;
		     x_isigma <= xp.max_isigma() ;
		     ++x_isigma)
		{
		const int II = ps.gpn_of_patch_irho_isigma(xp, x_irho,x_isigma);
		const fp new_xH = xp.gridfn(nominal_gfns::gfn__H,
					    x_irho,x_isigma);
		Jac(II,JJ) = (new_xH - save_H[II]) / perturbation_amplitude;
		}
		}
		}
	yp.ghosted_gridfn(ghosted_gfns::gfn__h, y_irho,y_isigma)
		= save_h_y;
	}
	}
   	} 

jtutil::array_copy(Jac.NN(), save_H, ps.gridfn_data(nominal_gfns::gfn__H));
delete[] save_H;
}