-
Notifications
You must be signed in to change notification settings - Fork 2
/
monopole.cpp
380 lines (243 loc) · 9.65 KB
/
monopole.cpp
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
//
// @(#)monopole.cpp 1.1 misha1-09oct108
//
// Copyright (c) 2008 of Mikhail Tchernychev
// All rights reserved.
//
// Created: 09oct108 by misha
// Version: 09oct108 by misha
//
#include "monopole.h"
#include "math.h"
/* ----------------------- computional functions -----------------------------*/
#define R(x,y,z) sqrt((x)*(x)+(y)*(y)+(z)*(z))
// Center of monopole is origin of local coordinat system
double Vm_z(double x, double y, double z) {
double r;
r=sqrt(x*x+y*y+z*z);
return -z/(r*r*r);
}
double Vm_x(double x, double y, double z) {
double r;
r=sqrt(x*x+y*y+z*z);
return -x/(r*r*r);
}
double T_mono(double y, double x, double z,
double y0, double x0, double z0,
double A, double B, double C) {
double X, Y, Z, deltaT;
x=x-x0; y=y-y0; z=z-z0;
X = Vm_x(x,y,z);
Y = Vm_x(y,x,z);
Z = Vm_z(x,y,z);
deltaT=(X*A+Y*B+Z*C);
return(deltaT);
}
int dTmono_d(double y, double x, double z,
double y0, double x0, double z0, double M,
double A, double B, double C,
double *dTdx, double *dTdy, double *dTdz) {
double Xx, Xy, Xz, Yx, Yy, Yz, Zx, Zy, Zz;
x=x-x0; y=y-y0; z=z-z0;
double r = sqrt(x*x+y*y+z*z);
double r3 = r*r*r;
double r5 = r3*r*r;
double r1 = 1/r3;
Xx = -(3*x*x/r5 - r1); Xy = -(3*x*y)/r5; Xz = -(3*x*z)/r5;
Yx = Xy; Yy = -(3*y*y/r5 - r1); Yz = -(3*y*z)/r5;
Zx = Xz; Zy = Yz; Zz = -(3*z*z/r5 - r1);
*dTdx = M*(Xx*A+ Yx*B+ Zx*C);
*dTdy = M*(Xy*A+ Yy*B+ Zy*C);
*dTdz = M*(Xz*A+ Yz*B+ Zz*C);
return 1;
}
CMagMonopole::CMagMonopole() : CMagObject(0, 1,3, "CMagMonopole", "monopole") {
m_iObjectLinearAbilities = 2;
m_iObjectNonLinearAbilities = 2;
}
CMagMonopole::~CMagMonopole() {
}
double CMagMonopole::ComputeField(FIELDTYPE type, double * add_params, double x_obs, double y_obs, double z_obs,
double x0, double y0, double z0,
double A, double B, double C,
int data_pos) {
if(!m_bIsValid) return 0.;
if(m_iFieldType != anyfield && m_iFieldType != type) return 0;
x_obs += add_params[X_OFFSET];
y_obs += add_params[Y_OFFSET];
z_obs += add_params[Z_OFFSET];
switch(type) {
case totalmagfield:
return ComputeTotalField(add_params, x_obs, y_obs, z_obs, x0, y0, z0, A, B, C);
/* case totalmaggradient:
return ComputeGradient(add_params, x_obs, y_obs, z_obs, x0, y0, z0, A, B, C);
case totalfinitemaggrad:
return ComputeFiniteGradient(add_params, x_obs, y_obs, z_obs, x0, y0, z0, A, B, C);
case absmaggradient:
return ComputeAbsGradient(add_params, x_obs, y_obs, z_obs, x0, y0, z0, A, B, C);
case absfinitegradient:
return ComputeAbsFiniteGradient(add_params, x_obs, y_obs, z_obs, x0, y0, z0, A, B, C);
*/
default:
return CMagObject::ComputeField(type, add_params, x_obs, y_obs, z_obs, x0, y0, z0, A, B, C, data_pos);
}
return 0.;
}
double CMagMonopole::ComputeTotalField(double * add_params, double x_obs, double y_obs, double z_obs,
double x0, double y0, double z0,
double A, double B, double C) {
return m_lfJ[0]*T_mono(x_obs,y_obs,z_obs, m_lfPos[0]-x0, m_lfPos[1]-y0, m_lfPos[2]-z0, A, B, C);
}
int CMagMonopole::GetLinearDerivatives(FIELDTYPE type, double * add_params, double * deriv, int size,
double * pos_deriv,
double x_obs, double y_obs, double z_obs,
double x0, double y0, double z0,
double A, double B, double C,
int data_pos, int *n_params, int * start) {
if(!m_bIsValid) return 0;
if(m_iFieldType != anyfield && m_iFieldType != type) {
deriv[0] = 0.;
return 0;
}
x_obs += add_params[X_OFFSET];
y_obs += add_params[Y_OFFSET];
z_obs += add_params[Z_OFFSET];
switch(type) {
case totalmagfield:
return GetTotalFieldLinearDerivatives(add_params, deriv, size, pos_deriv,
x_obs, y_obs, z_obs, x0, y0, z0, A, B, C, n_params, start);
/* case totalmaggradient:
return GetGradientLinearDerivatives(add_params, deriv, size, pos_deriv,
x_obs, y_obs, z_obs, x0, y0, z0, A, B, C, n_params, start);
case totalfinitemaggrad:
return GetFiniteGradientLinearDerivatives(add_params, deriv, size, pos_deriv,
x_obs, y_obs, z_obs, x0, y0, z0, A, B, C, n_params, start);
case absmaggradient:
return GetAbsGradientLinearDerivatives(add_params, deriv, size, pos_deriv,
x_obs, y_obs, z_obs, x0, y0, z0, A, B, C, n_params, start);
case absfinitegradient:
return GetAbsFiniteGradientLinearDerivatives(add_params, deriv, size, pos_deriv,
x_obs, y_obs, z_obs, x0, y0, z0, A, B, C, n_params, start);
*/
default:
return CMagObject::GetLinearDerivatives(type, add_params, deriv, size, pos_deriv,
x_obs, y_obs, z_obs, x0, y0, z0, A, B, C,
data_pos, n_params, start);
}
return 0;
}
int CMagMonopole::GetTotalFieldLinearDerivatives(double * add_params, double * deriv, int size,
double * pos_deriv,
double x_obs, double y_obs, double z_obs,
double x0, double y0, double z0,
double A, double B, double C,
int *n_params, int * start) {
if(start) *start = m_iLinearStart;
*n_params = 0;
if(size < 1) return 0;
if(m_pJfixed[0]) return 0;
deriv[0] = T_mono(x_obs,y_obs,z_obs, m_lfPos[0]-x0, m_lfPos[1]-y0, m_lfPos[2]-z0, A, B, C);
*n_params = 1;
return 1;
}
int CMagMonopole::GetNonLinearDerivatives(FIELDTYPE type, double * add_params, double * deriv, int size,
double * pos_deriv,
double x_obs, double y_obs, double z_obs,
double x0, double y0, double z0,
double A, double B, double C,
int data_pos, int * n_params, int * start) {
if(!m_bIsValid) return 0;
if(m_iFieldType != anyfield && m_iFieldType != type) {
deriv[0] = deriv[1] = deriv[2] = 0.;
return 0;
}
x_obs += add_params[X_OFFSET];
y_obs += add_params[Y_OFFSET];
z_obs += add_params[Z_OFFSET];
switch(type) {
case totalmagfield:
return GetTotalFieldNonLinearDerivatives(add_params, deriv, size, pos_deriv,
x_obs, y_obs, z_obs, x0, y0, z0, A, B, C, n_params, start);
/*
case totalmaggradient:
return GetGradientNonLinearDerivatives(add_params, deriv, size, pos_deriv,
x_obs, y_obs, z_obs, x0, y0, z0, A, B, C, n_params, start);
case totalfinitemaggrad:
return GetFiniteGradientNonLinearDerivatives(add_params, deriv, size, pos_deriv,
x_obs, y_obs, z_obs, x0, y0, z0, A, B, C, n_params, start);
case absmaggradient:
return GetAbsGradientNonLinearDerivatives(add_params, deriv, size, pos_deriv,
x_obs, y_obs, z_obs, x0, y0, z0, A, B, C, n_params, start);
case absfinitegradient:
return GetAbsFiniteGradientNonLinearDerivatives(add_params, deriv, size, pos_deriv,
x_obs, y_obs, z_obs, x0, y0, z0, A, B, C, n_params, start);
*/
default:
return CMagObject::GetNonLinearDerivatives(type, add_params, deriv, size, pos_deriv,
x_obs, y_obs, z_obs, x0, y0, z0, A, B, C,
data_pos, n_params, start);
}
return 0;
}
int CMagMonopole::GetTotalFieldNonLinearDerivatives(double * add_params, double * deriv, int size,
double * pos_deriv,
double x_obs, double y_obs, double z_obs,
double x0, double y0, double z0,
double A, double B, double C,
int * n_params, int * start) {
// non-linear parameters NOTE that d/dx and d/dy swapped!
int i, j;
double deriv_tmp[3];
if(!m_bIsValid || size < 3) return 0;
if(start) *start = m_iNonLinearStart;
*n_params = 0;
dTmono_d(x_obs,y_obs,z_obs, m_lfPos[0]-x0, m_lfPos[1]-y0, m_lfPos[2]-z0, m_lfJ[0], A, B, C,
&deriv_tmp[1], &deriv_tmp[0], &deriv_tmp[2]);
for(i=0; i<3; i++) pos_deriv[i] = deriv_tmp[i];
j=0;
for(i=0; i<3; i++) {
if(!m_pPosfixed[i]) {
deriv[j] = deriv_tmp[i];
j++;
(*n_params)++;
}
}
return 1;
}
void CMagMonopole::DumpParameters(FILE * dat){
CMagObject::DumpParameters(dat);
fprintf(dat,"Position:\n");
if(m_pPosfixed[0])
fprintf(dat," X: %.3lf FIXED\n", m_lfPos[0]);
else
fprintf(dat," X: %.3lf +/- %lg\n", m_lfPos[0], m_lfStdDevPos[0]);
if(m_pPosfixed[1])
fprintf(dat," Y: %.3lf FIXED\n", m_lfPos[1]);
else
fprintf(dat," Y: %.3lf +/- %lg\n", m_lfPos[1], m_lfStdDevPos[1]);
if(m_pPosfixed[2])
fprintf(dat," Z: %.3lf FIXED\n", m_lfPos[2]);
else
fprintf(dat," Z: %.3lf +/- %lg\n", m_lfPos[2], m_lfStdDevPos[2]);
fprintf(dat,"Monopole Ampitude:\n");
if(m_pJfixed[0])
fprintf(dat," M: %lg FIXED\n", m_lfJ[0]);
else
fprintf(dat," M: %lg +/- %lg\n", m_lfJ[0], m_lfStdDevJ[0]);
}
void CMagMonopole::DumpToXMLStream(std::ostream & str) {
char delimit = ' ';
char quot = '\"';
str << "<" << m_sObjectType << ">" << std::endl;
str << "<params ";
str << "X=" << quot << m_lfPos[0] << quot << delimit;
str << "Y=" << quot << m_lfPos[1] << quot << delimit;
str << "Z=" << quot << m_lfPos[2] << quot << delimit;
str << "X_std=" << quot << m_lfStdDevPos[0] << quot << delimit;
str << "Y_std=" << quot << m_lfStdDevPos[1] << quot << delimit;
str << "Z_std=" << quot << m_lfStdDevPos[2] << quot << delimit;
str << "Jtotal=" << quot << m_lfJ[0] << quot << delimit;
str << "Jtotal_std=" << quot << m_lfStdDevJ[0] << quot << delimit;
str << "/>" << std::endl;
str << "</" << m_sObjectType << ">" << std::endl;
}