Пример #1
0
class TestICOverride(unittest.TestCase):
    def setUp(self):
        self.sandbox = SandBox()

    def tearDown(self):
        self.sandbox.erase()

    def test_IC_override(self):
        # Run the script c1724.xml
        script_path = self.sandbox.path_to_jsbsim_file('scripts', 'c1724.xml')

        fdm = CreateFDM(self.sandbox)
        fdm.load_script(script_path)

        vt0 = fdm.get_property_value('ic/vt-kts')

        fdm.run_ic()
        self.assertEqual(fdm.get_property_value('simulation/sim-time-sec'),
                         0.0)
        self.assertAlmostEqual(fdm.get_property_value('velocities/vt-fps'),
                               vt0 / fpstokts,
                               delta=1E-7)

        ExecuteUntil(fdm, 1.0)

        # Check that the total velocity exported in the output file matches the
        # IC defined in the initialization file
        ref = Table()
        ref.ReadCSV(self.sandbox('JSBout172B.csv'))
        self.assertEqual(ref.get_column('Time')[1], 0.0)
        self.assertAlmostEqual(ref.get_column('V_{Total} (ft/s)')[1],
                               vt0 / fpstokts,
                               delta=1E-7)

        # Now, we will re-run the same test but the IC will be overridden in the
        # script. The initial total velocity is increased by 1 ft/s
        vt0 += 1.0

        # The script c1724.xml is loaded and the following line is added in it:
        #    <property value="..."> ic/vt-kts </property>
        # The modified script is then saved with the named 'c1724_0.xml'
        tree = et.parse(self.sandbox.elude(script_path))
        run_tag = tree.getroot().find("./run")
        property = et.SubElement(run_tag, 'property')
        property.text = 'ic/vt-kts'
        property.attrib['value'] = str(vt0)
        tree.write(self.sandbox('c1724_0.xml'))

        # Re-run the same check than above. This time we are making sure than
        # the total initial velocity is increased by 1 ft/s
        self.sandbox.delete_csv_files()

        # Because JSBSim internals use static pointers, we cannot rely on Python
        # garbage collector to decide when the FDM is destroyed otherwise we can
        # get dangling pointers.
        del fdm

        fdm = CreateFDM(self.sandbox)
        fdm.load_script('c1724_0.xml')

        self.assertAlmostEqual(fdm.get_property_value('ic/vt-kts'),
                               vt0,
                               delta=1E-6)

        fdm.run_ic()
        self.assertEqual(fdm.get_property_value('simulation/sim-time-sec'),
                         0.0)
        self.assertAlmostEqual(fdm.get_property_value('velocities/vt-fps'),
                               vt0 / fpstokts,
                               delta=1E-6)

        ExecuteUntil(fdm, 1.0)

        mod = Table()
        mod.ReadCSV(self.sandbox('JSBout172B.csv'))
        self.assertAlmostEqual(mod.get_column('V_{Total} (ft/s)')[1],
                               vt0 / fpstokts,
                               delta=1E-6)
