From 3a1743d57fd799eb04f92edbcb980e91a058029c Mon Sep 17 00:00:00 2001 From: Sylvain Guillet Date: Fri, 2 Aug 2024 11:15:25 +0200 Subject: [PATCH] Add clear kernels and consolidate kernel loading and unloading --- .../IO.Astrodynamics.CLI.csproj | 2 +- IO.Astrodynamics.Tests/APITest.cs | 75 +++++++++++++------ .../IO.Astrodynamics.Tests.csproj | 4 + IO.Astrodynamics/API.cs | 30 ++++++-- IO.Astrodynamics/IO.Astrodynamics.nuspec | 2 +- 5 files changed, 83 insertions(+), 30 deletions(-) diff --git a/IO.Astrodynamics.CLI/IO.Astrodynamics.CLI.csproj b/IO.Astrodynamics.CLI/IO.Astrodynamics.CLI.csproj index 10c91ed..53a870f 100644 --- a/IO.Astrodynamics.CLI/IO.Astrodynamics.CLI.csproj +++ b/IO.Astrodynamics.CLI/IO.Astrodynamics.CLI.csproj @@ -9,7 +9,7 @@ 0.0.1 true astro - 0.1.13 + 0.2.0 Astrodynamics command line interface Sylvain Guillet This CLI allows end user to exploit IO.Astrodynamics framework diff --git a/IO.Astrodynamics.Tests/APITest.cs b/IO.Astrodynamics.Tests/APITest.cs index 9684d97..75dbc31 100644 --- a/IO.Astrodynamics.Tests/APITest.cs +++ b/IO.Astrodynamics.Tests/APITest.cs @@ -25,6 +25,7 @@ namespace IO.Astrodynamics.Tests; + public class APITest { public APITest() @@ -307,7 +308,7 @@ public async Task ReadOrientation() spacecraft.AddPayload(new Payload("payload1", 50.0, "pay01")); spacecraft.AddCircularInstrument(-1794600, "CAM600", "mod1", 1.5, Vector3.VectorZ, Vector3.VectorX, Vector3.VectorX); - spacecraft.SetStandbyManeuver(new NadirAttitude(TestHelpers.EarthAtJ2000,DateTime.MinValue, TimeSpan.Zero, + spacecraft.SetStandbyManeuver(new NadirAttitude(TestHelpers.EarthAtJ2000, DateTime.MinValue, TimeSpan.Zero, spacecraft.Engines.First())); scenario.AddSpacecraft(spacecraft); @@ -322,12 +323,12 @@ public async Task ReadOrientation() //Read results Assert.Equal(0.70710678118654757, res.ElementAt(0).Rotation.W); - Assert.Equal(0.0, res.ElementAt(0).Rotation.VectorPart.X,6); - Assert.Equal(0.0, res.ElementAt(0).Rotation.VectorPart.Y,6); + Assert.Equal(0.0, res.ElementAt(0).Rotation.VectorPart.X, 6); + Assert.Equal(0.0, res.ElementAt(0).Rotation.VectorPart.Y, 6); Assert.Equal(0.70710678118654746, res.ElementAt(0).Rotation.VectorPart.Z); - Assert.Equal(0.0, res.ElementAt(0).AngularVelocity.X,6); - Assert.Equal(0.0, res.ElementAt(0).AngularVelocity.Y,6); - Assert.Equal(0.0, res.ElementAt(0).AngularVelocity.Z,6); + Assert.Equal(0.0, res.ElementAt(0).AngularVelocity.X, 6); + Assert.Equal(0.0, res.ElementAt(0).AngularVelocity.Y, 6); + Assert.Equal(0.0, res.ElementAt(0).AngularVelocity.Z, 6); Assert.Equal(window.StartDate, res.ElementAt(0).Epoch); Assert.Equal(Frames.Frame.ICRF, res.ElementAt(0).ReferenceFrame); @@ -542,28 +543,60 @@ void LoadKernelException() Assert.Throws(() => API.Instance.LoadKernels(null)); } + private static object lockobj = new Object(); [Fact] void UnloadKernels() { - API.Instance.LoadKernels(new FileInfo(@"Data/UserDataTest/scn100/Sites/MySite/Ephemeris/MySite.spk")); - API.Instance.LoadKernels(new FileInfo(@"Data/UserDataTest/scn100/Spacecrafts/DRAGONFLY32/Ephemeris/DRAGONFLY32.spk")); - API.Instance.LoadKernels(new DirectoryInfo(@"Data/UserDataTest/scn100")); - API.Instance.UnloadKernels(new FileInfo(@"Data/UserDataTest/scn100/Spacecrafts/DRAGONFLY32/Ephemeris/DRAGONFLY32.spk")); - Assert.Equal(2,API.Instance.GetLoadedKernels().Count()); - Assert.Equal(@"Data\UserDataTest\scn100\Sites\MySite\Ephemeris\MySite.spk",API.Instance.GetLoadedKernels().ElementAt(0).FullName); - Assert.Equal(@"Data\UserDataTest\scn100",API.Instance.GetLoadedKernels().ElementAt(1).FullName); + lock (lockobj) + { + API.Instance.LoadKernels(new FileInfo(@"Data/UserDataTest/scn100/Sites/MySite/Ephemeris/MySite.spk")); + API.Instance.LoadKernels(new FileInfo(@"Data/UserDataTest/scn100/Spacecrafts/DRAGONFLY32/Ephemeris/DRAGONFLY32.spk")); + API.Instance.LoadKernels(new DirectoryInfo(@"Data/UserDataTest/scn100")); + API.Instance.UnloadKernels(new FileInfo(@"Data/UserDataTest/scn100/Spacecrafts/DRAGONFLY32/Ephemeris/DRAGONFLY32.spk")); + var kernels = API.Instance.GetLoadedKernels().ToArray(); + Assert.Equal(1, @kernels.Count(x => x.FullName.Contains("scn100"))); + } } + [Fact] + void UnloadKernels2() + { + lock (lockobj) + { + API.Instance.LoadKernels(new FileInfo(@"Data/UserDataTest/scn100/Spacecrafts/DRAGONFLY32/Clocks/DRAGONFLY32.tsc")); + + API.Instance.UnloadKernels(new FileInfo(@"Data/UserDataTest/scn100/Spacecrafts/DRAGONFLY32/Clocks/DRAGONFLY32.tsc")); + var kernels = API.Instance.GetLoadedKernels().ToArray(); + Assert.Equal(0, kernels.Count(x => x.FullName.Contains("DRAGONFLY32.tsc"))); + } + } + [Fact] void LoadKernels() { - API.Instance.LoadKernels(new FileInfo(@"Data/UserDataTest/scn100/Sites/MySite/Ephemeris/MySite.spk")); - API.Instance.LoadKernels(new FileInfo(@"Data/UserDataTest/scn100/Spacecrafts/DRAGONFLY32/Ephemeris/DRAGONFLY32.spk")); - API.Instance.LoadKernels(new DirectoryInfo(@"Data/UserDataTest/scn100")); - - Assert.Equal(2,API.Instance.GetLoadedKernels().Count()); - Assert.Equal(Constants.SolarSystemKernelPath,API.Instance.GetLoadedKernels().ElementAt(0)); - Assert.Equal(@"scn100",API.Instance.GetLoadedKernels().ElementAt(1).Name); + lock (lockobj) + { + API.Instance.LoadKernels(new FileInfo(@"Data/UserDataTest/scn100/Sites/MySite/Ephemeris/MySite.spk")); + API.Instance.LoadKernels(new FileInfo(@"Data/UserDataTest/scn100/Spacecrafts/DRAGONFLY32/Ephemeris/DRAGONFLY32.spk")); + API.Instance.LoadKernels(new DirectoryInfo(@"Data/UserDataTest/scn100")); + + var kernels = API.Instance.GetLoadedKernels().ToArray(); + Assert.Equal(1, @kernels.Count(x => x.FullName.Contains("scn100"))); + } + } + + [Fact] + void LoadKernels2() + { + lock (lockobj) + { + API.Instance.LoadKernels(new DirectoryInfo(@"Data/UserDataTest/scn100")); + API.Instance.LoadKernels(new FileInfo(@"Data/UserDataTest/scn100/Spacecrafts/DRAGONFLY32/Ephemeris/DRAGONFLY32.spk")); + API.Instance.LoadKernels(new FileInfo(@"Data/UserDataTest/scn100/Sites/MySite/Ephemeris/MySite.spk")); + + var kernels = API.Instance.GetLoadedKernels().ToArray(); + Assert.Equal(1, @kernels.Count(x => x.FullName.Contains("scn100"))); + } } [Fact] @@ -598,6 +631,4 @@ void TLEElements() Assert.Equal(9, tle.O); Assert.Equal(10, tle.M); } - - } \ No newline at end of file diff --git a/IO.Astrodynamics.Tests/IO.Astrodynamics.Tests.csproj b/IO.Astrodynamics.Tests/IO.Astrodynamics.Tests.csproj index 679e701..b3634f3 100644 --- a/IO.Astrodynamics.Tests/IO.Astrodynamics.Tests.csproj +++ b/IO.Astrodynamics.Tests/IO.Astrodynamics.Tests.csproj @@ -116,6 +116,10 @@ Always + + + Always + diff --git a/IO.Astrodynamics/API.cs b/IO.Astrodynamics/API.cs index 5e910ce..8b4745d 100644 --- a/IO.Astrodynamics/API.cs +++ b/IO.Astrodynamics/API.cs @@ -166,13 +166,20 @@ public IEnumerable GetLoadedKernels() public void LoadKernels(FileSystemInfo path) { if (path == null) throw new ArgumentNullException(nameof(path)); - if (_kernels.Any(x => x.FullName == path.FullName)) - { - return; - } - lock (lockObject) { + if (_kernels.Any(x => path.FullName.Contains(x.FullName))) + { + foreach (var kernel in _kernels.Where(x => path.FullName.Contains(x.FullName)).ToArray()) + { + UnloadKernels(kernel); + LoadKernels(kernel); + } + + return; + } + + var existingKernels = _kernels.Where(x => x.FullName.Contains(path.FullName)).ToArray(); foreach (var existingKernel in existingKernels) { @@ -208,7 +215,7 @@ public void UnloadKernels(FileSystemInfo path) } _kernels.RemoveAll(x => x.FullName == path.FullName); - foreach (var kernel in _kernels.Where(x=>x.FullName.Contains(path.FullName)).ToArray()) + foreach (var kernel in _kernels.Where(x => x.FullName.Contains(path.FullName)).ToArray()) { UnloadKernels(kernel); } @@ -216,6 +223,17 @@ public void UnloadKernels(FileSystemInfo path) } } + public void ClearKernels() + { + lock (lockObject) + { + foreach (var kernel in _kernels.ToArray()) + { + UnloadKernels(kernel); + } + } + } + /// /// Find launch windows /// diff --git a/IO.Astrodynamics/IO.Astrodynamics.nuspec b/IO.Astrodynamics/IO.Astrodynamics.nuspec index d6c3359..8ce029c 100644 --- a/IO.Astrodynamics/IO.Astrodynamics.nuspec +++ b/IO.Astrodynamics/IO.Astrodynamics.nuspec @@ -4,7 +4,7 @@ IO.Astrodynamics Sylvain Guillet Sylvain Guillet - 3.2.3 + 3.3.0 Astrodynamics framework images\dragonfly-dark-trans.png docs\README.md