Unexpected value for 'n' in UDF

General discussion about Elmer
szlyc0706
Posts: 9
Joined: 04 Jan 2024, 15:33
Antispam: Yes

Unexpected value for 'n' in UDF

Post by szlyc0706 »

Hi Everyone,

I am meeting a problem when writing my UDF: From the book <<ELMER guide to FRM>> p38. Says that UDF should has format

Code: Select all

FUNCTION name_func ( model, n, var ) RESULT(result)
where n is the node, also from the Elmer source code, the way that UDF is called is by

Code: Select all

xecRealFunction( ptr % PROCEDURE,CurrentModel, NodeIndex, T )
However, when I query n from my UDF using

Code: Select all

FUNCTION MyFunc(Model,n, t)RESULT(br)
  USE DefUtils
  IMPLICIT None
  CHARACTER(len=20) filename
  TYPE(Model_t) :: Model
  INTEGER :: n
  REAL(KIND=dp) :: br, t

  filename = "C:\\Temp\\Test.txt"
  open(unit = 1, file = filename)
  write(1, '(i16)') n
  
  br = 0.3

END FUNCTION MyFunc

it seems that the values of n do not correspond to the node index (should from 0 to Number of nodes-1). Part of the result of n is shown below:
779498769
646292935
-1717986918
646292935
779498769
1120292398
779498769
-1717986918
-1717986918
779498769
-1717986918
-549755814
1344262916
-1717986918
-1717986918
1344262916
-549755814
-1717986918
1756579777
1344262916
148214167
1344262916
1756579777
-549755814
1870269279
282189659
-549755814
282189659

Anyone has any idea about what these numbers are or why I am not getting expected value for n?
Thanks
kevinarden
Posts: 2286
Joined: 25 Jan 2019, 01:28
Antispam: Yes

Re: Unexpected value for 'n' in UDF

Post by kevinarden »

1. The call has to match the catch

call
xecRealFunction( ptr % PROCEDURE,CurrentModel, NodeIndex, T )
1, 2, 3, 4

catch
FUNCTION name_func ( model, n, var ) RESULT(result)
1, 2, 3

You are passing CurrentModel to n
szlyc0706
Posts: 9
Joined: 04 Jan 2024, 15:33
Antispam: Yes

Re: Unexpected value for 'n' in UDF

Post by szlyc0706 »

Hi Kevinarden,

Thanks very much for your reply! but i am still confusing.
I have tried more variable in the 'catch' using code:

Code: Select all

MyFunc(Model,n, x, y, k,w)RESULT(br)
it seems that n stays as the old value, x, y, k catch the coordinate of xyz, w returns no value.

if the code of call is the right one to be refer to, then i think x should returns index of nodes.
szlyc0706
Posts: 9
Joined: 04 Jan 2024, 15:33
Antispam: Yes

Re: Unexpected value for 'n' in UDF

Post by szlyc0706 »

Hi,

Here is the detail of function 'xecRealFunction' in Elmer code:

Code: Select all

RECURSIVE FUNCTION execrealfunction(fptr, model, node, val) RESULT(realval)
            IMPLICIT NONE
            INTEGER(KIND=AddrInt) :: fptr
            TYPE(Model_t), POINTER :: model
            INTEGER :: node
            REAL(KIND=dp) :: val(*)
            REAL(KIND=dp) :: realval

            INTERFACE
                FUNCTION ElmerRealFn(model, node, val ) RESULT(realval)
                    IMPORT Model_t, dp
                    TYPE(Model_t) :: model
                    INTEGER :: node
                    REAL(KIND=dp) :: val(*)
                    REAL(KIND=dp) :: realval
                END FUNCTION ElmerRealFn
            END INTERFACE
            TYPE(C_FUNPTR) :: cfptr
            PROCEDURE(ElmerRealFn), POINTER :: pptr

            ! Ugly hack, fptr should be stored as C function pointer
            cfptr = TRANSFER(fptr, cfptr)
            CALL C_F_PROCPOINTER(cfptr, pptr)
            realval = pptr(model, node, val)
        END FUNCTION execrealfunction
