%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
%
% This function analyticaly computes the stress field for a collection of 
% arrays of infinitely sharp dislocations with the specified Burgers 
% vectors and positions.
%
% Arguments:
% - X, Y:                    computational grids (as generated by meshgrid())
% - num_dislocation_arrays:  number of dislocation arrays
% - burgers_vectors:         matrix containing Burgers vectors for 
%                            dislocation arrays.  The i-th row should
%                            contain the Burgers vector for the i-th
%                            dislocation array
% - positions:               matrix containing positions for dislocation
%                            arrays.  The i-th row should contain the (x,y)
%                            coordinates of the i-th dislocation array
% - G:                       shear modulus
% - poisson_ratio:           Poisson ratio
%
% Return values:
% - sigma_xx:                xx-component of stress field
% - sigma_yy:                yy-component of stress field
% - sigma_xy:                xy-component of stress field
%
%
% NOTES:
% - The line direction of all dislocations is assumed to be in the (0,0,1) 
%   direction.  
% - Each array is oriented such that the normal to the plane of the 
%   dislocation array is in the (1,0,0) direction and all dislocation 
%   arrays are periodically repeated in the x-direction.  
% - The stress fields for the dislocation configuration are computed using
%   the Fourier transform formulation in Xiang et. al.  
%
% Kevin T. Chu
% MAE, Princeton University
% 08/2007
%
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

function [sigma_xx, sigma_yy, sigma_xy] = ...
  compute_analytical_stress_fields_sharp(X, Y, ...
                                         num_dislocation_arrays, ...
                                         burgers_vectors, positions, ...
                                         G, poisson_ratio);

% check arguments
if (num_dislocation_arrays ~= size(burgers_vectors,1))
  error('Number of Burgers vectors does not match number of dislocation arrays');
end
if (num_dislocation_arrays ~= size(positions,1))
  error('Number of positions does not match number of dislocation arrays');
end


% compute Ly
Ly = size(Y,2)*(Y(1,2)-Y(1,1));

% initialize analytical solution for stress
sigma_xx = zeros(size(X));
sigma_yy = zeros(size(X));
sigma_xy = zeros(size(X));


% compute stress field by summing up the stress field from 
% individual dislocations
for line_num = 1:num_dislocation_arrays

  % get Burgers vector and position of current dislocation line
  b = burgers_vectors(line_num,:);
  pos = positions(line_num,:);

  % compute contribution to analytical solution for stress
  % including all periodic images in the y-direction and
  % first neighbors in the x-direction
  [sigma_xx_cur, sigma_yy_cur, sigma_xy_cur] = ...
    dislocation_array_stress_field_sharp( ...
      b, pos, Ly, X, Y, G, poisson_ratio );
  sigma_xx_cur = sigma_xx_cur;
  sigma_yy_cur = sigma_yy_cur;
  sigma_xy_cur = sigma_xy_cur;
  sigma_xx = sigma_xx + sigma_xx_cur;
  sigma_yy = sigma_yy + sigma_yy_cur;
  sigma_xy = sigma_xy + sigma_xy_cur;


end

