예제 #1
0
modena_index_set_t *modena_function_get_index_set
(
    const modena_function_t* self,
    const char* name
)
{
    PyObject *pRet = PyObject_CallMethod
    (
        self->pFunction,
        "indexSet",
        "(z)",
        name
    );
    if(!pRet){ Modena_PyErr_Print(); }

    PyObject *args = PyTuple_New(0);
    PyObject *kw = Py_BuildValue("{s:O}", "indexSet", pRet);

    PyObject *pNewObj = PyObject_Call
    (
        (PyObject *) &modena_index_set_tType,
        args,
        kw
    );

    Py_DECREF(args);
    Py_DECREF(kw);
    if(!pNewObj)
    {
       Modena_PyErr_Print();
    }

    return (modena_index_set_t *) pNewObj;
}
예제 #2
0
void modena_model_get_minMax
(
    modena_model_t *m
)
{
    PyObject *pObj = PyObject_CallMethod(m->pModel, "minMax", NULL);
    if(!pObj){ Modena_PyErr_Print(); }

    PyObject *pMin = PyTuple_GET_ITEM(pObj, 0); // Borrowed ref
    PyObject *pSeq = PySequence_Fast(pMin, "expected a sequence");
    m->inputs_minMax_size = PySequence_Size(pMin);
    m->inputs_min = malloc(m->inputs_minMax_size*sizeof(double));
    size_t i;
    for(i = 0; i < m->inputs_minMax_size; i++)
    {
        m->inputs_min[i] = PyFloat_AsDouble(PyList_GET_ITEM(pSeq, i));
    }
    Py_DECREF(pSeq);
    if(PyErr_Occurred()){ Modena_PyErr_Print(); }

    PyObject *pMax = PyTuple_GET_ITEM(pObj, 1); // Borrowed ref
    pSeq = PySequence_Fast(pMax, "expected a sequence");
    m->inputs_max = malloc(m->inputs_minMax_size*sizeof(double));
    for(i = 0; i < m->inputs_minMax_size; i++)
    {
        m->inputs_max[i] = PyFloat_AsDouble(PyList_GET_ITEM(pSeq, i));
    }
    Py_DECREF(pSeq);
    if(PyErr_Occurred()){ Modena_PyErr_Print(); }

    Py_DECREF(pObj);
}
예제 #3
0
void modena_model_read_substituteModels(modena_model_t *m)
{
    PyObject *pSubstituteModels = PyObject_GetAttrString
    (
        m->pModel, "substituteModels"
    );
    if(!pSubstituteModels){ Modena_PyErr_Print(); }

    PyObject *pSeq = PySequence_Fast
    (
        pSubstituteModels, "expected a sequence"
    );
    m->substituteModels_size = PySequence_Size(pSubstituteModels);
    m->substituteModels =
        malloc(m->substituteModels_size*sizeof(modena_substitute_model_t));
    size_t i;
    for(i = 0; i < m->substituteModels_size; i++)
    {
        PyObject *args = PyTuple_New(0);
        PyObject *kw = Py_BuildValue
        (
            "{s:O}", "model", PyList_GET_ITEM(pSeq, i)
        );

        m->substituteModels[i].model = (modena_model_t *) PyObject_Call
        (
            (PyObject *) &modena_model_tType, args, kw
        );
        Py_DECREF(args);
        Py_DECREF(kw);
        if(!m->substituteModels[i].model){ Modena_PyErr_Print(); }

        m->substituteModels[i].inputs = modena_inputs_new
        (
            m->substituteModels[i].model
        );

        m->substituteModels[i].outputs = modena_outputs_new
        (
            m->substituteModels[i].model
        );

        modena_substitute_model_calculate_maps(&m->substituteModels[i], m);
    }

    Py_DECREF(pSeq);
    Py_DECREF(pSubstituteModels);
    if(PyErr_Occurred()){ Modena_PyErr_Print(); }
}
예제 #4
0
void modena_function_load_library(modena_function_t* self)
{
    PyObject *pFunctionName =
        PyObject_GetAttrString(self->pFunction, "functionName");
    if(!pFunctionName){ Modena_PyErr_Print(); }

    PyObject *pLibraryName =
        PyObject_GetAttrString(self->pFunction, "libraryName");
    if(!pLibraryName){ Modena_PyErr_Print(); }

    self->handle = lt_dlopen(PyString_AsString(pLibraryName));

    if(!self->handle)
    {
        fprintf
        (
           stderr,
           "lt_dlopen: Could not open library %s\nlt_dlopen: %s\n",
           PyString_AsString(pLibraryName),
           lt_dlerror()
        );
        exit(1);
    }

    self->function = lt_dlsym(self->handle, PyString_AsString(pFunctionName));
    if(!self->function)
    {
        fprintf
        (
            stderr,
            "lt_dlsym: Could not find function %s in library %s\n"
            "lt_dlsym: %s",
            PyString_AsString(pFunctionName),
            PyString_AsString(pLibraryName),
            lt_dlerror()
        );
        lt_dlclose(self->handle);
        exit(1);
    }

    Py_DECREF(pFunctionName);
    Py_DECREF(pLibraryName);

    PyObject *pParameters =
        PyObject_GetAttrString(self->pFunction, "parameters");
    if(!pParameters){ Modena_PyErr_Print(); }
    self->parameters_size = PyObject_Size(pParameters);
    Py_DECREF(pParameters);
}
예제 #5
0
size_t modena_model_outputs_argPos(const modena_model_t *m, const char *name)
{
    PyObject *pRet = PyObject_CallMethod(m->pModel, "outputs_argPos", NULL);
    if(!pRet){ Modena_PyErr_Print(); }
    size_t ret = PyInt_AsSsize_t(pRet);
    Py_DECREF(pRet);

    return ret;
}
예제 #6
0
void modena_substitute_model_calculate_maps
(
    modena_substitute_model_t *sm,
    modena_model_t *parent
)
{
    PyObject *pMaps = PyObject_CallMethod
    (
        parent->pModel, "calculate_maps", "(O)", sm->model->pModel
    );
    if(!pMaps){ Modena_PyErr_Print(); }

    PyObject *pMapOutputs = PyTuple_GET_ITEM(pMaps, 0); // Borrowed ref
    if(!pMapOutputs){ Modena_PyErr_Print(); }
    PyObject *pSeq = PySequence_Fast(pMapOutputs, "expected a sequence");
    sm->map_outputs_size = PySequence_Size(pMapOutputs);
    sm->map_outputs = malloc(sm->map_outputs_size*sizeof(double));
    size_t i;
    for(i = 0; i < sm->map_outputs_size; i++)
    {
        sm->map_outputs[i] = PyInt_AsSsize_t(PyList_GET_ITEM(pSeq, i));
        parent->argPos_used[sm->map_outputs[i]] = true;
    }
    sm->map_outputs_size /= 2;
    Py_DECREF(pSeq);
    Py_DECREF(pMapOutputs);
    if(PyErr_Occurred()){ Modena_PyErr_Print(); }

    PyObject *pMapInputs = PyTuple_GET_ITEM(pMaps, 1); // Borrowed ref
    if(!pMapInputs){ Modena_PyErr_Print(); }
    pSeq = PySequence_Fast(pMapInputs, "expected a sequence");
    sm->map_inputs_size = PySequence_Size(pMapInputs);
    sm->map_inputs = malloc(sm->map_inputs_size*sizeof(double));
    for(i = 0; i < sm->map_inputs_size; i++)
    {
        sm->map_inputs[i] = PyInt_AsSsize_t(PyList_GET_ITEM(pSeq, i));
    }
    sm->map_inputs_size /= 2;
    Py_DECREF(pSeq);
    Py_DECREF(pMapInputs);
    if(PyErr_Occurred()){ Modena_PyErr_Print(); }

    Py_DECREF(pMaps);
}
예제 #7
0
size_t modena_model_inputs_argPos
(
    const modena_model_t *m,
    const char *name
)
{
    PyObject *pRet = PyObject_CallMethod
    (
        m->pModel, "inputs_argPos", "(z)", name
    );
    if(!pRet){ Modena_PyErr_Print(); }
    size_t argPos = PyInt_AsSsize_t(pRet);
    Py_DECREF(pRet);

    if(m->argPos_used)
    {
        m->argPos_used[argPos] = true;
    }

    return argPos;
}
예제 #8
0
modena_function_t *modena_function_new_from_model
(
    const modena_model_t *m
)
{
    modena_function_t *self = malloc(sizeof(modena_function_t));

    if(lt_dlinit())
    {
        fprintf(stderr, "lt_dlinit: %s\n", lt_dlerror());
        exit(1);
    }

    self->pFunction =
        PyObject_GetAttrString(m->pModel, "surrogateFunction");
    if(!self->pFunction){ Modena_PyErr_Print(); }

    modena_function_load_library(self);

    return self;
}
예제 #9
0
// modena_model_t instantiates the surrogate model
modena_model_t *modena_model_new
(
    const char *modelId
)
{
    // Initialize the Python Interpreter
    if(!Py_IsInitialized())
    {
        Py_Initialize();
    }

    // Initialize this module
    initlibmodena();

    PyObject *args = PyTuple_New(0);
    PyObject *kw = Py_BuildValue("{s:s}", "modelId", modelId);

    PyObject *m = PyObject_Call((PyObject *) &modena_model_tType, args, kw);
    Py_DECREF(args);
    Py_DECREF(kw);
    if(!m){ Modena_PyErr_Print(); }

    return (modena_model_t *) m;
}
예제 #10
0
// modena_model_t instantiates a surrogate function
modena_function_t *modena_function_new
(
    const modena_model_t *m
)
{
    modena_function_t *f = malloc(sizeof(modena_function_t));

    if(lt_dlinit())
    {
        fprintf(stderr, "lt_dlinit: %s\n", lt_dlerror());
        exit(1);
    }

    PyObject *pSurrogateFunction =
        PyObject_GetAttrString(m->pModel, "surrogateFunction");
    if(!pSurrogateFunction){ Modena_PyErr_Print(); }

    PyObject *pFunctionName =
        PyObject_GetAttrString(pSurrogateFunction, "functionName");
    if(!pFunctionName){ Modena_PyErr_Print(); }

    PyObject *pLibraryName =
        PyObject_GetAttrString(pSurrogateFunction, "libraryName");
    if(!pLibraryName){ Modena_PyErr_Print(); }

    Py_DECREF(pSurrogateFunction);

    f->handle = lt_dlopen(PyString_AsString(pLibraryName));
    if(!f->handle)
    {
        fprintf
        (
           stderr,
           "lt_dlopen: Could not open library %s\nlt_dlopen: %s\n",
           PyString_AsString(pLibraryName),
           lt_dlerror()
        );
        exit(1);
    }

    f->function = lt_dlsym(f->handle, PyString_AsString(pFunctionName));
    if(!f->function)
    {
        fprintf
        (
            stderr,
            "lt_dlsym: Could not find function %s in library %s\n"
            "lt_dlsym: %s",
            PyString_AsString(pFunctionName),
            PyString_AsString(pLibraryName),
            lt_dlerror()
        );
        lt_dlclose(f->handle);
        exit(1);
    }

    Py_DECREF(pFunctionName);
    Py_DECREF(pLibraryName);

    return f;
}
예제 #11
0
/*
modena_model_call returns:

201: requesting exit for new DOE without Restart
200: requesting exit for new DOE with Restart
100: updated model parameters, requesting to continue this run
1: failure
0: okay

If exit is requested, do what's necessary and exit with the same error code!

*/
int modena_model_call
(
    modena_model_t *m,
    modena_inputs_t *inputs,
    modena_outputs_t *outputs
)
{
    size_t j;
    for(j = 0; j < m->substituteModels_size; j++)
    {
        modena_substitute_model_call(&m->substituteModels[j], m, inputs);
    }

    for(j = 0; j < m->inputs_minMax_size; j++)
    {
        /*
        printf
        (
            "j = %zu %g < %g || %g > %g\n",
            j,
            inputs->inputs[j],
            m->inputs_min[j],
            inputs->inputs[j],
            m->inputs_max[j]
        );
        */

        if
        (
            inputs->inputs[j] < m->inputs_min[j]
         || inputs->inputs[j] > m->inputs_max[j]
        )
        {
            PyObject* pOutside = PyList_New(m->inputs_minMax_size);

            for(j = 0; j < m->inputs_minMax_size; j++)
            {
                PyList_SET_ITEM
                (
                    pOutside, j, PyFloat_FromDouble(inputs->inputs[j])
                );
            }

            PyObject *pRet = PyObject_CallMethod
            (
                m->pModel, "outOfBounds", "(O)", pOutside
            );
            Py_DECREF(pOutside);
            if(!pRet){ Modena_PyErr_Print(); }
            int ret = PyInt_AsLong(pRet);
            Py_DECREF(pRet);

            return ret;
        }
    }

    m->function
    (
        m->parameters,
        inputs->inherited_inputs,
        inputs->inputs,
        outputs->outputs
    );

    return 0;
}
예제 #12
0
static PyObject *
modena_model_t_call(modena_model_t* self, PyObject *args, PyObject *kwds)
{
    PyObject *pIn_i=NULL, *pI=NULL, *pCheckBounds=NULL;
    bool checkBounds = true;

    static char *kwlist[] = {"inputs", "inherited_inputs", "checkBounds"};

    if
    (
        !PyArg_ParseTupleAndKeywords
        (
            args, kwds, "OO|O", kwlist, &pIn_i, &pI, &pCheckBounds
        )
    )
    {
        printf("Expected two arguments\n");
        return NULL;
    }

    if(pCheckBounds)
    {
        checkBounds = PyObject_IsTrue(pCheckBounds);
    }

    if(!PyList_Check(pIn_i))
    {
        printf("First argument is not a list\n");
        return NULL;
    }

    if(!PyList_Check(pI))
    {
        printf("First argument is not a list\n");
        return NULL;
    }

    modena_inputs_t *inputs = modena_inputs_new(self);

    PyObject *pSeq = PySequence_Fast(pIn_i, "expected a sequence");
    size_t len = PySequence_Size(pIn_i);
    size_t j;
    for(j = 0; j < len; j++)
    {
        modena_inherited_inputs_set
        (
            inputs, j, PyFloat_AsDouble(PyList_GET_ITEM(pSeq, j))
        );
    }
    Py_DECREF(pSeq);
    if(PyErr_Occurred()){ Modena_PyErr_Print(); }

    pSeq = PySequence_Fast(pI, "expected a sequence");
    len = PySequence_Size(pI);
    for(j = 0; j < len; j++)
    {
        modena_inputs_set
        (
            inputs, j, PyFloat_AsDouble(PyList_GET_ITEM(pSeq, j))
        );
    }
    Py_DECREF(pSeq);
    if(PyErr_Occurred()){ Modena_PyErr_Print(); }

    modena_outputs_t *outputs = modena_outputs_new(self);

    if(checkBounds)
    {
        modena_model_call(self, inputs, outputs);
    }
    else
    {
        modena_model_call_no_check(self, inputs, outputs);
    }

    PyObject* pOutputs = PyList_New(self->outputs_size);
    for(j = 0; j < self->outputs_size; j++)
    {
        PyList_SET_ITEM
        (
            pOutputs, j, PyFloat_FromDouble(modena_outputs_get(outputs, j))
        );
    }
    if(PyErr_Occurred()){ Modena_PyErr_Print(); }

    modena_inputs_destroy(inputs);
    modena_outputs_destroy(outputs);

    return pOutputs;
}
예제 #13
0
static int
modena_model_t_init(modena_model_t *self, PyObject *args, PyObject *kwds)
{
    PyObject *pParameters=NULL, *pModel=NULL;
    char *modelId=NULL;

    static char *kwlist[] = {"model", "modelId", "parameters"};

    if
    (
        !PyArg_ParseTupleAndKeywords
        (
            args, kwds, "|OsO", kwlist, &pModel, &modelId, &pParameters
        )
    )
    {
        return -1;
    }

    if(!pModel)
    {
        PyObject *pName = PyString_FromString("modena.SurrogateModel");
        if(!pName){ Modena_PyErr_Print(); }

        PyObject *pModule = PyImport_Import(pName);
        Py_DECREF(pName);
        if(!pModule){ Modena_PyErr_Print(); }

        PyObject *pDict = PyModule_GetDict(pModule); // Borrowed ref
        if(!pDict){ Modena_PyErr_Print(); }

        pName = PyString_FromString("SurrogateModel");
        if(!pName){ Modena_PyErr_Print(); }

        PyObject *sModel = PyObject_GetItem(pDict, pName);
        Py_DECREF(pName);
        if(!sModel){ Modena_PyErr_Print(); }

        self->pModel = PyObject_CallMethod(sModel, "load", "(z)", modelId);
        Py_DECREF(sModel);
        if(!self->pModel){ Modena_PyErr_Print(); }

        modena_model_get_minMax(self);

        Py_DECREF(pModule);
    }
    else
    {
        Py_INCREF(pModel);
        self->pModel = pModel;

        // Set everything to zero
        self->inputs_minMax_size = 0;
        self->inputs_min = NULL;
        self->inputs_max = NULL;
    }

    //PyObject_Print(self->pModel, stdout, 0);
    //printf("\n");

    PyObject *pOutputs = PyObject_GetAttrString(self->pModel, "outputs");
    if(!pOutputs){ Modena_PyErr_Print(); }
    self->outputs_size = PyDict_Size(pOutputs);
    Py_DECREF(pOutputs);

    PyObject *pMaxArgPos = PyObject_CallMethod
    (
        self->pModel, "inputs_max_argPos", NULL
    );
    if(!pMaxArgPos){ Modena_PyErr_Print(); }
    self->inputs_size = 1 + PyInt_AsSsize_t(pMaxArgPos);
    Py_DECREF(pMaxArgPos);

    self->inherited_inputs_size = 0;

    // Avoiding double indirection in modena_model_call
    // Use modena_function_new to construct, then copy function pointer
    self->mf = modena_function_new(self);
    self->function = self->mf->function;

    self->argPos_used = malloc
    (
        (self->inputs_size + self->inherited_inputs_size)*sizeof(bool)
    );

    modena_model_read_substituteModels(self);

    if(!pParameters)
    {
        pParameters = PyObject_GetAttrString(self->pModel, "parameters");
        if(!pParameters){ Modena_PyErr_Print(); }
    }
    else
    {
        Py_INCREF(pParameters);
    }

    PyObject *pSeq = PySequence_Fast(pParameters, "expected a sequence");
    self->parameters_size = PySequence_Size(pParameters);
    self->parameters = malloc(self->parameters_size*sizeof(double));
    size_t i;
    for(i = 0; i < self->parameters_size; i++)
    {
        self->parameters[i] = PyFloat_AsDouble(PyList_GET_ITEM(pSeq, i));
    }
    Py_DECREF(pSeq);
    Py_DECREF(pParameters);
    if(PyErr_Occurred()){ Modena_PyErr_Print(); }

    return 0;
}
예제 #14
0
modena_function_t *modena_function_new
(
    const char *functionId
)
{
    // Initialize the Python Interpreter
    if(!Py_IsInitialized())
    {
        Py_Initialize();
    }

    // Initialize this module
    initlibmodena();

    PyObject *args = PyTuple_New(0);
    PyObject *kw = Py_BuildValue("{s:s}", "functionId", functionId);

    PyObject *pNewObj = PyObject_Call
    (
        (PyObject *) &modena_function_tType,
        args,
        kw
    );

    Py_DECREF(args);
    Py_DECREF(kw);
    if(!pNewObj)
    {
        if(PyErr_ExceptionMatches(modena_DoesNotExist))
        {
            PyErr_Clear();

            PyObject *pRet = PyObject_CallMethod
            (
                modena_SurrogateFunction,
                "exceptionLoad",
                "(z)",
                functionId
            );
            if(!pRet){ Modena_PyErr_Print(); }
            int ret = PyInt_AsLong(pRet);
            Py_DECREF(pRet);

            modena_error_code = ret;
            return NULL;
        }
        else
        {
            Modena_PyErr_Print();
        }
    }


    modena_function_t *self = (modena_function_t *) pNewObj;

    if(lt_dlinit())
    {
        fprintf(stderr, "lt_dlinit: %s\n", lt_dlerror());
        exit(1);
    }

    modena_function_load_library(self);

    return self;
}
예제 #15
0
static int modena_function_t_init
(
   modena_function_t *self,
   PyObject *args,
   PyObject *kwds
)
{
    PyObject *pFunction=NULL;
    char *functionId=NULL;

    static char *kwlist[] = {"function", "functionId", NULL};

    if
    (
        !PyArg_ParseTupleAndKeywords
        (
            args,
            kwds,
            "|Os",
            kwlist,
            &pFunction,
            &functionId
        )
    )
    {
        Modena_PyErr_Print();
    }

    if(!pFunction)
    {
        self->pFunction = PyObject_CallMethod
        (
            modena_SurrogateFunction,
            "load",
            "(z)",
            functionId
        );

        if(!self->pFunction)
        {
            PyErr_SetString(modena_DoesNotExist, "Function does not exist");

            Modena_PyErr_Print();
        }
    }
    else
    {
        Py_INCREF(pFunction);
        self->pFunction = pFunction;
    }

    // Shouldn't I load the function from the library here?

    PyObject *pParameters =
        PyObject_GetAttrString(self->pFunction, "parameters");
    if(!pParameters){ Modena_PyErr_Print(); }
    self->parameters_size = PyObject_Size(pParameters);
    Py_DECREF(pParameters);

    return 0;
}