示例#1
0
def test_store_config_composite_ports():
    """Test that it is possible to store the ports of a composite systems
    to a file.
    """

    sys = CompositeSpring()
    sim = Sim()
    sim.add_system(sys, "composite_root")

    # In this test a temp file is used, it should be deleted automatically
    # after the test.
    file = tempfile.NamedTemporaryFile()
    file.close()
    sim.save_config(file.name)

    file2 = open(file.name)
    simdict = json.load(file2)
    file2.close()
    print(simdict)

    ports = simdict["systems"]["composite_root"]["ports"]
    forceport = ports["in"]["force"]
    assert forceport["type"] == "scalar"
    assert forceport["value"] == 0
    assert forceport["description"] == "force acting on mass"
    assert forceport["connections"][0]["subsystem"] == "msd"
    assert forceport["connections"][0]["input"] == "f"

    posport = ports["out"]["position"]
    assert posport["type"] == "scalar"
    assert posport["value"] == 0
    assert posport["description"] == "Position"
    assert posport["connections"][0]["subsystem"] == "msd"
    assert posport["connections"][0]["output"] == "x1"
示例#2
0
def test_load_composite_connections():
    JSON_STRING = """
    {"systems": {
        "controlled_spring": {
            "type": "CompositeSystem",
            "module": "pysim.compositesystem",
            "connections": [],
            "inputs": {"amp": 0.0},
            "ports": {
                "in": {
                    "amp": {
                        "connections": [{"input": "amplitude", "subsystem": "wave_sys"}],
                        "description": "amplitude of wave",
                        "type": "scalar",
                        "value": 0.0
                        }
                    },
                "out": {
                    "out": {
                        "connections": [{"output": "x1", "subsystem": "msd"}],
                        "description": "position",
                        "type": "scalar",
                        "value": 0.0
                        },
                    "signal": {
                        "connections": [{"output": "signal", "subsystem": "wave_sys"}],
                        "description": "signal from wave",
                        "type": "scalar",
                        "value": 0.0
                        }
                    }
                },
            "stores": [],
            "subsystems": {
                "msd": {
                    "connections": [],
                    "inputs": {"b": 80.0, "f": 0.0, "k": 50.0, "m": 50.0},
                    "module": "pysim.systems.defaultsystemcollection1",
                    "stores": [],
                    "type": "MassSpringDamper"
                    },
                "wave_sys": {
                    "connections": [["signal", "msd", "f"]],
                    "inputs": {"amplitude": 50.0, "freq": 0.1, "phaseRad": 0.0},
                    "module": "pysim.systems.defaultsystemcollection1",
                    "stores": [],
                    "type": "SquareWave"
                    }
                }
            }
        }
    }
    """
    sim = Sim()
    file = tempfile.NamedTemporaryFile(delete=False, mode="w+")
    file.write(JSON_STRING)
    file.close()
    sim.load_config(file.name)
    sim.simulate(2, 0.1)
    assert np.abs(sim.systems["controlled_spring"].outputs.out-0.3240587706226495) < 1e-10
示例#3
0
def test_composite_vs_connected_outputs():
    """Test that the same result is given regardless if two systems are 
    connected externally or put together in a composite system"""

    sim = Sim()

    #Externally connected systems
    msd = MassSpringDamper()
    msd.inputs.b = 80
    msd.inputs.m = 50
    msd.inputs.f = 0
    sim.add_system(msd)

    sw = SquareWave()
    sw.inputs.amplitude = 50
    sw.inputs.freq = 0.1
    sim.add_system(sw)

    sw.connections.add_connection("signal",msd,"f")

    #Composite system
    cd = ControlledSpring()
    sim.add_system(cd)

    sim.simulate(2, 0.1)

    assert cd.outputs.out == msd.states.x1
