# Segmentation fault when trying to read in site and wavefunctions

Hello,

I'm attempting to read in a siteset and wavefunctions from a previous run, however I am running into some issues that I don't quite understand. From the documentation, I believe the code should look something like

int main()
{
int N = 3;
int dim = 10;

auto sites = Exciton(N,{"on_site_dim", dim});
auto psi = MPS(sites);

auto ampo = AutoMPO(sites);

...

auto H = toMPO(ampo);
}


where I define my Hamiltonian in the ellipses. However, the last line throws a segmentation fault. I have confirmed that if I don't try and read in a site file or wavefunction file, everything runs smoothly. Any ideas on what the problem might be? Thanks again for all of your help, Miles and Matt!

commented by (70.1k points)
Hi, I see. There's a strong chance this is causing problems. I would recommend redesigning op to analyze the tensor index (calling dim(s) on it) to figure out what dim is, not by using a global variable.

Also you could put a line Print(dim); inside the op function to check whether dim is being set to 22 or 21 or the correct value or not.
commented by (350 points)
This helped me better diagnose the problem, though I'm still not sure what the solution would be. The problem is that when I specify dim, typically dim=10, that creates an index with dimension 2*dim+1=21. Then, when that siteset is read back in, the code believes it's reading in an index with dim=21, and proceeds to try to access entries in s from 1 to 2*21+1.

So, long story short, my code might need some serious surgery in order to be able to use the read and write functions?
commented by (70.1k points)
Yes, that sounds like the problem. Why does the code think that when it's reading the indices back in, that it should interpret the dimension of the index as the number "n" that goes into the 2*n+1 formula? I don't think that's something ITensor is doing itself, but it likely a logical flaw in your code somewhere.

I think a much better design that would avoid this is to not have dim be defined in your ExcitonSite class itself. Rather, it just holds an index of some size, and knows how to extract the number you're calling dim from the size of this index (as  (dim(i)-1)/2 ). This would be used in the op function to set the for loop bounds, for example.

How does that sound?
commented by (350 points)
That solution works perfectly. As usual, thanks a million Miles.
commented by (70.1k points)