According to the codes above I really think that in function 'FUNCTION MyFunc ( model, n, var )', n should be the node index.
kevinarden
Posts: 2286
Joined: 25 Jan 2019, 01:28
Antispam: Yes

Re: Unexpected value for 'n' in UDF

Post by kevinarden »

the call has to match the name and the list order.

In the calling code

returnval = myfunction(val1,val2,val3)

in the function being called it has to be named the same

myfunction(s1,s2,s3)
return s1*s2*s3

and therefore
s1 =val1, s2 = val2, s3=val3)
and returnval = s1*s2*s3

the local variable names in each code mean nothing to each other, you would have to make them global to match.
using local variables, the order called and received must match.

In your code you are sending ;
(fptr, model, node, val)
and receiving
(model, node, val )

therefore;
model=fptr
node=model
val=node

it goes by order not name
szlyc0706
Posts: 9
Joined: 04 Jan 2024, 15:33
Antispam: Yes

Re: Unexpected value for 'n' in UDF

Post by szlyc0706 »

Yes, I think I know what you are trying to explain, but the problem is: The input variables of my UDF seems to be (model, 'unknown', coordinate x, coordinate y, coordinate z).
Following one of the previous post using UDF: https://www.elmerfem.org/forum/viewtopi ... C+n#p30127
UDF is called in the .sif file using 'Real Procedure "initrho" "initrho"'
Definition of UDF is

Code: Select all

Function initrho(model, n, dummyin) result(rho)
In this UDF, n is the node index:

Code: Select all

x = model % Nodes % x(n)
  y = model % Nodes % y(n)
  z = model % Nodes % z(n)
I am writing my UDF in the same format as this but the second argument into the function for me is not the node index.
How do I access the node index in my UDF?

Thanks
kevinarden
Posts: 2286
Joined: 25 Jan 2019, 01:28
Antispam: Yes

Re: Unexpected value for 'n' in UDF

Post by kevinarden »

Can you attach the complete sif file and the complete function file?
raback
Site Admin
Posts: 4821
Joined: 22 Aug 2009, 11:57
Antispam: Yes
Location: Espoo, Finland
Contact:

Re: Unexpected value for 'n' in UDF

Post by raback »

Hi

Could you give pointer to "ELMER guide to FRM". I haven't seen such a book.

I'm pretty sure the function API works ok. Probably there is something wrong how it is called. But hard to say without seeing the actual code.

-Peter
szlyc0706
Posts: 9
Joined: 04 Jan 2024, 15:33
Antispam: Yes

Re: Unexpected value for 'n' in UDF

Post by szlyc0706 »

Hi Both,

Thanks very much for your replies, please see the attachment for my mesh file, .sif and .f90.

The book <<ELMER Guide to FEM simulations>> is at link :https://diposit.ub.edu/dspace/bitstream ... ns_v03.pdf , the sentence about the format of UDF is on page 38.
Attachments
modelForPost.zip
(47.84 KiB) Downloaded 6 times
kevinarden
Posts: 2286
Joined: 25 Jan 2019, 01:28
Antispam: Yes

Re: Unexpected value for 'n' in UDF

Post by kevinarden »

I have not seen this before and do not know what the result would be;

Relative Permeability = Procedure "TestLib" "MyFunc"

What I have seen, used, and is in the manual

is
Relative Permeability = Variable SomeVariableinTheSimulation
Real Procedure "TestLib" "MyFunc"

where the function
FUNCTION MyFunc(Model,n,SomeVariableName) RESULT(NewMu)

Then SomeVariableinTheSimulation gets passed to SomeVariableName in the function
neither SomeVariableinTheSimulation nor SomeVariableName has to be used in the function
but RESULT(NewMu)
means that
Relative Permeability = NewMu

Both SomeVariableinTheSimulation and SomeVariableinTheSimulation can be an unused variable
but the pitch and catch is set up that way.
Post Reply