avtM3DC1Field.C 18.5 KB
Newer Older
allens's avatar
allens committed
1 2
 /*****************************************************************************
*
brugger's avatar
 
brugger committed
3
* Copyright (c) 2000 - 2010, Lawrence Livermore National Security, LLC
allens's avatar
allens committed
4
* Produced at the Lawrence Livermore National Laboratory
brugger's avatar
 
brugger committed
5
* LLNL-CODE-442911
allens's avatar
allens committed
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
* All rights reserved.
*
* This file is  part of VisIt. For  details, see https://visit.llnl.gov/.  The
* full copyright notice is contained in the file COPYRIGHT located at the root
* of the VisIt distribution or at http://www.llnl.gov/visit/copyright.html.
*
* Redistribution  and  use  in  source  and  binary  forms,  with  or  without
* modification, are permitted provided that the following conditions are met:
*
*  - Redistributions of  source code must  retain the above  copyright notice,
*    this list of conditions and the disclaimer below.
*  - Redistributions in binary form must reproduce the above copyright notice,
*    this  list of  conditions  and  the  disclaimer (as noted below)  in  the
*    documentation and/or other materials provided with the distribution.
*  - Neither the name of  the LLNS/LLNL nor the names of  its contributors may
*    be used to endorse or promote products derived from this software without
*    specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT  HOLDERS AND CONTRIBUTORS "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 LAWRENCE  LIVERMORE NATIONAL  SECURITY,
* LLC, THE  U.S.  DEPARTMENT OF  ENERGY  OR  CONTRIBUTORS 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.
*
*****************************************************************************/

// ************************************************************************* //
//                             avtM3DC1Field.C                            //
// ************************************************************************* //

#include "avtM3DC1Field.h"

#include <DebugStream.h>

#include <vtkCellData.h>
#include <vtkIntArray.h>
#include <vtkFloatArray.h>

#define ELEMENT_SIZE 7
#define SCALAR_SIZE 20

// ****************************************************************************
//  Method: avtM3DC1Field constructor
//
//  Creationist: Allen Sanderson
//  Creation:   20 November 2009
//
// ****************************************************************************

avtM3DC1Field::avtM3DC1Field( float *elementsPtr, int nelements ) 
  : elements( elementsPtr), neighbors(0),
    psi0(0), f0(0), psinr(0), psini(0), fnr(0), fni(0), nelms(nelements)
{
  findElementNeighbors();
}


// ****************************************************************************
//  Method: avtM3DC1Field destructor
//
//  Creationist: Allen Sanderson
//  Creation:   20 November 2009
//
// ****************************************************************************

avtM3DC1Field::~avtM3DC1Field()
{
  if( neighbors ) free(neighbors);
  if( trigtable ) free(trigtable);

  if( elements ) delete [] elements;
  if( psi0 )     delete [] psi0;
  if( f0 )       delete [] f0;
  if( psinr )    delete [] psinr;
  if( psini )    delete [] psini;
  if( fnr )      delete [] fnr;
  if( fni )      delete [] fni;
}


