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 381
|
/* C implementations of various lineshape functions
*
* Copyright (C) 2002,2003 Jochen Kpper <jochen@jochen-kuepper.de>
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice,
* this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
* 3. The name of the author may not be used to endorse or promote products
* derived from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
* WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
* EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
* OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
* WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
* OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
* ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Python.h"
#include <math.h>
#include "libnumarray.h"
#define sqr(x) ((x)*(x))
/* These are apparently not defined in MSVC */
#if !defined(M_PI)
#define M_PI 3.14159265358979323846
#endif
#if !defined(M_LN2)
#define M_LN2 0.69314718055994530942
#endif
/*** C implementation ***/
static void gauss(size_t n, double *x, double *y, double w, double xc)
/* Evaluate normalized Gauss profile around xc with FWHM w at all x_i,
return in y. */
{
int i;
for(i=0; i<n; i++)
y[i] = 2. * sqrt(M_LN2/M_PI) / w * exp(-4.*M_LN2 * sqr((x[i]-xc)/w));
}
static void lorentz(size_t n, double *x, double *y, double w, double xc)
/* Evaluate normalized Lorentz profile around xc with FWHM w at all x_i,
return in y. */
{
int i;
for(i=0; i<n; i++)
y[i] = 2.*w/M_PI / (sqr(w*w) + 4.*(sqr(x[i]-xc)));
}
static double humlicek_v12(double x, double y)
/** Approximation of Voigt profile by Humlicek's 12-point formula.
*
* J. Humlicek, J. Quant. Spectrosc. Radiat. Transfer, 21(1978), 309.
*
* Voigt-Profil:
* V(x, y) = 2/pi^(1.5) * y^2/FWHM_L * \int[-inf,+inf](e^(-y^2)/(x+y)^2+...)
*/
{
static const double T_v12[6] = {
0.314240376254359, 0.947788391240164, 1.597682635152605,
2.27950708050106, 3.020637025120890, 3.889724897869782
};
static const double alpha_v12[6] = {
-1.393236997981977, -0.231152406188676, 0.155351465642094,
-6.21836623696556e-3, -9.190829861057113e-5, 6.275259577497896e-7
};
static const double beta_v12[6] = {
1.011728045548831, -0.751971469674635, 1.255772699323164e-2,
1.0022008145159e-2, -2.42068134815573e-4, 5.008480613664573e-7
};
static const double y0_v12 = 1.50;
double yp, xp, xm, sum, yp2, xp2, xm2;
int k;
sum = 0.;
yp = y + y0_v12;
yp2 = yp * yp;
if((y > 0.85) || (fabs(x) < (18.1 * y + 1.65))) {
/* Bereich I */
for(k=0; k<6; k++) {
xp = x + T_v12[k];
xm = x - T_v12[k];
sum += ((alpha_v12[k] * xm + beta_v12[k] * yp) / (xm * xm + yp2)
+ (beta_v12[k] * yp - alpha_v12[k] * xp) / (xp * xp + yp2));
}
} else {
/* Bereich II */
for(k=0; k<6; k++) {
xp = x + T_v12[k];
xp2 = xp * xp;
xm = x - T_v12[k];
xm2 = xm * xm;
sum += (((beta_v12[k] * (xm2 - y0_v12 * yp) - alpha_v12[k] * xm * (yp + y0_v12))
/ ((xm2 + yp2) * (xm2 + y0_v12 * y0_v12)))
+ ((beta_v12[k] * (xp2 - y0_v12 * yp) + alpha_v12[k] * xp * (yp + y0_v12))
/ ((xp2 + yp2) * (xp2 + y0_v12 * y0_v12))));
}
if(fabs(x) < 100.)
sum = y * sum + exp(-pow(x, 2));
else
sum *= y;
}
return sum;
}
static void voigt(size_t n, double *x, double *y, double w[2], double xc)
/* Evaluate normalized Voigt profile at x around xc with Gaussian
* linewidth contribution w[0] and Lorentzian linewidth
* contribution w[1].
*/
{
/* Transform into reduced coordinates and call Humlicek's 12 point
* formula:
* x = 2 \sqrt{\ln2} \frac{\nu-\nu_0}{\Delta\nu_G}
* y = \sqrt{\ln2} \frac{\Delta\nu_L}{\Delta\nu_G}
*/
int i;
double yh = sqrt(M_LN2) * w[1] / w[0];
for(i=0; i<n; i++) {
double xh = 2. * sqrt(M_LN2) * (x[i]-xc) / w[0];
y[i] = 2.*sqrt(M_LN2/M_PI)/w[0] * humlicek_v12(xh, yh);
}
}
/*** Python interface ***/
static PyObject *_Error;
static PyObject *
_lineshape_gauss(PyObject *self, PyObject *args, PyObject *keywds)
{
int f;
double w, xc = 0.0;
static char *kwlist[] = {"x", "w", "xc", "y", NULL};
PyObject *ox, *oy=Py_None;
PyArrayObject *x, *y;
if(! PyArg_ParseTupleAndKeywords(args, keywds, "Od|dO", kwlist,
&ox, &w, &xc, &oy))
return PyErr_Format(PyExc_RuntimeError, "gauss: invalid parameters");
if((f = PyFloat_Check(ox)) || PyInt_Check(ox)) {
/* scalar arguments -- always *return* Float result */
double xa[1], ya[1];
if(f)
xa[0] = PyFloat_AS_DOUBLE(ox);
else
xa[0] = (double)PyInt_AS_LONG(ox);
Py_BEGIN_ALLOW_THREADS;
gauss(1, xa, ya, w, xc);
Py_END_ALLOW_THREADS;
Py_DECREF(ox);
return PyFloat_FromDouble(ya[0]);
} else {
/* array conversion */
if(! ((x = NA_InputArray(ox, tFloat64, C_ARRAY))
&& (y = NA_OptionalOutputArray(oy, tFloat64, C_ARRAY, x))))
return 0;
if(x->nd != 1)
return PyErr_Format(_Error, "gauss: x must be scalar or 1d array.");
if (!NA_ShapeEqual(x, y))
return PyErr_Format(_Error, "gauss: x and y numarray must have same length.");
/* calculate profile */
{
double *xa = NA_OFFSETDATA(x);
double *ya = NA_OFFSETDATA(y);
Py_BEGIN_ALLOW_THREADS;
gauss(x->dimensions[0], xa, ya, w, xc);
Py_END_ALLOW_THREADS;
}
/* cleanup and return */
Py_XDECREF(x);
return NA_ReturnOutput(oy, y);
}
}
static PyObject *
_lineshape_lorentz(PyObject *self, PyObject *args, PyObject *keywds)
{
int f;
double w, xc = 0.0;
static char *kwlist[] = {"x", "w", "xc", "y", NULL};
PyObject *ox, *oy=Py_None;
PyArrayObject *x, *y;
if(! PyArg_ParseTupleAndKeywords(args, keywds, "Od|dO", kwlist,
&ox, &w, &xc, &oy))
return PyErr_Format(PyExc_RuntimeError, "lorentz: invalid parameters");
if((f = PyFloat_Check(ox)) || PyInt_Check(ox)) {
/* scalar arguments -- always *return* Float result */
double xa[1], ya[1];
if(f)
xa[0] = PyFloat_AS_DOUBLE(ox);
else
xa[0] = (double)PyInt_AS_LONG(ox);
Py_BEGIN_ALLOW_THREADS;
lorentz(1, xa, ya, w, xc);
Py_END_ALLOW_THREADS;
Py_DECREF(ox);
return PyFloat_FromDouble(ya[0]);
} else {
/* array conversion */
if(! ((x = NA_InputArray(ox, tFloat64, C_ARRAY))
&& (y = NA_OptionalOutputArray(oy, tFloat64, C_ARRAY, x))))
return 0;
if(x->nd != 1)
return PyErr_Format(_Error, "lorentz: x must be scalar or 1d array.");
if (!NA_ShapeEqual(x, y))
return PyErr_Format(_Error, "lorentz: x and y numarray must have same length.");
/* calculate profile */
{
double *xa = NA_OFFSETDATA(x);
double *ya = NA_OFFSETDATA(y);
Py_BEGIN_ALLOW_THREADS;
lorentz(x->dimensions[0], xa, ya, w, xc);
Py_END_ALLOW_THREADS;
}
/* cleanup and return */
Py_XDECREF(x);
return NA_ReturnOutput(oy, y);
}
}
static PyObject *
_lineshape_voigt(PyObject *self, PyObject *args, PyObject *keywds)
{
int f;
double w[2], xc = 0.0;
static char *kwlist[] = {"x", "w", "xc", "y", NULL};
PyObject *wt, *ox, *oy=Py_None;
PyArrayObject *x, *y;
if(! PyArg_ParseTupleAndKeywords(args, keywds, "OO|dO", kwlist,
&ox, &wt, &xc, &oy))
return PyErr_Format(PyExc_RuntimeError, "voigt: invalid parameters");
/* parse linewidths tuple */
if(! PyArg_ParseTuple(wt, "dd", &(w[0]), &(w[1])))
return(0);
if((f = PyFloat_Check(ox)) || PyInt_Check(ox)) {
/* scalar arguments -- always *return* Float result */
double xa[1], ya[1];
if(f)
xa[0] = PyFloat_AS_DOUBLE(ox);
else
xa[0] = (double)PyInt_AS_LONG(ox);
Py_BEGIN_ALLOW_THREADS;
voigt(1, xa, ya, w, xc);
Py_END_ALLOW_THREADS;
Py_DECREF(ox);
return PyFloat_FromDouble(ya[0]);
} else {
/* array conversion */
if(! ((x = NA_InputArray(ox, tFloat64, C_ARRAY))
&& (y = NA_OptionalOutputArray(oy, tFloat64, C_ARRAY, x))))
return 0;
if(x->nd != 1)
return PyErr_Format(_Error, "voigt: x must be scalar or 1d array.");
if (!NA_ShapeEqual(x, y))
return PyErr_Format(_Error, "voigt: x and y numarray must have same length.");
/* calculate profile */
{
double *xa = NA_OFFSETDATA(x);
double *ya = NA_OFFSETDATA(y);
Py_BEGIN_ALLOW_THREADS;
voigt(x->dimensions[0], xa, ya, w, xc);
Py_END_ALLOW_THREADS;
}
/* cleanup and return */
Py_XDECREF(x);
return NA_ReturnOutput(oy, y);
}
}
/*** table of methods ***/
static PyMethodDef _lineshape_Methods[] = {
{"gauss", (PyCFunction)_lineshape_gauss, METH_VARARGS|METH_KEYWORDS,
"gauss(x, w, xc=0.0, y=None)\n\n"
"Gaussian lineshape function\n\n" \
"Calculate normalized Gaussian with full-width at half maximum |w| at |x|,\n" \
"optionally specifying the line-center |xc|.\n" \
"If, and only if |x| is an array an optional output array |y| can be\n" \
"specified. In this case |x| and |y| must be one-dimensional numarray\n" \
"with identical shapes.\n\n" \
"If |x| is an scalar the routine always gives the result as scalar\n" \
"return value."
},
{"lorentz", (PyCFunction)_lineshape_lorentz, METH_VARARGS|METH_KEYWORDS,
"lorentz(x, w, xc=0.0, y=None)\n\n"
"Lorentzian lineshape function\n\n" \
"Calculate normalized Lorentzian with full-width at half maximum |w| at |x|,\n" \
"optionally specifying the line-center |xc|.\n" \
"If, and only if |x| is an array an optional output array |y| can be\n" \
"specified. In this case |x| and |y| must be one-dimensional numarray\n" \
"with identical shapes.\n\n" \
"If |x| is an scalar the routine always gives the result as scalar\n" \
"return value."
},
{"voigt", (PyCFunction)_lineshape_voigt, METH_VARARGS|METH_KEYWORDS,
"voigt(x, w, xc=0.0, y=None)\n\n"
"Voigt-lineshape function\n\n" \
"Calculate normalized Voigt-profile with Gaussian full-width at half maximum |w[0]| and\n" \
"Lorentzian full-width at half maximum |w[1]| at |x|, optionally specifying the line-center\n" \
"|xc|.\n" \
"If, and only if |x| is an array an optional output array |y| can be\n" \
"specified. In this case |x| and |y| must be one-dimensional numarray\n" \
"with identical shapes.\n\n" \
"If |x| is an scalar the routine always gives the result as scalar\n" \
"return value.\n\n" \
"This function uses Humlicek's 12-point formula to approximate the Voigt\n" \
"profile (J. Humlicek, J. Quant. Spectrosc. Radiat. Transfer, 21, 309 (1978))."
},
{NULL, NULL, 0, ""}
};
/*** module initialization ***/
DL_EXPORT(void) init_lineshape(void)
{
PyObject *m, *d;
m = Py_InitModule("_lineshape", _lineshape_Methods);
d = PyModule_GetDict(m);
_Error = PyErr_NewException("_lineshape.error", NULL, NULL);
PyDict_SetItemString(d, "error", _Error);
import_libnumarray();
}
/*
* Local Variables:
* mode: c
* c-file-style: "Stroustrup"
* fill-column: 80
* End:
*/
|