示例#4
0
def test_load_config():
    """Tests the loading of a system configuration from file"""

    configstring = """{
    "systems": {
        "vanderpol1": {
            "inputs": {
                "a": 1.234,
                "b": 1.0
            },
            "module": "pysim.systems.defaultsystemcollection1",
            "type": "VanDerPol",
            "connections": []
        },
       "vanderpol2": {
            "inputs": {
                "a": 1.0,
                "b": 3.456
            },
            "module": "pysim.systems.python_systems",
            "type": "VanDerPol",
            "connections": []
        }
    }
}
"""

    sim = Sim()
    file = tempfile.NamedTemporaryFile(delete=False, mode="w+")
    file.write(configstring)
    file.close()
    sim.load_config(file.name)
    assert sim.systems["vanderpol1"].inputs.a == 1.234
    assert sim.systems["vanderpol2"].inputs.b == 3.456
示例#5
0
 def setUp(self):
     self.sim = Sim()
     self.sys = MassSpringDamper()
     self.sys.store("x1")
     self.sim.add_system(self.sys)
     self.integrationlength = 50
     self.timestep = 0.1
def test_example_system():
    """Test the example system against a known solutin"""
    sys = ExampleSystem()
    sim = Sim()
    sim.add_system(sys)
    sim.simulate(5,0.1)
    assert abs(sys.states.x - 0.609483796797075) < 1e-14
示例#7
0
def test_load_config():
    """Tests the loading of a system configuration from file"""

    configstring = """{
    "systems": {
        "vanderpol1": {
            "inputs": {
                "a": 1.234,
                "b": 1.0
            },
            "module": "pysim.systems.defaultsystemcollection1",
            "type": "VanDerPol",
            "connections": []
        },
       "vanderpol2": {
            "inputs": {
                "a": 1.0,
                "b": 3.456
            },
            "module": "pysim.systems.python_systems",
            "type": "VanDerPol",
            "connections": []
        }
    }
}
"""

    sim = Sim()
    file = tempfile.NamedTemporaryFile(delete=False, mode="w+")
    file.write(configstring)
    file.close()
    sim.load_config(file.name)
    assert sim.systems["vanderpol1"].inputs.a == 1.234
    assert sim.systems["vanderpol2"].inputs.b == 3.456
示例#8
0
def test_load_connections():
    JSON_STRING = """
    {"systems": {
        "msd": {
            "connections": [],
            "inputs": {"b": 80.0, "f": 0.0, "k": 50.0, "m": 50.0},
            "module": "pysim.systems.defaultsystemcollection1",
            "stores": [],
            "type": "MassSpringDamper"
            },
        "wave_sys": {
            "connections": [["signal", "msd", "f"]],
            "inputs": {"amplitude": 50.0, "freq": 0.1,"phaseRad": 0.0},
            "module": "pysim.systems.defaultsystemcollection1",
            "stores": [],
            "type": "SquareWave"
            }
        }
    }
    """
    sim = Sim()
    file = tempfile.NamedTemporaryFile(delete=False, mode="w+")
    file.write(JSON_STRING)
    file.close()
    sim.load_config(file.name)
    sim.simulate(2, 0.1)
    res = sim.systems["msd"].states.x1
    assert np.abs(sim.systems["msd"].states.x1-0.3240587706226495) < 1e-10
示例#9
0
def test_store_vector():
    """Test that it is possible to store a boost vector"""
    sim = Sim()
    sys = RigidBody()
    sys.store("position")
    sys.inputs.force =  [1,0,0]
    sim.add_system(sys)
    sim.simulate(20,0.01)
    diff = sys.res.position[-1,:]-sys.states.position
    assert np.all(diff == np.array([0.0, 0.0, 0.0]))
示例#10
0
def test_composite_poststep():
    """Test that composite post step functionality is working"""
    sim = Sim()
    ps = PostStepCompositeSystem()

    sim.add_system(ps)
    sim.simulate(2, 0.1)

    assert ps.outputs.state_scalar_out == 1.23*2
    assert np.all(ps.outputs.state_vector_out == np.ones(3)*4.56*2)
    assert np.all(ps.outputs.state_matrix_out == np.ones((3,3))*7.89*2)
