PROGRAM WRM1X11NONG
C=======================================================================
C SOLUTION OF D2U/DXDX + ALPHASQ*U = 0 USING WEIGHTED RESIDUAL METHOD
C         WITH AN APPROXIMATING FUNCTION OF U(X)=F0(X)+A1*F1(X)
C        AND DIRICHLET BOUNDARY CONDITIONS OF U(0)=1. & U(1)=1;
C        ********** NON GALERKINS WEIGHTING FUNCTION: F2 **********
C -------------- VARIABLE DEFNITION ----------- NOV/2023  EIJI FUKUMORI 
C   XST & XEN: INTEGRATION LIMITS; 
C   NSEG: NUMBER OF SEGMENTS IN BETWEEN THE LIMITS;
C   UNKNOWN COEFFICENT (A1) IN THE APPROXIMATING FUNCTION IS EVALUATED
C    BY THE FOLLOWING EQUATION:  B1 * A1 + C1 = 0.
C                   WHERE C1=H(F0,F1) AND B1=H(F1,F1)
C  INTEPT: NUMBER OF GAUSS-LEGENDRE INTEGRATION SAMPLING POINTS
C=======================================================================
      IMPLICIT REAL * 8 ( A-H , O-Z )
      PARAMETER ( INTEPT = 3, NSEG=10 )
      DIMENSION SAI(INTEPT) , W(INTEPT)
      COMMON / DEL / DELTAX     /DOMAIN/ RL      /BORDER/ U0, UL
      COMMON / COFF / ALPHASQ
      EXTERNAL F0, F1, F2
C=======================================================================
C            THREE-SAMPLING-POINT GAUSS INTEGRATION METHOD
C            N: NUMBER OF SAMPLING POINTS IN EACH SEGMENET
C   SAI(I) & W(I): NON-DIMENSIONALIZED COORDINATE & WEIGHTING FACTOR
      DATA SAI/-0.7745966692415D0,0.0000000000000D0, 0.7745966692415D0/
      DATA W  / 0.5555555555555D0,0.8888888888888D0, 0.5555555555555D0/
C=======================================================================
C    MATERIAL DATA AND BOUNDARY VALUES
      WRITE(*,fmt='(a)', advance='no') 'TYPE IN ALPHASQ='
      READ(*,*) ALPHASQ
      IF ( ALPHASQ .LE. 0.D0 ) THEN
      WRITE (*,*) 'INAPPROPRIATE VALUE. FORCE THE VALUE OF ALPHASQ TO 1'
      ALPHASQ=1.D0
      END IF
      XST=0.D0
      XEN=1.D0
      U0 = 1.D0
      UL = 1.D0
C=======================================================================
      OPEN ( 1, FILE='WRM1X1NONGALERKIN.FEM',STATUS='UNKNOWN' )
      WRITE(1,*)'==== ONE DIMENSIONAL HELMHOLTZ EQUATION ===='
      WRITE(1,*)'==== DIRICHLET - DIRICHLET BOUNDARY PROBLEM ===='
      WRITE(1,*)'---- NON GALERKINS WEIGHTING FUNCTION F2----'
      WRITE(1,*)'# OF GL INTEGRATION SAMPLING POINTS =',INTEPT
      WRITE(1,*)'APPROXIMATING FUNCTION: F0(X) + A1*F1(X)'
      WRITE(1,*)'WHERE F0(X) = U(0)*X/L + U(L)*(1-X/L)'
      WRITE(1,*)'AND F1(X) = (X/L)*(1-X/L)'
      WRITE(1,*)'WEITHING FUNCTION: F2(X)= (X/RL)**2*(1.D0-X/RL)**2'
      WRITE(1,*)'SQUARE OF ALPHA =',ALPHASQ
      WRITE(1,*)'X-COORDINATE OF LEFT  END BOUNDARY =',XST
      WRITE(1,*)'X-COORDINATE OF RIGHT END BOUNDARY =',XEN
      WRITE(1,*)'U(0) =',U0
      WRITE(1,*)'U(L) =',UL
C=======================================================================
C   DELTAX: SPACIAL DEFERENTIAL LENGTH FOR DERIVATIVE EVALUATION.
      RL = XEN - XST
      DELTAX = RL / ( 10*NSEG )
