diff --git a/CMakeLists.txt b/CMakeLists.txt index 524c8bc..865aac1 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,8 +32,8 @@ file(COPY ${CMAKE_SOURCE_DIR}/lattice_files/ add_library(yaml_c_wrapper SHARED src/yaml_c_wrapper.cpp) target_link_libraries(yaml_c_wrapper PUBLIC yaml-cpp) -add_executable(example_read_write examples/example_read_write.cpp) -target_link_libraries(example_read_write yaml_c_wrapper yaml-cpp) +add_executable(example_rw examples/example_rw.cpp) +target_link_libraries(example_rw yaml_c_wrapper yaml-cpp) add_executable(get_lattices src/get_lattices.cpp) target_link_libraries(get_lattices yaml_c_wrapper yaml-cpp) \ No newline at end of file diff --git a/README.md b/README.md index 9feff0d..a9d058f 100644 --- a/README.md +++ b/README.md @@ -9,6 +9,8 @@ repeat YAML::Nodes are values that act like pointers, so editing a node will cause the tree the node is contained in to reflect the changes. +Lattice files should be placed in the `lattice_files/` directory. + ## Usage In pals-cpp, run @@ -20,12 +22,22 @@ cmake --build build This builds `libyaml_c_wrapper.dylib`, a shared object library that can be used by other languages. -It also builds an executable using yaml_reader.cpp containing examples for how +It also builds an executable at build/example_rw containing examples for how to use the library to read lattice files, perform basic manipulations, and write to other lattice files. To see the output, navigate to the directory `build` and run +The program `get_lattices` will create a struct containing three lattices: +- `original` is a map containing the base lattice as well as any lattices included +in the base lattice. +- `included` is the base lattice but with all included files substituted in. +- `expanded` is the base lattice after lattice expansion has been performed. +Specify the lattice file with the first argument to the function. On default, +the lattice specified by the last `use` statement will be used, and if no `use` +statement exists, the lattice lattice in the file will be used. An optional flag +`-lat lattice_name` can be used to specify the lattice to expand, which has greatest +priority. For example, in the build directory, run ```console -./yaml_reader +./get_lattices ex.pals.yaml -lat lat2 ``` It will also build the tests. diff --git a/examples/example_read_write.cpp b/examples/example_rw.cpp similarity index 100% rename from examples/example_read_write.cpp rename to examples/example_rw.cpp diff --git a/examples/yaml_reader.cpp b/examples/yaml_reader.cpp deleted file mode 100644 index 548097d..0000000 --- a/examples/yaml_reader.cpp +++ /dev/null @@ -1,71 +0,0 @@ -#include -#include "../src/yaml_c_wrapper.h" - -// If file name is provided as a command line argument, this will print out the -// expanded contents of the file to the terminal, as well as to expand.pals.yaml. -// Otherwise, it will use the example file ex.pals.yaml - -int main(int argc, char* argv[]) { - if (argc != 1) { - char* filename = argv[1]; - std::string path = "../lattice_files/"; - path += filename; - YAMLNodeHandle handle = parse_file(path.c_str()); - - lattice_expand(handle); - std::cout << "Printing out contents of file: " << filename << std::endl; - std::cout << yaml_to_string(handle) << std::endl; - write_file(handle, "../lattice_files/expand.pals.yaml"); - return 0; - } - // reading a lattice from a yaml file - YAMLNodeHandle handle = parse_file("../lattice_files/ex.pals.yaml"); - std::cout << "Printing out contents of file: " << "ex.pals.yaml" << std::endl; - // printing to terminal - std::cout << yaml_to_string(handle) << std::endl << std::endl; - - // type checking - // prints "handle is of type sequence: 1", 1 meaning true - std::cout << "handle is of type sequence: " << (is_sequence(handle)) - << "\n"; - - // accessing sequence - YAMLNodeHandle node = get_index(handle, 0); - /* prints - the first element is: - thingB: - kind: Sextupole - */ - std::cout << "the first element is: \n" << yaml_to_string(node) << "\n"; - - // accessing map - // prints "the value at key 'thingB' is: kind: Sextupole" - std::cout << "\nthe value at key 'thingB' is: " - << yaml_to_string(get_key(node, "thingB")) << "\n"; - - // creating a new node that's a map - YAMLNodeHandle map = create_map(); - set_value_int(map, "apples", 5); - - // creating a new node that's a sequence - YAMLNodeHandle sequence = create_sequence(); - push_string(sequence, "magnet1"); - push_string(sequence, ""); - YAMLNodeHandle scalar = create_scalar(); - set_scalar_string(scalar, "magnet2"); - set_at_index(sequence, 1, scalar); - // give sequence a name by putting it in a map: - YAMLNodeHandle magnets = create_map(); - set_value_node(magnets, "magnets", sequence); - - // adding new nodes to lattice - push_node(handle, map); - push_node(handle, magnets); - - // performing lattice expansion - lattice_expand(handle); - - // writing modified lattice file to expand.pals.yaml - write_file(handle, "../lattice_files/expand.pals.yaml"); - return 0; -} diff --git a/lattice_files/ex.pals.yaml b/lattice_files/ex.pals.yaml index d892512..f9fff91 100644 --- a/lattice_files/ex.pals.yaml +++ b/lattice_files/ex.pals.yaml @@ -22,7 +22,7 @@ direction: -1 - a_subline: # Item a_subline is repeated three times repeat: 3 -- include: "include.pals.yaml" +- include: "../lattice_files/include.pals.yaml" - a_subline: kind: BeamLine line: diff --git a/lattice_files/include.pals.yaml b/lattice_files/include.pals.yaml index d2af9d3..aa243f0 100644 --- a/lattice_files/include.pals.yaml +++ b/lattice_files/include.pals.yaml @@ -2,4 +2,4 @@ - first: 1 - second: 2 - third: 3 - - include: "include2.pals.yaml" + - include: "../lattice_files/include2.pals.yaml" diff --git a/src/get_lattices.cpp b/src/get_lattices.cpp index ff2d4c8..3aaa515 100644 --- a/src/get_lattices.cpp +++ b/src/get_lattices.cpp @@ -3,12 +3,12 @@ #include int main(int argc, char* argv[]) { - std::string file_name = "ex.pals.yaml"; + const char* file_name = "../lattice_files/ex.pals.yaml"; const char* lattice_name = ""; - if (argc >= 1) { - file_name = argv[0]; - } if (argc >= 2) { + file_name = argv[1]; + } + if (argc >= 3) { for (int i = 1; i < argc; i++) { if (strcmp(argv[i], "-lat") == 0) { lattice_name = argv[i+1]; @@ -16,7 +16,7 @@ int main(int argc, char* argv[]) { } } - struct lattices lat = get_lattices("ex.pals.yaml", lattice_name); + struct lattices lat = get_lattices(file_name, lattice_name); std::cout << "Printing original lattice information: " << std::endl; std::cout << yaml_to_string(lat.original) << std::endl << "\n\n"; diff --git a/src/yaml_c_wrapper.cpp b/src/yaml_c_wrapper.cpp index d5a3ca2..fe868b9 100644 --- a/src/yaml_c_wrapper.cpp +++ b/src/yaml_c_wrapper.cpp @@ -69,6 +69,12 @@ std::vector search_kind(YAML::Node root, return search(root, condition_wrapper); } +// /* `node` should be a Lattice or Beamline. For each element in the line, +// add the map {parent, `node`.}*/ +// void add_parents(YAML::Node node) { + +// } + /* Recursively loops through the node to record all the lattices and beamlines and their corresponding parameters. @@ -113,7 +119,7 @@ void add_to_original(YAML::Node original, std::string filename) { if (!original[filename]) { YAML::Node node = YAML::Node(YAML::NodeType::Map); node["path"] = ""; - node["info"] = YAML::LoadFile("../lattice_files/" + filename); + node["info"] = YAML::LoadFile(filename); original[filename] = node; } } @@ -158,7 +164,7 @@ YAML::Node original_lattice(std::string filename) { Constructs the included lattice. */ YAML::Node included_lattice(std::string filename) { - YAML::Node included = YAML::LoadFile("../lattice_files/" + filename); + YAML::Node included = YAML::LoadFile(filename); std::vector include_files = search(included, [](YAML::Node node) { return node.IsMap() && node["include"]; }); @@ -328,7 +334,7 @@ statement will be expanded. that occurs latest in the file will be expanded. */ YAML::Node expanded_lattice(std::string filename, std::string lattice_name) { - YAML::Node root = YAML::LoadFile("../lattice_files/" + filename); + YAML::Node root = YAML::LoadFile(filename); root = included_lattice(filename); std::map* elements_map = get_dict(root);