示例#11
0
def test_time_store(test_class):
    """Check that the time is stored, and that the stored values includes
    both the beginning and the end of the simulation"""
    sim = Sim()
    sys = test_class()
    sim.add_system(sys)

    sim.simulate(2, 0.1)
    reftime = np.linspace(0,2,21)
    simtime = sys.res.time
    assert np.all(np.abs(simtime-reftime) <= np.finfo(float).eps)
示例#12
0
def test_store_der(test_class):
    """Test that it is possible to store a der"""
    sim = Sim()
    sys = test_class()
    sys.store("dx")

    sim.add_system(sys)
    sim.simulate(10, 0.1)
    dxres = sys.res.dx
    assert isinstance(dxres, np.ndarray)
    assert dxres.size == 101
示例#13
0
文件: work.py 项目: aldebjer/pysim
def main():
    """Test that the elapsed time is returned from the simulation"""
    sys = VanDerPol()
    sys.store("x")
    sys.store("y")
    sim = Sim()
    sim.add_system(sys)
    sim.simulate(20, 0.1)
    plt.plot(sys.res.time,sys.res.x)
    plt.plot(sys.res.time,sys.res.y)
    plt.show()
示例#14
0
class CythonIntegrationTest(IntegrationTest):
    """Use a Cython System to simulate. The results are compared with the
    analytical results.
    """
    def setUp(self):
        self.sim = Sim()
        self.sys = PyMassSpringDamper()
        self.sys.store("x1")
        self.sim.add_system(self.sys)
        self.integrationlength = 50
        self.timestep = 0.1
示例#15
0
def test_store_output():
    """Test that it is possible to store a state"""
    sim = Sim()
    sys = SquareWave()
    sys.store("signal")

    sim.add_system(sys)
    sim.simulate(10, 0.1)
    res = sys.res.signal
    assert isinstance(res,np.ndarray)
    assert res.size == 101
示例#16
0
def test_store_state(test_class):
    """Test that it is possible to store a state"""
    sim = Sim()
    sys = test_class()
    sys.store("x")

    sim.add_system(sys)
    sim.simulate(10, 0.1)
    xres = sys.res.x
    assert isinstance(xres, np.ndarray)
    assert xres.size == 101
示例#17
0
class CythonIntegrationTest(IntegrationTest):
    """Use a Cython System to simulate. The results are compared with the
    analytical results.
    """

    def setUp(self):
        self.sim = Sim()
        self.sys = PyMassSpringDamper()
        self.sys.store("x1")
        self.sim.add_system(self.sys)
        self.integrationlength = 50
        self.timestep = 0.1
示例#18
0
def test_store_matrix(test_class):
    """Test that it is possible to store a matrix"""
    sim = Sim()
    sys = test_class()
    ref_mat = np.array([[1,0,0],
                        [0,2,0],
                        [0,0,3]])
    sys.store("input_output_matrix")
    sys.inputs.input_matrix =ref_mat
    sim.add_system(sys)
    sim.simulate(2,0.1)
    assert np.all(sys.res.input_output_matrix[-1]==ref_mat)
示例#19
0
def test_state_break_smaller():
    """Stop the simulation once the value of a state is 
    larger than a preset value
    """
    sim = Sim()
    sys = VanDerPol()
    sys.add_break_smaller("x",-1.0)
    sim.add_system(sys)
    sim.simulate(20,0.01)

    #If correct the simulation should break at time 2.52
    assert sys.res.time[-1] == 2.52
示例#20
0
def test_store_after_added_system(test_class):
    """Tests that it is possible to first add a system to a simulation and
    then store parameters.
    """
    sim = Sim()
    sys = test_class()
    sim.add_system(sys)
    sys.store("x")

    sim.simulate(10, 0.1)
    ressize = sys.res.x.size
    assert ressize == 101
