def test_gust_reset(self): fdm = CreateFDM(self.sandbox) fdm.load_script(self.sandbox.path_to_jsbsim_file('scripts', 'c172_cruise_8K.xml')) fdm['simulation/randomseed'] = 0.0 fdm.set_output_directive(self.sandbox.path_to_jsbsim_file('tests', 'output.xml')) fdm.run_ic() ExecuteUntil(fdm, 15.5) ref = pd.read_csv('output.csv', index_col=0) fdm['simulation/randomseed'] = 0.0 fdm.reset_to_initial_conditions(1) ExecuteUntil(fdm, 15.5) current = pd.read_csv('output_0.csv', index_col=0) # Check the data are matching i.e. the time steps are the same between # the two data sets and that the output data are also the same. self.assertTrue(isDataMatching(ref, current)) # Find all the data that are differing by more than 1E-8 between the # two data sets. diff = FindDifferences(ref, current, 1E-8) self.longMessage = True self.assertEqual(len(diff), 0, msg='\n'+diff.to_string())
def testChannelRate(self): os.environ['JSBSIM_DEBUG'] = str(0) fdm = CreateFDM(self.sandbox) fdm.load_script( self.sandbox.path_to_jsbsim_file('scripts', 'systems-rate-test-0.xml')) fdm.run_ic() while fdm['simulation/sim-time-sec'] < 30: fdm.run() self.assertEqual(fdm['simulation/frame'], fdm['tests/rate-1']) self.assertEqual(int(fdm['simulation/frame'] / 4), fdm['tests/rate-4']) self.assertEqual(fdm['simulation/sim-time-sec'], fdm['tests/rate-1-dt-sum']) self.assertAlmostEqual( fdm['simulation/dt'] * fdm['tests/rate-4'] * 4, fdm['tests/rate-4-dt-sum']) self.assertEqual(fdm['simulation/dt'], fdm['tests/rate-1-dt']) self.assertEqual(fdm['simulation/dt'] * 4, fdm['tests/rate-4-dt']) try: fdm['simulation/do_simple_trim'] = 1 except RuntimeError as e: # The trim cannot succeed. Just make sure that the raised exception # is due to the trim failure otherwise rethrow. if e.args[0] != 'Trim Failed': raise while fdm['simulation/sim-time-sec'] < 40: self.assertEqual(fdm['simulation/frame'], fdm['tests/rate-1']) self.assertEqual(int(fdm['simulation/frame'] / 4), fdm['tests/rate-4']) self.assertEqual(fdm['simulation/sim-time-sec'], fdm['tests/rate-1-dt-sum']) self.assertAlmostEqual( fdm['simulation/dt'] * fdm['tests/rate-4'] * 4, fdm['tests/rate-4-dt-sum']) fdm.run() fdm.reset_to_initial_conditions(1) tf = fdm['tests/rate-1-dt-sum'] xtraFrames = fdm['simulation/frame'] % 4 while fdm['simulation/sim-time-sec'] < 30: fdm.run() self.assertEqual(fdm['simulation/frame'], fdm['tests/rate-1']) self.assertEqual(int((fdm['simulation/frame'] - xtraFrames) / 4), fdm['tests/rate-4']) self.assertAlmostEqual(fdm['simulation/sim-time-sec'], fdm['tests/rate-1-dt-sum'] - tf) self.assertAlmostEqual( fdm['simulation/dt'] * fdm['tests/rate-4'] * 4, fdm['tests/rate-4-dt-sum'])
def testChannelRate(self): os.environ['JSBSIM_DEBUG'] = str(0) fdm = CreateFDM(self.sandbox) fdm.load_script( self.sandbox.path_to_jsbsim_file('scripts', 'systems-rate-test-0.xml')) fdm.run_ic() while fdm['simulation/sim-time-sec'] < 30: fdm.run() self.assertEqual(fdm['simulation/frame'], fdm['tests/rate-1']) self.assertEqual(int(fdm['simulation/frame'] / 4), fdm['tests/rate-4']) self.assertEqual(fdm['simulation/sim-time-sec'], fdm['tests/rate-1-dt-sum']) self.assertAlmostEqual( fdm['simulation/dt'] * fdm['tests/rate-4'] * 4, fdm['tests/rate-4-dt-sum']) self.assertEqual(fdm['simulation/dt'], fdm['tests/rate-1-dt']) self.assertEqual(fdm['simulation/dt'] * 4, fdm['tests/rate-4-dt']) # Trigger the trimming and check that it fails (i.e. it raises an # exception TrimFailureError) with self.assertRaises(TrimFailureError): fdm['simulation/do_simple_trim'] = 1 while fdm['simulation/sim-time-sec'] < 40: self.assertEqual(fdm['simulation/frame'], fdm['tests/rate-1']) self.assertEqual(int(fdm['simulation/frame'] / 4), fdm['tests/rate-4']) self.assertEqual(fdm['simulation/sim-time-sec'], fdm['tests/rate-1-dt-sum']) self.assertAlmostEqual( fdm['simulation/dt'] * fdm['tests/rate-4'] * 4, fdm['tests/rate-4-dt-sum']) fdm.run() fdm.reset_to_initial_conditions(1) tf = fdm['tests/rate-1-dt-sum'] xtraFrames = fdm['simulation/frame'] % 4 while fdm['simulation/sim-time-sec'] < 30: fdm.run() self.assertEqual(fdm['simulation/frame'], fdm['tests/rate-1']) self.assertEqual(int((fdm['simulation/frame'] - xtraFrames) / 4), fdm['tests/rate-4']) self.assertAlmostEqual(fdm['simulation/sim-time-sec'], fdm['tests/rate-1-dt-sum'] - tf) self.assertAlmostEqual( fdm['simulation/dt'] * fdm['tests/rate-4'] * 4, fdm['tests/rate-4-dt-sum'])
def testChannelRate(self): os.environ['JSBSIM_DEBUG'] = str(0) fdm = CreateFDM(self.sandbox) fdm.load_script(self.sandbox.path_to_jsbsim_file('scripts', 'systems-rate-test-0.xml')) fdm.run_ic() while fdm['simulation/sim-time-sec'] < 30: fdm.run() self.assertEqual(fdm['simulation/frame'], fdm['tests/rate-1']) self.assertEqual(int(fdm['simulation/frame']/4), fdm['tests/rate-4']) self.assertEqual(fdm['simulation/sim-time-sec'], fdm['tests/rate-1-dt-sum']) self.assertAlmostEqual(fdm['simulation/dt']*fdm['tests/rate-4']*4, fdm['tests/rate-4-dt-sum']) self.assertEqual(fdm['simulation/dt'], fdm['tests/rate-1-dt']) self.assertEqual(fdm['simulation/dt']*4, fdm['tests/rate-4-dt']) try: fdm['simulation/do_simple_trim'] = 1 except RuntimeError as e: # The trim cannot succeed. Just make sure that the raised exception # is due to the trim failure otherwise rethrow. if e.args[0] != 'Trim Failed': raise while fdm['simulation/sim-time-sec'] < 40: self.assertEqual(fdm['simulation/frame'], fdm['tests/rate-1']) self.assertEqual(int(fdm['simulation/frame']/4), fdm['tests/rate-4']) self.assertEqual(fdm['simulation/sim-time-sec'], fdm['tests/rate-1-dt-sum']) self.assertAlmostEqual(fdm['simulation/dt']*fdm['tests/rate-4']*4, fdm['tests/rate-4-dt-sum']) fdm.run() fdm.reset_to_initial_conditions(1) tf = fdm['tests/rate-1-dt-sum'] xtraFrames = fdm['simulation/frame'] % 4 while fdm['simulation/sim-time-sec'] < 30: fdm.run() self.assertEqual(fdm['simulation/frame'], fdm['tests/rate-1']) self.assertEqual(int((fdm['simulation/frame']-xtraFrames)/4), fdm['tests/rate-4']) self.assertAlmostEqual(fdm['simulation/sim-time-sec'], fdm['tests/rate-1-dt-sum']-tf) self.assertAlmostEqual(fdm['simulation/dt']*fdm['tests/rate-4']*4, fdm['tests/rate-4-dt-sum'])
def test_script_start_time_0(self): script_name = 'ball_orbit.xml' script_path = self.sandbox.path_to_jsbsim_file('scripts', script_name) fdm = CreateFDM(self.sandbox) fdm.load_script(script_path) fdm.run_ic() self.assertEqual(fdm['simulation/sim-time-sec'], 0.0) ExecuteUntil(fdm, 5.0) fdm.reset_to_initial_conditions(1) self.assertEqual(fdm['simulation/sim-time-sec'], 0.0) del fdm
def test_script_start_time_0(self): script_name = 'ball_orbit.xml' script_path = self.sandbox.path_to_jsbsim_file('scripts', script_name) fdm = CreateFDM(self.sandbox) fdm.load_script(script_path) fdm.run_ic() self.assertEqual(fdm.get_property_value('simulation/sim-time-sec'), 0.0) ExecuteUntil(fdm, 5.0) fdm.reset_to_initial_conditions(1) self.assertEqual(fdm.get_property_value('simulation/sim-time-sec'), 0.0) del fdm
def test_script_start_time(self): script_name = 'ball_orbit.xml' script_path = self.sandbox.path_to_jsbsim_file('scripts', script_name) tree = et.parse(self.sandbox.elude(script_path)) run_tag = tree.getroot().find('./run') run_tag.attrib['start'] = '1.2' tree.write(self.sandbox(script_name)) fdm = CreateFDM(self.sandbox) fdm.load_script(script_name) fdm.run_ic() self.assertEqual(fdm.get_property_value('simulation/sim-time-sec'), 1.2) ExecuteUntil(fdm, 5.0) fdm.reset_to_initial_conditions(1) self.assertEqual(fdm.get_property_value('simulation/sim-time-sec'), 1.2) del fdm
def test_script_start_time(self): script_name = 'ball_orbit.xml' script_path = self.sandbox.path_to_jsbsim_file('scripts', script_name) tree = et.parse(script_path) run_tag = tree.getroot().find('./run') run_tag.attrib['start'] = '1.2' tree.write(script_name) fdm = CreateFDM(self.sandbox) fdm.load_script(script_name) fdm.run_ic() self.assertEqual(fdm['simulation/sim-time-sec'], 1.2) ExecuteUntil(fdm, 5.0) fdm.reset_to_initial_conditions(1) self.assertEqual(fdm['simulation/sim-time-sec'], 1.2) del fdm
def test_script_no_start_time(self): script_name = 'ball_orbit.xml' script_path = self.sandbox.path_to_jsbsim_file('scripts', script_name) tree = et.parse(script_path) run_tag = tree.getroot().find('./run') # Remove the parameter 'start' from the tag <run> del run_tag.attrib['start'] tree.write(script_name) fdm = CreateFDM(self.sandbox) fdm.load_script(script_name) fdm.run_ic() self.assertEqual(fdm['simulation/sim-time-sec'], 0.0) ExecuteUntil(fdm, 5.0) fdm.reset_to_initial_conditions(1) self.assertEqual(fdm['simulation/sim-time-sec'], 0.0) del fdm
def test_no_script(self): fdm = CreateFDM(self.sandbox) aircraft_path = self.sandbox.path_to_jsbsim_file('aircraft') fdm.load_model('c172x') aircraft_path = os.path.join(aircraft_path, 'c172x') fdm.load_ic(os.path.join(aircraft_path, 'reset01.xml'), False) fdm.run_ic() self.assertEqual(fdm['simulation/sim-time-sec'], 0.0) ExecuteUntil(fdm, 5.0) t = fdm['simulation/sim-time-sec'] fdm['simulation/do_simple_trim'] = 1 self.assertEqual(fdm['simulation/sim-time-sec'], t) fdm.reset_to_initial_conditions(1) self.assertEqual(fdm['simulation/sim-time-sec'], 0.0) del fdm
def test_gust_reset(self): fdm = CreateFDM(self.sandbox) fdm.load_script(self.sandbox.path_to_jsbsim_file('scripts', 'c172_cruise_8K.xml')) fdm.set_property_value('simulation/randomseed', 0.0) fdm.set_output_directive(self.sandbox.path_to_jsbsim_file('tests', 'output.xml')) fdm.run_ic() ExecuteUntil(fdm, 15.5) ref, current = Table(), Table() ref.ReadCSV(self.sandbox('output.csv')) fdm.set_property_value('simulation/randomseed', 0.0) fdm.reset_to_initial_conditions(1) ExecuteUntil(fdm, 15.5) current.ReadCSV(self.sandbox('output_0.csv')) diff = ref.compare(current) self.longMessage = True self.assertTrue(diff.empty(), msg='\n'+repr(diff))
def test_script_no_start_time(self): script_name = 'ball_orbit.xml' script_path = self.sandbox.path_to_jsbsim_file('scripts', script_name) tree = et.parse(self.sandbox.elude(script_path)) run_tag = tree.getroot().find('./run') # Remove the parameter 'start' from the tag <run> del run_tag.attrib['start'] tree.write(self.sandbox(script_name)) fdm = CreateFDM(self.sandbox) fdm.load_script(script_name) fdm.run_ic() self.assertEqual(fdm.get_property_value('simulation/sim-time-sec'), 0.0) ExecuteUntil(fdm, 5.0) fdm.reset_to_initial_conditions(1) self.assertEqual(fdm.get_property_value('simulation/sim-time-sec'), 0.0) del fdm
def test_gust_reset(self): fdm = CreateFDM(self.sandbox) fdm.load_script( self.sandbox.path_to_jsbsim_file('scripts', 'c172_cruise_8K.xml')) fdm.set_property_value('simulation/randomseed', 0.0) fdm.set_output_directive( self.sandbox.path_to_jsbsim_file('tests', 'output.xml')) fdm.run_ic() ExecuteUntil(fdm, 15.5) ref, current = Table(), Table() ref.ReadCSV(self.sandbox('output.csv')) fdm.set_property_value('simulation/randomseed', 0.0) fdm.reset_to_initial_conditions(1) ExecuteUntil(fdm, 15.5) current.ReadCSV(self.sandbox('output_0.csv')) diff = ref.compare(current) self.longMessage = True self.assertTrue(diff.empty(), msg='\n' + repr(diff))
# fdm = CreateFDM(sandbox) fdm.load_script(sandbox.path_to_jsbsim_file('scripts', 'c1722.xml')) fdm.run_ic() ExecuteUntil(fdm, 1.0) if not sandbox.exists('JSBout172B.csv'): print "Standard run: the file 'JSBout172B.csv' should exist." sys.exit(-1) # 'make test' will report the test failed. # # Reset the simulation and check that iteration number is correctly appended to # the filename. # fdm.reset_to_initial_conditions(1) ExecuteUntil(fdm, 1.0) if not sandbox.exists('JSBout172B_0.csv'): print "Reset: the file 'JSBout172B_0.csv' should exist." sys.exit(-1) # 'make test' will report the test failed. # # 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) if not sandbox.exists('dummy.csv'):
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.")
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.")