diff --git a/src/main/java/org/orekit/rugged/utils/AbsoluteDateArrayHandling.java b/src/main/java/org/orekit/rugged/utils/AbsoluteDateArrayHandling.java new file mode 100644 index 0000000000000000000000000000000000000000..b3be2f9748d98f39b0db2501b8605e84609012f8 --- /dev/null +++ b/src/main/java/org/orekit/rugged/utils/AbsoluteDateArrayHandling.java @@ -0,0 +1,90 @@ +/* Copyright 2013-2022 CS GROUP + * Licensed to CS GROUP (CS) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * CS licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.orekit.rugged.utils; + +import org.orekit.time.AbsoluteDate; + +/** AbsoluteDateForVectorisation consist of additions to AbsoluteDate to handle arrays for vectorization + * @author Melina Vanel + */ +public class AbsoluteDateArrayHandling { + + /** Dates array on which we want to apply time shift or compute duration */ + public AbsoluteDate[] dates; + + /** Simple constructor. + * @param dates is an array of absolute dates on which we want to apply time shift or + * compute duration + */ + public AbsoluteDateArrayHandling(final AbsoluteDate[] dates) { + this.dates = dates.clone(); + } + + /** Get time-shifted dates for several dates or several time shifts. + * @param dts time shifts array in seconds we want to apply to dates + * @return a matrix of new dates, shifted with respect to wanted time + * shifts. If instance dates = [date1, date2, ..., daten] each line + * correspond to one date (for example date1 shiftedBy all timeshifts + * (building the different columns)) + */ + public AbsoluteDate[][] shiftedBySeveralTimeShift(final double[] dts) { + + AbsoluteDate[][] datesShifted = new AbsoluteDate[dates.length][dts.length]; + int index_dates = 0; + + for (AbsoluteDate date: this.dates) { + AbsoluteDate[] dateShifted = new AbsoluteDate[dts.length]; + int index_dts = 0; + for (double dt: dts) { + dateShifted[index_dts] = date.shiftedBy(dt); + index_dts += 1; + } + datesShifted[index_dates] = dateShifted; + index_dates += 1; + + } + return (AbsoluteDate[][]) datesShifted; + } + + /** Get array with durations between instances dates and given dates + * @param datesForDuration dates for which we want to compute the duration form instances dates + * @return a matrix of double representing durations from instance dates + * If instance dates = [date1, date2, ..., daten] each line + * correspond to one date (for example date1 duration from all given dates in arguments + * (building the different columns)) + */ + public double[][] durationsFromSeveralTimeShifts(final AbsoluteDate[] datesForDuration) { + + double[][] durationsFromDates = new double[dates.length][datesForDuration.length]; + int index_dates = 0; + + for (AbsoluteDate date: this.dates) { + double[] durationFromDate = new double[datesForDuration.length]; + int index_datesForDuration = 0; + for (AbsoluteDate dateForDuration: datesForDuration) { + durationFromDate[index_datesForDuration] = date.durationFrom(dateForDuration); + index_datesForDuration += 1; + } + durationsFromDates[index_dates] = durationFromDate; + index_dates += 1; + + } + return (double[][]) durationsFromDates; + } + + +} diff --git a/src/test/java/org/orekit/rugged/utils/AbsoluteDateForVectorisationTest.java b/src/test/java/org/orekit/rugged/utils/AbsoluteDateForVectorisationTest.java new file mode 100644 index 0000000000000000000000000000000000000000..779cde50d14ffbb556f7667b132ee1a3ff489854 --- /dev/null +++ b/src/test/java/org/orekit/rugged/utils/AbsoluteDateForVectorisationTest.java @@ -0,0 +1,75 @@ +/* Copyright 2013-2022 CS GROUP + * Licensed to CS GROUP (CS) under one or more + * contributor license agreements. See the NOTICE file distributed with + * this work for additional information regarding copyright ownership. + * CS licenses this file to You under the Apache License, Version 2.0 + * (the "License"); you may not use this file except in compliance with + * the License. You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +package org.orekit.rugged.utils; + + +import org.junit.Assert; +import org.junit.Test; +import org.orekit.time.AbsoluteDate; + +public class AbsoluteDateForVectorisationTest { + @Test + public void testShiftedBySeveralTimeShiftOneDate() { + + AbsoluteDate date1 = new AbsoluteDate(); + AbsoluteDate[] dates = new AbsoluteDate[] {date1}; + double[] dts = new double[] {10.0, 20.0, 30.0}; + AbsoluteDateArrayHandling datesForVect = new AbsoluteDateArrayHandling(dates); + AbsoluteDate[][] datesShifted = datesForVect.shiftedBySeveralTimeShift(dts); + Assert.assertEquals(datesShifted[0][0].durationFrom(date1.shiftedBy(10)), 0.0, 1e-5); + Assert.assertEquals(datesShifted[0][1].durationFrom(date1.shiftedBy(20)), 0.0, 1e-5); + + } + + @Test + public void testShiftedBySeveralTimeShiftSeveralDates() { + + AbsoluteDate date1 = new AbsoluteDate(); + AbsoluteDate date2 = date1.shiftedBy(10000); + AbsoluteDate[] dates = new AbsoluteDate[] {date1, date2}; + double[] dts = new double[] {10.0, 20.0, 30.0}; + AbsoluteDateArrayHandling datesForVect = new AbsoluteDateArrayHandling(dates); + AbsoluteDate[][] datesShifted = datesForVect.shiftedBySeveralTimeShift(dts); + Assert.assertEquals(datesShifted[0][0].durationFrom(date1.shiftedBy(10)), 0.0, 1e-5); + Assert.assertEquals(datesShifted[0][1].durationFrom(date1.shiftedBy(20)), 0.0, 1e-5); + Assert.assertEquals(datesShifted[1][1].durationFrom(date2.shiftedBy(20)), 0.0, 1e-5); + Assert.assertEquals(datesShifted[1][2].durationFrom(date2.shiftedBy(30)), 0.0, 1e-5); + + } + + @Test + public void testDurationFromSeveralDates() { + + AbsoluteDate date1 = new AbsoluteDate(); + AbsoluteDate date2 = date1.shiftedBy(10000); + AbsoluteDate date3 = date1.shiftedBy(20000); + AbsoluteDate date4 = date1.shiftedBy(100000); + AbsoluteDate[] dates = new AbsoluteDate[] {date1, date2, date3}; + AbsoluteDate[] datesComputeDuration = new AbsoluteDate[] {date4, date1}; + AbsoluteDateArrayHandling datesForVect = new AbsoluteDateArrayHandling(dates); + double[][] datesDurations = datesForVect.durationsFromSeveralTimeShifts(datesComputeDuration); + Assert.assertEquals(datesDurations[0][0], date1.durationFrom(date4), 1e-5); + Assert.assertEquals(datesDurations[0][1], date1.durationFrom(date1), 1e-5); + Assert.assertEquals(datesDurations[1][0], date2.durationFrom(date4), 1e-5); + Assert.assertEquals(datesDurations[1][1], date2.durationFrom(date1), 1e-5); + Assert.assertEquals(datesDurations[2][0], date3.durationFrom(date4), 1e-5); + Assert.assertEquals(datesDurations[2][1], date3.durationFrom(date1), 1e-5); + + } + + +}