Пример #2
0
class TestICOverride(unittest.TestCase):
    def setUp(self):
        self.sandbox = SandBox()

    def tearDown(self):
        self.sandbox.erase()

    def test_IC_override(self):
        # Run the script c1724.xml
        script_path = self.sandbox.path_to_jsbsim_file('scripts', 'c1724.xml')

        fdm = CreateFDM(self.sandbox)
        fdm.load_script(script_path)

        vt0 = fdm.get_property_value('ic/vt-kts')

        fdm.run_ic()
        ExecuteUntil(fdm, 1.0)

        # Check that the total velocity exported in the output file matches the IC
        # defined in the initialization file
        ref = Table()
        ref.ReadCSV(self.sandbox('JSBout172B.csv'))

        for col, title in enumerate(ref._lines[0]):
            if title == 'V_{Total} (ft/s)':
                self.assertTrue(abs(ref._lines[1][col] - (vt0 / fpstokts)) < 1E-5,
                                msg="Original script %s\nThe total velocity is %f. The value %f was expected" % (script_path, ref._lines[1][col], vt0 / fpstokts))
                break
        else:
            self.fail("The total velocity is not exported in %s" % (script_path,))

        # Now, we will re-run the same test but the IC will be overridden in the scripts
        # The initial total velocity is increased by 1 ft/s
        vt0 += 1.0

        # The script c1724.xml is loaded and the following line is added in it:
        #    <property value="..."> ic/vt-kts </property>
        # The modified script is then saved with the named 'c1724_0.xml'
        tree = et.parse(self.sandbox.elude(script_path))
        run_tag = tree.getroot().find("./run")
        property = et.SubElement(run_tag, 'property')
        property.text = 'ic/vt-kts'
        property.attrib['value'] = str(vt0)
        tree.write(self.sandbox('c1724_0.xml'))

        # Re-run the same check than above. This time we are making sure than the total
        # initial velocity is increased by 1 ft/s
        self.sandbox.delete_csv_files()

        # Because JSBSim internals use static pointers, we cannot rely on Python
        # garbage collector to decide when the FDM is destroyed otherwise we can
        # get dangling pointers.
        del fdm

        fdm = CreateFDM(self.sandbox)
        fdm.load_script('c1724_0.xml')

        self.assertTrue(abs(fdm.get_property_value('ic/vt-kts') - vt0) < 1E-5,
                        msg="Modified script %s\nThe total velocity in the IC (%f) is different from %f" % (self.sandbox('JSBout172B.csv'), fdm.get_property_value('ic/vt-kts'), vt0))

        fdm.run_ic()
        ExecuteUntil(fdm, 1.0)

        mod = Table()
        mod.ReadCSV(self.sandbox('JSBout172B.csv'))

        for col, title in enumerate(mod._lines[0]):
            if title == 'V_{Total} (ft/s)':
                self.assertTrue(abs(mod._lines[1][col] - (vt0 / fpstokts)) < 1E-5,
                                msg="Modified script %s\nThe total velocity is %f. The value %f was expected" % (self.sandbox('JSBout172B.csv'), mod._lines[1][col], vt0 / fpstokts))
                break
        else:
            self.fail("The total velocity is not exported in %s" % (sandbox('JSBout172B.csv'),))
Пример #3
0
class ResetOutputFiles(unittest.TestCase):
    def setUp(self):
        self.sandbox = SandBox()

    def tearDown(self):
        self.sandbox.erase()

    def test_reset_output_files(self):
      #
      # Regular run that checks the correct CSV file is created
      # We are just checking its existence, not its content. To accelerate the
      # test execution, the simulation is interrupted after 1.0sec of simulated
      # time.
      #
      fdm = CreateFDM(self.sandbox)
      fdm.load_script(self.sandbox.path_to_jsbsim_file('scripts', 'c1722.xml'))

      fdm.run_ic()
      ExecuteUntil(fdm, 1.0)

      self.assertTrue(self.sandbox.exists('JSBout172B.csv'),
                      msg="Standard run: the file 'JSBout172B.csv' should exist.")

      #
      # Reset the simulation and check that iteration number is correctly
      # appended to the filename.
      #
      fdm.reset_to_initial_conditions(1)
      ExecuteUntil(fdm, 1.0)

      self.assertTrue(self.sandbox.exists('JSBout172B_0.csv'),
                      msg="Reset: the file 'JSBout172B_0.csv' should exist.")

      #
      # Change the output filename and check that the naming logic is reset
      # (e.g. that no iteration number is appended to the filename)
      #
      fdm.set_output_filename(0, 'dummy.csv')
      fdm.reset_to_initial_conditions(1)
      ExecuteUntil(fdm, 1.0)

      self.assertTrue(self.sandbox.exists('dummy.csv'),
                      msg="Output name renaming: the file 'dummy.csv' should exist.")

      #
      # Call FGFDMExec::SetOutputFileName() after the simulation is reset. And
      # verify that the new output file name is ignored until the next call to
      # FGOutput::SetStartNewOutput(). This should be so according to the
      # documentation of FGOutput::SetOutputName().
      #
      fdm.reset_to_initial_conditions(1)
      fdm.set_output_filename(0, 'dummyx.csv')
      ExecuteUntil(fdm, 1.0)

      self.assertTrue(not self.sandbox.exists('dummyx.csv'),
                      msg="Late renaming: 'dummyx.csv' should not exist.")
      self.assertTrue(self.sandbox.exists('dummy_0.csv'),
                      msg="Late renaming: 'dummy_0.csv' should exist.")

      #
      # Check that the new filename is taken into account when the simulation is
      # reset.
      #
      fdm.reset_to_initial_conditions(1)
      ExecuteUntil(fdm, 1.0)

      self.assertTrue(self.sandbox.exists('dummyx.csv'),
                      msg="Reset after late renaming: 'dummyx.csv' should exist.")

      #
      # Check against multiple calls to FGFDMExec::SetOutputFileName()
      #
      fdm.set_output_filename(0, 'this_one.csv')
      fdm.set_output_filename(0, 'that_one.csv')
      fdm.reset_to_initial_conditions(1)
      ExecuteUntil(fdm, 1.0)

      self.assertTrue(not self.sandbox.exists('this_one.csv'),
                      msg="Output name overwritten: 'this_one.csv' should not exist.")
      self.assertTrue(self.sandbox.exists('that_one.csv'),
                      msg="Output name overwritten: 'that_one.csv' should exist.")

      #
      # Check again on a brand new FDM
      #
      self.sandbox.delete_csv_files()

      # Because JSBSim internals use static pointers, we cannot rely on Python
      # garbage collector to decide when the FDM is destroyed otherwise we can
      # get dangling pointers.
      del fdm

      fdm = CreateFDM(self.sandbox)
      fdm.load_script(self.sandbox.path_to_jsbsim_file('scripts', 'c1722.xml'))

      fdm.run_ic()
      fdm.set_output_filename(0,'oops.csv') # Oops!! Changed my mind
      ExecuteUntil(fdm, 1.0)

      self.assertTrue(not self.sandbox.exists('oops.csv'),
                      msg="New FDM: 'oops.csv' should not exist.")
      self.assertTrue(self.sandbox.exists('JSBout172B.csv'),
                      msg="New FDM: 'JSBout172B.csv' should exist.")

      #
      # The new file name 'oops.csv' has been ignored.
      # Check if it is now taken into account.
      #
      fdm.reset_to_initial_conditions(1)
      ExecuteUntil(fdm, 1.0)

      self.assertTrue(self.sandbox.exists('oops.csv'),
                      msg="Reset new FDM: 'oops.csv' should exist.")
