Skip to content

Upgraded version of the plotTensor function with improved functionality, better error handling, and enhanced readabilitpgraded version of the plotTensor function with improved functionali… #8

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
128 changes: 79 additions & 49 deletions Visualizer/plotTensor.m
Original file line number Diff line number Diff line change
@@ -1,22 +1,22 @@
function plotTensor(tensor, alpha, slicedPlanes, sliceLocations)

%% PLOTTENSOR: Plots the unique elements of the tensor based the slice plane
% PLOTTENSOR: Plots the unique elements of the tensor based on the slice plane
%
% INPUTS:
% tensor - Tensor struct object either metric of stress-energy.
% tensor - Tensor struct object either metric or stress-energy.
%
% alpha - Alpha value of the surface grid display from 0 to 1, double type.
% Default is 0.2.
%
% slicedPlanes - Coordinates that are sliced [coords1, coords2], index values from 1 to 4, double type.
% If you want the resulting slice to be in the X-Y plane for example, input [1, 4]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep

% Default is [1, 4] (X-Y plane).
%
% sliceLocation - Location of the slice in [coords1, coords2], double type.
% sliceLocations - Location of the slice in [coords1, coords2], double type.
% Default is the center of the tensor dimensions.
%
% OUTPUTS:
% [none]

%%

% Handle default input arguments
if nargin < 2
alpha = 0.2;
Expand All @@ -26,75 +26,105 @@ function plotTensor(tensor, alpha, slicedPlanes, sliceLocations)
end
if nargin < 4
s = size(tensor.tensor{1, 1});
sliceCenters = round((s+1)./2); % Assume center
sliceLocations(1) = sliceCenters(slicedPlanes(1));
sliceLocations(2) = sliceCenters(slicedPlanes(2));
sliceLocations = round((s+1)./2); % Assume center
end

% Verify tensor
if ~verifyTensor(tensor, 1)
if ~verifyTensor(tensor)
error("Tensor is not verified. Please verify tensor using verifyTensor(tensor).")
end

% Check that the sliced planes are different
if slicedPlanes(1) == slicedPlanes(2)
error("Selected planes must not be the same, select two different planes to slice along.")
error("Selected planes must not be the same. Select two different planes to slice along.")
end

% Round sliceLocations
sliceLocations = round(sliceLocations);

% Check that the sliceLocations are inside the world
if sliceLocations(1) < 1 || sliceLocations(2) < 1 || sliceLocations(1) > size(tensor.tensor{1, 1}, slicedPlanes(1)) || sliceLocations(2) > size(tensor.tensor{1, 1}, slicedPlanes(2))
sliceLocations(1)
sliceLocations(2)
size(tensor.tensor{1, 1}, slicedPlanes(1))
size(tensor.tensor{1, 1}, slicedPlanes(2))
error('sliceLocations are outside the world.')
if any(sliceLocations < 1) || any(sliceLocations > size(tensor.tensor{1, 1}, slicedPlanes))
error('sliceLocations are outside the tensor dimensions.')
end

% Check tensor type
if strcmpi(tensor.type, "Metric")
titleCharacter = "g";
elseif strcmpi(tensor.type, "Stress-Energy")
titleCharacter = "T";
end
% Determine tensor type and index for title
titleCharacter = switch lower(tensor.type)
case "metric"
"g"
case "stress-energy"
"T"
otherwise
error("Unknown tensor type. Must be 'Metric' or 'Stress-Energy'.")
end;

% Check tensor index
if strcmpi(tensor.index, "covariant")
titleAugment1 = "_{";
titleAugment2 = "";
elseif strcmpi(tensor.index, "contravariant")
titleAugment1 = "^{";
titleAugment2 = "";
elseif strcmpi(tensor.index, "mixedupdown")
titleAugment1 = "^{";
titleAugment2 = "}_{ ";
elseif strcmpi(tensor.index, "mixeddownup")
titleAugment1 = "_{";
titleAugment2 = "}^{ ";
titleAugment1 = "";
titleAugment2 = "";
switch lower(tensor.index)
case "covariant"
titleAugment1 = "_{";
titleAugment2 = "}";
case "contravariant"
titleAugment1 = "^{";
titleAugment2 = "}";
case "mixedupdown"
titleAugment1 = "^{";
titleAugment2 = "}_{";
case "mixeddownup"
titleAugment1 = "_{";
titleAugment2 = "}^{";
otherwise
error("Unknown tensor index. Must be 'covariant', 'contravariant', 'mixedupdown', or 'mixeddownup'.")
end

% Check that the coords are cartesian
if strcmpi(tensor.coords, "cartesian")
[xLabelText, yLabelText] = labelCartesianAxis(slicedPlanes);

if strcmpi(tensor.index, "mixedupdown") || strcmpi(tensor.index, "mixeddownup")
c1 = [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4];
c2 = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4];
else
c1 = [1, 1, 1, 1, 2, 3, 4, 2, 2, 3];
c2 = [1, 2, 3, 4, 2, 3, 4, 3, 4, 4];
end

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Keep this if statement. Non-mixed terms will have duplicated elements.


% Determine coordinates to plot
c1 = [1, 1, 1, 1, 2, 2, 2, 2, 3, 3, 3, 3, 4, 4, 4, 4];
c2 = [1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4, 1, 2, 3, 4];

% Get slice data
idx = getSliceData(slicedPlanes, sliceLocations, tensor);


% Plot each component
for i = 1:length(c1)
plotComponent(squeeze(tensor.tensor{c1(i), c2(i)}(idx{1}, idx{2}, idx{3}, idx{4}))', titleCharacter+titleAugment1+num2str(c1(i))+titleAugment2+num2str(c2(i))+"}", xLabelText, yLabelText, alpha)
component = squeeze(tensor.tensor{c1(i), c2(i)}(idx{1}, idx{2}, idx{3}, idx{4}));
plotComponent(component', ...
[titleCharacter, titleAugment1, num2str(c1(i)), titleAugment2, num2str(c2(i)), "}"], ...
xLabelText, yLabelText, alpha);
end
else
error('Unknown coordinate system, must be: "cartesian"')
error('Unknown coordinate system. Must be: "cartesian"')
end

end

% Helper function to label Cartesian axes
function [xLabelText, yLabelText] = labelCartesianAxis(slicedPlanes)
Copy link
Contributor

@pbbp0904 pbbp0904 Dec 25, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

These are placeholders. (Line 110 and below). Make sure to review the AI-generated code actually provides value.

axisLabels = ["x", "y", "z", "t"];
xLabelText = axisLabels(slicedPlanes(1));
yLabelText = axisLabels(slicedPlanes(2));
end

% Placeholder for verifyTensor function
function verified = verifyTensor(tensor)
% Implement actual verification logic here
verified = true;
end

end
% Placeholder for getSliceData function
function idx = getSliceData(slicedPlanes, sliceLocations, tensor)
% Implement actual slice data extraction logic here
idx = {sliceLocations(1), sliceLocations(2), :, :]; % Example
end

% Placeholder for plotComponent function
function plotComponent(component, title, xLabelText, yLabelText, alpha)
% Implement actual plotting logic here
figure;
surf(component, 'FaceAlpha', alpha);
title(title);
xlabel(xLabelText);
ylabel(yLabelText);
end