0 votes
asked by (340 points)

Hello,

I have a spin chain with a random magnetic field and a very practical way to save my parameters in a file is to save them as Args and use the writetofile() function. I wanted to save the local random magnetic field vector<double> as well but an error occurs. Is it possible that Args takes more inputs than just Vals?

Thank you !

1 Answer

0 votes
answered by (70.1k points)
selected by
 
Best answer

Hi jsf,
While that's a clever way to store and retrieve values, the Args system is really intended to pass named arguments to functions, not as a database to hold arbitrary types. If we made it possible to do so, it would cause an unacceptably high performance penalty to most of the functions using Args and go beyond the intended purpose of that system.

Instead, you can already read and write most types used in ITensor, including standard library types such as std::vector, to files using the read and write functions provided with ITensor. Here is some example code showing how.

Writing objects to a file "myfile.dat":

auto v = vector<int>(4);
v[0] = 1;
v[1] = 3;
v[2] = 5;
v[3] = 9;

auto i = Index("i",2);
auto T = randomTensor(i);

std::ofstream f("myfile.dat",std::ios::binary);
write(f,v);
write(f,i);
write(f,T);
f.close();

Reading the data back in from the file:

std::ifstream f("myfile.dat",std::ios::binary);
vector<int> v;
read(f,v);
Index i;
read(f,i);
ITensor T;
read(f,T);
f.close();

Print(v.size());
Print(v[0]);
Print(v[1]);
Print(v[2]);
Print(v[3]);
Print(T);
commented by (340 points)
Thank you for your answer. My args are actually the arguments of my class but I'll do the way you recommend
Welcome to ITensor Support Q&A, where you can ask questions and receive answers from other members of the community.

Formatting Tips:
  • To format code, indent by four spaces
  • To format inline LaTeX, surround it by @@ on both sides
  • To format LaTeX on its own line, surround it by $$ above and below
  • For LaTeX, it may be necessary to backslash-escape underscore characters to obtain proper formatting. So for example writing \sum\_i to represent a sum over i.
If you cannot register due to firewall issues (e.g. you cannot see the capcha box) please email Miles Stoudenmire to ask for an account.

To report ITensor bugs, please use the issue tracker.

Categories

...