Home > tests > utilities > test_grid2fvcom.m

test_grid2fvcom

PURPOSE ^

Unit test for grid2fvcom.

SYNOPSIS ^

This is a script file.

DESCRIPTION ^

 Unit test for grid2fvcom.

 DESCRIPTION:
   Currently checks against a reference data set for the following:
       - number of nodes in the output
       - number of elements in the output
       - number of time steps in the output
       - range of values in the node arrays
       - range of values in the element arrays

 It uses some NCEP data for the Irish Sea as the base input. This data is
 from January, 2001. This includes an unstructured grid object (Mobj), the
 NCEP forcing data struct (forcing) and a 'known good' result
 (forcing_interp) for comparison against the new result.

 Author(s):
   Pierre Cazenave (Plymouth Marine Laboratory)

 Revision history:
   2013-05-17 First version.
   2016-06-02 Fix paths to data to load.

==========================================================================

CROSS-REFERENCE INFORMATION ^

This function calls: This function is called by:

SOURCE CODE ^

0001 % Unit test for grid2fvcom.
0002 %
0003 % DESCRIPTION:
0004 %   Currently checks against a reference data set for the following:
0005 %       - number of nodes in the output
0006 %       - number of elements in the output
0007 %       - number of time steps in the output
0008 %       - range of values in the node arrays
0009 %       - range of values in the element arrays
0010 %
0011 % It uses some NCEP data for the Irish Sea as the base input. This data is
0012 % from January, 2001. This includes an unstructured grid object (Mobj), the
0013 % NCEP forcing data struct (forcing) and a 'known good' result
0014 % (forcing_interp) for comparison against the new result.
0015 %
0016 % Author(s):
0017 %   Pierre Cazenave (Plymouth Marine Laboratory)
0018 %
0019 % Revision history:
0020 %   2013-05-17 First version.
0021 %   2016-06-02 Fix paths to data to load.
0022 %
0023 %==========================================================================
0024 
0025 matlabrc
0026 close all
0027 clc
0028 
0029 % Set up our test environment.
0030 [base, subname] = fileparts(mfilename('fullpath'));
0031 addpath(fullfile(base, '../../fvcom_prepro'))
0032 
0033 load(fullfile(base, '../data/grid2fvcom_data.mat'));
0034 
0035 interpfields = {'uwnd', 'vwnd', 'slp', 'nshf', 'nlwrs', 'nswrs', 'P_E', ...
0036     'Et', 'time', 'lon', 'lat', 'x', 'y'};
0037 
0038 % Perform the interpolation using the new routine and check the outputs are
0039 % the same.
0040 forcing_interp_new = grid2fvcom(Mobj, interpfields, forcing);
0041 
0042 % Check we have the same number of fields (if we don't, we can't go any
0043 % further).
0044 fnames = fieldnames(forcing_interp);
0045 if length(fnames) ~= length(fieldnames(forcing_interp_new))
0046     error(['The number of reference struct field names (%d) does', ...
0047         ' not equal the number in the new struct (%d)'], ...
0048         length(fnames), length(fieldnames(forcing_interp_new)))
0049 end
0050 
0051 %%
0052 
0053 results = struct();
0054 
0055 for ff = 1:length(fnames)
0056 
0057     results.(fnames{ff}) = struct();
0058 
0059     switch fnames{ff}
0060         case {'time', 'lon', 'lat', 'x', 'y'}
0061 
0062             results.(fnames{ff}).vectorValues = 'FAIL';
0063 
0064             results.(fnames{ff}).check = ...
0065                 forcing_interp.(fnames{ff}) - forcing_interp_new.(fnames{ff});
0066             checkDiff = max(results.(fnames{ff}).check) - ...
0067                 min(results.(fnames{ff}).check);
0068             if checkDiff == 0
0069                 results.(fnames{ff}).vectorValues = 'PASS';
0070             end
0071 
0072         otherwise
0073 
0074             %--------------------------------------------------------------
0075             % Set the pass/fail flags for the tests. Assume fail and only
0076             % change if proven otherwise.
0077             %--------------------------------------------------------------
0078             results.(fnames{ff}).nodeNumber = 'FAIL';
0079             results.(fnames{ff}).numNodeTimes = 'FAIL';
0080             results.(fnames{ff}).nodeValues = 'FAIL';
0081 
0082             %--------------------------------------------------------------
0083             % Check we have the same number of points and time steps in the
0084             % new interpolation as in the original.
0085             %--------------------------------------------------------------
0086             [~, results.(fnames{ff}).origNodeTimes] = ...
0087                 size(forcing_interp.(fnames{ff}).node);
0088             [results.(fnames{ff}).nNodes, ...
0089                 results.(fnames{ff}).nNodeTimes] = ...
0090                 size(forcing_interp_new.(fnames{ff}).node);
0091 
0092             if results.(fnames{ff}).nNodes == Mobj.nVerts
0093                 results.(fnames{ff}).nodeNumber = 'PASS';
0094             end
0095             if results.(fnames{ff}).nNodeTimes == ...
0096                     results.(fnames{ff}).origNodeTimes
0097                 results.(fnames{ff}).numNodeTimes = 'PASS';
0098             end
0099 
0100             %--------------------------------------------------------------
0101             % Check the values in the node arrays match the reference
0102             % values.
0103             %--------------------------------------------------------------
0104             results.(fnames{ff}).nodeDiff = ...
0105                 forcing_interp.(fnames{ff}).node - ...
0106                 forcing_interp_new.(fnames{ff}).node;
0107 
0108             results.(fnames{ff}).nodeRange = ...
0109                 max(results.(fnames{ff}).nodeDiff(:));
0110 
0111             if results.(fnames{ff}).nodeRange == 0
0112                 results.(fnames{ff}).nodeValues = 'PASS';
0113             end
0114     end
0115 end
0116 
0117 %%
0118 %--------------------------------------------------------------------------
0119 % Print a summary of the testing
0120 %--------------------------------------------------------------------------
0121 totalTests = 0;
0122 totalPasses = 0;
0123 
0124 for ff = 1:length(fnames)
0125     resultnames = fieldnames(results.(fnames{ff}));
0126     numRes = length(resultnames);
0127 
0128     for fi = 1:numRes
0129 
0130         % Skip if the field is not a string (we're only interested in
0131         % pass/fail really.
0132         if ~ischar(results.(fnames{ff}).(resultnames{fi}))
0133             continue
0134         else
0135             % Increment the number of tests performed.
0136             totalTests = totalTests + 1;
0137         end
0138 
0139         % Get the total number of PASSed tests.
0140         if strcmp(results.(fnames{ff}).(resultnames{fi}), 'PASS')
0141             totalPasses = totalPasses + 1;
0142         end
0143 
0144         S = results.(fnames{ff}).(resultnames{fi});
0145 
0146         switch resultnames{fi}
0147             case 'vectorValues'
0148                 fprintf('%s %s values test\n', S, fnames{ff})
0149                 if strcmp(S, 'FAIL')
0150                     fprintf('\tmin/max of %s range: %f, %f\n', ...
0151                         fnames{ff}, ...
0152                         min(results.(fnames{ff}).check), ...
0153                         max(results.(fnames{ff}).check))
0154                 end
0155 
0156             case 'nodeNumber'
0157                 fprintf('%s %s node number test\n', S, fnames{ff})
0158                 if strcmp(S, 'FAIL')
0159                     fprintf('\toriginal/new number of %s nodes: %d, %d\n', ...
0160                         fnames{ff}, ...
0161                         Mobj.nVerts, ...
0162                         results.(fnames{ff}).nNodes)
0163                 end
0164 
0165             case 'numNodeTimes'
0166                 fprintf('%s %s node time steps test\n', S, fnames{ff})
0167                 if strcmp(S, 'FAIL')
0168                     fprintf('\toriginal/new number of %s node times: %d, %d\n', ...
0169                         fnames{ff}, ...
0170                         results.(fnames{ff}).origNodeTimes, ...
0171                         results.(fnames{ff}).nNodeTimes)
0172                 end
0173 
0174             case 'nodeValues'
0175                 fprintf('%s %s node values test\n', S, fnames{ff})
0176                 if strcmp(S, 'FAIL')
0177                     fprintf('\trange of %s node values: %d\n', ...
0178                         fnames{ff}, ...
0179                         results.(fnames{ff}).nodeRange)
0180                 end
0181         end
0182     end
0183 end
0184 
0185 fprintf('\n------------------SUMMARY------------------\n')
0186 fprintf('           %d of %d tests passed', totalPasses, totalTests)
0187 fprintf('\n-------------------------------------------\n')

Generated on Wed 20-Feb-2019 16:06:01 by m2html © 2005