示例#21
0
def test_cpp_poststep():
    """Test that cpp system post step functionality is working"""

    sim = Sim()

    ps = PostStepTestSystemCpp()
    sim.add_system(ps)
    sim.simulate(2, 0.1)

    assert ps.states.state_scalar == 1.23*2
    assert np.all(ps.states.state_vector == np.ones(3)*4.56*2)
    assert np.all(ps.states.state_matrix == np.zeros((3,3)))
示例#22
0
def test_store_input(test_class):
    """Test that it is possible to store an input"""
    sim = Sim()
    sys = test_class()
    sys.store("a")

    sim.add_system(sys)
    sim.simulate(10, 0.1)
    ares = sys.res.a
    assert isinstance(ares,np.ndarray)
    assert ares.size == 101
    assert np.all(ares==1.0)
示例#23
0
def test_interval_store(test_class):
    """Check that it is possible to change the interval between stored
    values.
    """
    sim = Sim()
    sys = test_class()
    sys.set_store_interval(0.2)
    sim.add_system(sys)

    sim.simulate(2, 0.1)
    reftime = np.linspace(0,2,11)
    simtime = sys.res.time
    assert np.all(np.abs(simtime-reftime) <= np.finfo(float).eps)
示例#24
0
def test_connected_adder_system(adder_class1,adder_class2):
    """Check that it is possible to connect systems to each other 
    with boost vector outputs/inputs"""
    sys1 = adder_class1()
    sys2 = adder_class2()
    sys1.inputs.input1 = [1,2,3]
    sys1.connections.add_connection("output1",sys2,"input1")
    sim = Sim()
    sim.add_system(sys1)
    sim.add_system(sys2)
    assert np.all(sys2.outputs.output1 == [0.0, 0.0, 0.0])
    sim.simulate(1,0.1)
    assert np.all(sys2.outputs.output1 == [1.0, 2.0, 3.0])
示例#25
0
def test_continue_store(test_class):
    """Test that it is possible to continue a simulation without storing
    values twice.
    """
    sim = Sim()
    sys = test_class()
    sim.add_system(sys)

    sys.store("x")
    sim.simulate(1, 0.1)
    sim.simulate(1, 0.1)
    reftime = np.linspace(0,2,21)
    simtime = sys.res.time
    assert np.all(np.abs(simtime-reftime) <= np.finfo(float).eps)
示例#26
0
def test_store_connections():
    """Test that it is possible to store connections in a config file"""
    sim = Sim()
    wave_sys = SquareWave()
    wave_sys.inputs.amplitude = 50
    wave_sys.inputs.freq = 0.1
    sim.add_system(wave_sys, "wave_sys")

    msd = MassSpringDamper()
    msd.inputs.b = 80
    msd.inputs.m = 50
    msd.inputs.f = 0
    sim.add_system(msd, "msd")

    wave_sys.connections.add_connection("signal", msd, "f")

    # In this test a temp file is used, it should be deleted automatically
    # after the test.
    file = tempfile.NamedTemporaryFile()
    file.close()
    sim.save_config(file.name)

    file2 = open(file.name)
    simdict = json.load(file2)
    file2.close()
    print(simdict)

    cons = simdict["systems"]["wave_sys"]["connections"]
    assert cons[0] == ["signal", "msd", "f"]
示例#27
0
def test_der_as_output():
    """Test that it is possible to connect a derivative as output
    
    The der output should behave as a normal output. That this is the case is
    tested by connecting two systems to each output (der/output) and compare
    them.
    
    """
    sys1 = MassSpringDamper()
    sys2 = InOutTestSystem()
    sys3 = InOutTestSystem()
    sys1.connections.add_connection("dx2", sys2, "input_scalar")
    sys1.connections.add_connection("acceleration", sys3, "input_scalar")

    sys2.store("input_output_scalar")
    sys3.store("input_output_scalar")

    sim = Sim()
    sim.add_system(sys1)
    sim.add_system(sys2)
    sim.add_system(sys3)

    sim.simulate(1, 0.1)

    output_from_der = sys2.res.input_output_scalar
    output_from_output = sys3.res.input_output_scalar
    assert np.allclose(output_from_der, output_from_output)