// ****************************************************************************
//  Method: findElementNeighbors
//
96
//  Creationist: Joshua Breslau
allens's avatar
allens committed
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
//  Creation:   20 November 2009
//
// ****************************************************************************
void avtM3DC1Field::findElementNeighbors()
{
  v_entry *vert_list = 0;
  edge    *edge_list = 0;
  float   *ptr;
  double  x[3], y[3], co, sn;
  int     el, vert, tri[3], vlen;

  /* Allocate, initialize neighbor table */
  neighbors = (int *)malloc(3 * nelms * sizeof(int));
  if (neighbors == NULL) {
    fputs("Insufficient memory in findElementNeighbors.\n", stderr);
    exit(1);
  }

  for (el=0; el<3*nelms; el++)
    neighbors[el] = -1;

  /* Allocate trig table */
  trigtable = (double *)malloc(2 * nelms * sizeof(double));
  if (trigtable == NULL) {
    fputs("Insufficient memory in findElementNeighbors.\n", stderr);
    exit(1);
  }

  /* Allocate, initialize temporary hash tables */
  vert_list = (v_entry *)malloc(3 * nelms * sizeof(v_entry));
  if (vert_list == NULL) {
    fputs("Insufficient memory in findElementNeighbors.\n", stderr);
    exit(1);
  }
  vlen = 0;
  edge_list = (edge *)malloc(3 * nelms * sizeof(edge));
  if (edge_list == NULL) {
    fputs("Insufficient memory in findElementNeighbors.\n", stderr);
    exit(1);
  }

  for (vert=0; vert<3*nelms; vert++)
    edge_list[vert].n = 0;

  /* Loop over elements, finding vertices, edges, neighbors */

  //For each element, the first 6 values are a, b, c, theta, x, and z.
  //The nodes of the element are located at
  // (x,z),
  // (x+(a+b)*cos(theta),z+(a+b)*sin(theta)),
  // (x+b*cos(theta)-c*sin(theta),z+b*sin(theta)+c*cos(theta)).

  for (el=0; el<nelms; el++) {
    ptr = elements + ELEMENT_SIZE*el;
    co = trigtable[2*el]     = cos(ptr[3]);
    sn = trigtable[2*el + 1] = sin(ptr[3]);

    x[0] = ptr[4];
    y[0] = ptr[5];

    x[1] = x[0] + (ptr[0] + ptr[1])*co;
    y[1] = y[0] + (ptr[0] + ptr[1])*sn;

    x[2] = x[0] + ptr[1]*co - ptr[2]*sn;
    y[2] = y[0] + ptr[1]*sn + ptr[2]*co;

    for (vert=0; vert<3; vert++)
      register_vert(vert_list, &vlen, x[vert], y[vert], tri+vert);

    for (vert=0; vert<3; vert++)
      add_edge(edge_list, tri, vert, el, neighbors);
  } /* end loop el */

//   fprintf(stderr, "%d / %d unique vertices\n", vlen, 3*nelms);
//   fprintf(stderr, "Neighbors of element 0: %d, %d, %d\n", neighbors[0],
//           neighbors[1], neighbors[2]);

  /* Use unique vert list to find mesh bounds */
  Rmin = Rmax = vert_list[0].x;
  zmin = zmax = vert_list[0].y;
  for (vert=1; vert<vlen; vert++) {
    if (Rmin > vert_list[vert].x) Rmin = vert_list[vert].x;
    if (Rmax < vert_list[vert].x) Rmax = vert_list[vert].x;
    if (zmin > vert_list[vert].y) zmin = vert_list[vert].y;
    if (zmax < vert_list[vert].y) zmax = vert_list[vert].y;    
  }

//   fprintf(stderr, "R bounds: %lf, %lf\nz bounds: %lf, %lf\n",
//           Rmin, Rmax, zmin, zmax);
  
  free(vert_list);
  free(edge_list);
}


// ****************************************************************************
//  Method: register_vert
//
195
//  Creationist: Joshua Breslau
allens's avatar
allens committed
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
//  Creation:   20 November 2009
//
// ****************************************************************************
void avtM3DC1Field::register_vert(v_entry *vlist, int *len,
                                     double x, double y, int *index)
{
  const double tol=2.5e-13;
  double dx, dy;
  int    vert;

  for (vert=0; vert<(*len); vert++) {
    dx = x - vlist[vert].x;  dy = y - vlist[vert].y;
    if (dx*dx + dy*dy < tol) { /* Found in list! */
      *index = vert;
      return;
    }
  }

  /* Vertex not found -> add to end */
  vlist[*len].x = x;  vlist[*len].y = y;
  *index = *len;
  ++(*len);
}


// ****************************************************************************
//  Method: add_edge
//
224
//  Creationist: Joshua Breslau
allens's avatar
allens committed
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
//  Creation:   20 November 2009
//
// ****************************************************************************
void avtM3DC1Field::add_edge(edge *list, int *tri,
                                int side, int el, int *nlist)
{
  int  i, v1, v2, vo;
  edge *ed;

  /* Sort the vertices */
  v1 = tri[side];  v2 = tri[(side+1)%3];
  if (v1 < v2) { ed = list+v1;  vo = v2; }
  else         { ed = list+v2;  vo = v1; }

  /* See if this edge is already present */
  for (i=0; i<ed->n; i++)
    if (ed->o[i].v == vo) {           /* It is! Update the neighbor table. */
      nlist[3*el + side] = ed->o[i].el0;
      nlist[3*ed->o[i].el0 + ed->o[i].side] = el;
      return;
    }

  /* The edge was not present; add it. */
  ed->o[ed->n].v = vo;
  ed->o[ed->n].el0 = el;
  ed->o[ed->n].side = side;
  ed->n++;
}

