Weird std::map problem

Pages: 12
I'm running into a weird problem where std::map.at("...") is throwing an out_of_range exception, but when I iterate over the map with an iterator, and when I call map.find("..."), it can find the key. Also, at a previous point in the program, map.at("...") is working on the same key as expected. After doing a find/search through the code base, the only methods I am invoking on the map are map["..."] = ... to insert values, and then a whole lot of map.at("...") to retrieve, so I don't have any idea why this isn't working.
Last edited on
I suppose you've forgotten to ask smart questions in the time since your last post.

Please carefully read:
http://www.catb.org/esr/faqs/smart-questions.html

Last edited on
@mbozzi If you could be more specific, Id happily follow up with more details. I didn't want to post the code as the file is quite large. And I thought I already explained the relevant parts. Would you like me to post the entire file?
make a small example that re-creates the problem if you can. If you can't replicate the issue that way, try cutting up the original big block of code with comments until the issue goes away, to find where the issue is, and then wrap up that block in a small sample that shows the problem.
I thought I already explained the relevant parts.

If you've really explained all the relevant parts than you're saying that something impossible is happening. What are we supposed to say, that your hard drive is haunted? Clearly you're overlooking something.

You either need to upload a zip of the code somewhere and post a link here, or create a pared-down version that demonstrates the same misbehavior.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
#include <iostream>
#include <map>
#include <string>

int main()
{
    std::map<std::string, std::string> mapping;
    
    mapping["AAA"] = "aaa";
    mapping["..."] = "dot_dot_dot";
    mapping[".."]  = "...";
    for(auto i:mapping)
        std::cout << i.first << ' ' << i.second << '\n';
    
    std::string item;
    item = mapping.find("...")->second;
    std::cout << item << '\n';
    
    mapping.at(mapping.find("...")->first) = "xyz"; // <--
    
    for(auto i:mapping)
    std::cout << i.first << ' ' << i.second << '\n';
    
    return 0;
}
.. ...
... dot_dot_dot
AAA aaa
dot_dot_dot
.. ...
... xyz
AAA aaa
Program ended with exit code: 0
Last edited on
First, thank you all for taking a look.

Unfortunately I haven't been able to reproduce the problem in another sample program (so far).

