User defined function not working

sudo
sudo Altair Community Member

I have created a sample user defined function used in a solver variable in motionsolve.

Here is how I did it:

image.png

Here is the py code:

image.png

I tried to run the simulation and plot the results, however I am getting a value of 0 in the primary pulley radius throughout the simualation.

Best Answer

  • GTT Adam
    GTT Adam
    Altair Employee
    Answer ✓

    Hi @sudo,

    I believe that an array must be returned and not a single parameter. If we look at the example provided in MV-1203, they show the following syntax:

    def REQSUB(id, time, par, npar, iflag):
    [D, errflg] = py_sysfnc("DM",[par[0],par[1]]) ## sets "D" as piston
    displacement mag

    result = [0,D,0,0,0,0,0,0] ## lists results for output
    return

    return result ## sends list with results to motionsolve as
    output

    In this example, we can see that the distance measurement is returned as an array called 'result' with 8 inputs. I would try to return your 'result' as an array as well.

    Hope this helps!

    Adam Reid

Answers

  • GTT Adam
    GTT Adam
    Altair Employee
    Answer ✓

    Hi @sudo,

    I believe that an array must be returned and not a single parameter. If we look at the example provided in MV-1203, they show the following syntax:

    def REQSUB(id, time, par, npar, iflag):
    [D, errflg] = py_sysfnc("DM",[par[0],par[1]]) ## sets "D" as piston
    displacement mag

    result = [0,D,0,0,0,0,0,0] ## lists results for output
    return

    return result ## sends list with results to motionsolve as
    output

    In this example, we can see that the distance measurement is returned as an array called 'result' with 8 inputs. I would try to return your 'result' as an array as well.

    Hope this helps!

    Adam Reid

  • sudo
    sudo Altair Community Member
    edited April 20

    Thanks @GTT Adam .

    Also I noticed that this function is being called just once at the start of the simulation. Would it be possible to call it everystep of the simulation so that I would get the updated values of the parameters that I am passing to the user defined function?

  • GTT Adam
    GTT Adam
    Altair Employee

    Hi @sudo,

    Since your User Defined Function does not have any inputs, it may only calculate it once. I would try to pass in a dummy parameter so that it tries to calculate something.

    Try passing in USER(34) in the function and instead of result = 34, replace it with result = [0, par[0], 0,0,0,0,0,0]

    Hope this helps!

    Adam Reid