C=======================================================================
      WRITE(1,*)'LENGTH OF DOMAIN =',RL
      WRITE(1,*)' NUMBER OF SEGMENTS FOR INTEGRATION =', NSEG
      WRITE(1,*)' DX FOR DERIVATIVE EVALUATION =', DELTAX
C=======================================================================
C                COMPUTATION OF H(F0,F1) AND H(F1,F1)
      CALL INTE ( ALPHASQ, XST, XEN, NSEG, INTEPT, SAI, W, F0, F2, C1 )
      CALL INTE ( ALPHASQ, XST, XEN, NSEG, INTEPT, SAI, W, F1, F2, B1 )
C=======================================================================
C      EVALUATION OF UNKNOWN A1 IN THE APPROXIMATING FUNCTION U(X)
      A1 = - C1 / B1
C=======================================================================
C                     PRINTING RESULTS
      WRITE (1,*) 'H(F1,F2)=',B1
      WRITE (1,*) 'H(F0,F2)=',C1
      WRITE (1,*) 'U(X) = F0(X) + ',A1, ' * F1(X)'
      CALL OUTPUT ( XST, XEN, A1 )
      CLOSE (1)
      STOP 'Successful Termination'
      END
C
C
      SUBROUTINE INTE ( ALPHASQ,XST,XEN,NSEG,INTEPT,SAI,W,G1,G2,TOTAL )
      IMPLICIT REAL * 8 ( A-H , O-Z )
      DIMENSION SAI(INTEPT) , W(INTEPT)
      EXTERNAL G1, G2
      TOTAL = 0.D0
      DX = ( XEN - XST ) / NSEG
      DO I = 1 , NSEG
      X1 = DX*(I-1) + XST
      X2 = X1 + DX
      SUM = 0.D0
      SH  = ( X2 - X1 ) / 2.D0
      XMID = ( X1 + X2 ) / 2.D0
      DO J = 1 , INTEPT
      X = SH * SAI(J) + XMID
      SUM = SUM + (-DERIV(G1,X)*DERIV(G2,X)+ALPHASQ*G1(X)*G2(X))*W(J)
      END DO
      TOTAL = TOTAL + SH * SUM
      END DO
      RETURN
      END
C
C
      FUNCTION F0(X)
      IMPLICIT REAL * 8 ( A-H , O-Z )
      COMMON /DOMAIN/ RL
      COMMON /BORDER/ U0, UL
      F0 = U0*(1.D0-X/RL) + UL*(X/RL)
      RETURN
      END
C
C
      FUNCTION F1(X)
      IMPLICIT REAL * 8 ( A-H , O-Z )
      COMMON /DOMAIN/ RL
      F1 = (X/RL) * ( 1.D0 - X/RL )
      RETURN
      END
C
C
      FUNCTION F2(X)
      IMPLICIT REAL * 8 ( A-H , O-Z )
      COMMON /DOMAIN/ RL
      F2 = (X/RL)**2 * ( 1.D0 - X/RL )**2
      RETURN
      END
C
C
      FUNCTION DERIV(F,X)
      IMPLICIT REAL * 8 ( A-H , O-Z )
      COMMON / DEL / DELTAX
      EXTERNAL F
      DERIV = ( F(X+DELTAX) - F(X-DELTAX) ) / ( 2.D0*DELTAX )
      RETURN
      END
C
C
      SUBROUTINE OUTPUT ( XST,XEN,A1 )
      IMPLICIT REAL * 8 ( A-H , O-Z )
      EXTERNAL F0, F1
      NDIV = 10
      DX = ( XEN - XST ) / NDIV
      WRITE(1,*)'X-COORDINATE U(X) DU/DX EXACT'
      DO I = 1 , NDIV+1
      X = DX*(I-1) + XST
      UX = F0(X) + A1*F1(X)
      DUDX = DERIV(F0,X)+A1*DERIV(F1,X)
      WRITE(1,*) X, UX, DUDX, EXACT(X)
      END DO
      RETURN
      END
C
C
      FUNCTION EXACT (X)
      IMPLICIT REAL * 8 ( A-H , O-Z )
      COMMON / DEL / DELTAX     /DOMAIN/ RL    /BORDER/ U0, UL
      COMMON / COFF / ALPHASQ
      A = U0
      AL = DSQRT (ALPHASQ)
      B = (UL-U0*DCOS(AL*RL))/DSIN(AL*RL)
      EXACT = A*DCOS(AL*X) + B*DSIN(AL*X)
      RETURN
      END