> For the complete documentation index, see [llms.txt](https://applied-physics.gitbook.io/warp-factory/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://applied-physics.gitbook.io/warp-factory/examples/metrics/m3-building-a-metric.md).

# M3 - Building a Metric

## Building a Custom Metric

Now that you understand the basics of metrics, let's provide the foundation for building a custom metric.

### Needed properties

A custom metric will require all of the properties described in the first metric example. Namely:

&#x20;`.type`&#x20;

`.index`&#x20;

`.coords`

`.scaling`

`.tensor`

## Step-by-Step

You can use the code blocks below as a basis for creating a custom metric in Warp Factory.

### Create a new MATLAB function file

Inside the 'Metrics' folder, right-click and select New -> Function&#x20;

Name the file 'myCustomMetric.m'

### Replace the function definition with

{% code lineNumbers="true" %}

```matlab
function [Metric] = myCustomMetric(gridSize,worldCenter,gridScaling,[args])
```

{% endcode %}

Where \[args] are any needed parameters to create your metric.

### Define needed properties besides tensor

<pre><code>Metric.type = "metric";
Metric.name = "My Custom Metric";
Metric.scaling = gridScaling;
Metric.coords = "cartesian";
<strong>Metric.index = "covariant";
</strong>Metric.date = date;
</code></pre>

### Define metric tensor components

The tensor object is a 4x4 cell array. Each cell contains an array of values for each point in spacetime.

{% code overflow="wrap" lineNumbers="true" fullWidth="true" %}

```matlab
T = 1:gridSize(1);
X = 1:gridSize(2);
Y = 1:gridSize(3);
Z = 1:gridSize(4);

% Set Minkowski terms
% This sets all 4x4 terms of the tensor to flat space
% This allows you to delete flat parts of the metric in the assignments
% below
Metric.tensor = setMinkowski(gridSize);

% Assign metric terms
for h = T
    for i = X
        for j = Y
            for k = Z
                % Define grid coordinates
                t = h*gridScaling(1)-wolrdCenter(1)
                x = i*gridScaling(2)-worldCenter(2);
                y = j*gridScaling(3)-worldCenter(3);
                z = k*gridScaling(4)-worldCenter(4);
                
                % Diagonal terms
                Metric.tensor{1,1}(h,i,j,k) = % Your equations as a function of t,x,y,z here
                Metric.tensor{2,2}(h,i,j,k) = % Your equations as a function of t,x,y,z here
                Metric.tensor{3,3}(h,i,j,k) = % Your equations as a function of t,x,y,z here
                Metric.tensor{4,4}(h,i,j,k) = % Your equations as a function of t,x,y,z here

                % Time cross terms
                Metric.tensor{1,2}(h,i,j,k) = % Your equations as a function of t,x,y,z here
                Metric.tensor{1,3}(h,i,j,k) = % Your equations as a function of t,x,y,z here
                Metric.tensor{1,4}(h,i,j,k) = % Your equations as a function of t,x,y,z here
                
                % Spatial cross terms
                Metric.tensor{2,3}(h,i,j,k) = % Your equations as a function of t,x,y,z here
                Metric.tensor{2,4}(h,i,j,k) = % Your equations as a function of t,x,y,z here
                Metric.tensor{3,4}(h,i,j,k) = % Your equations as a function of t,x,y,z here
                
                % Assign Symmetric Values
                Metric.tensor{2,1}(h,i,j,k) = Metric.tensor{1,2}(h,i,j,k);
                Metric.tensor{3,1}(h,i,j,k) = Metric.tensor{1,3}(h,i,j,k);
                Metric.tensor{4,1}(h,i,j,k) = Metric.tensor{1,4}(h,i,j,k);
                Metric.tensor{3,2}(h,i,j,k) = Metric.tensor{2,3}(h,i,j,k);
                Metric.tensor{4,2}(h,i,j,k) = Metric.tensor{2,4}(h,i,j,k);
                Metric.tensor{4,3}(h,i,j,k) = Metric.tensor{3,4}(h,i,j,k);
            end
        end
    end
end
```

{% endcode %}

### End the function

```matlab
end
```

### Calling Your Function

In the Command Window or in custom script, your metric can be called via:

{% code overflow="wrap" lineNumbers="true" %}

```matlab
% Specify default args
gridSize = [1 20 20 20];
worldCenter = (gridSize+1)./2;
gridScaling = [1 1 1 1];

% Specify custom args here
args = 

Metric = myCustomMetric(gridSize,worldCenter,gridScaling,[args]);
```

{% endcode %}

## 3+1 Definition

Similar to building your metric directly via the metric tensor components, you can also build your metric via the 3+1 components.&#x20;

The defining metric tensor components section gets changed to:

{% code overflow="wrap" lineNumbers="true" fullWidth="true" %}

```matlab
T = 1:gridSize(1);
X = 1:gridSize(2);
Y = 1:gridSize(3);
Z = 1:gridSize(4);

% Set Minkowski terms
% This sets all 4x4 3+1 components to that flat space
% This allows you to delete flat parts of the 3+1 in the assignments
% below
[alpha, beta, gamma] = setMinkowskiThreePlusOne(gridSize);

% Assign 3+1 terms
for h = T
    for i = X
        for j = Y
            for k = Z
                % Define grid coordinates
                t = h*gridScaling(1)-worldCenter(1)
                x = i*gridScaling(2)-worldCenter(2);
                y = j*gridScaling(3)-worldCenter(3);
                z = k*gridScaling(4)-worldCenter(4);
                
                % Alpha term
                alpha(h,i,j,k) = % Your equations as a function of t,x,y,z here

                % Beta terms
                beta{1}(h,i,j,k) = % Your equations as a function of t,x,y,z here
                beta{2}(h,i,j,k) = % Your equations as a function of t,x,y,z here
                beta{3}(h,i,j,k) = % Your equations as a function of t,x,y,z here
                
                % Gamma terms
                gamma{1,1}(h,i,j,k) = % Your equations as a function of t,x,y,z here
                gamma{2,2}(h,i,j,k) = % Your equations as a function of t,x,y,z here
                gamma{3,3}(h,i,j,k) = % Your equations as a function of t,x,y,z here
                gamma{1,2}(h,i,j,k) = % Your equations as a function of t,x,y,z here
                gamma{1,3}(h,i,j,k) = % Your equations as a function of t,x,y,z here
                gamma{2,3}(h,i,j,k) = % Your equations as a function of t,x,y,z here
                
                % Assign Symmetric Gamma Terms
                gamma{2,1}(h,i,j,k) = gamma{1,2}(h,i,j,k);
                gamma{3,1}(h,i,j,k) = gamma{1,3}(h,i,j,k);
                gamma{3,2}(h,i,j,k) = gamma{2,3}(h,i,j,k);
            end
        end
    end
end

% Convert 3+1 terms into the Metric
Metric.tensor = threePlusOneBuilder(alpha,beta,gamma);
```

{% endcode %}


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter, and the optional `goal` query parameter:

```
GET https://applied-physics.gitbook.io/warp-factory/examples/metrics/m3-building-a-metric.md?ask=<question>&goal=<endgoal>
```

`ask` is the immediate question: it should be specific, self-contained, and written in natural language.
`goal` is optional and describes the broader end goal you are ultimately trying to accomplish on behalf of the user. GitBook uses it to tailor the answer towards what is most useful for that goal.

The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
