20
Using the NAG Library from LabVIEW John Keightley NPL, Centre for Ionising Radiation Metrology NAG Workshop and Surgery 6 December 2001

Using the NAG Library from LabVIEW John Keightley NPL, Centre for Ionising Radiation Metrology NAG Workshop and Surgery 6 December 2001

Embed Size (px)

Citation preview

Page 1: Using the NAG Library from LabVIEW John Keightley NPL, Centre for Ionising Radiation Metrology NAG Workshop and Surgery 6 December 2001

Using the NAG Library

from

LabVIEW

John KeightleyNPL, Centre for Ionising Radiation

Metrology

NAG Workshop and Surgery

6 December 2001

Page 2: Using the NAG Library from LabVIEW John Keightley NPL, Centre for Ionising Radiation Metrology NAG Workshop and Surgery 6 December 2001

Outline of Talk

Basics of calling the NAG DLLs from LabVIEW Similarities with C/C++ calling conventions Introduction to LabVIEW “Call Library Function”

Problems I’ve encountered so far !! Complex Numbers 2D Arrays must be TRANSPOSED ! Functions and Subroutines Character Strings returned via function name Arrays MUST be large enough to hold output

Example

Page 3: Using the NAG Library from LabVIEW John Keightley NPL, Centre for Ionising Radiation Metrology NAG Workshop and Surgery 6 December 2001

Parameter Types in Fortran and LabVIEW

FORTRAN LabVIEWINTEGER I32 or “signed long int”

REAL Double (8 bytes)

LOGICAL I32 or “signed long int”

COMPLEX double re, double im

CHARACTER*n Blank character string,

I32 for string length

EXTERNAL ? ? ? ? ?

Don’t know how !!

Page 4: Using the NAG Library from LabVIEW John Keightley NPL, Centre for Ionising Radiation Metrology NAG Workshop and Surgery 6 December 2001

BASICS

Very similar approach to C/C++ C/C++ headers are VERY useful in determining calling notation for

LabVIEW NB : Stdcall(WINAPI) calling convention

Ie: C++ header from NAG help

extern void __stdcall E02ACF(

CONST double x[],

CONST double y[],

CONST int *n,

double aa[],

CONST int *m1,

double *ref

);

Page 5: Using the NAG Library from LabVIEW John Keightley NPL, Centre for Ionising Radiation Metrology NAG Workshop and Surgery 6 December 2001

LAbVIEW “Call Library Function”“Advanced” Functions Palette from VI diagram

Page 6: Using the NAG Library from LabVIEW John Keightley NPL, Centre for Ionising Radiation Metrology NAG Workshop and Surgery 6 December 2001

Complex NumbersExample A02ABF :Complex Modulus

FORTRAN : real FUNCTION A02ABF(XR,XI) real XR,XI

Refer to C/C++ headers (type definitions) typedef struct { double re,im; } Complex; ??? “Call Library Function” does not allow pointer to a “cluster” !!

Refer to C/C++ header for A02ABF

extern double __stdcall A02ABF(

CONST double *xxr,

CONST double *xxi

);

Page 7: Using the NAG Library from LabVIEW John Keightley NPL, Centre for Ionising Radiation Metrology NAG Workshop and Surgery 6 December 2001

A02ABF : LabVIEW Call

Page 8: Using the NAG Library from LabVIEW John Keightley NPL, Centre for Ionising Radiation Metrology NAG Workshop and Surgery 6 December 2001

2D Arrays In FORTRAN :

Column major order

In LabVIEW Row major order (Same as C/C++)

Thus, MUST TRANSPOSE arrays BEFORE inputting to NAG functions in the DLLs AFTER retrieving output from the DLLs.

Can use LabVIEW “Transpose 2D Array” function from the Array palette !

or use the NAG routine F01CRF (more complicated as you need to know the size of the array to start with)

Page 9: Using the NAG Library from LabVIEW John Keightley NPL, Centre for Ionising Radiation Metrology NAG Workshop and Surgery 6 December 2001

FORTRAN Subroutines and Functions

SUBROUTINE returns VOIDFUNCTION returns a value !!

eg:SUBROUTINE X05AAF(ITIME)

INTEGER ITIME(7) time data array passed through parameter list

CHARACTER*30 FUNCTION X05ABF(ITIME)

INTEGER ITIME(7)

function returns a character array