Пример #4
0
fdm.set_output_filename(0, 'this_one.csv')
fdm.set_output_filename(0, 'that_one.csv')
fdm.reset_to_initial_conditions(1)
ExecuteUntil(fdm, 1.0)

if sandbox.exists('this_one.csv') or not sandbox.exists('that_one.csv'):
  if sandbox.exists('this_one.csv'):
    print "Output name overwritten: 'this_one.csv' should not exist."
  if not sandbox.exists('that_one.csv'):
    print "Output name overwritten: 'that_one.csv' should exist."
  sys.exit(-1) # 'make test' will report the test failed.

#
# Check again on a brand new FDM
#
sandbox.delete_csv_files()
fdm = CreateFDM(sandbox)
fdm.load_script(sandbox.path_to_jsbsim_file('scripts', 'c1722.xml'))

fdm.run_ic()
fdm.set_output_filename(0,'oops.csv') # Oops!! Changed my mind
ExecuteUntil(fdm, 1.0)

if sandbox.exists('oops.csv') or not sandbox.exists('JSBout172B.csv'):
  if sandbox.exists('oops.csv'):
    print "New FDM: 'oops.csv' should not exist."
  if not sandbox.exists('JSBout172B.csv'):
    print "New FDM: 'JSBout172B.csv' should exist."
  sys.exit(-1) # 'make test' will report the test failed.

