| 1 |
// Copyright(C) 1999-2024 National Technology & Engineering Solutions |
| 2 |
// of Sandia, LLC (NTESS). Under the terms of Contract DE-NA0003525 with |
| 3 |
// NTESS, the U.S. Government retains certain rights in this software. |
| 4 |
// |
| 5 |
// See packages/seacas/LICENSE for details |
| 6 |
|
| 7 |
#include "apr_builtin.h" |
| 8 |
#include "apr_symrec.h" |
| 9 |
#include "enumerate.h" |
| 10 |
|
| 11 |
#include <algorithm> |
| 12 |
#include <cctype> |
| 13 |
#include <cerrno> |
| 14 |
#include <cfenv> |
| 15 |
#include <cmath> |
| 16 |
#include <cstdio> |
| 17 |
#include <cstdlib> |
| 18 |
#include <ctime> |
| 19 |
#include <fstream> |
| 20 |
#include <stack> |
| 21 |
#include <stdexcept> |
| 22 |
#include <string> |
| 23 |
#include <unordered_map> |
| 24 |
#include <utility> |
| 25 |
#include <vector> |
| 26 |
|
| 27 |
#include <fmt/ostream.h> |
| 28 |
#include <fmt/printf.h> |
| 29 |
|
| 30 |
#if defined(WIN32) || defined(__WIN32__) || defined(_WIN32) || defined(_MSC_VER) || \ |
| 31 |
defined(__MINGW32__) || defined(_WIN64) || defined(__MINGW64__) |
| 32 |
#include <io.h> |
| 33 |
#endif |
| 34 |
#include "apr_scanner.h" |
| 35 |
#include "apr_tokenize.h" |
| 36 |
#include "apr_util.h" |
| 37 |
#include "aprepro.h" |
| 38 |
#include "aprepro_parser.h" |
| 39 |
#include <cassert> |
| 40 |
#include <cstring> |
| 41 |
|
| 42 |
#include <random> |
| 43 |
|
| 44 |
#ifndef PI |
| 45 |
#define PI 3.141592653589793238462643 |
| 46 |
#endif |
| 47 |
|
| 48 |
namespace { |
| 49 |
std::unordered_map<size_t, std::vector<std::string>> tokenized_strings; |
| 50 |
|
| 51 |
std::vector<std::string> &get_tokenized_strings(const char *string, const char *delm) |
| 52 |
{ |
| 53 |
// key is address of string + hash of delimiter |
| 54 |
size_t key = std::hash<std::string>{}(string) + std::hash<std::string>{}(delm); |
| 55 |
if (tokenized_strings.find(key) == tokenized_strings.end()) { |
| 56 |
std::string temp = string; |
| 57 |
auto tokens = SEAMS::tokenize(temp, delm); |
| 58 |
tokenized_strings[key] = std::move(tokens); |
| 59 |
} |
| 60 |
return tokenized_strings[key]; |
| 61 |
} |
| 62 |
|
| 63 |
std::random_device rd; |
| 64 |
std::mt19937_64 rng(rd()); |
| 65 |
|
| 66 |
void reset_error() |
| 67 |
{ |
| 68 |
#if !defined(WIN32) && !defined(__WIN32__) && !defined(_WIN32) && !defined(_MSC_VER) && \ |
| 69 |
!defined(__MINGW32__) && !defined(_WIN64) && !defined(__MINGW64__) |
| 70 |
#ifndef math_errhandling |
| 71 |
#define math_errhandling MATH_ERRNO |
| 72 |
#endif |
| 73 |
if (math_errhandling & MATH_ERREXCEPT) { |
| 74 |
std::feclearexcept(FE_ALL_EXCEPT); |
| 75 |
} |
| 76 |
if (math_errhandling & MATH_ERRNO) { |
| 77 |
errno = 0; |
| 78 |
} |
| 79 |
#endif |
| 80 |
} |
| 81 |
} // namespace |
| 82 |
|
| 83 |
namespace SEAMS { |
| 84 |
|
| 85 |
extern SEAMS::Aprepro *aprepro; |
| 86 |
|
| 87 |
#define d2r(x) ((x) * PI / 180.) |
| 88 |
#define r2d(x) ((x) * 180. / PI) |
| 89 |
|
| 90 |
#ifndef max |
| 91 |
#define max(x, y) (x) > (y) ? (x) : (y) |
| 92 |
#define min(x, y) (x) < (y) ? (x) : (y) |
| 93 |
#endif |
| 94 |
|
| 95 |
double do_time() |
| 96 |
{ |
| 97 |
time_t timer = time(nullptr); |
| 98 |
return timer; |
| 99 |
} |
| 100 |
|
| 101 |
double do_FtoC(double F) { return (F - 32.0) / 1.8; } |
| 102 |
|
| 103 |
double do_CtoF(double C) { return (C * 1.8) + 32.0; } |
| 104 |
|
| 105 |
// DO_INT: Calculate integer nearest to zero from value |
| 106 |
double do_int(double x) |
| 107 |
{ |
| 108 |
reset_error(); |
| 109 |
double temp = (x < 0 ? -std::floor(-(x)) : std::floor(x)); |
| 110 |
SEAMS::math_error("int"); |
| 111 |
return temp; |
| 112 |
} |
| 113 |
|
| 114 |
// DO_NINT: Calculate integer nearest value |
| 115 |
double do_nint(double x) |
| 116 |
{ |
| 117 |
reset_error(); |
| 118 |
double temp = (x < 0 ? -std::floor(0.5 - x) : std::floor(x + 0.5)); |
| 119 |
SEAMS::math_error("nint"); |
| 120 |
return temp; |
| 121 |
} |
| 122 |
|
| 123 |
// DO_DIST: Calculate distance between point 1 at (x1,y1) and |
| 124 |
// point 2 at (x2,y2). |
| 125 |
|
| 126 |
double do_dist(double x1, double y1, double x2, double y2) |
| 127 |
{ |
| 128 |
reset_error(); |
| 129 |
double temp = std::hypot((x1 - x2), (y1 - y2)); |
| 130 |
SEAMS::math_error("hypot"); |
| 131 |
return temp; |
| 132 |
} |
| 133 |
|
| 134 |
// DO_ANGLE: Calculate angle (radians) between vector 1 at (0,0; x1,y1) and |
| 135 |
// vector 2 at (0,0; x2,y2). |
| 136 |
|
| 137 |
double do_angle(double x1, double y1, double x2, double y2) |
| 138 |
{ |
| 139 |
double temp = ((x1 * x2) + (y1 * y2)) / (std::hypot(x1, y1) * std::hypot(x2, y2)); |
| 140 |
reset_error(); |
| 141 |
temp = acos(temp); |
| 142 |
SEAMS::math_error("angle"); |
| 143 |
return temp; |
| 144 |
} |
| 145 |
|
| 146 |
// DO_ANGLE: Calculate angle (degrees) between vector 1 at (0,0; x1,y1) and |
| 147 |
// vector 2 at (0,0; x2,y2). |
| 148 |
|
| 149 |
double do_angled(double x1, double y1, double x2, double y2) |
| 150 |
{ |
| 151 |
double temp = ((x1 * x2) + (y1 * y2)) / (std::hypot(x1, y1) * std::hypot(x2, y2)); |
| 152 |
reset_error(); |
| 153 |
temp = r2d(acos(temp)); |
| 154 |
SEAMS::math_error("angled"); |
| 155 |
return temp; |
| 156 |
} |
| 157 |
|
| 158 |
// DO_HYPOT: calculate sqrt(p^2 + q^2) |
| 159 |
double do_hypot(double x, double y) { return std::hypot(x, y); } |
| 160 |
|
| 161 |
double do_pow(double x, double y) |
| 162 |
{ |
| 163 |
// X^Y |
| 164 |
return std::pow(x, y); |
| 165 |
} |
| 166 |
|
| 167 |
double do_max(double x, double y) |
| 168 |
{ |
| 169 |
reset_error(); |
| 170 |
double temp = max(x, y); |
| 171 |
SEAMS::math_error("max"); |
| 172 |
return temp; |
| 173 |
} |
| 174 |
|
| 175 |
double do_min(double x, double y) |
| 176 |
{ |
| 177 |
reset_error(); |
| 178 |
double temp = min(x, y); |
| 179 |
SEAMS::math_error("min"); |
| 180 |
return temp; |
| 181 |
} |
| 182 |
|
| 183 |
double do_d2r(double x) { return (d2r(x)); } |
| 184 |
|
| 185 |
double do_r2d(double x) { return (r2d(x)); } |
| 186 |
|
| 187 |
double do_sind(double x) |
| 188 |
{ |
| 189 |
reset_error(); |
| 190 |
double temp = sin(d2r(x)); |
| 191 |
SEAMS::math_error("sind"); |
| 192 |
return temp; |
| 193 |
} |
| 194 |
|
| 195 |
double do_sin(double x) |
| 196 |
{ |
| 197 |
reset_error(); |
| 198 |
double temp = sin(x); |
| 199 |
SEAMS::math_error("sin"); |
| 200 |
return temp; |
| 201 |
} |
| 202 |
|
| 203 |
double do_cosd(double x) |
| 204 |
{ |
| 205 |
reset_error(); |
| 206 |
double temp = cos(d2r(x)); |
| 207 |
SEAMS::math_error("cosd"); |
| 208 |
return temp; |
| 209 |
} |
| 210 |
|
| 211 |
double do_cos(double x) |
| 212 |
{ |
| 213 |
reset_error(); |
| 214 |
double temp = cos(x); |
| 215 |
SEAMS::math_error("cos"); |
| 216 |
return temp; |
| 217 |
} |
| 218 |
|
| 219 |
double do_tand(double x) |
| 220 |
{ |
| 221 |
reset_error(); |
| 222 |
double temp = tan(d2r(x)); |
| 223 |
SEAMS::math_error("tand"); |
| 224 |
return temp; |
| 225 |
} |
| 226 |
|
| 227 |
double do_tan(double x) |
| 228 |
{ |
| 229 |
reset_error(); |
| 230 |
double temp = tan(x); |
| 231 |
SEAMS::math_error("tan"); |
| 232 |
return temp; |
| 233 |
} |
| 234 |
|
| 235 |
double do_atan2d(double x, double y) |
| 236 |
{ |
| 237 |
reset_error(); |
| 238 |
double temp = r2d(atan2(x, y)); |
| 239 |
SEAMS::math_error("atan2d"); |
| 240 |
return temp; |
| 241 |
} |
| 242 |
|
| 243 |
double do_atan2(double x, double y) |
| 244 |
{ |
| 245 |
reset_error(); |
| 246 |
double temp = atan2(x, y); |
| 247 |
SEAMS::math_error("atan2"); |
| 248 |
return temp; |
| 249 |
} |
| 250 |
|
| 251 |
double do_atand(double x) |
| 252 |
{ |
| 253 |
reset_error(); |
| 254 |
double temp = r2d(atan(x)); |
| 255 |
SEAMS::math_error("atand"); |
| 256 |
return temp; |
| 257 |
} |
| 258 |
|
| 259 |
double do_atan(double x) |
| 260 |
{ |
| 261 |
reset_error(); |
| 262 |
double temp = atan(x); |
| 263 |
SEAMS::math_error("atan"); |
| 264 |
return temp; |
| 265 |
} |
| 266 |
|
| 267 |
double do_asind(double x) |
| 268 |
{ |
| 269 |
reset_error(); |
| 270 |
double temp = r2d(asin(x)); |
| 271 |
SEAMS::math_error("asind"); |
| 272 |
return temp; |
| 273 |
} |
| 274 |
|
| 275 |
double do_asin(double x) |
| 276 |
{ |
| 277 |
reset_error(); |
| 278 |
double temp = asin(x); |
| 279 |
SEAMS::math_error("asin"); |
| 280 |
return temp; |
| 281 |
} |
| 282 |
|
| 283 |
double do_acosd(double x) |
| 284 |
{ |
| 285 |
reset_error(); |
| 286 |
double temp = r2d(acos(x)); |
| 287 |
SEAMS::math_error("acosd"); |
| 288 |
return temp; |
| 289 |
} |
| 290 |
|
| 291 |
double do_acos(double x) |
| 292 |
{ |
| 293 |
reset_error(); |
| 294 |
double temp = acos(x); |
| 295 |
SEAMS::math_error("acos"); |
| 296 |
return temp; |
| 297 |
} |
| 298 |
|
| 299 |
// do_srand(x) Seed the random generator with the specified integer value |
| 300 |
double do_srand(double seed) |
| 301 |
{ |
| 302 |
rng.seed(static_cast<size_t>(seed)); |
| 303 |
return (0); |
| 304 |
} |
| 305 |
|
| 306 |
// do_rand(x) returns a random double in the range 0<= do_rand <= x |
| 307 |
double do_rand(double xl, double xh) |
| 308 |
{ |
| 309 |
std::uniform_real_distribution<double> dist(xl, xh); |
| 310 |
return dist(rng); |
| 311 |
} |
| 312 |
|
| 313 |
double do_rand_normal(double mean, double stddev) |
| 314 |
{ |
| 315 |
std::normal_distribution<> dist(mean, stddev); |
| 316 |
return dist(rng); |
| 317 |
} |
| 318 |
|
| 319 |
double do_rand_lognormal(double mean, double stddev) |
| 320 |
{ |
| 321 |
std::lognormal_distribution<> dist(mean, stddev); |
| 322 |
return dist(rng); |
| 323 |
} |
| 324 |
|
| 325 |
double do_rand_weibull(double alpha, double beta) |
| 326 |
{ |
| 327 |
std::weibull_distribution<> dist(alpha, beta); |
| 328 |
return dist(rng); |
| 329 |
} |
| 330 |
|
| 331 |
double do_sign(double x, double y) |
| 332 |
{ |
| 333 |
reset_error(); |
| 334 |
double temp = (y) >= 0 ? fabs(x) : -fabs(x); |
| 335 |
SEAMS::math_error("sign"); |
| 336 |
return temp; |
| 337 |
} |
| 338 |
|
| 339 |
double do_dim(double x, double y) |
| 340 |
{ |
| 341 |
reset_error(); |
| 342 |
double temp = x - (min(x, y)); |
| 343 |
SEAMS::math_error("dim"); |
| 344 |
return temp; |
| 345 |
} |
| 346 |
|
| 347 |
double do_fabs(double x) |
| 348 |
{ |
| 349 |
reset_error(); |
| 350 |
double temp = fabs(x); |
| 351 |
SEAMS::math_error("fabs"); |
| 352 |
return temp; |
| 353 |
} |
| 354 |
|
| 355 |
double do_ceil(double x) |
| 356 |
{ |
| 357 |
reset_error(); |
| 358 |
double temp = ceil(x); |
| 359 |
SEAMS::math_error("ceil"); |
| 360 |
return temp; |
| 361 |
} |
| 362 |
|
| 363 |
double do_cosh(double x) |
| 364 |
{ |
| 365 |
reset_error(); |
| 366 |
double temp = cosh(x); |
| 367 |
SEAMS::math_error("cosh"); |
| 368 |
return temp; |
| 369 |
} |
| 370 |
|
| 371 |
double do_exp(double x) |
| 372 |
{ |
| 373 |
reset_error(); |
| 374 |
double temp = exp(x); |
| 375 |
// `exp` will report ERANGE error on both overflow and underflow |
| 376 |
// We want no error on underflow, so see if `temp == 0` and if so, |
| 377 |
// don't check math_error. |
| 378 |
if (temp != 0.0) { |
| 379 |
SEAMS::math_error("exp"); |
| 380 |
} |
| 381 |
else { |
| 382 |
reset_error(); |
| 383 |
} |
| 384 |
return temp; |
| 385 |
} |
| 386 |
|
| 387 |
double do_expm1(double x) |
| 388 |
{ |
| 389 |
reset_error(); |
| 390 |
double temp = std::expm1(x); |
| 391 |
SEAMS::math_error("exp"); |
| 392 |
return temp; |
| 393 |
} |
| 394 |
|
| 395 |
double do_erf(double x) { return std::erf(x); } |
| 396 |
|
| 397 |
double do_erfc(double x) { return std::erfc(x); } |
| 398 |
|
| 399 |
double do_floor(double x) |
| 400 |
{ |
| 401 |
reset_error(); |
| 402 |
double temp = floor(x); |
| 403 |
SEAMS::math_error("floor"); |
| 404 |
return temp; |
| 405 |
} |
| 406 |
|
| 407 |
double do_fmod(double x, double y) |
| 408 |
{ |
| 409 |
reset_error(); |
| 410 |
double temp = fmod(x, y); |
| 411 |
SEAMS::math_error("fmod"); |
| 412 |
return temp; |
| 413 |
} |
| 414 |
|
| 415 |
double do_log(double x) |
| 416 |
{ |
| 417 |
reset_error(); |
| 418 |
double temp = std::log(x); |
| 419 |
SEAMS::math_error("log"); |
| 420 |
return temp; |
| 421 |
} |
| 422 |
|
| 423 |
double do_log10(double x) |
| 424 |
{ |
| 425 |
reset_error(); |
| 426 |
double temp = std::log10(x); |
| 427 |
SEAMS::math_error("log10"); |
| 428 |
return temp; |
| 429 |
} |
| 430 |
|
| 431 |
double do_sinh(double x) |
| 432 |
{ |
| 433 |
reset_error(); |
| 434 |
double temp = sinh(x); |
| 435 |
SEAMS::math_error("sinh"); |
| 436 |
return temp; |
| 437 |
} |
| 438 |
|
| 439 |
double do_sqrt(double x) |
| 440 |
{ |
| 441 |
feclearexcept(FE_ALL_EXCEPT); |
| 442 |
reset_error(); |
| 443 |
double temp = std::sqrt(x); |
| 444 |
if (fetestexcept(FE_INVALID | FE_OVERFLOW | FE_DIVBYZERO) != 0) { |
| 445 |
SEAMS::math_error("sqrt"); |
| 446 |
} |
| 447 |
return temp; |
| 448 |
} |
| 449 |
|
| 450 |
double do_cbrt(double x) |
| 451 |
{ |
| 452 |
feclearexcept(FE_ALL_EXCEPT); |
| 453 |
reset_error(); |
| 454 |
double temp = std::cbrt(x); |
| 455 |
if (fetestexcept(FE_INVALID | FE_OVERFLOW | FE_DIVBYZERO) != 0) { |
| 456 |
SEAMS::math_error("sqrt"); |
| 457 |
} |
| 458 |
return temp; |
| 459 |
} |
| 460 |
|
| 461 |
double do_tanh(double x) |
| 462 |
{ |
| 463 |
reset_error(); |
| 464 |
double temp = tanh(x); |
| 465 |
SEAMS::math_error("tanh"); |
| 466 |
return temp; |
| 467 |
} |
| 468 |
|
| 469 |
double do_polarX(double rad, double ang) { return (rad * cos(d2r(ang))); } |
| 470 |
|
| 471 |
double do_polarY(double rad, double ang) { return (rad * sin(d2r(ang))); } |
| 472 |
|
| 473 |
double do_lgamma(double val) { return std::lgamma(val); } |
| 474 |
|
| 475 |
double do_tgamma(double val) { return std::tgamma(val); } |
| 476 |
|
| 477 |
double do_juldayhms(double mon, double day, double year, double h, double mi, double se) |
| 478 |
{ |
| 479 |
double seconds = h * 3600.0 + mi * 60 + se; |
| 480 |
|
| 481 |
auto m = static_cast<int64_t>(mon); |
| 482 |
auto d = static_cast<int64_t>(day); |
| 483 |
auto y = static_cast<int64_t>(year); |
| 484 |
|
| 485 |
if (m > 2) { |
| 486 |
m -= 3; |
| 487 |
} |
| 488 |
else { |
| 489 |
m += 9; |
| 490 |
--y; |
| 491 |
} |
| 492 |
int64_t c = y / 100L; |
| 493 |
int64_t ya = y - (100L * c); |
| 494 |
int64_t j = (146097L * c) / 4L + (1461L * ya) / 4L + (153L * m + 2L) / 5L + d + 1721119L; |
| 495 |
if (seconds < 12 * 3600.0) { |
| 496 |
j--; |
| 497 |
seconds += 12.0 * 3600.0; |
| 498 |
} |
| 499 |
else { |
| 500 |
seconds = seconds - 12.0 * 3600.0; |
| 501 |
} |
| 502 |
return (j + (seconds / 3600.0) / 24.0); |
| 503 |
} |
| 504 |
|
| 505 |
double do_julday(double mon, double day, double year) |
| 506 |
{ |
| 507 |
return do_juldayhms(mon, day, year, 0.0, 0.0, 0.0); |
| 508 |
} |
| 509 |
|
| 510 |
double do_log1p(double x) { return std::log1p(x); } |
| 511 |
|
| 512 |
double do_acosh(double x) { return std::acosh(x); } |
| 513 |
|
| 514 |
double do_asinh(double x) { return std::asinh(x); } |
| 515 |
|
| 516 |
double do_atanh(double x) { return std::atanh(x); } |
| 517 |
|
| 518 |
double do_rows(const array *arr) { return arr->rows; } |
| 519 |
|
| 520 |
double do_cols(const array *arr) { return arr->cols; } |
| 521 |
|
| 522 |
// --------------------------STRING FUNCTIONS------------------------ |
| 523 |
const char *do_version() |
| 524 |
{ |
| 525 |
char *tmp; |
| 526 |
new_string(SEAMS::Aprepro::version().c_str(), &tmp); |
| 527 |
return tmp; |
| 528 |
} |
| 529 |
|
| 530 |
const char *do_get_date() |
| 531 |
{ |
| 532 |
char *tmp; |
| 533 |
const size_t bufsize = 32; |
| 534 |
static char tmpstr[32]; |
| 535 |
|
| 536 |
time_t timer = time(nullptr); |
| 537 |
struct tm *timeptr = localtime(&timer); |
| 538 |
|
| 539 |
// First the date in the form CCYY/MM/DD |
| 540 |
strftime(tmpstr, bufsize, "%Y/%m/%d", timeptr); |
| 541 |
new_string(tmpstr, &tmp); |
| 542 |
return (tmp); |
| 543 |
} |
| 544 |
|
| 545 |
const char *do_get_iso_date() |
| 546 |
{ |
| 547 |
char *tmp; |
| 548 |
const size_t bufsize = 32; |
| 549 |
static char tmpstr[32]; |
| 550 |
|
| 551 |
time_t timer = time(nullptr); |
| 552 |
struct tm *timeptr = localtime(&timer); |
| 553 |
|
| 554 |
// First the date in the form CCYY/MM/DD |
| 555 |
strftime(tmpstr, bufsize, "%Y%m%d", timeptr); |
| 556 |
new_string(tmpstr, &tmp); |
| 557 |
return (tmp); |
| 558 |
} |
| 559 |
|
| 560 |
const char *do_get_time() |
| 561 |
{ |
| 562 |
char *tmp; |
| 563 |
const size_t bufsize = 32; |
| 564 |
static char tmpstr[32]; |
| 565 |
|
| 566 |
time_t timer = time(nullptr); |
| 567 |
struct tm *timeptr = localtime(&timer); |
| 568 |
|
| 569 |
// Now the time in the form HH:MM:SS where 0 <= HH < 24 |
| 570 |
strftime(tmpstr, bufsize, "%H:%M:%S", timeptr); |
| 571 |
new_string(tmpstr, &tmp); |
| 572 |
return (tmp); |
| 573 |
} |
| 574 |
|
| 575 |
const char *do_tolower(char *string) |
| 576 |
{ |
| 577 |
char *p = string; |
| 578 |
while (*p != '\0') { |
| 579 |
if (isupper(static_cast<int>(*p)) != 0) { |
| 580 |
*p = static_cast<char>(tolower(static_cast<int>(*p))); |
| 581 |
} |
| 582 |
p++; |
| 583 |
} |
| 584 |
return (string); |
| 585 |
} |
| 586 |
|
| 587 |
const char *do_toupper(char *string) |
| 588 |
{ |
| 589 |
char *p = string; |
| 590 |
while (*p != '\0') { |
| 591 |
if (islower(static_cast<int>(*p)) != 0) { |
| 592 |
*p = static_cast<char>(toupper(static_cast<int>(*p))); |
| 593 |
} |
| 594 |
p++; |
| 595 |
} |
| 596 |
return (string); |
| 597 |
} |
| 598 |
|
| 599 |
const char *do_tostring(double x) |
| 600 |
{ |
| 601 |
char *tmp; |
| 602 |
if (x == 0.0) { |
| 603 |
new_string("0", &tmp); |
| 604 |
return (tmp); |
| 605 |
} |
| 606 |
|
| 607 |
const SEAMS::symrec *format = aprepro->getsym("_FORMAT"); |
| 608 |
if (format->value.svar.empty()) { |
| 609 |
std::ostringstream lines; |
| 610 |
fmt::print(lines, "{}", x); |
| 611 |
new_string(lines.str(), &tmp); |
| 612 |
return tmp; |
| 613 |
} |
| 614 |
else { |
| 615 |
auto tmpstr = fmt::sprintf(format->value.svar, x); |
| 616 |
new_string(tmpstr.c_str(), &tmp); |
| 617 |
return tmp; |
| 618 |
} |
| 619 |
} |
| 620 |
|
| 621 |
const char *do_output(char *filename) |
| 622 |
{ |
| 623 |
aprepro->outputStream.top()->flush(); |
| 624 |
|
| 625 |
if (std::strcmp(filename, "pop") == 0 || std::strcmp(filename, "stdout") == 0) { |
| 626 |
while (aprepro->outputStream.size() > 1) { |
| 627 |
std::ostream *output = aprepro->outputStream.top(); |
| 628 |
aprepro->outputStream.pop(); |
| 629 |
delete output; |
| 630 |
} |
| 631 |
|
| 632 |
aprepro->info("Output now redirected to original output stream.\n"); |
| 633 |
} |
| 634 |
else { |
| 635 |
std::ostream *output = new std::ofstream(filename); |
| 636 |
aprepro->outputStream.push(output); |
| 637 |
aprepro->info("Output now redirected to file'" + std::string(filename) + "'.\n"); |
| 638 |
} |
| 639 |
return (nullptr); |
| 640 |
} |
| 641 |
|
| 642 |
const char *do_append(char *filename) |
| 643 |
{ |
| 644 |
aprepro->outputStream.top()->flush(); |
| 645 |
|
| 646 |
if (std::strcmp(filename, "pop") == 0 || std::strcmp(filename, "stdout") == 0) { |
| 647 |
while (aprepro->outputStream.size() > 1) { |
| 648 |
std::ostream *output = aprepro->outputStream.top(); |
| 649 |
aprepro->outputStream.pop(); |
| 650 |
delete output; |
| 651 |
} |
| 652 |
|
| 653 |
aprepro->info("Output now redirected to original output stream.\n"); |
| 654 |
} |
| 655 |
else { |
| 656 |
auto output = new std::ofstream(filename, std::ios_base::app); // Append |
| 657 |
aprepro->outputStream.push(output); |
| 658 |
|
| 659 |
aprepro->info("Output now redirected to file '" + std::string(filename) + "'\n"); |
| 660 |
} |
| 661 |
return (nullptr); |
| 662 |
} |
| 663 |
|
| 664 |
double do_word_count(char *string, char *delm) |
| 665 |
{ |
| 666 |
return static_cast<double>(get_tokenized_strings(string, delm).size()); |
| 667 |
} |
| 668 |
|
| 669 |
double do_find_word(char *word, char *string, char *delm) |
| 670 |
{ |
| 671 |
const auto &tokens = get_tokenized_strings(string, delm); |
| 672 |
std::string sword{word}; |
| 673 |
for (auto [i, token] : enumerate(tokens)) { |
| 674 |
if (token == sword) { |
| 675 |
return i + 1; |
| 676 |
} |
| 677 |
} |
| 678 |
return 0; |
| 679 |
} |
| 680 |
|
| 681 |
const char *do_get_word(double n, char *string, char *delm) |
| 682 |
{ |
| 683 |
auto &tokens = get_tokenized_strings(string, delm); |
| 684 |
|
| 685 |
auto in = static_cast<size_t>(n); |
| 686 |
if (in == 0) { |
| 687 |
aprepro->error("Index to get_word must be positive.", false); |
| 688 |
return ""; |
| 689 |
} |
| 690 |
if (tokens.size() >= in) { |
| 691 |
char *word = nullptr; |
| 692 |
new_string(tokens[in - 1], &word); |
| 693 |
return word; |
| 694 |
} |
| 695 |
return ""; |
| 696 |
} |
| 697 |
|
| 698 |
const char *do_file_to_string(char *filename) |
| 699 |
{ |
| 700 |
char *ret_string = nullptr; |
| 701 |
std::fstream *file = aprepro->open_file(filename, "r"); |
| 702 |
|
| 703 |
if (file != nullptr) { |
| 704 |
std::ostringstream lines; |
| 705 |
std::string line; |
| 706 |
while (std::getline(*file, line)) { |
| 707 |
lines << line << '\n'; |
| 708 |
} |
| 709 |
|
| 710 |
new_string(lines.str(), &ret_string); |
| 711 |
} |
| 712 |
return ret_string; |
| 713 |
} |
| 714 |
|
| 715 |
const char *do_getenv(char *env) |
| 716 |
{ |
| 717 |
if (env == nullptr) { |
| 718 |
return ""; |
| 719 |
} |
| 720 |
char *tmp = getenv(env); |
| 721 |
if (tmp != nullptr) { |
| 722 |
char *ret_string; |
| 723 |
new_string(tmp, &ret_string); |
| 724 |
return (ret_string); |
| 725 |
} |
| 726 |
|
| 727 |
return ""; |
| 728 |
} |
| 729 |
|
| 730 |
double do_strtod(char *string) |
| 731 |
{ |
| 732 |
reset_error(); |
| 733 |
double x = strtod(string, nullptr); |
| 734 |
SEAMS::math_error("strtod"); |
| 735 |
return x; |
| 736 |
} |
| 737 |
|
| 738 |
const char *do_dumpsym() |
| 739 |
{ |
| 740 |
aprepro->dumpsym(SEAMS::Parser::token::VAR, false); |
| 741 |
return (nullptr); |
| 742 |
} |
| 743 |
|
| 744 |
const char *do_dumpsym_json() |
| 745 |
{ |
| 746 |
aprepro->dumpsym_json(); |
| 747 |
return (nullptr); |
| 748 |
} |
| 749 |
|
| 750 |
const char *do_dumpfunc() |
| 751 |
{ |
| 752 |
aprepro->dumpsym(SEAMS::Parser::token::FNCT, true); |
| 753 |
return (nullptr); |
| 754 |
} |
| 755 |
|
| 756 |
const char *do_dumpvar() |
| 757 |
{ |
| 758 |
aprepro->dumpsym(SEAMS::Parser::token::VAR, true); |
| 759 |
return (nullptr); |
| 760 |
} |
| 761 |
|
| 762 |
const char *do_dumpsym1(char *pre) |
| 763 |
{ |
| 764 |
aprepro->dumpsym(SEAMS::Parser::token::VAR, pre, false); |
| 765 |
return (nullptr); |
| 766 |
} |
| 767 |
|
| 768 |
const char *do_dumpfunc1(char *pre) |
| 769 |
{ |
| 770 |
aprepro->dumpsym(SEAMS::Parser::token::FNCT, pre, true); |
| 771 |
return (nullptr); |
| 772 |
} |
| 773 |
|
| 774 |
const char *do_dumpvar1(char *pre) |
| 775 |
{ |
| 776 |
aprepro->dumpsym(SEAMS::Parser::token::VAR, pre, true); |
| 777 |
return (nullptr); |
| 778 |
} |
| 779 |
|
| 780 |
double do_option(char *option, double value) |
| 781 |
{ |
| 782 |
double current = -1; |
| 783 |
|
| 784 |
if (std::strcmp(option, "warning") == 0) { |
| 785 |
current = static_cast<double>(aprepro->ap_options.warning_msg); |
| 786 |
aprepro->ap_options.warning_msg = value != 0.0; |
| 787 |
} |
| 788 |
|
| 789 |
else if (std::strcmp(option, "info") == 0) { |
| 790 |
current = static_cast<double>(aprepro->ap_options.info_msg); |
| 791 |
aprepro->ap_options.info_msg = value != 0.0; |
| 792 |
} |
| 793 |
|
| 794 |
else if (std::strcmp(option, "debugging") == 0) { |
| 795 |
current = static_cast<double>(aprepro->ap_options.debugging); |
| 796 |
aprepro->ap_options.debugging = value != 0.0; |
| 797 |
} |
| 798 |
|
| 799 |
else if (std::strcmp(option, "statistics") == 0) { |
| 800 |
aprepro->statistics(); |
| 801 |
} |
| 802 |
|
| 803 |
else { |
| 804 |
aprepro->error( |
| 805 |
"Valid arguments to option are: 'warning', 'info', 'debugging', and 'statistics'\n", |
| 806 |
false); |
| 807 |
} |
| 808 |
return current; |
| 809 |
} |
| 810 |
|
| 811 |
const char *do_include_path(char *new_path) |
| 812 |
{ |
| 813 |
aprepro->ap_options.include_path = new_path; |
| 814 |
return (nullptr); |
| 815 |
} |
| 816 |
|
| 817 |
const char *do_intout(double intval) |
| 818 |
{ |
| 819 |
// convert 'intval' to a string using an integer format |
| 820 |
// This can be used if you need to set the default output |
| 821 |
// format to force the decimal point. In that case, integers |
| 822 |
// also have a decimal point which is usually not wanted. |
| 823 |
// Using 'intout(val)', val will be converted to a string |
| 824 |
// using an integer format |
| 825 |
|
| 826 |
char *tmp; |
| 827 |
if (intval == 0.0) { |
| 828 |
new_string("0", &tmp); |
| 829 |
return (tmp); |
| 830 |
} |
| 831 |
|
| 832 |
std::string tmpstr = std::to_string(static_cast<int>(intval)); |
| 833 |
new_string(tmpstr.c_str(), &tmp); |
| 834 |
return (tmp); |
| 835 |
} |
| 836 |
|
| 837 |
const char *do_format(double var, char *format) |
| 838 |
{ |
| 839 |
auto tmpstr = std::strlen(format) > 0 ? fmt::sprintf(format, var) : fmt::format("{}", var); |
| 840 |
char *tmp; |
| 841 |
new_string(tmpstr.c_str(), &tmp); |
| 842 |
return tmp; |
| 843 |
} |
| 844 |
|
| 845 |
const char *do_execute(char *string) |
| 846 |
{ |
| 847 |
aprepro->lexer->execute(string); |
| 848 |
return nullptr; |
| 849 |
} |
| 850 |
|
| 851 |
const char *do_rescan(char *string) |
| 852 |
{ |
| 853 |
aprepro->lexer->rescan(string); |
| 854 |
return nullptr; |
| 855 |
} |
| 856 |
|
| 857 |
const char *do_import(char *string) |
| 858 |
{ |
| 859 |
aprepro->lexer->import_handler(string); |
| 860 |
return nullptr; |
| 861 |
} |
| 862 |
|
| 863 |
const char *do_if(double x) |
| 864 |
{ |
| 865 |
aprepro->inIfdefGetvar = false; |
| 866 |
aprepro->lexer->if_handler((x != 0.0)); |
| 867 |
return nullptr; |
| 868 |
} |
| 869 |
|
| 870 |
const char *do_notif(double x) |
| 871 |
{ |
| 872 |
aprepro->lexer->if_handler(x == 0.0); |
| 873 |
return nullptr; |
| 874 |
} |
| 875 |
|
| 876 |
const char *do_elseif(double x) |
| 877 |
{ |
| 878 |
aprepro->lexer->elseif_handler(x); |
| 879 |
return nullptr; |
| 880 |
} |
| 881 |
|
| 882 |
const char *do_str_if(char *string) |
| 883 |
{ |
| 884 |
std::string test(string); |
| 885 |
aprepro->lexer->if_handler(static_cast<double>(!test.empty())); |
| 886 |
|
| 887 |
return nullptr; |
| 888 |
} |
| 889 |
|
| 890 |
const char *do_str_notif(char *string) |
| 891 |
{ |
| 892 |
std::string test(string); |
| 893 |
aprepro->lexer->if_handler(static_cast<double>(test.empty())); |
| 894 |
|
| 895 |
return nullptr; |
| 896 |
} |
| 897 |
|
| 898 |
const char *do_str_elseif(char *string) |
| 899 |
{ |
| 900 |
std::string test(string); |
| 901 |
aprepro->lexer->elseif_handler(static_cast<double>(!test.empty())); |
| 902 |
|
| 903 |
return nullptr; |
| 904 |
} |
| 905 |
|
| 906 |
const char *do_switch(double x) |
| 907 |
{ |
| 908 |
aprepro->lexer->switch_handler(x); |
| 909 |
return nullptr; |
| 910 |
} |
| 911 |
|
| 912 |
const char *do_case(double x) |
| 913 |
{ |
| 914 |
aprepro->lexer->case_handler(x); |
| 915 |
return nullptr; |
| 916 |
} |
| 917 |
|
| 918 |
const char *do_extract(char *string, char *begin, char *end) |
| 919 |
{ |
| 920 |
// From 'string' return a substring delimited by 'begin' and 'end'. |
| 921 |
// 'begin' is included in the string, but 'end' is not. If |
| 922 |
// 'begin' does not appear in the string, return nullptr; If 'end' |
| 923 |
// does not appear, then return the remainder of the string. If |
| 924 |
// 'begin' == "", then start at beginning; if 'end' == "", then |
| 925 |
// return remainder of the string. |
| 926 |
auto *start = string; |
| 927 |
|
| 928 |
if (std::strlen(begin) > 0) { |
| 929 |
start = std::strstr(string, begin); |
| 930 |
if (start == nullptr) { |
| 931 |
return ""; |
| 932 |
} |
| 933 |
} |
| 934 |
|
| 935 |
auto len = std::strlen(start); |
| 936 |
if (std::strlen(end) > 0) { |
| 937 |
auto *finish = std::strstr(start, end); |
| 938 |
if (finish != nullptr) { |
| 939 |
len = static_cast<size_t>(finish - start); |
| 940 |
} |
| 941 |
} |
| 942 |
|
| 943 |
std::string tmpstr(start, 0, len); |
| 944 |
char *tmp; |
| 945 |
new_string(tmpstr, &tmp); |
| 946 |
return tmp; |
| 947 |
} |
| 948 |
|
| 949 |
const char *do_get_temp_filename() |
| 950 |
{ |
| 951 |
char *filename; |
| 952 |
new_string(get_temp_filename(), &filename); |
| 953 |
return filename; |
| 954 |
} |
| 955 |
|
| 956 |
const char *do_error(char *error_string) |
| 957 |
{ |
| 958 |
// Print error message (to stderr) and exit |
| 959 |
yyerror(*aprepro, error_string); |
| 960 |
throw std::runtime_error(std::string(error_string)); |
| 961 |
// NOTREACHED |
| 962 |
return (nullptr); |
| 963 |
} |
| 964 |
|
| 965 |
const char *do_print_array(const array *my_array_data) |
| 966 |
{ |
| 967 |
if (my_array_data != nullptr) { |
| 968 |
std::ostringstream lines; |
| 969 |
|
| 970 |
auto rows = my_array_data->rows; |
| 971 |
auto cols = my_array_data->cols; |
| 972 |
|
| 973 |
size_t idx = 0; |
| 974 |
|
| 975 |
for (size_t ir = 0; ir < rows; ir++) { |
| 976 |
if (ir > 0) { |
| 977 |
lines << "\n"; |
| 978 |
} |
| 979 |
lines << "\t"; |
| 980 |
for (size_t ic = 0; ic < cols; ic++) { |
| 981 |
const SEAMS::symrec *format = aprepro->getsym("_FORMAT"); |
| 982 |
if (format->value.svar.empty()) { |
| 983 |
fmt::print(lines, "{}", my_array_data->data[idx++]); |
| 984 |
} |
| 985 |
else { |
| 986 |
auto tmpstr = fmt::sprintf(format->value.svar, my_array_data->data[idx++]); |
| 987 |
lines << tmpstr; |
| 988 |
} |
| 989 |
if (ic < cols - 1) { |
| 990 |
lines << "\t"; |
| 991 |
} |
| 992 |
} |
| 993 |
} |
| 994 |
char *ret_string; |
| 995 |
new_string(lines.str(), &ret_string); |
| 996 |
return ret_string; |
| 997 |
} |
| 998 |
|
| 999 |
return ""; |
| 1000 |
} |
| 1001 |
|
| 1002 |
const char *do_delete(char *string) |
| 1003 |
{ |
| 1004 |
aprepro->remove_variable(string); |
| 1005 |
|
| 1006 |
return nullptr; |
| 1007 |
} |
| 1008 |
|
| 1009 |
array *do_make_array(double rows, double cols) |
| 1010 |
{ |
| 1011 |
auto array_data = aprepro->make_array(static_cast<size_t>(rows), static_cast<size_t>(cols)); |
| 1012 |
return array_data; |
| 1013 |
} |
| 1014 |
|
| 1015 |
array *do_make_array_init(double rows, double cols, double init) |
| 1016 |
{ |
| 1017 |
auto array_data = aprepro->make_array(static_cast<size_t>(rows), static_cast<size_t>(cols)); |
| 1018 |
auto isize = static_cast<size_t>(rows) * static_cast<size_t>(cols); |
| 1019 |
for (size_t i = 0; i < isize; i++) { |
| 1020 |
array_data->data[i] = init; |
| 1021 |
} |
| 1022 |
return array_data; |
| 1023 |
} |
| 1024 |
|
| 1025 |
array *do_identity(double size) |
| 1026 |
{ |
| 1027 |
auto array_data = aprepro->make_array(static_cast<size_t>(size), static_cast<size_t>(size)); |
| 1028 |
|
| 1029 |
size_t isize = static_cast<size_t>(size); |
| 1030 |
for (size_t i = 0; i < isize; i++) { |
| 1031 |
array_data->data[i * isize + i] = 1.0; |
| 1032 |
} |
| 1033 |
return array_data; |
| 1034 |
} |
| 1035 |
|
| 1036 |
array *do_linear_array(double init, double final, double count) |
| 1037 |
{ |
| 1038 |
// Create 1D array with `count` rows and 1 column. |
| 1039 |
// Values are linearly spaced from `init` to `final` |
| 1040 |
auto isize = static_cast<size_t>(count); |
| 1041 |
auto array_data = aprepro->make_array(isize, 1); |
| 1042 |
|
| 1043 |
double inc = (final - init) / (count - 1); |
| 1044 |
for (size_t i = 0; i < isize; i++) { |
| 1045 |
array_data->data[i] = init + (double)i * inc; |
| 1046 |
} |
| 1047 |
return array_data; |
| 1048 |
} |
| 1049 |
|
| 1050 |
array *do_transpose(const array *a) |
| 1051 |
{ |
| 1052 |
auto array_data = aprepro->make_array(a->cols, a->rows); |
| 1053 |
|
| 1054 |
for (size_t i = 0; i < a->rows; i++) { |
| 1055 |
for (size_t j = 0; j < a->cols; j++) { |
| 1056 |
array_data->data[j * a->rows + i] = a->data[i * a->cols + j]; |
| 1057 |
} |
| 1058 |
} |
| 1059 |
return array_data; |
| 1060 |
} |
| 1061 |
|
| 1062 |
array *do_principal(const array *a) |
| 1063 |
{ |
| 1064 |
// Good calculator and a version of malvern's method at: |
| 1065 |
// https://www.continuummechanics.org/principalstress.html |
| 1066 |
|
| 1067 |
auto array_data = aprepro->make_array(3, 1); |
| 1068 |
|
| 1069 |
if (a->rows != 3 || a->cols != 3) { |
| 1070 |
aprepro->error("Invalid array size. Must be 3x3 for principal values calculation.\n", false); |
| 1071 |
return array_data; |
| 1072 |
} |
| 1073 |
if (a->data[1] != a->data[3] || a->data[2] != a->data[6] || a->data[5] != a->data[7]) { |
| 1074 |
aprepro->error("Array is not symmetric in principal values calculation.\n", false); |
| 1075 |
return array_data; |
| 1076 |
} |
| 1077 |
|
| 1078 |
const double third = 1.0 / 3.0; |
| 1079 |
const double sqrt3 = sqrt(3.0); |
| 1080 |
|
| 1081 |
// Find principal trial stresses and directions - |
| 1082 |
// [ 0 1 2 ] [ sk1 sk4 sk6 ] |
| 1083 |
// [ 3 4 5 ] [ sk4 sk2 sk5 ] |
| 1084 |
// [ 6 7 8 ] [ sk6 sk5 sk3 ] |
| 1085 |
|
| 1086 |
const double sk1 = a->data[0]; |
| 1087 |
const double sk2 = a->data[4]; |
| 1088 |
const double sk3 = a->data[8]; |
| 1089 |
const double sk4 = a->data[1]; |
| 1090 |
const double sk5 = a->data[5]; |
| 1091 |
const double sk6 = a->data[2]; |
| 1092 |
|
| 1093 |
double dsk12 = sk1 - sk2; |
| 1094 |
double dsk13 = sk1 - sk3; |
| 1095 |
double dsk23 = sk2 - sk3; |
| 1096 |
|
| 1097 |
double i1 = (sk1 + sk2 + sk3); |
| 1098 |
double i2 = |
| 1099 |
(dsk12 * dsk12 + dsk13 * dsk13 + dsk23 * dsk23) / 6.0 + sk4 * sk4 + sk5 * sk5 + sk6 * sk6; |
| 1100 |
|
| 1101 |
double s1 = (dsk12 + dsk13) * third; |
| 1102 |
double s2 = (-dsk12 + dsk23) * third; |
| 1103 |
double s3 = (-dsk13 - dsk23) * third; |
| 1104 |
|
| 1105 |
double i3 = s1 * s2 * s3 + (2. * sk4 * sk5 * sk6) - (s1 * sk5 * sk5) - (s2 * sk6 * sk6) - |
| 1106 |
(s3 * sk4 * sk4); |
| 1107 |
|
| 1108 |
// calculate constants for malvern method (p92) |
| 1109 |
double fi2 = (i2 == 0.0) ? 1.0 : i2; |
| 1110 |
|
| 1111 |
double cos3al = sqrt3 * 1.5 * i3 / fi2 / sqrt(fi2); |
| 1112 |
// cos3al = sign( min( 1.0, abs(cos3al) ),cos3al ); |
| 1113 |
|
| 1114 |
double calpha = cos(acos(cos3al) / 3.0); |
| 1115 |
double salpha = sqrt(1.0 - calpha * calpha); |
| 1116 |
|
| 1117 |
double t = sqrt3 * sqrt(i2); |
| 1118 |
double p1 = (i1 + t * 2. * calpha) * third; |
| 1119 |
double p2 = (i1 - t * (calpha + salpha * sqrt3)) * third; |
| 1120 |
double p3 = (i1 - t * (calpha - salpha * sqrt3)) * third; |
| 1121 |
|
| 1122 |
array_data->data[0] = p1; |
| 1123 |
array_data->data[1] = p2; |
| 1124 |
array_data->data[2] = p3; |
| 1125 |
|
| 1126 |
// ... Sort Into Correct Position (p1 > p2 > p3) |
| 1127 |
std::sort(array_data->data.begin(), array_data->data.end(), std::greater<double>()); |
| 1128 |
|
| 1129 |
return array_data; |
| 1130 |
} |
| 1131 |
|
| 1132 |
array *do_csv_array1(const char *filename) { return do_csv_array(filename, 0.0); } |
| 1133 |
|
| 1134 |
array *do_csv_array(const char *filename, double skip) |
| 1135 |
{ |
| 1136 |
auto rows_to_skip = static_cast<size_t>(skip); |
| 1137 |
|
| 1138 |
std::fstream *file = aprepro->open_file(filename, "r"); |
| 1139 |
if (file != nullptr) { |
| 1140 |
|
| 1141 |
size_t rows = 0; |
| 1142 |
size_t cols = 0; |
| 1143 |
|
| 1144 |
const char *delim = ",\t "; |
| 1145 |
std::string line; |
| 1146 |
while (std::getline(*file, line)) { |
| 1147 |
rows++; |
| 1148 |
if (rows > rows_to_skip) { |
| 1149 |
auto tokens = tokenize(line, delim); |
| 1150 |
cols = tokens.size() > cols ? tokens.size() : cols; |
| 1151 |
} |
| 1152 |
} |
| 1153 |
|
| 1154 |
auto array_data = aprepro->make_array(rows - rows_to_skip, cols); |
| 1155 |
|
| 1156 |
// Read file again storing entries in array_data->data |
| 1157 |
file->clear(); |
| 1158 |
file->seekg(0); |
| 1159 |
|
| 1160 |
size_t idx = 0; |
| 1161 |
rows = 0; |
| 1162 |
while (std::getline(*file, line)) { |
| 1163 |
if (++rows > rows_to_skip) { |
| 1164 |
auto tokens = tokenize(line, delim); |
| 1165 |
for (size_t i = 0; i < static_cast<size_t>(array_data->cols); i++) { |
| 1166 |
if (i < tokens.size()) { |
| 1167 |
array_data->data[idx++] = std::stod(tokens[i]); |
| 1168 |
} |
| 1169 |
else { |
| 1170 |
array_data->data[idx++] = 0.0; |
| 1171 |
} |
| 1172 |
} |
| 1173 |
} |
| 1174 |
} |
| 1175 |
assert(rows - rows_to_skip == (size_t)array_data->rows); |
| 1176 |
delete file; |
| 1177 |
return array_data; |
| 1178 |
} |
| 1179 |
|
| 1180 |
return nullptr; |
| 1181 |
} |
| 1182 |
|
| 1183 |
array *do_csv_array2(const char *filename, const char *comment) |
| 1184 |
{ |
| 1185 |
std::fstream *file = aprepro->open_file(filename, "r"); |
| 1186 |
if (file != nullptr) { |
| 1187 |
|
| 1188 |
size_t rows = 0; |
| 1189 |
size_t cols = 0; |
| 1190 |
const char *delim = ",\t "; |
| 1191 |
|
| 1192 |
std::string line; |
| 1193 |
while (std::getline(*file, line)) { |
| 1194 |
if (line[0] != comment[0]) { |
| 1195 |
rows++; |
| 1196 |
auto tokens = tokenize(line, delim); |
| 1197 |
cols = tokens.size() > cols ? tokens.size() : cols; |
| 1198 |
} |
| 1199 |
} |
| 1200 |
|
| 1201 |
auto array_data = aprepro->make_array(static_cast<size_t>(rows), static_cast<size_t>(cols)); |
| 1202 |
|
| 1203 |
// Read file again storing entries in array_data->data |
| 1204 |
file->clear(); |
| 1205 |
file->seekg(0); |
| 1206 |
|
| 1207 |
size_t idx = 0; |
| 1208 |
rows = 0; |
| 1209 |
while (std::getline(*file, line)) { |
| 1210 |
if (line[0] != comment[0]) { |
| 1211 |
rows++; |
| 1212 |
auto tokens = tokenize(line, delim); |
| 1213 |
for (size_t i = 0; i < static_cast<size_t>(array_data->cols); i++) { |
| 1214 |
if (i < tokens.size()) { |
| 1215 |
array_data->data[idx++] = std::stod(tokens[i]); |
| 1216 |
} |
| 1217 |
else { |
| 1218 |
array_data->data[idx++] = 0.0; |
| 1219 |
} |
| 1220 |
} |
| 1221 |
} |
| 1222 |
} |
| 1223 |
assert(static_cast<size_t>(rows) == array_data->rows); |
| 1224 |
delete file; |
| 1225 |
return array_data; |
| 1226 |
} |
| 1227 |
return nullptr; |
| 1228 |
} |
| 1229 |
|
| 1230 |
array *do_array_from_string(const char *string, const char *delm) |
| 1231 |
{ |
| 1232 |
auto tokens = SEAMS::tokenize(string, delm); |
| 1233 |
auto array_data = aprepro->make_array(tokens.size(), 1); |
| 1234 |
|
| 1235 |
size_t idx = 0; |
| 1236 |
for (const auto &token : tokens) { |
| 1237 |
array_data->data[idx++] = std::stod(token); |
| 1238 |
} |
| 1239 |
assert(idx == array_data->rows); |
| 1240 |
return array_data; |
| 1241 |
} |
| 1242 |
|
| 1243 |
array *do_sym_tensor_from_string(const char *string, const char *delm) |
| 1244 |
{ |
| 1245 |
auto array_data = aprepro->make_array(3, 3); |
| 1246 |
auto tokens = SEAMS::tokenize(string, delm); |
| 1247 |
if (tokens.size() != 6) { |
| 1248 |
aprepro->error("Incorrect number of values found in sym_tensor_from_string function. Must " |
| 1249 |
"be 6: xx, yy, zz, xy, yz, xz.\n", |
| 1250 |
false); |
| 1251 |
return array_data; |
| 1252 |
} |
| 1253 |
|
| 1254 |
array_data->data[0] = std::stod(tokens[0]); |
| 1255 |
array_data->data[4] = std::stod(tokens[1]); |
| 1256 |
array_data->data[8] = std::stod(tokens[2]); |
| 1257 |
array_data->data[1] = std::stod(tokens[3]); |
| 1258 |
array_data->data[5] = std::stod(tokens[4]); |
| 1259 |
array_data->data[2] = std::stod(tokens[5]); |
| 1260 |
array_data->data[3] = array_data->data[1]; |
| 1261 |
array_data->data[7] = array_data->data[5]; |
| 1262 |
array_data->data[6] = array_data->data[2]; |
| 1263 |
|
| 1264 |
return array_data; |
| 1265 |
} |
| 1266 |
} // namespace SEAMS |