Set a configurable Block

Register the Block Type

To make a Block object configurable, we have to register the block type with a type name we can use in the configuration.

This is done with the @register_type decorator.

Note

Every type name we use in configuration files are registered this way.

Let’s register our RCRBlock from the last example.

@dataclass
@register_type("rcr_block")
class RCRBlock(Block):

    # Block Definition here

Dynamic Block import at simulation runtime

To use the Block object in any configuration, it has to be imported when we use the launcher. Since we don’t want to update the launcher module every time new blocks need to be imported, we import the block modules dynamically.

Dynamically import Blocks with the launcher

When you configure a launcher (see the level 1 User Guide), the user_library folder is created. Every module in the folder (and in the recursive sub-folders you created with a __init__.py file at the root) will be imported dynamically.

To use your own Block object with the launcher, just drop your block file in the folder.

Note

For our example RCR Block is already in the PhysioBlocks library. You don’t have to copy its definition to the user_library folder.

Now that we know how a Block object can be used with the launcher, we are going to see how to use it in a net description.

Set up a Block Alias

A net actually holds BlockDescription objects and not Block objects. It is the block description that holds the block type.

So if we wanted to use our Block in a net description file now, it would look like this:

{
    "nodes": ["..."],
    "blocks": {
        "block_k": {
            "type": "block_description",
            "model_type": "rcr_block",
            "flux_type": "the flux type definition here",
            "nodes": "..."
        }
    }
}

We can simplify the block description saving an alias for your specific block type:

{
    "type": "block_description",
    "model_type": "rcr_block",
    "time": "time"
}

Note

The RCR block has already an alias defined in the PhysioBlocks library. Otherwise to use it you would have to save the alias to the user_aliases folder.

Notice that we also set the time parameter to match the global name for the simulation time in our alias. The net definition now simplifies:

{
    "nodes": ["..."],
    "blocks": {
        "block_k": {
            "type": "rcr_block",
            "flux_type": "the flux type definition here",
            "nodes": "..."
        }
    }
}

We now know how to use Block we wrote in a net configuration file. In the next section, we will see how to test the Block objects we created with the gradient_test module.