示例#28
0
def test_connected_system():
    """Check that the time for stored values in a discrete system is
    regurarly spaced"""

    #Create Simulaton
    sim = Sim()

    #Create, setup and add system to simulation
    sys = MassSpringDamper()
    sys.store("x1")
    sys.inputs.b = 50
    sys.inputs.f = 0
    sim.add_system(sys)

    controlsys = DiscretePID()
    controlsys.inputs.refsig = 1.0
    controlsys.inputs.p = 1
    controlsys.inputs.plim = 400.0
    controlsys.inputs.i = 0
    controlsys.inputs.stepsize = 0.3
    controlsys.store("outsig")
    sim.add_system(controlsys)

    sys.connections.add_connection("x1", controlsys, "insig")
    sys.connections.add_connection("x2", controlsys, "dsig")
    controlsys.connections.add_connection("outsig", sys, "f")
    controlsys.inputs.d = 1

    sim.simulate(5, 0.1)

    assert np.max(np.abs(np.diff(controlsys.res.time)) - 0.1) < 1e-14
    assert np.max(np.abs(np.diff(sys.res.time)) - 0.1) < 1e-14
示例#29
0
def test_connected_system(test_class1,test_class2):
    """Check that it is possible to connect systems to each other 
    with matrix outputs/inputs"""
    sys1 = test_class1()
    sys2 = test_class2()
    refarray =  [[1,2,3],[4,5,6],[7,8,9]]
    sys1.inputs.input_matrix = refarray
    sys1.connections.add_connection("input_output_matrix",sys2,"input_matrix")
    sim = Sim()
    sim.add_system(sys1)
    sim.add_system(sys2)
    assert np.all(sys2.outputs.input_output_matrix == np.zeros((3,3)))
    sim.simulate(1,0.1)
    assert np.all(sys2.outputs.input_output_matrix == refarray)
示例#30
0
def test_nan_connection_matrix(sys1_class,sys2_class):
    """Check that an exception is thrown if a scalar NaN is being copied
     to another system.
    """
    sys1 = sys1_class()
    sys2 = sys2_class()

    sys1.inputs.input_matrix= [[float('nan'),0,0],[0,0,0],[0,0,0]]
    sys1.connections.add_connection("input_output_matrix",sys2,"input_matrix")

    sim = Sim()
    sim.add_system(sys1)
    sim.add_system(sys2)
    with pytest.raises(RuntimeError):
        sim.simulate(0.1,0.1)
示例#31
0
def test_boost_vector_states():
    """Perform a basic simulation of a system with boost vector states"""
    sim = Sim()
    sys = RigidBody()

    sys.store("position")

    sys.inputs.force =  [1.0,0.0,0.0]
    sys.inputs.mass = 1.0

    sim.add_system(sys)
    sim.simulate(20,0.01)

    pos = sys.res.position
    diff = np.abs(pos[-1,:]-[200,0,0])
    assert np.max(diff) <= 1
示例#32
0
 def setUp(self):
     self.sim = Sim()
     self.sys = MassSpringDamper()
     self.sys.store("x1")
     self.sim.add_system(self.sys)
     self.integrationlength = 50
     self.timestep = 0.1