#
Пример #5
0
class ResetOutputFiles(unittest.TestCase):
    def setUp(self):
        self.sandbox = SandBox()

    def tearDown(self):
        self.sandbox.erase()

    def test_reset_output_files(self):
        #
        # Regular run that checks the correct CSV file is created
        # We are just checking its existence, not its content. To accelerate the
        # test execution, the simulation is interrupted after 1.0sec of simulated
        # time.
        #
        fdm = CreateFDM(self.sandbox)
        fdm.load_script(
            self.sandbox.path_to_jsbsim_file('scripts', 'c1722.xml'))

        fdm.run_ic()
        ExecuteUntil(fdm, 1.0)

        self.assertTrue(
            self.sandbox.exists('JSBout172B.csv'),
            msg="Standard run: the file 'JSBout172B.csv' should exist.")

        #
        # Reset the simulation and check that iteration number is correctly
        # appended to the filename.
        #
        fdm.reset_to_initial_conditions(1)
        ExecuteUntil(fdm, 1.0)

        self.assertTrue(self.sandbox.exists('JSBout172B_0.csv'),
                        msg="Reset: the file 'JSBout172B_0.csv' should exist.")

        #
        # Change the output filename and check that the naming logic is reset
        # (e.g. that no iteration number is appended to the filename)
        #
        fdm.set_output_filename(0, 'dummy.csv')
        fdm.reset_to_initial_conditions(1)
        ExecuteUntil(fdm, 1.0)

        self.assertTrue(
            self.sandbox.exists('dummy.csv'),
            msg="Output name renaming: the file 'dummy.csv' should exist.")

        #
        # Call FGFDMExec::SetOutputFileName() after the simulation is reset. And
        # verify that the new output file name is ignored until the next call to
        # FGOutput::SetStartNewOutput(). This should be so according to the
        # documentation of FGOutput::SetOutputName().
        #
        fdm.reset_to_initial_conditions(1)
        fdm.set_output_filename(0, 'dummyx.csv')
        ExecuteUntil(fdm, 1.0)

        self.assertTrue(not self.sandbox.exists('dummyx.csv'),
                        msg="Late renaming: 'dummyx.csv' should not exist.")
        self.assertTrue(self.sandbox.exists('dummy_0.csv'),
                        msg="Late renaming: 'dummy_0.csv' should exist.")

        #
        # Check that the new filename is taken into account when the simulation is
        # reset.
        #
        fdm.reset_to_initial_conditions(1)
        ExecuteUntil(fdm, 1.0)

        self.assertTrue(
            self.sandbox.exists('dummyx.csv'),
            msg="Reset after late renaming: 'dummyx.csv' should exist.")

        #
        # Check against multiple calls to FGFDMExec::SetOutputFileName()
        #
        fdm.set_output_filename(0, 'this_one.csv')
        fdm.set_output_filename(0, 'that_one.csv')
        fdm.reset_to_initial_conditions(1)
        ExecuteUntil(fdm, 1.0)

        self.assertTrue(
            not self.sandbox.exists('this_one.csv'),
            msg="Output name overwritten: 'this_one.csv' should not exist.")
        self.assertTrue(
            self.sandbox.exists('that_one.csv'),
            msg="Output name overwritten: 'that_one.csv' should exist.")

        #
        # Check again on a brand new FDM
        #
        self.sandbox.delete_csv_files()

        # Because JSBSim internals use static pointers, we cannot rely on Python
        # garbage collector to decide when the FDM is destroyed otherwise we can
        # get dangling pointers.
        del fdm

        fdm = CreateFDM(self.sandbox)
        fdm.load_script(
            self.sandbox.path_to_jsbsim_file('scripts', 'c1722.xml'))

        fdm.run_ic()
        fdm.set_output_filename(0, 'oops.csv')  # Oops!! Changed my mind
        ExecuteUntil(fdm, 1.0)

        self.assertTrue(not self.sandbox.exists('oops.csv'),
                        msg="New FDM: 'oops.csv' should not exist.")
        self.assertTrue(self.sandbox.exists('JSBout172B.csv'),
                        msg="New FDM: 'JSBout172B.csv' should exist.")

        #
        # The new file name 'oops.csv' has been ignored.
        # Check if it is now taken into account.
        #
        fdm.reset_to_initial_conditions(1)
        ExecuteUntil(fdm, 1.0)

        self.assertTrue(self.sandbox.exists('oops.csv'),
                        msg="Reset new FDM: 'oops.csv' should exist.")