// ****************************************************************************
//  Method: get_tri_coords2D
//
257
//  Creationist: Joshua Breslau
allens's avatar
allens committed
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
//  Creation:   20 November 2009
//
// ****************************************************************************
int avtM3DC1Field::get_tri_coords2D(double *xin, int el, double *xout)
{
  float     *tri;
  double     co, sn, rrel, zrel;

  /* Compute coordinates local to the current element */
  co = trigtable[2*el];
  sn = trigtable[2*el + 1];
  
  tri = elements + ELEMENT_SIZE*el;
  
  rrel = xin[0] - (tri[4] + tri[1]*co);
  zrel = xin[2] - (tri[5] + tri[1]*sn);
  
  xout[0] = rrel*co + zrel*sn;  /* = xi */
  xout[1] = zrel*co - rrel*sn;  /* = eta */

  return el;
}


// ****************************************************************************
//  Method: get_tri_coords2D
//
285
//  Creationist: Joshua Breslau
allens's avatar
allens committed
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
//  Creation:   20 November 2009
//
// ****************************************************************************
int avtM3DC1Field::get_tri_coords2D(double *xin, double *xout)
{
  static int el=0;
  float     *tri;
  double     co, sn, rrel, zrel;
  int        last=-1, next, flag0, flag1, flag2;

  for (int count=0; count<nelms; ++count) {

    /* Compute coordinates local to the current element */
    co = trigtable[2*el];
    sn = trigtable[2*el + 1];

    tri = elements + ELEMENT_SIZE*el;

    rrel = xin[0] - (tri[4] + tri[1]*co);
    zrel = xin[2] - (tri[5] + tri[1]*sn);

    xout[0] = rrel*co + zrel*sn;  /* = xi */
    xout[1] = zrel*co - rrel*sn;  /* = eta */
    /* Determine whether point is inside element */
    /* "Outside" side 0? */
    if ((flag0 = ((*tri + tri[1])*xout[1] < 0.0)))
    {
      if ((next = neighbors[3*el]) >= 0) {
        if (next != last) // not on the boundary so continue;
        {
          last = el;
          el = next;
          continue;
        }
        else // on the boundary so reset the flag and check the other edges;
          flag0 = 0;
      }
    }

    /* "Outside" side 1? */
    if ((flag1 = (*tri*xout[1] > tri[2]*(*tri - xout[0]))))
    {
      if ((next = neighbors[3*el + 1]) >= 0) {
        if (next != last) // not on the boundary so continue;
        {
          last = el;
          el = next;
          continue;
        }
        else // on the boundary so reset the flag and check the other edges;
          flag1 = 0;
      }
    }

    /* "Outside" side 2? */
    if ((flag2 = (tri[2]*xout[0] < tri[1]*(xout[1] - tri[2]))))
    {
      if ((next = neighbors[3*el + 2]) >= 0) {
        if (next != last) // on the boundary so continue;
        {
          last = el;
          el = next;
          continue;
        }
        else // on the boundary so reset the flag and check the other edges;
          flag2 = 0;
      }
    }

    if (flag0 || flag1 || flag2)
      return -1;
    else
      break;

  } /* end loop count */

// fprintf(stderr, "Searched %d elements.\n", count);

  return el;
}

// ****************************************************************************
//  Method: interp basic interpolation
//
370
//  Creationist: Joshua Breslau
allens's avatar
allens committed
371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388
//  Creation:   20 November 2009
//
// ****************************************************************************
float avtM3DC1Field::interp(float *var, int el, double *lcoords)
{
  float *a = var + SCALAR_SIZE*el;
  double xi = *lcoords, eta = lcoords[1];

  return *a + eta*(a[2] + eta*(a[5] + eta*(a[9] + eta*(a[14] + eta*a[19])))) +
    xi*(a[1] + eta*(a[4] + eta*(a[8] + eta*(a[13] + eta*a[18]))) +
        xi*(a[3] + eta*(a[7] + eta*(a[12] + eta*a[17])) +
            xi*(a[6] + eta*(a[11] + eta*a[16]) +
                xi*(a[10] + xi*a[15]))));
}

