| 133 | |
| 134 | template <typename T> |
| 135 | bool GridFile::getField(Mesh* m, T& var, const std::string& name, BoutReal def, |
| 136 | CELL_LOC location) { |
| 137 | static_assert( |
| 138 | bout::utils::is_Field_v<T>, |
| 139 | "templated GridFile::getField only works for Field2D, Field3D or FieldPerp"); |
| 140 | |
| 141 | Timer timer("io"); |
| 142 | AUTO_TRACE(); |
| 143 | |
| 144 | if (not data.isSet(name)) { |
| 145 | // Variable not found |
| 146 | output_warn.write("\tWARNING: Could not read '{:s}' from grid. Setting to {:e}\n", |
| 147 | name, def); |
| 148 | var = def; |
| 149 | var.setLocation(location); |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | Options& option = data[name]; |
| 154 | |
| 155 | // Global (x, y, z) dimensions of field |
| 156 | const std::vector<int> size = option.getShape(); |
| 157 | |
| 158 | switch (size.size()) { |
| 159 | case 1: { |
| 160 | // 0 or 1 dimension |
| 161 | if (size[0] != 1) { |
| 162 | throw BoutException( |
| 163 | "Expecting a 2D variable, but '{:s}' is 1D with {:d} elements\n", name, |
| 164 | size[0]); |
| 165 | } |
| 166 | var = option.as<BoutReal>(); |
| 167 | var.setLocation(location); |
| 168 | return true; |
| 169 | } |
| 170 | case 2: { |
| 171 | // Check size |
| 172 | break; |
| 173 | } |
| 174 | case 3: { |
| 175 | // Check size if getting Field3D |
| 176 | if constexpr (bout::utils::is_Field2D_v<T> or bout::utils::is_FieldPerp_v<T>) { |
| 177 | output_warn.write( |
| 178 | "WARNING: Variable '{:s}' should be 2D, but has {:d} dimensions. Ignored\n", |
| 179 | name, size.size()); |
| 180 | var = def; |
| 181 | var.setLocation(location); |
| 182 | return false; |
| 183 | } |
| 184 | break; |
| 185 | } |
| 186 | default: { |
| 187 | output_warn.write( |
| 188 | "WARNING: Variable '{:s}' should be 2D or 3D, but has {:d} dimensions. Ignored\n", |
| 189 | name, size.size()); |
| 190 | var = def; |
| 191 | var.setLocation(location); |
| 192 | return false; |
nothing calls this directly
no test coverage detected