Пример #6
0
class TestModelLoading(unittest.TestCase):
    def setUp(self):
        self.sandbox = SandBox()

    def tearDown(self):
        self.sandbox.erase()

    def BuildReference(self, script_name):
        # Run the script
        self.script = self.sandbox.path_to_jsbsim_file(os.path.join('scripts',
                                                               script_name))
        self.sandbox.delete_csv_files()
        fdm = CreateFDM(self.sandbox)
        fdm.set_output_directive(self.sandbox.path_to_jsbsim_file('tests',
                                                                  'output.xml'))
        fdm.load_script(self.script)
        fdm.set_property_value('simulation/randomseed', 0.0)

        fdm.run_ic()
        ExecuteUntil(fdm, 50.0)

        self.ref = Table()
        self.ref.ReadCSV(self.sandbox("output.csv"))

        # Since the script will work with modified versions of the aircraft XML
        # definition file, we need to make a copy of the directory that contains
        # all the input data of that aircraft

        tree, self.aircraft_name, self.path_to_jsbsim_aircrafts = CopyAircraftDef(self.script, self.sandbox)
        self.aircraft_path = self.sandbox('aircraft', self.aircraft_name)

    def ProcessAndCompare(self, section):
        # Here we determine if the original aircraft definition <section> is
        # inline or read from an external file.
        tree = et.parse(os.path.join(self.path_to_jsbsim_aircrafts,
                                     self.aircraft_name + '.xml'))
        root = tree.getroot()

        # Iterate over all the tags named <section>
        for section_element in root.findall(section):
            if 'file' in section_element.keys():
                self.InsertAndCompare(section_element, tree)
            else:
                self.DetachAndCompare(section_element, tree)

    def DetachAndCompare(self, section_element, tree):
        # Extract <section> from the original aircraft definition file and copy
        # it in a separate XML file 'section.xml'
        section_tree = et.ElementTree(element=section_element)
        if 'name' in section_element.keys():
            section = section_element.attrib['name']
        else:
            section = section_element.tag

        section_tree.write(os.path.join(self.aircraft_path, section+'.xml'),
                           xml_declaration=True)

        # Now, we need to clean up the aircraft definition file from all
        # references to <section>. We just need a single <section> tag that
        # points to the file 'section.xml'
        for element in list(section_element):
            section_element.remove(element)

        section_element.attrib = {'file': section+'.xml'}
        tree.write(os.path.join(self.aircraft_path, self.aircraft_name+'.xml'),
                   xml_declaration=True)

        self.Compare(section)

    def InsertAndCompare(self, section_element, tree):
        file_name = append_xml(section_element.attrib['file'])
        section_file = os.path.join(self.path_to_jsbsim_aircrafts, file_name)

        # If <section> is actually <system>, we need to iterate over all the
        # directories in which the file is allowed to be stored until the file
        # is located.
        if not os.path.exists(section_file) and section_element.tag == 'system':
            section_file = os.path.join(self.path_to_jsbsim_aircrafts, "systems", file_name)
            if not os.path.exists(section_file):
                section_file = self.sandbox.elude(self.sandbox.path_to_jsbsim_file("systems", file_name))

        # The original <section> tag is dropped and replaced by the content of
        # the file.
        section_root = et.parse(section_file).getroot()

        del section_element.attrib['file']
        section_element.attrib.update(section_root.attrib)
        section_element.extend(section_root)

        tree.write(os.path.join(self.aircraft_path, self.aircraft_name+'.xml'))

        self.Compare(section_element.tag+" file:"+section_file)

    def Compare(self, section):
        # Rerun the script with the modified aircraft definition
        self.sandbox.delete_csv_files()
        fdm = CreateFDM(self.sandbox)
        # We need to tell JSBSim that the aircraft definition is located in the
        # directory build/.../aircraft
        fdm.set_aircraft_path('aircraft')
        fdm.set_output_directive(self.sandbox.path_to_jsbsim_file('tests', 'output.xml'))
        fdm.load_script(self.script)
        fdm.set_property_value('simulation/randomseed', 0.0)

        fdm.run_ic()
        ExecuteUntil(fdm, 50.0)

        mod = Table()
        mod.ReadCSV(self.sandbox('output.csv'))

        # Whether the data is read from the aircraft definition file or from an
        # external file, the results shall be exactly identical. Hence the
        # precision set to 0.0.
        diff = self.ref.compare(mod, 0.0)
        self.assertTrue(diff.empty(),
                        msg='\nTesting section "'+section+'"\n'+repr(diff))

    def test_model_loading(self):
        self.longMessage = True

        self.BuildReference('c1724.xml')
        output_ref = Table()
        output_ref.ReadCSV(self.sandbox('JSBout172B.csv'))

        self.ProcessAndCompare('aerodynamics')
        self.ProcessAndCompare('autopilot')
        self.ProcessAndCompare('flight_control')
        self.ProcessAndCompare('ground_reactions')
        self.ProcessAndCompare('mass_balance')
        self.ProcessAndCompare('metrics')
        self.ProcessAndCompare('propulsion')
        self.ProcessAndCompare('system')

        # The <output> section needs special handling. In addition to the check
        # conducted by ProcessAndCompare with a directive file, we need to
        # verify that the <output> tag has been correctly executed by JSBSim.
        # In the case of the script c1724.xml, this means that the data output
        # in JSBout172B.csv is the same between the reference 'output_ref' and
        # the result 'mod' below where the <output> tag was moved in a separate
        # file.
        self.ProcessAndCompare('output')
        mod = Table()
        mod.ReadCSV(self.sandbox('JSBout172B.csv'))
        diff = output_ref.compare(mod, 0.0)
        self.assertTrue(diff.empty(),
                        msg='\nTesting section "output"\n'+repr(diff))

        self.BuildReference('weather-balloon.xml')
        self.ProcessAndCompare('buoyant_forces')

        self.BuildReference('Concorde_runway_test.xml')
        self.ProcessAndCompare('external_reactions')
