[1]:
# import cosapp base classes
from cosapp.base import System, Port

Create a simple system

  • From scratch (for more information, see tutorials)

[2]:
class XPort(Port):
    def setup(self):
        self.add_variable('x', 1.0)


class Multiply(System):

    def setup(self):
        """Defines system structure"""
        self.add_inward('K1', 5.0)      # define a new input variable
        self.add_input(XPort, 'p_in')   # define a new input port
        self.add_output(XPort, 'p_out') # define a new output port

    def compute(self):
        """Defines what the system does"""
        self.p_out.x = self.p_in.x * self.K1

Instanciate a system; it creates a new object with the class properties:

[3]:
s1 = Multiply('mult')

quickstart system

  • Reuse an existing System from a library (here the default cosapp test library)

[4]:
from cosapp.tests.library.systems import Multiply1

s2 = Multiply1('s2')

Run it!

Change inputs if necessary

[5]:
s1.K1 = 2.8
s1.p_in.x = 10.

s2.K1 = 5.0
s2.p_in.x = 1.5
[6]:
s1.run_once()
s2.run_once()

Have a look at your inwards, inputs and outputs:

[7]:
print(
    f"{s1.inwards = }",
    f"{s1.p_in  = }",
    f"{s1.p_out = }",
    "",
    f"{s2.inwards = }",
    f"{s2.p_in  = }",
    f"{s2.p_out = }",
    sep="\n",
)
s1.inwards = ExtensiblePort: {'K1': 2.8}
s1.p_in  = XPort: {'x': 10.0}
s1.p_out = XPort: {'x': 28.0}

s2.inwards = ExtensiblePort: {'K1': 5.0}
s2.p_in  = XPort: {'x': 1.5}
s2.p_out = XPort: {'x': 7.5}

Congrats! You’ve run your first CoSApp model!

Let’s continue

Because CoSApp is much more, have a look at the tutorials!