Quandary
Loading...
Searching...
No Matches
util.hpp
Go to the documentation of this file.
1#include <petscmat.h>
2
3#include <cctype>
4#include <cstring>
5#include <map>
6#include <optional>
7#include <string>
8#include <fstream>
9#include <iostream>
10#include <vector>
11#include "version.hpp"
12#ifdef WITH_SLEPC
13#include <slepceps.h>
14#endif
15
16#pragma once
17
21struct ParsedArgs {
22 bool quietmode = false;
23 std::string config_filename;
24 int petsc_argc = 0;
25 std::vector<std::string> petsc_tokens;
26 std::vector<char*> petsc_argv;
27};
28
32void printHelp();
33
41ParsedArgs parseArguments(int argc, char** argv);
42
50double sigmoid(double width, double x);
51
59double sigmoid_diff(double width, double x);
60
73double getRampFactor(const double time, const double tstart, const double tstop, const double tramp);
74
84double getRampFactor_diff(const double time, const double tstart, const double tstop, const double tramp);
85
94PetscInt getVecID(const PetscInt row, const PetscInt col, const PetscInt dim);
95
104PetscInt mapEssToFull(const PetscInt i, const std::vector<size_t> &nlevels, const std::vector<size_t> &nessential);
105
114PetscInt mapFullToEss(const PetscInt i, const std::vector<size_t> &nlevels, const std::vector<size_t> &nessential);
115
124int isEssential(const int i, const std::vector<size_t> &nlevels, const std::vector<size_t> &nessential);
125
137int isGuardLevel(const int i, const std::vector<size_t> &nlevels, const std::vector<size_t> &nessential);
138
152PetscErrorCode Ikron(const Mat A, const int dimI, const double alpha, Mat *Out, InsertMode insert_mode);
153
167PetscErrorCode kronI(const Mat A, const int dimI, const double alpha, Mat *Out, InsertMode insert_mode);
168
183PetscErrorCode AkronB(const Mat A, const Mat B, const double alpha, Mat *Out, InsertMode insert_mode);
184
193PetscErrorCode MatIsAntiSymmetric(Mat A, PetscReal tol, PetscBool *flag);
194
206PetscErrorCode StateIsHermitian(Vec x, PetscReal tol, PetscBool *flag);
207
216PetscErrorCode StateHasTrace1(Vec x, PetscReal tol, PetscBool *flag);
217
225PetscErrorCode SanityTests(Vec x, PetscReal time);
226
238int read_vector(const char *filename, double *var, int dim, bool quietmode=false, int skiplines=0, const std::string testheader="");
239
251int getEigvals(const Mat A, const int neigvals, std::vector<double>& eigvals, std::vector<Vec>& eigvecs);
252
262bool isUnitary(const Mat A, const Mat B);
263
273template <typename Tval>
274void copyLast(std::vector<Tval>& fillme, int tosize){
275 // int norg = fillme.size();
276
277 for (int i=fillme.size(); i<tosize; i++)
278 fillme.push_back(fillme[fillme.size()-1]);
279
280 // if (norg < tosize) {
281 // std::cout<< "I filled this: ";
282 // for (int i=0; i<fillme.size(); i++) std::cout<< " " << fillme[i];
283 // std::cout<<std::endl;
284 // }
285};
286
287
294std::string toLower(std::string str);
295
303bool hasSuffix(const std::string& str, const std::string& suffix);
304
305
313template<typename T>
314std::optional<T> parseEnum(const std::string& str, const std::map<std::string, T>& enum_map) {
315 auto it = enum_map.find(toLower(str));
316 if (it != enum_map.end()) {
317 return it->second;
318 } else {
319 return std::nullopt;
320 }
321}
322
330template <typename EnumType>
331std::string enumToString(EnumType value, const std::map<std::string, EnumType>& type_map) {
332 for (const auto& [str, enum_val] : type_map) {
333 if (enum_val == value) return str;
334 }
335 return "unknown";
336}
337
346template<typename T>
347T parseEnum(const std::optional<std::string>& opt_str, const std::map<std::string, T>& enum_map, const T& default_value) {
348 if (!opt_str.has_value()) {
349 return default_value;
350 }
351 auto result = parseEnum(opt_str.value(), enum_map);
352 return result.value_or(default_value);
353}
Structure for parsed command-line arguments.
Definition util.hpp:21
std::string config_filename
Configuration filename.
Definition util.hpp:23
int petsc_argc
PETSc argument count.
Definition util.hpp:24
bool quietmode
Flag for quiet mode (reduced output)
Definition util.hpp:22
std::vector< std::string > petsc_tokens
PETSc option tokens.
Definition util.hpp:25
std::vector< char * > petsc_argv
PETSc argument vector.
Definition util.hpp:26
PetscInt mapFullToEss(const PetscInt i, const std::vector< size_t > &nlevels, const std::vector< size_t > &nessential)
Maps index from full dimension to essential dimension system.
Definition util.cpp:179
int getEigvals(const Mat A, const int neigvals, std::vector< double > &eigvals, std::vector< Vec > &eigvecs)
Computes eigenvalues and eigenvectors of matrix A.
Definition util.cpp:635
void printHelp()
Definition util.cpp:9
double getRampFactor(const double time, const double tstart, const double tstop, const double tramp)
Computes ramping factor for control pulse shaping.
Definition util.cpp:94
double sigmoid_diff(double width, double x)
Derivative of sigmoid function.
Definition util.cpp:90
PetscInt getVecID(const PetscInt row, const PetscInt col, const PetscInt dim)
Returns vectorized index for matrix element (row,col).
Definition util.cpp:152
PetscErrorCode Ikron(const Mat A, const int dimI, const double alpha, Mat *Out, InsertMode insert_mode)
Computes Kronecker product .
Definition util.cpp:282
PetscErrorCode AkronB(const Mat A, const Mat B, const double alpha, Mat *Out, InsertMode insert_mode)
Computes general Kronecker product .
Definition util.cpp:373
std::string enumToString(EnumType value, const std::map< std::string, EnumType > &type_map)
Converts enum value back to string.
Definition util.hpp:331
std::optional< T > parseEnum(const std::string &str, const std::map< std::string, T > &enum_map)
Generic enum parsing utility with case-insensitive lookup.
Definition util.hpp:314
std::string toLower(std::string str)
Returns a lowercase version of the input string.
Definition util.cpp:737
double sigmoid(double width, double x)
Sigmoid function for smooth transitions.
Definition util.cpp:86
PetscErrorCode StateIsHermitian(Vec x, PetscReal tol, PetscBool *flag)
Tests if vectorized state represents a Hermitian matrix.
Definition util.cpp:432
PetscErrorCode StateHasTrace1(Vec x, PetscReal tol, PetscBool *flag)
Tests if vectorized state vector x=[u,v] represents matrix with trace 1.
Definition util.cpp:484
PetscInt mapEssToFull(const PetscInt i, const std::vector< size_t > &nlevels, const std::vector< size_t > &nessential)
Maps index from essential level system to full-dimension system.
Definition util.cpp:157
PetscErrorCode kronI(const Mat A, const int dimI, const double alpha, Mat *Out, InsertMode insert_mode)
Computes Kronecker product .
Definition util.cpp:324
ParsedArgs parseArguments(int argc, char **argv)
Definition util.cpp:29
int isGuardLevel(const int i, const std::vector< size_t > &nlevels, const std::vector< size_t > &nessential)
Tests if density matrix index corresponds to a guard level.
Definition util.cpp:261
int read_vector(const char *filename, double *var, int dim, bool quietmode=false, int skiplines=0, const std::string testheader="")
Reads data vector from file.
Definition util.cpp:568
bool hasSuffix(const std::string &str, const std::string &suffix)
Checks if string ends with specified suffix.
Definition util.cpp:742
PetscErrorCode MatIsAntiSymmetric(Mat A, PetscReal tol, PetscBool *flag)
Tests if matrix A is anti-symmetric (A^T = -A).
Definition util.cpp:412
bool isUnitary(const Mat A, const Mat B)
Tests if complex matrix A+iB is unitary.
Definition util.cpp:701
int isEssential(const int i, const std::vector< size_t > &nlevels, const std::vector< size_t > &nessential)
Tests if density matrix index corresponds to an essential level.
Definition util.cpp:239
void copyLast(std::vector< Tval > &fillme, int tosize)
Extends vector by repeating the last element.
Definition util.hpp:274
PetscErrorCode SanityTests(Vec x, PetscReal time)
Performs all sanity tests on state vector.
double getRampFactor_diff(const double time, const double tstart, const double tstop, const double tramp)
Derivative of ramping factor with respect to stop time.
Definition util.cpp:124