Пример #7
0
class TestICOverride(unittest.TestCase):
    def setUp(self):
        self.sandbox = SandBox()

    def tearDown(self):
        self.sandbox.erase()

    def test_IC_override(self):
        # Run the script c1724.xml
        script_path = self.sandbox.path_to_jsbsim_file('scripts', 'c1724.xml')

        fdm = CreateFDM(self.sandbox)
        fdm.load_script(script_path)

        vt0 = fdm.get_property_value('ic/vt-kts')

        fdm.run_ic()
        self.assertEqual(fdm.get_property_value('simulation/sim-time-sec'), 0.0)
        self.assertAlmostEqual(fdm.get_property_value('velocities/vt-fps'),
                               vt0 / fpstokts, delta=1E-7)

        ExecuteUntil(fdm, 1.0)

        # Check that the total velocity exported in the output file matches the
        # IC defined in the initialization file
        ref = Table()
        ref.ReadCSV(self.sandbox('JSBout172B.csv'))
        self.assertEqual(ref.get_column('Time')[1], 0.0)
        self.assertAlmostEqual(ref.get_column('V_{Total} (ft/s)')[1],
                               vt0 / fpstokts, delta=1E-7)

        # Now, we will re-run the same test but the IC will be overridden in the
        # script. The initial total velocity is increased by 1 ft/s
        vt0 += 1.0

        # The script c1724.xml is loaded and the following line is added in it:
        #    <property value="..."> ic/vt-kts </property>
        # The modified script is then saved with the named 'c1724_0.xml'
        tree = et.parse(self.sandbox.elude(script_path))
        run_tag = tree.getroot().find("./run")
        property = et.SubElement(run_tag, 'property')
        property.text = 'ic/vt-kts'
        property.attrib['value'] = str(vt0)
        tree.write(self.sandbox('c1724_0.xml'))

        # Re-run the same check than above. This time we are making sure than
        # the total initial velocity is increased by 1 ft/s
        self.sandbox.delete_csv_files()

        # Because JSBSim internals use static pointers, we cannot rely on Python
        # garbage collector to decide when the FDM is destroyed otherwise we can
        # get dangling pointers.
        del fdm

        fdm = CreateFDM(self.sandbox)
        fdm.load_script('c1724_0.xml')

        self.assertAlmostEqual(fdm.get_property_value('ic/vt-kts'), vt0,
                               delta=1E-6)

        fdm.run_ic()
        self.assertEqual(fdm.get_property_value('simulation/sim-time-sec'), 0.0)
        self.assertAlmostEqual(fdm.get_property_value('velocities/vt-fps'),
                               vt0 / fpstokts, delta=1E-6)

        ExecuteUntil(fdm, 1.0)

        mod = Table()
        mod.ReadCSV(self.sandbox('JSBout172B.csv'))
        self.assertAlmostEqual(mod.get_column('V_{Total} (ft/s)')[1],
                               vt0 / fpstokts, delta=1E-6)
