diff --git a/apps/backend/crydaq-algorithm-runtime/src/main/java/com/opentest/algorithm/runtime/PublicAlgorithmService.java b/apps/backend/crydaq-algorithm-runtime/src/main/java/com/opentest/algorithm/runtime/PublicAlgorithmService.java index 54d145f1..807a21a7 100644 --- a/apps/backend/crydaq-algorithm-runtime/src/main/java/com/opentest/algorithm/runtime/PublicAlgorithmService.java +++ b/apps/backend/crydaq-algorithm-runtime/src/main/java/com/opentest/algorithm/runtime/PublicAlgorithmService.java @@ -188,17 +188,18 @@ public PublicAlgorithmService(AlgorithmLibJNIBridgeService runtime, CurveNativeA SoundPowerSurveyOptions options, double[] levels, double[] backgroundLevels) { Objects.requireNonNull(options, "options"); var parameters = new HashMap(); - parameters.put("sphereRadius", options.sphereRadius()); - parameters.put("measureDistance", options.measureDistance()); - parameters.put("length", options.length()); - parameters.put("width", options.width()); - parameters.put("height", options.height()); + if (options.measurementSurfaceArea() == null) { + parameters.put("sphereRadius", options.sphereRadius()); + parameters.put("measureDistance", options.measureDistance()); + parameters.put("length", options.length()); + parameters.put("width", options.width()); + parameters.put("height", options.height()); + } parameters.put("roomSurface", options.roomSurface()); parameters.put("absortionCoef", options.absorptionCoefficient()); - if (options.measurementSurfaceArea() != null) - parameters.put("measurementSurfaceArea", options.measurementSurfaceArea()); + int nativeSurfaceType = soundPowerNativeSurface(options.surfaceType(), options.measurementSurfaceArea(), parameters); return Optional.ofNullable(runtime.calculateSimplifiedSoundPowerSurvey( - options.surfaceType(), parameters, levels, backgroundLevels)).map(value -> + nativeSurfaceType, parameters, levels, backgroundLevels)).map(value -> new SoundPowerSurveyResult(value.getSoundPowerLevel(), value.getBackgroundCorrection(), value.getEnvironmentalCorrection(), value.getEquivalentSoundPressureLevelAverage())); } @@ -225,30 +226,47 @@ private Optional calculateEngineeringSoundPower( if (levels == null || levels.isEmpty() || backgroundLevels == null || backgroundLevels.isEmpty()) return Optional.empty(); var parameters = new HashMap(); - parameters.put("sphereRadius", options.sphereRadius()); - parameters.put("measureDistance", options.measureDistance()); - parameters.put("length", options.length()); parameters.put("width", options.width()); - parameters.put("height", options.height()); - parameters.put("cylinderMeasureDistance1", options.cylinderMeasureDistance1()); - parameters.put("cylinderMeasureDistance2", options.cylinderMeasureDistance2()); - parameters.put("cylinderMeasureDistance3", options.cylinderMeasureDistance3()); + if (options.measurementSurfaceArea() == null) { + parameters.put("sphereRadius", options.sphereRadius()); + parameters.put("measureDistance", options.measureDistance()); + parameters.put("length", options.length()); parameters.put("width", options.width()); + parameters.put("height", options.height()); + parameters.put("cylinderMeasureDistance1", options.cylinderMeasureDistance1()); + parameters.put("cylinderMeasureDistance2", options.cylinderMeasureDistance2()); + parameters.put("cylinderMeasureDistance3", options.cylinderMeasureDistance3()); + } parameters.put("roomSurface", options.roomSurface()); parameters.put("absortionCoef", options.absorptionCoefficient()); parameters.put("temperature", options.temperature()); parameters.put("humidity", options.humidity()); parameters.put("atmosphericPressure", options.atmosphericPressure()); - if (options.measurementSurfaceArea() != null) - parameters.put("measurementSurfaceArea", options.measurementSurfaceArea()); + int nativeSurfaceType = soundPowerNativeSurface(options.surfaceType(), options.measurementSurfaceArea(), parameters); parameters.values().removeIf(Objects::isNull); var response = terminal - ? runtime.calculateTerminalEngineeringSoundPower(options.surfaceType(), options.calculationType(), + ? runtime.calculateTerminalEngineeringSoundPower(nativeSurfaceType, options.calculationType(), parameters, frequencies, levels, backgroundLevels) - : runtime.calculateEngineeringSoundPowerSurvey(options.surfaceType(), options.calculationType(), + : runtime.calculateEngineeringSoundPowerSurvey(nativeSurfaceType, options.calculationType(), parameters, frequencies, levels, backgroundLevels); return Optional.ofNullable(response).map(value -> new EngineeringSoundPowerResult(value.getFrequency(), value.getWeightedSoundPowerSpectrum(), value.getWeightedSoundPowerLevel(), value.getSoundPowerSpectrum(), value.getEquivalentSoundPressureSpectrum(), value.getK1(), value.getDs(), value.getK2(), value.getC1(), value.getC2(), value.getC3())); } + /** + * The released JNI calculates area from supported geometry. Encode the caller's + * area as an equal-area hemisphere only at that boundary; the measurement's + * actual geometry and point channels remain part of the plugin configuration. + * The option records require a finite positive area and surface -1 for this path. + */ + private static int soundPowerNativeSurface(int surface, Double area, Map parameters) { + if (area == null) { + return surface; + } + double radius = Math.sqrt(area) / Math.sqrt(2.0 * Math.PI); + parameters.put("sphereRadius", radius); + parameters.put("measureDistance", radius); + return 0; // Hemisphere with one reflecting plane, S = 2πr². + } + @Override public synchronized Optional calculateFrequencyWeighting(double[] frequencies, double[] levels, double sampleRate, int nativeWeightingType) { return Optional.ofNullable(runtime.calculateFrequencyWeighting(frequencies, levels, sampleRate, nativeWeightingType)); diff --git a/apps/backend/crydaq-algorithm-runtime/src/test/java/com/opentest/algorithm/runtime/PublicSoundPowerMathTest.java b/apps/backend/crydaq-algorithm-runtime/src/test/java/com/opentest/algorithm/runtime/PublicSoundPowerMathTest.java index 2b1d69fa..7d9c3631 100644 --- a/apps/backend/crydaq-algorithm-runtime/src/test/java/com/opentest/algorithm/runtime/PublicSoundPowerMathTest.java +++ b/apps/backend/crydaq-algorithm-runtime/src/test/java/com/opentest/algorithm/runtime/PublicSoundPowerMathTest.java @@ -70,15 +70,19 @@ class PublicSoundPowerMathTest { bridge.surveyStatus=13200001; assertTrue(service.calculateSoundPowerSurvey(options,new double[]{80},new double[]{40}).isEmpty()); } - @Test void explicitSurveyAreaUsesDedicatedSurfaceAndDoesNotChangeLegacyOperands() { + @Test void explicitSurveyAreaReachesTheReleasedNativeInterfaceAsTheSameArea() { var bridge = new NativeObservation(); var service = new PublicAlgorithmService(new AlgorithmLibJNIBridgeService(bridge)); - var options = SoundPowerSurveyOptions.withArea(48.849555921539, 1000D, .5); + var options = new SoundPowerSurveyOptions(-1, 9D, 1D, 4D, 5D, 6D, + 1000D, .5, 48.849555921539); assertTrue(service.calculateSoundPowerSurvey(options,new double[]{75},new double[]{65}).isPresent()); - assertEquals(-1, bridge.surface); - assertEquals(48.849555921539, bridge.parameters.get("measurementSurfaceArea")); - assertNull(bridge.parameters.get("length")); - assertNull(bridge.parameters.get("sphereRadius")); + assertEquals(0, bridge.surface); + double radius = bridge.parameters.get("sphereRadius"); + assertEquals(options.measurementSurfaceArea(), 2.0 * Math.PI * radius * radius, 1e-10); + assertEquals(radius, bridge.parameters.get("measureDistance")); + assertEquals(1000D, bridge.parameters.get("roomSurface")); + assertEquals(.5, bridge.parameters.get("absortionCoef")); + assertEquals(4, bridge.parameters.size()); assertThrows(IllegalArgumentException.class, () -> SoundPowerSurveyOptions.withArea(Double.NaN,1000D,.5)); assertThrows(IllegalArgumentException.class, () -> new SoundPowerSurveyOptions(3,1D,1D,1D,1D,1D,1000D,.5,50D)); } diff --git a/apps/backend/crydaq-algorithm-runtime/src/test/java/com/opentest/algorithm/runtime/TerminalEngineeringSoundPowerTest.java b/apps/backend/crydaq-algorithm-runtime/src/test/java/com/opentest/algorithm/runtime/TerminalEngineeringSoundPowerTest.java index b5a5bef2..01580d2d 100644 --- a/apps/backend/crydaq-algorithm-runtime/src/test/java/com/opentest/algorithm/runtime/TerminalEngineeringSoundPowerTest.java +++ b/apps/backend/crydaq-algorithm-runtime/src/test/java/com/opentest/algorithm/runtime/TerminalEngineeringSoundPowerTest.java @@ -36,6 +36,39 @@ class TerminalEngineeringSoundPowerTest { frequencies[0] = -1; assertEquals(100, terminal.frequency()[0]); } + @Test void explicitEngineeringAreaUsesTheSameReleasedInterfaceForLiveAndTerminalResults() { + var nativeCall = new NativeCall(); + var api = api(nativeCall); + var options = new EngineeringSoundPowerOptions(-1, 0, 9D, 1D, 4D, 5D, 6D, + 7D, 8D, 9D, 1000D, .5, null, null, null, 31.46); + + assertTrue(api.calculateEngineeringSoundPower(options, new double[]{1000}, LEVELS, BACKGROUNDS).isPresent()); + Map liveParameters = Map.copyOf(nativeCall.parameters); + assertTrue(api.calculateTerminalEngineeringSoundPower(options, new double[]{1000}, LEVELS, BACKGROUNDS).isPresent()); + assertEquals(2, nativeCall.calls); + assertEquals(0, nativeCall.surface); + assertEquals(0, nativeCall.type); + assertEquals(liveParameters, nativeCall.parameters); + double radius = nativeCall.parameters.get("sphereRadius"); + assertEquals(31.46, 2.0 * Math.PI * radius * radius, 1e-10); + assertEquals(radius, nativeCall.parameters.get("measureDistance")); + assertEquals(1000D, nativeCall.parameters.get("roomSurface")); + assertEquals(.5, nativeCall.parameters.get("absortionCoef")); + assertEquals(4, nativeCall.parameters.size()); + } + + @Test void engineeringOptionsRejectInvalidAreaAndAreaWithLegacySurfaceType() { + for (Double invalidArea : new Double[]{null, 0D, -1D, Double.NaN, Double.POSITIVE_INFINITY}) { + assertThrows(IllegalArgumentException.class, () -> engineeringAreaOptions(-1, invalidArea)); + } + assertThrows(IllegalArgumentException.class, () -> engineeringAreaOptions(0, 31.46)); + } + + private static EngineeringSoundPowerOptions engineeringAreaOptions(int surface, Double area) { + return new EngineeringSoundPowerOptions(surface, 0, null, null, null, null, null, + null, null, null, 1000D, .5, null, null, null, area); + } + @Test void actualNativeExceptionIsEmptyForOrdinaryAndRetainedAtTheTerminalBoundary() { var nativeCall = new NativeCall(); var api = api(nativeCall); var original = new IllegalStateException("native failed while calculating"); nativeCall.failure = original; diff --git a/apps/backend/opentest-algorithm-api/src/main/java/com/opentest/algorithm/api/SoundPowerSurveyOptions.java b/apps/backend/opentest-algorithm-api/src/main/java/com/opentest/algorithm/api/SoundPowerSurveyOptions.java index 721289c5..39649428 100644 --- a/apps/backend/opentest-algorithm-api/src/main/java/com/opentest/algorithm/api/SoundPowerSurveyOptions.java +++ b/apps/backend/opentest-algorithm-api/src/main/java/com/opentest/algorithm/api/SoundPowerSurveyOptions.java @@ -14,7 +14,7 @@ public SoundPowerSurveyOptions(int surfaceType, Double sphereRadius, Double meas this(surfaceType, sphereRadius, measureDistance, length, width, height, roomSurface, absorptionCoefficient, null); } - /** Area in m²; no synthetic geometry is required. Requires the explicit-area native runtime. */ + /** Area in m², independent of the shape used to transport it to the numerical backend. */ public static SoundPowerSurveyOptions withArea(double area, Double roomSurface, Double absorptionCoefficient) { return new SoundPowerSurveyOptions(-1, null, null, null, null, null, roomSurface, absorptionCoefficient, area); }