// ****************************************************************************
//  Method: interpdR interpolation in dR
//
389
//  Creationist: Joshua Breslau
allens's avatar
allens committed
390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413
//  Creation:   20 November 2009
//
// ****************************************************************************
float avtM3DC1Field::interpdR(float *var, int el, double *lcoords)
{
  float *a = var + SCALAR_SIZE*el;
  double xi = lcoords[0], eta = lcoords[1], xicoef, etacoef;

  xicoef = a[1] + eta*(a[4] + eta*(a[8] + eta*(a[13] + a[18]*eta))) +
    xi*(2.0*(a[3] + eta*(a[7] + eta*(a[12] + a[17]*eta))) +
        xi*(3.0*(a[6] + eta*(a[11] + a[16]*eta)) +
            xi*(4.0*a[10] + xi*5.0*a[15])));

  etacoef = a[2] + xi*(a[4] + xi*(a[7] + a[11]*xi)) +
    eta*(2.0*(a[5] + xi*(a[8] + xi*(a[12] + a[16]*xi))) +
         eta*(3.0*(a[9] + xi*(a[13] + a[17]*xi)) +
              eta*(4.0*(a[14] + a[18]*xi) + eta*5.0*a[19])));

  return xicoef*trigtable[2*el] - etacoef*trigtable[2*el + 1];
}

// ****************************************************************************
//  Method: interpdz interpolation in dz
//
414
//  Creationist: Joshua Breslau
allens's avatar
allens committed
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439
//  Creation:   20 November 2009
//
// ****************************************************************************
float avtM3DC1Field::interpdz(float *var, int el, double *lcoords)
{
  float *a = var + SCALAR_SIZE*el;
  double xi = lcoords[0], eta = lcoords[1], xicoef, etacoef;

  xicoef = a[1] + eta*(a[4] + eta*(a[8] + eta*(a[13] + a[18]*eta))) +
    xi*(2.0*(a[3] + eta*(a[7] + eta*(a[12] + a[17]*eta))) +
        xi*(3.0*(a[6] + eta*(a[11] + a[16]*eta)) +
            xi*(4.0*a[10] + xi*5.0*a[15])));

  etacoef = a[2] + xi*(a[4] + xi*(a[7] + a[11]*xi)) +
    eta*(2.0*(a[5] + xi*(a[8] + xi*(a[12] + a[16]*xi))) +
         eta*(3.0*(a[9] + xi*(a[13] + a[17]*xi)) +
              eta*(4.0*(a[14] + a[18]*xi) + eta*5.0*a[19])));

  return xicoef*trigtable[2*el + 1] + etacoef*trigtable[2*el];
}


// ****************************************************************************
//  Method: interpdR2 interpolation in dR2
//
440
//  Creationist: Joshua Breslau
allens's avatar
allens committed
441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469
//  Creation:   20 November 2009
//
// ****************************************************************************
float avtM3DC1Field::interpdR2(float *var, int el, double *lcoords)
{
  float *a = var + SCALAR_SIZE*el;
  double co=trigtable[2*el], sn=trigtable[2*el + 1];
  double xi = lcoords[0], eta = lcoords[1], xixicoef, etaetacoef, xietacoef;

  xixicoef = 2.0*(a[3] + eta*(a[7] + eta*(a[12] + eta*a[17]))) +
    xi*(6.0*(a[6] + eta*(a[11] + eta*a[16])) +
        xi*(12.0*a[10] + xi*20.0*a[15]));

  etaetacoef = 2.0*(a[5] + xi*(a[8] + xi*(a[12] + xi*a[16]))) +
    eta*(6.0*(a[9] + xi*(a[13] + xi*a[17])) +
         eta*(12.0*(a[14] + xi*a[18]) + 20.0*eta*a[19]));

  xietacoef = 2.0*a[4] +
    eta*(4.0*a[8] + xi*(8.0*a[12] + 12.0*xi*a[16]) +
         eta*(6.0*a[13] + 12.0*xi*a[17] + 8.0*eta*a[18])) +
    xi*(4.0*a[7] + 6.0*xi*a[11]);

  return (xixicoef*co - xietacoef*sn)*co + etaetacoef*sn*sn;
}


// ****************************************************************************
//  Method: interpdz2 interpolation in dz2
//
470
//  Creationist: Joshua Breslau
allens's avatar
allens committed
471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499
//  Creation:   20 November 2009
//
// ****************************************************************************
float avtM3DC1Field::interpdz2(float *var, int el, double *lcoords)
{
  float *a = var + SCALAR_SIZE*el;
  double co=trigtable[2*el], sn=trigtable[2*el + 1];
  double xi = lcoords[0], eta = lcoords[1], xixicoef, etaetacoef, xietacoef;

  xixicoef = 2.0*(a[3] + eta*(a[7] + eta*(a[12] + eta*a[17]))) +
    xi*(6.0*(a[6] + eta*(a[11] + eta*a[16])) +
        xi*(12.0*a[10] + xi*20.0*a[15]));

  etaetacoef = 2.0*(a[5] + xi*(a[8] + xi*(a[12] + xi*a[16]))) +
    eta*(6.0*(a[9] + xi*(a[13] + xi*a[17])) +
         eta*(12.0*(a[14] + xi*a[18]) + 20.0*eta*a[19]));

  xietacoef = 2.0*a[4] +
    eta*(4.0*a[8] + xi*(8.0*a[12] + 12.0*xi*a[16]) +
         eta*(6.0*a[13] + 12.0*xi*a[17] + 8.0*eta*a[18])) +
    xi*(4.0*a[7] + 6.0*xi*a[11]);

  return (xixicoef*sn + xietacoef*co)*sn + etaetacoef*co*co;
}