Пример #8
0
class TestICOverride(unittest.TestCase):
    def setUp(self):
        self.sandbox = SandBox()

    def tearDown(self):
        self.sandbox.erase()

    def test_IC_override(self):
        # Run the script c1724.xml
        script_path = self.sandbox.path_to_jsbsim_file('scripts', 'c1724.xml')

        fdm = CreateFDM(self.sandbox)
        fdm.load_script(script_path)

        vt0 = fdm.get_property_value('ic/vt-kts')

        fdm.run_ic()
        ExecuteUntil(fdm, 1.0)

        # Check that the total velocity exported in the output file matches the IC
        # defined in the initialization file
        ref = Table()
        ref.ReadCSV(self.sandbox('JSBout172B.csv'))

        for col, title in enumerate(ref._lines[0]):
            if title == 'V_{Total} (ft/s)':
                self.assertTrue(
                    abs(ref._lines[1][col] - (vt0 / fpstokts)) < 1E-5,
                    msg=
                    "Original script %s\nThe total velocity is %f. The value %f was expected"
                    % (script_path, ref._lines[1][col], vt0 / fpstokts))
                break
        else:
            self.fail("The total velocity is not exported in %s" %
                      (script_path, ))

        # Now, we will re-run the same test but the IC will be overridden in the scripts
        # The initial total velocity is increased by 1 ft/s
        vt0 += 1.0

        # The script c1724.xml is loaded and the following line is added in it:
        #    <property value="..."> ic/vt-kts </property>
        # The modified script is then saved with the named 'c1724_0.xml'
        tree = et.parse(self.sandbox.elude(script_path))
        run_tag = tree.getroot().find("./run")
        property = et.SubElement(run_tag, 'property')
        property.text = 'ic/vt-kts'
        property.attrib['value'] = str(vt0)
        tree.write(self.sandbox('c1724_0.xml'))

        # Re-run the same check than above. This time we are making sure than the total
        # initial velocity is increased by 1 ft/s
        self.sandbox.delete_csv_files()

        # Because JSBSim internals use static pointers, we cannot rely on Python
        # garbage collector to decide when the FDM is destroyed otherwise we can
        # get dangling pointers.
        del fdm

        fdm = CreateFDM(self.sandbox)
        fdm.load_script('c1724_0.xml')

        self.assertTrue(
            abs(fdm.get_property_value('ic/vt-kts') - vt0) < 1E-5,
            msg=
            "Modified script %s\nThe total velocity in the IC (%f) is different from %f"
            % (self.sandbox('JSBout172B.csv'),
               fdm.get_property_value('ic/vt-kts'), vt0))

        fdm.run_ic()
        ExecuteUntil(fdm, 1.0)

        mod = Table()
        mod.ReadCSV(self.sandbox('JSBout172B.csv'))

        for col, title in enumerate(mod._lines[0]):
            if title == 'V_{Total} (ft/s)':
                self.assertTrue(
                    abs(mod._lines[1][col] - (vt0 / fpstokts)) < 1E-5,
                    msg=
                    "Modified script %s\nThe total velocity is %f. The value %f was expected"
                    % (self.sandbox('JSBout172B.csv'), mod._lines[1][col],
                       vt0 / fpstokts))
                break
        else:
            self.fail("The total velocity is not exported in %s" %
                      (sandbox('JSBout172B.csv'), ))