I uploaded the code to GitHub. (I couldn't post the whole code here directly. It was too long.) https://github.com/Mathhead200/Molecular-Spintronics-Research-Project/blob/master/MSD_Research_Project_01-25-2020/MSD%20Research%20Project%20(v2.2.0)/src/metropolis.cpp#L425

The code works if I comment out the map.at calls here, but during debugging, it appears that the keys (and values) are in the map. Also I call map.at on the same keys earlier on line 255.

The map is being populated from a text file: https://github.com/Mathhead200/Molecular-Spintronics-Research-Project/blob/master/MSD_Research_Project_01-25-2020/MSD%20Research%20Project%20(v2.2.0)/parameters-metropolis.txt

And the program is being run from metropolis.bat, which simply sets up paths for output and parameter files and such.

Any insight into what could be going on would be greatly appreciated. This is code I wrote 6+ years ago that I'm resurrecting to try and add features to, and I'm sure I just over looked something.

Thank you again, very much!
With all due respect I don't think the aforementioned xy technique now being further compounded is the way forward to you debugging your program.

Either you understand how <maps> work or you don't in which case read the stuff already posted here and/or the tutorials/references on this site..

By widening the issue, and yes the problem might be elsewhere, you are just wasting your time.

If you have written a pile of code that won't fit here then the chances are you need to go back to some form of unit testing and outputting some trace-style intermediate results to ensure each part is doing what you planned (in the unlikely event that you actually have a plan).

To read in some data from a text file, <map>-ing, and displaying the contents takes about a dozen lines of code.

What's say you show us the code for just doing that?
Out-of-range from a map means it cannot find the element you're asking for.

What kind of map is it? Show us the line of code that defines what kind of map it is.

What kind of key are you putting into it? Are you putting in std::string or are you putting in char* ? What kind of key are you trying to retrieve with? std::string or char* ?

Are you just trying to return the object stored, or are you immediately trying to reach into that returned object as well? What happens if you simplify the code to just fetch the object?
Last edited on
Try putting your code to iterate through the map immediately before the code where it crashes. I think you'll find that the item isn't in the map.

That means it's getting inserted somewhere between where the code crashes and where you were iterating through the map before.
@againtry Harsh dude. It's just code that I haven't worked on in a long time. Cut me some slack.

Unit testing for this program would be a nightmare to develop. It uses random numbers over millions of iterations on floating point vectors to determine each output. And the outputs are essential CSV or XML files.

I already know the previous version of the code from 2014 works. And I did some preliminary testing on the model (underlying logic) changes which are working (so far). I know (ok, I suspect heavily) that this problem is somewhere in this code: metropolis.cpp, but std::map seams to be acting in a way I've never seen before in my 11+ years of programming. Granted, I haven't used C++ in awhile. I do mainly JS and Java programming these days. But I do know what I'm doing... most of the time. :)

Also, as far as the code for reading data from a file and storing it into a map, it's in metropolis.cpp: lines 167 - 208.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
	map<string, vector<double>> p;
	{	//initialize parameters from file
		stringstream ss;
		ss << argv[1];
		string str = ss.str();
		ifstream fin(str);
		string key;
		try {
			while( fin >> key ) {
				vector<double> vec;
				double val;
				if( !(fin >> str >> val) )
					throw 1;
				if( str == "=" )
					vec.push_back(val);
				else if( str == ":" ) {
					double lim, inc;
					if( !(fin >> lim >> inc) || inc == 0 )
						throw 2;
					lim += inc / 256; //small compinsation for floating point error
					while( (inc > 0 && val < lim) || (inc < 0 && val > lim) ) {
						vec.push_back(val);
						val += inc;
					}
				} else if( str == "{" ) {
					do {
						vec.push_back(val);
					} while( fin >> val );
					fin.clear();
					fin >> str;
					if( str != "}" )
						throw 3;
				} else
					throw 4;
				p[key] = vec;
				// cout << key << " => " << vec[0] << endl; //DEBUG
			}
		} catch(int e) {
			cerr << '(' << (e |= 0x10) << ") Corrupted parameters file!\n";
			return e;
		}
	}



@Repeater
It's a std::map<std::string, std::vector<double>>
It's defined on the first line in the above code block (line 167 in metropolis.cpp)
I am retrieving with char * literals. There could be an issue with the automatic casting from char * to std::string, but if so, it isn't an issue for any of the other calls like I mentioned in my first post.

I am immediately fetching the first element of the returned vector, which I realized already can also throw an out_of_range exception, although I'm using the [] operator which doesn't throw exception (as far as I know). When I simplified the code to only return the object and not do anything with it, it still throws the exception. And when I print the error message, it's obvious that its coming from map and not vector. "<K, V>" was in the error message.


@dhayden
I think I already tried that, but I'm happy to do it again and post the results. Hold on...

C:\Users\mathh\GitHub\Molecular-Spintronics-Research-Project\MSD_Research_Project_01-25-2020\MSD Research Project (v2.2.0)>bin\metropolis parameters-metropolis.txt "out\metropolis, CONTINUOUS_SPIN_MODEL, 01-29-2020, 4.xml" CONTINUOUS_SPIN_MODEL 1
0% [0 days,  0: 0: 1]
 -- AL => 0.50
 -- AR => 0.50
 -- Am => 0.00
 -- B_x => 0.00 
 -- B_y => -1.50 -1.45 -1.40 -1.35 -1.30 -1.25 -1.20 -1.15 -1.10 -1.05 -1.00 -0.95 -0.90 -0.85 -0.80 -0.75 -0.70 -0.65 -0.60 -0.55 -0.50 -0.45 -0.40 -0.35 -0.30 -0.25 -0.20 -0.15 -0.10 -0.05 0.00 0.05 0.10 0.15 0.20 0.25 0.30 0.35 0.40 0.45 0.50 0.55 0.60 
0.65 0.70 0.75 0.80 0.85 0.90 0.95 1.00 1.05 1.10 1.15 1.20 1.25 1.30 1.35 1.40 1.45 1.50 
 -- B_z => 0.00 
 -- JL => 1.00
 -- JLR => 0.00
 -- JR => 1.00
 -- Jm => 0.00 
 -- JmL => 0.20 
 -- JmR => -0.20
 -- bL => 0.00
 -- bLR => 0.00
 -- bR => 0.00
 -- backR => 6.00 
 -- bm => 0.00
 -- bmL => 0.00
 -- bmR => 0.00
 -- bottomL => 6.00 
 -- depth => 10.00
 -- freq => 1.00
 -- frontR => 3.00
 -- height => 10.00 
 -- kT => 0.34
 -- molPosL => 5.00
 -- molPosR => 5.00
 -- simCount => 0.00
 -- t_eq => 10000.00
 -- topL => 3.00
 -- width => 11.00

 -- 1

Parameter file is missing some data!
invalid map<K, T> key
----------------------------------------
Wed 01/29/2020 
02:13 AM
Press any key to continue . . .


See I'm not crazy. Clearly you can see topL => 3.00 in the map, but then on the next line I'm getting the error. I know it's being caused by that line, because when I switch the comment, to just hard code the value and not call p.at("topL"), it doesn't crash. Also, I'm running the code with only 1 thread to make sure there wasn't any concurrency issues.

Here's the code that produced that output:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
			// TODO: Why are these lines causing an error?!!!!!!!!!!!!!!!!! WTF!
			// DEBUG print map keys and values
			for (auto iDebug = p.begin(); iDebug != p.end(); iDebug++) {
				cout << " -- " << iDebug->first << " => ";
				auto val = iDebug->second;
				for (auto iDebug2 = val.begin(); iDebug2 != val.end(); iDebug2++)
					cout << *iDebug2 << ' ';
				cout << '\n';
			}
			cout << "\n -- " << (p.find("topL") != p.end()) << "\n\n";  // WTF!
			/*  preInfo.topL = 0; */ preInfo.topL = p.at("topL")[0];
			preInfo.bottomL = preInfo.height - 1; // preInfo.bottomL = p.at("bottomL")[0];
			preInfo.frontR = 0; // preInfo.frontR = p.at("frontR")[0];
			preInfo.backR = preInfo.depth - 1; // preInfo.backR = p.at("backR")[0]; 


I've updated the GitHub as well. Code on lines 425 - 438 in metropolis.cpp

Also, in case it helps, here's the parameters file that is initializing the keys and values:

width  = 11
height = 10
depth  = 10

molPosL = 5
molPosR = 5

topL    = 3
bottomL = 6
frontR  = 3
backR   = 6

t_eq     = 10000
simCount = 0
freq     = 1

kT = 0.34

B_x = 0
B_y : -1.5  1.5  0.05
B_z = 0

JL  = 1
JR  = 1
Jm  = 0
JmL = 0.2
JmR = -0.2
JLR = 0

AL = 0.5
AR = 0.5
Am = 0

bL  = 0
bR  = 0
bm  = 0
bmL = 0
bmR = 0
bLR = 0


Update: also just tried commenting out the "find", it' not causing any side effects. (This is a real head scratcher for me.)

For the record, I wrote all of this code myself (except the 3rd part libraries I'm using for mersenne twister (random numbers), and the XML stuff.) Just wrote it a long time ago. So if you have any questions about things that you think could be causing the problem, I can definitely answer them. I don't expect anyone to ready through all of the included header files after all. :)
Last edited on
@Mathhead200,

Could you give us some clue as to how to compile and link your code.

When I tried (by guesswork)
g++ iterate.cpp heat.cpp extract.cpp magnetize.cpp spin-test.cpp metropolis.cpp simple_msd_sim.cpp

then I got a huge number of errors. Many of them were very similar, and the typical ones are listed below. Note that a lot of them may well be caused by the fact that:
(1) you have a lot of "definition" code in your header (.h) files;
(2) you appear to have a lot of circular dependencies in rapidxml_print.hpp
(I can work around most of the errors, except for the ones emerging from rapidxml_print.hpp)

In file included from iterate.cpp:10:
MSD.h:135:43: error: extra qualification 'udc::MSD::' on member 'rand' [-fpermissive]
         uniform_real_distribution<double> MSD::rand; //uniform probability density function on the interval [0, 1)
                                           ^~~

<MASSES OF SIMILAR ERRORS>

                                           ^~~
metropolis.cpp: In function 'int main(int, char**)':
metropolis.cpp:250:64: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
    recordVar( doc, *global, "param", "width", p.at("width")[0] );
                                                                ^
metropolis.cpp:254:68: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
metropolis.cpp:255:62: warning: ISO C++ forbids converting a string constant to 'char*' [-Wwrite-strings]
    recordVar( doc, *global, "param", "topL", p.at("topL")[0] );
                                                              ^
                                                   ^

<MASSES OF SIMILAR ERRORS>

                                           ^~~
In file included from metropolis.cpp:19:
rapidxml_print.hpp: In instantiation of 'OutIt rapidxml::internal::print_node(OutIt, const rapidxml::xml_node<Ch>*, int, int) [with OutIt = std::ostream_iterator<char, char, std::char_traits<char> >; Ch = char]':
rapidxml_print.hpp:390:36:   required from 'OutIt rapidxml::print(OutIt, const rapidxml::xml_node<Ch>&, int) [with OutIt = std::ostream_iterator<char, char, std::char_traits<char> >; Ch = char]'
rapidxml_print.hpp:403:14:   required from 'std::basic_ostream<Ch>& rapidxml::print(std::basic_ostream<Ch>&, const rapidxml::xml_node<Ch>&, int) [with Ch = char]'
rapidxml_print.hpp:414:21:   required from 'std::basic_ostream<Ch>& rapidxml::operator<<(std::basic_ostream<Ch>&, const rapidxml::xml_node<Ch>&) [with Ch = char]'
metropolis.cpp:283:11:   required from here
rapidxml_print.hpp:115:37: error: 'print_children' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
                 out = print_children(out, node, flags, indent);
                       ~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
rapidxml_print.hpp:169:22: note: 'template<class OutIt, class Ch> OutIt rapidxml::internal::print_children(OutIt, const rapidxml::xml_node<Ch>*, int, int)' declared here, later in the translation unit
         inline OutIt print_children(OutIt out, const xml_node<Ch> *node, int flags, int indent)
                      ^~~~~~~~~~~~~~
rapidxml_print.hpp:120:41: error: 'print_element_node' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
                 out = print_element_node(out, node, flags, indent);
                       ~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
rapidxml_print.hpp:242:22: note: 'template<class OutIt, class Ch> OutIt rapidxml::internal::print_element_node(OutIt, const rapidxml::xml_node<Ch>*, int, int)' declared here, later in the translation unit
         inline OutIt print_element_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
                      ^~~~~~~~~~~~~~~~~~
rapidxml_print.hpp:125:38: error: 'print_data_node' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation [-fpermissive]
                 out = print_data_node(out, node, flags, indent);
                       ~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
rapidxml_print.hpp:208:22: note: 'template<class OutIt, class Ch> OutIt rapidxml::internal::print_data_node(OutIt, const rapidxml::xml_node<Ch>*, int, int)' declared here, later in the translation unit
         inline OutIt print_data_node(OutIt out, const xml_node<Ch> *node, int flags, int indent)
                      ^~~~~~~~~~~~~~~

<MASSES OF SIMILAR ERRORS>

                                           ^~~
                      ^~~~~~~~~~~~~
In file included from simple_msd_sim.cpp:16:
MSD.h:135:43: error: extra qualification 'udc::MSD::' on member 'rand' [-fpermissive]
         uniform_real_distribution<double> MSD::rand; //uniform probability density function on the interval [0, 1)
                                           ^~~
Last edited on
Unit testing for this program would be a nightmare to develop.

Please yourself "Dude" because long experience with this sort of rubbish shows that untrained hackers in denial don't actually want to get their problem solved.

They thrive on the problem not the solution.

By saying unit testing would be a nightmare, not only misses my point which related directly to your original <map> difficulty, all you are indicating is your code is just the un-managed pile of crap with no plan, no framework, no documentation and no chance of being de-bugged, as indicated on github.

Good luck with it though, it's fascinating with this sort of OP how the hole gets deeper and deeper and strays off on the proverbial tangent of ever increasing complexity.
OK, got it to compile (sort of, with some potentially-significant grumbles) with just
g++ -Wall -pedantic -Wextra metropolis.cpp

after I put some forward declarations in the rapidxml_print.hpp file and removed the bugs in MSD.h.


Please could you state exactly
(i) what the command is to run it (it appears to need command-line arguments);
(ii) the contents of the parameters file.
(Please don't run these into each other, as in your earlier post).



Last edited on
OK, I managed to make a params file and guess your command to run.

Mathhead200 wrote:
I know it's being caused by that line

I'm not convinced.



When I run it (yes, finally!), I locate the call that fails here...
1
2
3
4
5
6
                        } else {
                                cout << "About to run algorithm ...\n";
                                //mono-threaded
                                postInfo = algorithm(preInfo);                 // <==== IT FAILS DURING THIS CALL
                                cout << "... Ok";
                        }


When I go to algorithm I find
1
2
3
4
5
        // msd.randomize();
        cout << "OK1\n";
        msd.metropolis( info.t_eq, 0 );            // IT FAILS DURING THIS CALL
        cout << "OK2\n";
        msd.metropolis( info.simCount, info.freq );




Now I have to go to the file MSD.h. I put some comment lines in the following routine
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
void MSD::metropolis(unsigned long long N) {
        using std::cout;
        cout << "N=" << N << '\n';
        function<double()> random = bind( rand, ref(prng) );
        cout << "Got past random\n";
        Results r = getResults(); //get the energy of the system
        cout << "Got past getResults\n";
        //start loop (will iterate N times)
        for( unsigned long long i = 0; i < N; i++ ) {
                cout << "i=" << i << '\n';
                unsigned int a = indices[static_cast<unsigned int>( random() * indices.size() )]; //pick an atom (pseudo) randomly
                Vector s = getSpin(a);
                setSpin( a, flippingAlgorithm(s, random) ); //"flip" that atom
                Results r2 = getResults(); //get the energy of the system (for the new state)
                if( r2.U <= r.U || random() < pow( E, (r.U - r2.U) / parameters.kT ) ) {
                        //either the new system requires less energy or external energy (kT) is disrupting it
                        r = r2; //in either case we keep the new system
                } else {
                        //neither thing (above) happened so we revert the system
                        atoms[a] = s; //revert the system by flipping the atom back
                        results = r;
                }
        }
        cout << "Got here\n";
        results.t += N;
}


and I get
0% [0 days,  0: 0: 0]
 -- AL => 0.50 
 -- AR => 0.50 
 -- Am => 0.00 
 -- B_x => 0.00 
 -- B_y => -1.50 
 -- B_z => 0.00 
 -- JL => 1.00 
 -- JLR => 0.00 
 -- JR => 1.00 
 -- Jm => 0.00 
 -- JmL => 0.20 
 -- JmR => -0.20 
 -- bL => 0.00 
 -- bLR => 0.00 
 -- bR => 0.00 
 -- backR => 6.00 
 -- bm => 0.00 
 -- bmL => 0.00 
 -- bmR => 0.00 
 -- bottomL => 6.00 
 -- depth => 10.00 
 -- freq => 1.00 
 -- frontR => 3.00 
 -- height => 10.00 
 -- kT => 0.34 
 -- molPosL => 5.00 
 -- molPosR => 5.00 
 -- simCount => 0.00 
 -- t_eq => 10000.00 
 -- topL => 3.00 
 -- width => 11.00 
Success!
About to run algorithm ...
OK1
N=10000
Got past random
Got past getResults
i=0
i=1
i=2
i=3
i=4
i=5


Now, because of the randomisation, that last line could be any value of i ... although it tends to be quite early.


So, I think that in one of the calls in this loop there is an error for your particular parameters. There are too many onward calls for me to check here just now - I have to get back to work.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
        for( unsigned long long i = 0; i < N; i++ ) {
                cout << "i=" << i << '\n';
                unsigned int a = indices[static_cast<unsigned int>( random() * indices.size() )]; //pick an atom (pseudo) randomly
                Vector s = getSpin(a);
                setSpin( a, flippingAlgorithm(s, random) ); //"flip" that atom
                Results r2 = getResults(); //get the energy of the system (for the new state)
                if( r2.U <= r.U || random() < pow( E, (r.U - r2.U) / parameters.kT ) ) {
                        //either the new system requires less energy or external energy (kT) is disrupting it
                        r = r2; //in either case we keep the new system
                } else {
                        //neither thing (above) happened so we revert the system
                        atoms[a] = s; //revert the system by flipping the atom back
                        results = r;
                }
        }

Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <map>
#include <vector>
#include <iomanip>

int main()
{
    std::vector<double> vec_dbl_1, vec_dbl_2;
    
    for(int i = 0; i < 5; i++)
        vec_dbl_1.push_back(1/(1. + i));
    
    for(int i = 0; i < 7; i++)
        vec_dbl_2.push_back(3.7/(19.2 + i));
    
    std::map<std::string, std::vector<double> > a_map;
    
    a_map["vec_dbl_1"] = vec_dbl_1;
    a_map["vec_dbl_2"] = vec_dbl_2;
    a_map.emplace("xy prob", vec_dbl_2);
    
    for(auto i: a_map)
    {
        std::cout << std::setw(10) << i.first << ' ';
        for(auto j:i.second)
            std::cout << std::right << std::setw(10) << j << ' ';
        std::cout << '\n';
    }
    return 0;
}


 vec_dbl_1          1        0.5   0.333333       0.25        0.2 
 vec_dbl_2   0.192708   0.183168   0.174528   0.166667   0.159483   0.152893   0.146825 
   xy prob   0.192708   0.183168   0.174528   0.166667   0.159483   0.152893   0.146825 
Program ended with exit code: 0
Last edited on
Further investigation ...

The code invariably gets (several times) into the following routine in MSD.h
void MSD::setSpin(unsigned int a, const Vector &spin)

Eventually, it gets in ... but fails to get out without throwing ... of this routine by either of the two return points.

There is a map (atoms) there; unlike a fully-initialised array, not all of the parameters you are giving to atoms.at() have been set up.

You would be well advised to set your seed for the randomisation to be a fixed value (e.g. 0). Then you will produce the same set of random numbers and will always throw at the same place. Depending on the seed I can always make one of those .at values fail.




Incidentally, you are being hoodwinked by your "Parameter file is missing some data" message, because that will be given for anything that throws, not just parameter problems. Moreover, exceptions get thrown onward, I'm afraid. Catch them early.
Last edited on
WTF has any of that got to do with the original question?
WTF has any of that got to do with the original question?


I believe that the OP has incorrectly identified which line (indeed, which procedure and which file) is at fault. There are several places in the code where a map.at() call can throw ... and I think it is within a particular routine in the separate file MSD.h, not the one that the OP has identified here. The OP has put an error message at such a point in his code that makes him think the problem lies with parameter loading; it is not. Basically, he is looking at the wrong map. He states that he tried hard-coding a value instead and it didn't crash. That may well be the case, but he hard-coded a trivial value (0), not the value that was set originally in the parameters file (3).

Obviously I could be completely wrong, but (with some editing, guesswork and crossed fingers) I did eventually make his code successfully compile, link and start running. It correctly read some parameters and (first) threw from routine
void MSD::setSpin(unsigned int a, const Vector &spin)
For some random values it goes IN, but doesn't successfully get OUT. (Note that there are two possible return points to worry about in this procedure.) It throws from this routine at a .at() call because some keys in a separate map, atoms[], do not exist. (Possibly this map has not been fully initialised in a completely different routine; personally I would have used an array not a map.) By using the same seed for random numbers I can home in on the actual line that throws, although I suspect this will be different for different seeds.


EDIT: I didn't report @againtry and I wasn't remotely offended by his question. There seems to be an "XYProblem" in the original post here.
Last edited on
> Could you give us some clue as to how to compile and link your code.
$ cd Molecular-Spintronics-Research-Project/MSD_Research_Project_01-25-2020/MSD Research Project (v2.2.0)
$ g++ -ggdb src/metropolis.cpp -fpermissive -pthread -o metropolis.bin


> what the command is to run it (it appears to need command-line arguments);
$ ./metropolist.bin parameters-metropolis.txt output CONTINUOUS_SPIN_MODEL 1


that causes an exception to be thrown
Parameter file is missing some data!
map::at
so I commented your try-catch to actually know where the exception was happening
(gdb) backtrace
#8  0x000055555555f183 in udc::MSD::setSpin (this=0x7ffffffebf80, a=116, spin=...) at src/MSD.h:844
#9  0x000055555556022e in udc::MSD::metropolis (this=0x7ffffffebf80, N=10000) at src/MSD.h:999
#10 0x0000555555560629 in udc::MSD::metropolis (this=0x7ffffffebf80, N=10000, freq=0) at src/MSD.h:1015
#11 0x0000555555563806 in algorithm (info=...) at src/metropolis.cpp:81
#12 0x00005555555692a1 in main (argc=5, argv=0x7fffffffdde8) at src/metropolis.cpp:485


1
2
//std::map<unsigned int, Vector> atoms;
Vector neighbor = atoms.at(index(molPosL - 1, y, z));

where index() is giving 114, but the map jumps from 109 to 116
Pages: 12