{ "metadata": { "name": "", "signature": "sha256:a865f5b852cc7ed7df67140646e6246fb9c0ab54e7fce2474f2203abd7a5b63f" }, "nbformat": 3, "nbformat_minor": 0, "worksheets": [ { "cells": [ { "cell_type": "code", "collapsed": false, "input": [ "import os\n", "import json\n", "import bmi.wrapper\n", "\n", "dll_path = '../../bin/Win32/Release/unstruc.dll'\n", "config_file = '../../test_data/flow_salt_2d_small/2d.mdu'\n", "\n", "model = bmi.wrapper.BMIWrapper(engine=dll_path, configfile=config_file)\n", "\n", "def model_initialize(message, array):\n", " print('crashing ...')\n", " model.initialize()\n", " send_reply()\n", "\n", "def model_update(message, array):\n", " arguments = json.loads(message['arguments'])\n", " time_step = float(arguments['time_step'])\n", " model.update(time_step)\n", " send_reply()\n", " \n", "def model_get_current_time(message, array):\n", " current_time = model.get_current_time()\n", " send_reply(np.array([current_time]))\n", " \n", "def model_get_var(message, array):\n", " name = message['name']\n", " print(type(name))\n", " result_array = model.get_var(name)\n", " send_reply(result_array)\n", " \n", "def model_set_var(message, array):\n", " arguments = json.loads(message['arguments'])\n", " model.set_var(message['name'], array)\n", " send_reply()\n", "\n", "def model_set_var_slice(message, array):\n", " arguments = json.loads(message['arguments'])\n", " start = int(arguments['start'])\n", " count = int(arguments['count'])\n", " model.set_var_slice(message['name'], start, count, array)\n", " send_reply()\n", "\n", "def model_finalize(message, array):\n", " model.finalize()\n", " send_reply()\n", "\n", "actions = {}\n", "actions['initialize'] = model_initialize\n", "actions['update'] = model_update\n", "actions['get_current_time'] = model_get_current_time\n", "actions['get_var'] = model_get_var\n", "actions['set_var'] = model_set_var\n" ], "language": "python", "metadata": {}, "outputs": [ { "ename": "WindowsError", "evalue": "[Error 126] The specified module could not be found", "output_type": "pyerr", "traceback": [ "\u001b[1;31m---------------------------------------------------------------------------\u001b[0m\n\u001b[1;31mWindowsError\u001b[0m Traceback (most recent call last)", "\u001b[1;32m\u001b[0m in \u001b[0;36m\u001b[1;34m()\u001b[0m\n\u001b[0;32m 6\u001b[0m \u001b[0mconfig_file\u001b[0m \u001b[1;33m=\u001b[0m \u001b[1;34m'../../test_data/flow_salt_2d_small/2d.mdu'\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 7\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m----> 8\u001b[1;33m \u001b[0mmodel\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mbmi\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mwrapper\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mBMIWrapper\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mengine\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mdll_path\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mconfigfile\u001b[0m\u001b[1;33m=\u001b[0m\u001b[0mconfig_file\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 9\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 10\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mmodel_initialize\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mmessage\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0marray\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32md:\\src\\github\\bmi-python\\bmi\\wrapper.pyc\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, engine, configfile)\u001b[0m\n\u001b[0;32m 239\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mconfigfile\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mconfigfile\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 240\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mknown_paths\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mappend\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'/opt/{}/lib'\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mformat\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mengine\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 241\u001b[1;33m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mlibrary\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_load_library\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 242\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 243\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0m_libname\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32md:\\src\\github\\bmi-python\\bmi\\wrapper.pyc\u001b[0m in \u001b[0;36m_load_library\u001b[1;34m(self)\u001b[0m\n\u001b[0;32m 309\u001b[0m \u001b[0mwin32api\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mSetDllDirectory\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;34m'.'\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 310\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 311\u001b[1;33m \u001b[0mresult\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mcdll\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0mLoadLibrary\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mpath\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 312\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 313\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mplatform\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0msystem\u001b[0m\u001b[1;33m(\u001b[0m\u001b[1;33m)\u001b[0m \u001b[1;33m==\u001b[0m \u001b[1;34m'Windows'\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32mC:\\Anaconda\\lib\\ctypes\\__init__.pyc\u001b[0m in \u001b[0;36mLoadLibrary\u001b[1;34m(self, name)\u001b[0m\n\u001b[0;32m 441\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 442\u001b[0m \u001b[1;32mdef\u001b[0m \u001b[0mLoadLibrary\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 443\u001b[1;33m \u001b[1;32mreturn\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_dlltype\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mname\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 444\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 445\u001b[0m \u001b[0mcdll\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mLibraryLoader\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mCDLL\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;32mC:\\Anaconda\\lib\\ctypes\\__init__.pyc\u001b[0m in \u001b[0;36m__init__\u001b[1;34m(self, name, mode, handle, use_errno, use_last_error)\u001b[0m\n\u001b[0;32m 363\u001b[0m \u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 364\u001b[0m \u001b[1;32mif\u001b[0m \u001b[0mhandle\u001b[0m \u001b[1;32mis\u001b[0m \u001b[0mNone\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[1;32m--> 365\u001b[1;33m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_handle\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0m_dlopen\u001b[0m\u001b[1;33m(\u001b[0m\u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_name\u001b[0m\u001b[1;33m,\u001b[0m \u001b[0mmode\u001b[0m\u001b[1;33m)\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0m\u001b[0;32m 366\u001b[0m \u001b[1;32melse\u001b[0m\u001b[1;33m:\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n\u001b[0;32m 367\u001b[0m \u001b[0mself\u001b[0m\u001b[1;33m.\u001b[0m\u001b[0m_handle\u001b[0m \u001b[1;33m=\u001b[0m \u001b[0mhandle\u001b[0m\u001b[1;33m\u001b[0m\u001b[0m\n", "\u001b[1;31mWindowsError\u001b[0m: [Error 126] The specified module could not be found" ] } ], "prompt_number": 1 }, { "cell_type": "code", "collapsed": false, "input": [ "import zmq\n", "import mmi\n", "\n", "# start server and listen to the requests\n", "context = zmq.Context()\n", "socket = context.socket(zmq.REP)\n", "\n", "socket.bind('tcp://*:9002')\n", "\n", "def send_reply(array=None, error=None):\n", " reply={}\n", " if error:\n", " print error\n", " reply['error'] = error\n", " mmi.send_array(socket, array, metadata=reply)" ], "language": "python", "metadata": {}, "outputs": [], "prompt_number": 2 }, { "cell_type": "code", "collapsed": false, "input": [ "import os\n", "print os.getpid()" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "18412\n" ] } ], "prompt_number": 3 }, { "cell_type": "code", "collapsed": false, "input": [ "# process messages\n", "\n", "while(1):\n", " array, message = mmi.recv_array(socket)\n", "\n", " print '[message]: ' + str(message)\n", "\n", " reply = '{}'\n", " \n", " if message.has_key('action'):\n", " action = message['action']\n", " \n", " if not actions.has_key(action):\n", " send_reply(error='error: unknown action found: ' + action)\n", " else:\n", " actions[action](message, array)\n", " else:\n", " send_reply(error='error: action was not specified, skipping message: ' + str(message))" ], "language": "python", "metadata": {}, "outputs": [ { "output_type": "stream", "stream": "stdout", "text": [ "[message]: {u'action': u'initialize', u'timestamp': u'2014-07-02T22:47:33.767000'}\n", "crashing ...\n", "[message]: {u'action': u'update', u'timestamp': u'2014-07-02T22:47:34.309000', u'arguments': u'{\"time_step\": 0.1}'}" ] }, { "output_type": "stream", "stream": "stdout", "text": [ "\n", "[message]: {u'action': u'get_var', u'timestamp': u'2014-07-02T22:47:35.474000', u'name': u'xk'}" ] } ] }, { "cell_type": "code", "collapsed": false, "input": [ "model.get_var_rank('xk')" ], "language": "python", "metadata": {}, "outputs": [ { "metadata": {}, "output_type": "pyout", "prompt_number": 5, "text": [ "1" ] } ], "prompt_number": 5 }, { "cell_type": "code", "collapsed": false, "input": [], "language": "python", "metadata": {}, "outputs": [] } ], "metadata": {} } ] }