示例#33
0
def test_port_connections():
    """Test the port connections to and from subsystems"""
    cs = CompositeTestSystem()
    ref_scalar = 5.0
    ref_vector = (6.0, 7.0, 8.0)
    ref_matrix = ((9.0, 10.0, 11.0),(12.0, 13.0, 14.0),(15.0, 16.0, 17.0))
    cs.inputs.scalar_in_0 = ref_scalar
    cs.inputs.vector_in_0 = ref_vector
    cs.inputs.matrix_in_0 = ref_matrix
    sim = Sim()
    sim.add_system(cs)
    sim.simulate(0.5, 0.1)

    assert cs.outputs.output_scalar_0 == ref_scalar
    assert_array_almost_equal(cs.outputs.output_vector_0, ref_vector, 18)
    assert_array_almost_equal(cs.outputs.output_matrix_0,ref_matrix, 18)
示例#34
0
def test_gettime(test_class):
    """Test that the elapsed time is returned from the simulation"""
    sys = test_class()
    sim = Sim()
    sim.add_system(sys)
    integrationlength = 2.0
    assert sim.get_time() == 0.0
    sim.simulate(integrationlength, 0.1)
    assert sim.get_time() == integrationlength
示例#35
0
def test_output_change(adder_class):
    """Tests that it is possible to change input array.
    To make sure that the input is actually used in the simulation
    this is done by setting a input, run a simulation and
    see that the output value has changed.
    """
    sys = adder_class()
    refarray1 = np.array((0.0, 0.0, 0.0))
    inputarray = (1.0, 2.0, 3.0)
    sys.inputs.input1 = inputarray
    x1 = sys.outputs.output1
    assert np.array_equal(x1, refarray1)
    sim = Sim()
    sim.add_system(sys)
    sim.simulate(1, 0.1)
    x2 = sys.outputs.output1
    assert np.array_equal(x2, inputarray)
示例#36
0
def test_store_config_composite_ports():
    """Test that it is possible to store the ports of a composite systems
    to a file.
    """

    sys = CompositeSpring()
    sim = Sim()
    sim.add_system(sys, "composite_root")

    # In this test a temp file is used, it should be deleted automatically
    # after the test.
    file = tempfile.NamedTemporaryFile()
    file.close()
    sim.save_config(file.name)

    file2 = open(file.name)
    simdict = json.load(file2)
    file2.close()
    print(simdict)

    ports = simdict["systems"]["composite_root"]["ports"]
    forceport = ports["in"]["force"]
    assert forceport["type"] == "scalar"
    assert forceport["value"] == 0
    assert forceport["description"] == "force acting on mass"
    assert forceport["connections"][0]["subsystem"] == "msd"
    assert forceport["connections"][0]["input"] == "f"

    posport = ports["out"]["position"]
    assert posport["type"] == "scalar"
    assert posport["value"] == 0
    assert posport["description"] == "Position"
    assert posport["connections"][0]["subsystem"] == "msd"
    assert posport["connections"][0]["output"] == "x1"
示例#37
0
def test_load_connections():
    JSON_STRING = """
    {"systems": {
        "msd": {
            "connections": [],
            "inputs": {"b": 80.0, "f": 0.0, "k": 50.0, "m": 50.0},
            "module": "pysim.systems.defaultsystemcollection1",
            "stores": [],
            "type": "MassSpringDamper"
            },
        "wave_sys": {
            "connections": [["signal", "msd", "f"]],
            "inputs": {"amplitude": 50.0, "freq": 0.1,"phaseRad": 0.0},
            "module": "pysim.systems.defaultsystemcollection1",
            "stores": [],
            "type": "SquareWave"
            }
        }
    }
    """
    sim = Sim()
    file = tempfile.NamedTemporaryFile(delete=False, mode="w+")
    file.write(JSON_STRING)
    file.close()
    sim.load_config(file.name)
    sim.simulate(2, 0.1)
    res = sim.systems["msd"].states.x1
    assert np.abs(sim.systems["msd"].states.x1 - 0.3240587706226495) < 1e-10
示例#38
0
def test_matrix_parameter():
    """Test that it is possible to set a vector parameter"""
    sys = get_system()
    assert sys.pars.parameter_matrix == [[1, 2, 3], [4, 5, 6]]
    sim = Sim()
    sim.add_system(sys)
    sim.simulate(0.1, 0.1)
    assert sys.outputs.output_from_matrix == 1
