TEOS-10 / GSW-Cplusplus

1 stars 0 forks source link

Segmentation Fault #1

Open lkdo opened 9 months ago

lkdo commented 9 months ago

Function void TeosBase::gsw_util_sort_dbl(double dArray,int nx,int iArray,bool ASC) seems to be called with a partially uninitialized iArray parameter, for example in gsw_util_interp1q_int(), causing segmentation fault. The call is gsw_util_sort_dbl(xi, n, k); where the k array is only initialized from index n circa.

The following code was used as main() for a quick test:

#include <iostream>
#include "TeosSea.h"

int main()
{

    // https://www.teos-10.org/pubs/gsw/html/gsw_geo_strf_dyn_height.html

    TeosSea mTB;

    // INPUT 
    const int N = 6;
    double SA[N] = { 34.7118, 34.8915, 35.0256, 34.8472, 34.7366, 34.7324 };
    double CT[N] = { 28.8099, 28.439, 22.7862, 10.2262,  6.8272,  4.3236 };
    double p[N] = { 10, 50, 125, 250, 600, 1000 };
    double p_ref[1] = { 1000 };

    // OUTPUT 
    double dynamic_anomaly_result[N] = { 16.829126675036644, 14.454693755102685, 10.727894578402342,  7.699845274649316,  3.578081589449148,  0 };

    // CALCULATE
    double dynamic_anomaly[N];
    int k = 0;
    if (mTB.gsw_geo_strf_dyn_height(&SA[k], &CT[k], &p[k], p_ref[0], N, &dynamic_anomaly[k]) == NULL)
    {
        std::cout << "FAILURE NULL\n";
        return -1;
    }

    // VERIFY RESULT
    for (int i = 0; i < N; i++)
        std::cout << "Expected:\t" << dynamic_anomaly_result[i] << "; Actual:\t" << dynamic_anomaly[i] << "\n";

    return 0;
}
rkwhited commented 8 months ago

Did you run "in funnel" to validate your parameters? /*** % gsw_geo_strf_dyn_height dynamic height anomaly % (75-term equation) %==========================================================================

% Note that the 75-term equation has been fitted in a restricted range of % parameter space, and is most accurate inside the "oceanographic funnel" % described in McDougall et al. (2003). For dynamical oceanography we may % take the 75-term rational function expression for specific volume as % essentially reflecting the full accuracy of TEOS-10. The GSW library % function "gsw_infunnel(SA,CT,p)" is avaialble to be used if one wants to % test if some of one's data lies outside this "funnel".

rkwhited commented 3 months ago

The k value cannot be zero. Set it to 1. The expected values are not correct. Test 1 CT = 28.8099, SA = 34.7118, p = 10 (received: 1.01234e-320) Test 2 CT = 28.439, SA = 34.8915, p = 50 (received: 2.91441e-317) Test 3 CT = 22.7862, SA = 35.0256, p = 125 (received: 4.94066e-324) Test 4 CT = 10.2262, SA = 34.8472, p = 250 (received: 1.63971e-319) Test 5 CT = 6.8272, SA = 34.7366, p = 600 (received: 0) Test 6 CT = 4.3236, SA = 34.7324, p = 1000 (received: 0)