Пример #9
0
class TestModelLoading(unittest.TestCase):
    def setUp(self):
        self.sandbox = SandBox()

    def tearDown(self):
        self.sandbox.erase()

    def BuildReference(self, script_name):
        # Run the script
        self.script = self.sandbox.path_to_jsbsim_file(
            os.path.join('scripts', script_name))
        self.sandbox.delete_csv_files()
        fdm = CreateFDM(self.sandbox)
        fdm.set_output_directive(
            self.sandbox.path_to_jsbsim_file('tests', 'output.xml'))
        fdm.load_script(self.script)
        fdm.set_property_value('simulation/randomseed', 0.0)

        fdm.run_ic()
        ExecuteUntil(fdm, 50.0)

        self.ref = Table()
        self.ref.ReadCSV(self.sandbox("output.csv"))

        # Since the script will work with modified versions of the aircraft XML
        # definition file, we need to make a copy of the directory that contains
        # all the input data of that aircraft

        tree, self.aircraft_name, self.path_to_jsbsim_aircrafts = CopyAircraftDef(
            self.script, self.sandbox)
        self.aircraft_path = self.sandbox('aircraft', self.aircraft_name)

    def ProcessAndCompare(self, section):
        # Here we determine if the original aircraft definition <section> is
        # inline or read from an external file.
        tree = et.parse(
            os.path.join(self.path_to_jsbsim_aircrafts,
                         self.aircraft_name + '.xml'))
        root = tree.getroot()

        # Iterate over all the tags named <section>
        for section_element in root.findall(section):
            if 'file' in section_element.keys():
                self.InsertAndCompare(section_element, tree)
            else:
                self.DetachAndCompare(section_element, tree)

    def DetachAndCompare(self, section_element, tree):
        # Extract <section> from the original aircraft definition file and copy
        # it in a separate XML file 'section.xml'
        section_tree = et.ElementTree(element=section_element)
        if 'name' in section_element.keys():
            section = section_element.attrib['name']
        else:
            section = section_element.tag

        section_tree.write(os.path.join(self.aircraft_path, section + '.xml'),
                           xml_declaration=True)

        # Now, we need to clean up the aircraft definition file from all
        # references to <section>. We just need a single <section> tag that
        # points to the file 'section.xml'
        for element in list(section_element):
            section_element.remove(element)

        section_element.attrib = {'file': section + '.xml'}
        tree.write(os.path.join(self.aircraft_path,
                                self.aircraft_name + '.xml'),
                   xml_declaration=True)

        self.Compare(section)

    def InsertAndCompare(self, section_element, tree):
        file_name = append_xml(section_element.attrib['file'])
        section_file = os.path.join(self.path_to_jsbsim_aircrafts, file_name)

        # If <section> is actually <system>, we need to iterate over all the
        # directories in which the file is allowed to be stored until the file
        # is located.
        if not os.path.exists(
                section_file) and section_element.tag == 'system':
            section_file = os.path.join(self.path_to_jsbsim_aircrafts,
                                        "systems", file_name)
            if not os.path.exists(section_file):
                section_file = self.sandbox.elude(
                    self.sandbox.path_to_jsbsim_file("systems", file_name))

        # The original <section> tag is dropped and replaced by the content of
        # the file.
        section_root = et.parse(section_file).getroot()

        del section_element.attrib['file']
        section_element.attrib.update(section_root.attrib)
        section_element.extend(section_root)

        tree.write(
            os.path.join(self.aircraft_path, self.aircraft_name + '.xml'))

        self.Compare(section_element.tag + " file:" + section_file)

    def Compare(self, section):
        # Rerun the script with the modified aircraft definition
        self.sandbox.delete_csv_files()
        fdm = CreateFDM(self.sandbox)
        # We need to tell JSBSim that the aircraft definition is located in the
        # directory build/.../aircraft
        fdm.set_aircraft_path('aircraft')
        fdm.set_output_directive(
            self.sandbox.path_to_jsbsim_file('tests', 'output.xml'))
        fdm.load_script(self.script)
        fdm.set_property_value('simulation/randomseed', 0.0)

        fdm.run_ic()
        ExecuteUntil(fdm, 50.0)

        mod = Table()
        mod.ReadCSV(self.sandbox('output.csv'))

        # Whether the data is read from the aircraft definition file or from an
        # external file, the results shall be exactly identical. Hence the
        # precision set to 0.0.
        diff = self.ref.compare(mod, 0.0)
        self.assertTrue(diff.empty(),
                        msg='\nTesting section "' + section + '"\n' +
                        repr(diff))

    def test_model_loading(self):
        self.longMessage = True

        self.BuildReference('c1724.xml')
        output_ref = Table()
        output_ref.ReadCSV(self.sandbox('JSBout172B.csv'))

        self.ProcessAndCompare('aerodynamics')
        self.ProcessAndCompare('autopilot')
        self.ProcessAndCompare('flight_control')
        self.ProcessAndCompare('ground_reactions')
        self.ProcessAndCompare('mass_balance')
        self.ProcessAndCompare('metrics')
        self.ProcessAndCompare('propulsion')
        self.ProcessAndCompare('system')

        # The <output> section needs special handling. In addition to the check
        # conducted by ProcessAndCompare with a directive file, we need to
        # verify that the <output> tag has been correctly executed by JSBSim.
        # In the case of the script c1724.xml, this means that the data output
        # in JSBout172B.csv is the same between the reference 'output_ref' and
        # the result 'mod' below where the <output> tag was moved in a separate
        # file.
        self.ProcessAndCompare('output')
        mod = Table()
        mod.ReadCSV(self.sandbox('JSBout172B.csv'))
        diff = output_ref.compare(mod, 0.0)
        self.assertTrue(diff.empty(),
                        msg='\nTesting section "output"\n' + repr(diff))

        self.BuildReference('weather-balloon.xml')
        self.ProcessAndCompare('buoyant_forces')

        self.BuildReference('Concorde_runway_test.xml')
        self.ProcessAndCompare('external_reactions')