示例#39
0
def test_map_parameter():
    """Test that it is possible to set a map parameter"""
    sys = get_system()
    assert sys.pars.parameter_map == {'a': 1, 'b': 2}
    sim = Sim()
    sim.add_system(sys)
    sim.simulate(0.1, 0.1)
    assert sys.outputs.output_from_map == 1
示例#40
0
def test_vectormap_parameter():
    """Test that it is possible to set a vectormap parameter"""
    sys = get_system()
    assert sys.pars.parameter_vectormap == {'a': [1, 2], 'b': [3, 4]}
    sim = Sim()
    sim.add_system(sys)
    sim.simulate(0.1, 0.1)
    assert sys.outputs.output_from_vectormap == 2
示例#41
0
def test_midsim_store(test_class):
    """Check that it is possible to store a variable mid-simulation
    and that the result array is properly aligned with the timesteps
    and contains np.nan for locations where the variable was not stored.
    """
    sim = Sim()
    sys = test_class()

    sim.add_system(sys)
    sim.simulate(5, 1)
    sys.store("a")
    sim.simulate(5, 1)
    ares = sys.res.a
    assert np.all(np.isnan(ares[:6]))
    assert not np.any(np.isnan(ares[6:]))
示例#42
0
def test_connected_subsystems():
    """Test that subsystems can be connected"""

    cd = ControlledSpring()
    sim = Sim()
    sim.add_system(cd)

    sim.simulate(2, 0.1)
    assert np.abs(cd.outputs.out-0.3240587706226495) < 1e-10
示例#43
0
def test_connection_to_composite(spring_class):
    """Test that it is possible to connect from an ordinary system to
    a composite
    """
    sim = Sim()

    msd = spring_class()
    sim.add_system(msd)

    sw = SquareWave()
    sw.inputs.amplitude = 50
    sw.inputs.freq = 0.1
    sim.add_system(sw)

    sw.connections.add_connection("signal",msd,"force")
    sim.simulate(2, 0.1)
    assert np.abs(msd.outputs.position - 0.3240587706226495) < 1e-10
示例#44
0
def test_store_vector():
    """Test that it is possible to store a boost vector"""
    sim = Sim()
    sys = RigidBody()
    sys.store("position")
    sys.inputs.force = [1, 0, 0]
    sim.add_system(sys)
    sim.simulate(20, 0.01)
    diff = sys.res.position[-1, :] - sys.states.position
    assert np.all(diff == np.array([0.0, 0.0, 0.0]))
示例#45
0
def test_store_matrix(test_class):
    """Test that it is possible to store a matrix"""
    sim = Sim()
    sys = test_class()
    ref_mat = np.array([[1, 0, 0], [0, 2, 0], [0, 0, 3]])
    sys.store("input_output_matrix")
    sys.inputs.input_matrix = ref_mat
    sim.add_system(sys)
    sim.simulate(2, 0.1)
    assert np.all(sys.res.input_output_matrix[-1] == ref_mat)
示例#46
0
def test_composite_prestep():
    """Test that composite post step functionality is working"""
    sim = Sim()
    ps = PreStepCompositeSystem()

    sim.add_system(ps)
    sim.simulate(1, 1)

    assert np.all(ps.outputs.state_vector_derived == 
                  [-31.919999999999995, 54.71999999999999, -22.799999999999997])
示例#47
0
def test_composite_poststep():
    """Test that composite post step functionality is working"""
    sim = Sim()
    ps = PostStepCompositeSystem()

    sim.add_system(ps)
    sim.simulate(2, 0.1)

    assert ps.outputs.state_scalar_out == 1.23 * 2
    assert np.all(ps.outputs.state_vector_out == np.ones(3) * 4.56 * 2)
    assert np.all(ps.outputs.state_matrix_out == np.ones((3, 3)) * 7.89 * 2)
