aboutsummaryrefslogtreecommitdiff
path: root/src/elliptic/Jacobian.cc
blob: 4d3e411ea55b18c1c573b2fa93ddb54971190177 (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
// Jacobian.cc -- data structures for the Jacobian matrix
// $Id$

//
// Jacobian::Jacobian
//
// print_Jacobian
// print_Jacobians
//
// dense_Jacobian::dense_Jacobian
// dense_Jacobian::~dense_Jacobian
// dense_Jacobian::zero
// dense_Jacobian::zero_row
// dense_Jacobian::solve_linear_system
//

#include <stdio.h>
using std::fopen;
using std::printf;
using std::fprintf;
using std::fclose;
using std::FILE;
#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 "Jacobian.hh"
//#include "lapack.h"
//**************************************
/* lapack.h -- C/C++ prototypes for (some) LAPACK routines */
/* $Id$ */

/*
 * prerequisites:
 *	"cctk.h"
 *	"config.hh"	// for "integer" = Fortran integer
 */

#ifdef __cplusplus
extern "C" {
#endif

void CCTK_FCALL
  CCTK_FNAME(sgesv)(const integer* N, const integer* NRHS,
		    float A[], const int* LDA,
		    integer IPIV[],
		    float B[], const integer* LDB, integer* info);
void CCTK_FCALL
  CCTK_FNAME(dgesv)(const integer* N, const integer* NRHS,
		    double A[], const int* LDA,
		    integer IPIV[],
		    double B[], const integer* LDB, integer* info);

#ifdef __cplusplus
           };	/* extern "C" */
#endif
//**************************************

namespace {
void print_Jacobians_internal(const char file_name[],
			      const Jacobian& SD_Jac, const Jacobian& NP_Jac,
			      bool pair_flag);
	  };

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

//
// This function constructs a  Jacobian  object.
//
Jacobian::Jacobian(patch_system& ps)
	: ps_(ps),
	  NN_(ps.N_grid_points())
{ }

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

//
// This function prints a Jacobian matrix to a named output file.
//
void print_Jacobian(const char file_name[], const Jacobian& Jac)
{
print_Jacobians_internal(file_name, Jac, Jac, false);
}

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

//
// This function prints a pair of Jacobian matrices (and their difference)
// to a named output file.
//
void print_Jacobians(const char file_name[],
		     const Jacobian& SD_Jac, const Jacobian& NP_Jac)
{
print_Jacobians_internal(file_name, SD_Jac, NP_Jac, true);
}

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

//
// If pair_flag = false, this prints SD_Jac.
// If pair_flag = true, this prints both Jacobians, and the error in SD_Jac.
//
namespace {
void print_Jacobians_internal(const char file_name[],
			      const Jacobian& SD_Jac, const Jacobian& NP_Jac,
			      bool pair_flag)
{
const patch_system& ps = SD_Jac.my_patch_system();

FILE *fileptr = fopen(file_name, "w");
if (fileptr == NULL)
   then error_exit(ERROR_EXIT,
"***** dense_Jacobian::print(): can't open output file \"%s\"!",
		   file_name);					/*NOTREACHED*/

fprintf(fileptr, "# column 1 = x II\n");
fprintf(fileptr, "# column 2 = x patch number\n");
fprintf(fileptr, "# column 3 = x irho\n");
fprintf(fileptr, "# column 4 = x isigma\n");
fprintf(fileptr, "# column 5 = y JJ\n");
fprintf(fileptr, "# column 6 = y patch number\n");
fprintf(fileptr, "# column 7 = y irho\n");
fprintf(fileptr, "# column 8 = y isigma\n");
if (pair_flag)
   then {
	fprintf(fileptr, "# column 9 = SD_Jac(II,JJ)\n");
	fprintf(fileptr, "# column 10 = NP_Jac(II,JJ)\n");
	fprintf(fileptr, "# column 11 = abs error\n");
	fprintf(fileptr, "# column 12 = rel error\n");
	}
   else fprintf(fileptr, "# column 9 = Jac(II,JJ)\n");

	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);

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

		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);

		if (! SD_Jac.is_explicitly_stored(II,JJ))
		   then continue;			// skip sparse points

		const fp SD = SD_Jac(II,JJ);
		const fp NP = NP_Jac(II,JJ);
		const fp abs_error = SD - NP;

		if (pair_flag ? ((SD == 0.0) && (NP == 0.0))
			      : (SD == 0.0))
		   then continue;		// skip zero values (if == )

		const fp abs_SD = jtutil::abs(SD);
		const fp abs_NP = jtutil::abs(NP);
		const fp rel_error = abs_error / jtutil::max(abs_SD, abs_NP);

		fprintf(fileptr,
			"%d %d %d %d\t%d %d %d %d\t",
			II, xpn, x_irho, x_isigma,
			JJ, ypn, y_irho, y_isigma);
		if (pair_flag)
		   then fprintf(fileptr,
				"%.10g\t%.10g\t%e\t%e\n",
				double(SD), double(NP),
				double(abs_error), double(rel_error));
		   else fprintf(fileptr,
				"%.10g\n",
				double(SD));
		}
		}
		}
	}
	}
	}

fclose(fileptr);
}
	  };

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

//
// This function constructs a  dense_Jacobian  object.
//
dense_Jacobian::dense_Jacobian(patch_system& ps)
	: Jacobian(ps),
	  matrix_(0,NN()-1, 0,NN()-1),
	  pivot_(new integer[NN()])
{ }

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

//
// THis function destroys a  dense_Jacobian  object.
//
dense_Jacobian::~dense_Jacobian()
{
delete[] pivot_;
}

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

//
// This function zeros a  dense_Jacobian  object.
//
void dense_Jacobian::zero()
{
jtutil::array_zero(matrix_.N_array(), matrix_.data_array());
}

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

//
// This function zeros a single row of a  dense_Jacobian  object.
//
void dense_Jacobian::zero_row(int II)
{
	for (int JJ = 0 ; JJ < NN() ; ++JJ)
	{
	matrix_(JJ,II) = 0.0;
	}
}

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

//
// This function solves the linear system J.x = rhs, with rhs and x
// being nominal-grid gridfns.  The computation is done using the LAPACK
// [sd]gesv() subroutines; which modify the Jacobian matrix J in-place
// for the LU decomposition.
//
void dense_Jacobian::solve_linear_system(int rhs_gfn, int x_gfn)
{
const fp *rhs = my_patch_system().gridfn_data(rhs_gfn);
fp       *x   = my_patch_system().gridfn_data(x_gfn);

//
// [sd]gesv() use an "in out" design, where the same argument is used for
// both rhs and x ==> first copy rhs to x so we can pass that to [sd]gesv()
//
jtutil::array_copy(NN(), rhs, x);

integer N = NN();
integer NRHS = 1;
integer info;

#ifdef FP_IS_FLOAT
  CCTK_FNAME(sgesv)(&N, &NRHS, matrix_.data_array(), &N, pivot_, x, &N, &info);
#endif
#ifdef FP_IS_DOUBLE
  CCTK_FNAME(dgesv)(&N, &NRHS, matrix_.data_array(), &N, pivot_, x, &N, &info);
#endif

if (info != 0)
   then error_exit(ERROR_EXIT,
"\n"
"***** dense_Jacobian::solve_linear_system(rhs_gfn=%d, x_gfn=%d):\n"
"        error return info=%d from [sd]gesv() LAPACK routine!\n"
,
		   rhs_gfn, x_gfn,
		   int(info));					/*NOTREACHED*/
}