Page 10: Using the NAG Library from LabVIEW John Keightley NPL, Centre for Ionising Radiation Metrology NAG Workshop and Surgery 6 December 2001

Returning a character string via a function nameCHARACTER*30 FUNCTION X05ABF(ITIME)

INTEGER ITIME(7)

Fortran has no NULL TERMINATED strings: Instead it uses TWO parameters for strings

character string AND an int for string length

cannot return TWO values !! Refer to C headers for solution :

extern void __stdcall X05ABF (char [], int, CONST int itime[] );

1st 2 parameters refer to string which was being returned via function name ? !

??????

Page 11: Using the NAG Library from LabVIEW John Keightley NPL, Centre for Ionising Radiation Metrology NAG Workshop and Surgery 6 December 2001

Correct Calling Convention: returning a character string

Page 12: Using the NAG Library from LabVIEW John Keightley NPL, Centre for Ionising Radiation Metrology NAG Workshop and Surgery 6 December 2001

A Simple Example : E02ADF and E02AEF

Curve Fitting and Interpolation by Chebyshev Polynomials

Program Reads Input Data :

Least Squares Fitting by method of Chebyshev PolynomialsE02ADF

Interpolation of valuesE02AEF

Page 13: Using the NAG Library from LabVIEW John Keightley NPL, Centre for Ionising Radiation Metrology NAG Workshop and Surgery 6 December 2001

Curve Fitting by Chebshev Polynomials

Page 14: Using the NAG Library from LabVIEW John Keightley NPL, Centre for Ionising Radiation Metrology NAG Workshop and Surgery 6 December 2001

Curve Fitting by Chebshev Polynomials

Page 15: Using the NAG Library from LabVIEW John Keightley NPL, Centre for Ionising Radiation Metrology NAG Workshop and Surgery 6 December 2001

Curve Fitting by Chebshev PolynomialsInterpolation

Page 16: Using the NAG Library from LabVIEW John Keightley NPL, Centre for Ionising Radiation Metrology NAG Workshop and Surgery 6 December 2001

MY BIG PROBLEM : EXTERNAL calls

Example : C05AJF : locates zero of a user supplied continuous functionC/C++ header

extern void __stdcall C05AJF( double *x, CONST double *eps, CONST double *eta, double (__stdcall *f)(double *), CONST int *nfmax, int *ifail);

Page 17: Using the NAG Library from LabVIEW John Keightley NPL, Centre for Ionising Radiation Metrology NAG Workshop and Surgery 6 December 2001

EXTERNAL calls : C05AJF #include <math.h>

#include <stdio.h>

#include "nagmk19.h“

main(){

double eps, eta, x;

int ifail, k, nfmax;

double __stdcall f(double *);

for (k=1; k<=2; k++){

eps = k==1 ? 0.1e-3 : 0.1e-4;

x = 1.0; eta = 0.0; nfmax = 200; ifail = 1;

C05AJF(&x,&eps,&eta,f,&nfmax,&ifail);

if (ifail==0)

printf("With eps = %e root = %f\n",eps,x);

else{

printf("ifail = %d\n",ifail);

if (ifail==3 | ifail==4)

printf("With eps = %e final value = %f\n",eps,x);

}

}

return 0;

}

double __stdcall f(double *x)

{

return exp(-*x) - *x;

}

Page 18: Using the NAG Library from LabVIEW John Keightley NPL, Centre for Ionising Radiation Metrology NAG Workshop and Surgery 6 December 2001

EXTENAL CALLS

I DON’T KNOW HOW TO DO THIS IN LabVIEW !! ie: I need a pointer to the user supplied function !!!

LabVIEW call library function doesn’t help me !

Wrapper DLL in C++ required ???

Page 19: Using the NAG Library from LabVIEW John Keightley NPL, Centre for Ionising Radiation Metrology NAG Workshop and Surgery 6 December 2001

Conclusion

Use LabVIEW “Call Library Function”

LabVIEW calls are VERY SIMILAR to those in C/C++

Use NAG C headers for help !

Still to resolve issues with EXTERNAL calls to user supplied functions !!

Nick Williamson from National Instruments is looking into this

Wrapper DLL in C/C++ ??

I can help anyone wanting to use the NAG DLLs from LabVIEW !

Page 20: Using the NAG Library from LabVIEW John Keightley NPL, Centre for Ionising Radiation Metrology NAG Workshop and Surgery 6 December 2001