示例#48
0
def test_store_der(test_class):
    """Test that it is possible to store a der"""
    sim = Sim()
    sys = test_class()
    sys.store("dx")

    sim.add_system(sys)
    sim.simulate(10, 0.1)
    dxres = sys.res.dx
    assert isinstance(dxres, np.ndarray)
    assert dxres.size == 101
示例#49
0
def test_store_state(test_class):
    """Test that it is possible to store a state"""
    sim = Sim()
    sys = test_class()
    sys.store("x")

    sim.add_system(sys)
    sim.simulate(10, 0.1)
    xres = sys.res.x
    assert isinstance(xres, np.ndarray)
    assert xres.size == 101
示例#50
0
def test_system_store():
    """Test that it is possible to store the output from a composite system"""
    cd = CompositeSpring()
    sim = Sim()
    sim.add_system(cd)
    cd.store("position")

    sim.simulate(2, 0.1)

    assert np.abs(cd.res.position[5]-0.90450499444532406) < 1e-7
    assert np.abs(cd.res.position[-1]-0.3240587706226495) < 1e-7
示例#51
0
def test_time_store(test_class):
    """Check that the time is stored, and that the stored values includes
    both the beginning and the end of the simulation"""
    sim = Sim()
    sys = test_class()
    sim.add_system(sys)

    sim.simulate(2, 0.1)
    reftime = np.linspace(0, 2, 21)
    simtime = sys.res.time
    assert np.all(np.abs(simtime - reftime) <= np.finfo(float).eps)
示例#52
0
def test_store_output():
    """Test that it is possible to store a state"""
    sim = Sim()
    sys = SquareWave()
    sys.store("signal")

    sim.add_system(sys)
    sim.simulate(10, 0.1)
    res = sys.res.signal
    assert isinstance(res, np.ndarray)
    assert res.size == 101
示例#53
0
文件: work.py 项目: freol35241/pysim
def main():
    """Test that the elapsed time is returned from the simulation"""
    sys = VanDerPol()
    sys.store("x")
    sys.store("y")
    sim = Sim()
    sim.add_system(sys)
    sim.simulate(20, 0.1)
    plt.plot(sys.res.time, sys.res.x)
    plt.plot(sys.res.time, sys.res.y)
    plt.show()
示例#54
0
def main():
    """Test that the elapsed time is returned from the simulation"""
    vdp = VanDerPol()
    vdp.store("x")
    vdp.store("y")
    sim = Sim()

    sim.add_system(vdp, "Cython1")
    sim.simulate(20, 0.1)
    plt.plot(vdp.res.x)
    plt.plot(vdp.res.y)
    plt.show()
示例#55
0
def test_state_break_larger():
    """Stop the simulation once the value of a state is 
    larger than a preset value
    """
    sim = Sim()
    sys = VanDerPol()
    sys.add_break_greater("y", 1.0)
    sim.add_system(sys)
    sim.simulate(20, 0.01)

    #If correct the simulation should break at time 0.79
    assert sys.res.time[-1] == 0.79
示例#56
0
def test_store_input(test_class):
    """Test that it is possible to store an input"""
    sim = Sim()
    sys = test_class()
    sys.store("a")

    sim.add_system(sys)
    sim.simulate(10, 0.1)
    ares = sys.res.a
    assert isinstance(ares, np.ndarray)
    assert ares.size == 101
    assert np.all(ares == 1.0)
示例#57
0
def test_cpp_poststep():
    """Test that cpp system post step functionality is working"""

    sim = Sim()

    ps = PostStepTestSystemCpp()
    sim.add_system(ps)
    sim.simulate(2, 0.1)

    assert ps.states.state_scalar == 1.23 * 2
    assert np.all(ps.states.state_vector == np.ones(3) * 4.56 * 2)
    assert np.all(ps.states.state_matrix == np.zeros((3, 3)))