%%DEMOGRAVCODENOCOMPILE This file demonstrates the functions for spherical
%                       harmonic synthesis of gravitational and terrain
%                       data. Unlike the function DemoGravCode, this
%                       function does not make any plots, but does run fast
%                       even when the mex files have not ben compiled. The
%                       gravitational code library (that this function is
%                       part of) must have been added to Matlab's search
%                       path for this function to work.
%
%Rather than using advanced spherical harmonic models at high orders, this
%function loads spherical harmonic coefficients only up to degree and order
%50 for a simple ellipsoidal Earth model. The gravitational potential and
%acceleration due to gravity from the ellipsoidal model as generated by the
%coefficients are shown to match (within reasonable precision bounds) the
%results obtained from closed-form functions.
%
%When run, the error in the potential should be 5.9474e-14% and the error
%in the magnitude of the gravitational acceleration should be 3.3790e-12%.
%
%February 2014 David F. Crouse, Naval Research Laboratory, Washington D.C.
%(UNCLASSIFIED) DISTRIBUTION STATEMENT A. Approved for public release.

M=50;%The maximum degree and order of the model.
%Generate coefficients for an ellipsoidal Earth model.
[C,S,a,c]=ellipsGravCoeffs(M);

lambda=-20*pi/180;%Longitude
theta=80*pi/180;%Ellipsoidal latitude
%The zero altitude places the point on the reference ellipsoid.
pointEllips=[theta;lambda;0];
pointSpher=ellips2Sphere(pointEllips);%Convert to spherical coordinates.

%Compute the potential and acceleration due to gravity.
[V,gradV]=spherHarmonicEval(C,S,pointSpher,a,c);

%Add in the centripetal effects of the Earth's rotation. The formulae for
%the extra terms in the potential and acceleration due to gravity can be
%found in Chapters 2.1 and 2.7 of
%B. Hofmann-Wellenhof and H. Moritz, Physical Geodesy, 2nd ed. 
%SpringerWienNewYork, 2006.
CartPoint=ellips2Cart(pointEllips);%Convert to Cartesian coordinates.
omega=Constants.WGS84EarthRotationRate;
U=V+0.5*omega^2*(CartPoint(1)^2+CartPoint(2)^2);%The gravity potential
%Acceleration due to gravity with the Earth's rotation.
g=gradV+omega^2*[CartPoint(1);CartPoint(2);0];

%Get the exact values of U and g from explicit formulas for the ellipsoidal
%Earth approximation.
[U0,g0]=ellipsParam2Grav(CartPoint);

disp('The geodetic latitude and longitude of the point on the surface of')
disp('the WGS-84 reference ellipsoid where the values are computed are (in degrees)')
latitude=theta*(180/pi)
longitude=lambda*(180/pi)

disp('The gravity potential from the spherical harmonic solution is (in m^2/s^2)')
U

disp('The gravitational acceleration vector (with centripetal acceleration)')
disp('from the spherical harmonic solution is (in m/s^2)')
g

disp('The percent error in the potential from the spherical harmonic')
disp('approximation and from the explicit solution is')
percentErrorPotential=100*(U-U0)/U0

disp('The percent error in the gravitational acceleration magnitude from')
disp('the spherical harmonic approximation and from the explicit solution is')
percentErrorGravAccel=100*(norm(g)-norm(g0))/norm(g0)

%LICENSE:
%
%The source code is in the public domain and not licensed or under
%copyright. The information and software may be used freely by the public.
%As required by 17 U.S.C. 403, third parties producing copyrighted works
%consisting predominantly of the material produced by U.S. government
%agencies must provide notice with such work(s) identifying the U.S.
%Government material incorporated and stating that such material is not
%subject to copyright protection.
%
%Derived works shall not identify themselves in a manner that implies an
%endorsement by or an affiliation with the Naval Research Laboratory.
%
%RECIPIENT BEARS ALL RISK RELATING TO QUALITY AND PERFORMANCE OF THE
%SOFTWARE AND ANY RELATED MATERIALS, AND AGREES TO INDEMNIFY THE NAVAL
%RESEARCH LABORATORY FOR ALL THIRD-PARTY CLAIMS RESULTING FROM THE ACTIONS
%OF RECIPIENT IN THE USE OF THE SOFTWARE.