// ****************************************************************************
//  Method: interpdR interpolation in dR
//
500
//  Creationist: Joshua Breslau
allens's avatar
allens committed
501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530
//  Creation:   20 November 2009
//
// ****************************************************************************
float avtM3DC1Field::interpdRdz(float *var, int el, double *lcoords)
{
  float *a = var + SCALAR_SIZE*el;
  double co=trigtable[2*el], sn=trigtable[2*el + 1];
  double xi = lcoords[0], eta = lcoords[1], xixicoef, etaetacoef, xietacoef;

  xixicoef = 2.0*(a[3] + eta*(a[7] + eta*(a[12] + eta*a[17]))) +
    xi*(6.0*(a[6] + eta*(a[11] + eta*a[16])) +
        xi*(12.0*a[10] + xi*20.0*a[15]));

  etaetacoef = 2.0*(a[5] + xi*(a[8] + xi*(a[12] + xi*a[16]))) +
    eta*(6.0*(a[9] + xi*(a[13] + xi*a[17])) +
         eta*(12.0*(a[14] + xi*a[18]) + 20.0*eta*a[19]));

  xietacoef = a[4] +
    eta*(2.0*a[8] + xi*(4.0*a[12] + 6.0*xi*a[16]) +
         eta*(3.0*a[13] + 6.0*xi*a[17] + 4.0*eta*a[18])) +
    xi*(2.0*a[7] + 3.0*xi*a[11]);

  return (xixicoef - etaetacoef)*co*sn + xietacoef*(co*co - sn*sn);
}


// ****************************************************************************
//  Method: interpBcomps
//
//  Simultaneously interpolate all three cylindrical components of
531 532
//  magnetic field - should not be used for calculating integral
//  curves.
allens's avatar
allens committed
533
//
534
//  Creationist: Joshua Breslau
allens's avatar
allens committed
535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581
//  Creation:   20 November 2009
//
// ****************************************************************************
void avtM3DC1Field::interpBcomps(float *B, double *x,
                                    int element, double *xieta)
{
  float *B_R   = &(B[0]);
  float *B_z   = &(B[2]);
  float *B_phi = &(B[1]);

  double co, sn, dfnrdr, dfnidr;

  /* n=0 components */
  /* B_R = -1/R dpsi/dz - df'/dR */
  *B_R = -interpdz(psi0, element, xieta) / x[0];

  /* B_z = 1/R dpsi/dR - df'/dz */
  *B_z = interpdR(psi0, element, xieta) / x[0];

  /* B_phi = d^2f/dR^2 + 1/R df/dR + d^2f/dz^2 + F0/R^2 */
  *B_phi = interpdR2(f0, element, xieta) +
    interpdz2(f0, element, xieta) +
    (interpdR(f0, element, xieta) + F0/x[0])/ x[0];

  /* n>0 components, if applicable */
  if (linflag) {
    co = cos(tmode * x[1]);  sn = sin(tmode * x[1]);

    dfnrdr = interpdR(fnr, element, xieta);
    dfnidr = interpdR(fni, element, xieta);

    *B_R += (interpdz(psini, element, xieta)*sn -
             interpdz(psinr, element, xieta)*co) / x[0]
      + tmode*(dfnrdr*sn + dfnidr*co);

    *B_z += (interpdR(psinr, element, xieta)*co -
             interpdR(psini, element, xieta)*sn)/ x[0]
      + tmode*(interpdz(fnr, element, xieta)*sn +
                      interpdz(fni, element, xieta)*co);

    *B_phi += (interpdR2(fnr, element, xieta) +
               interpdz2(fnr, element, xieta))*co
      - (interpdR2(fni, element, xieta) +
         interpdz2(fni, element, xieta))*sn +
      (dfnrdr*co - dfnidr*sn) / x[0];
  }
}