Debian Codegraph @

packages/stk/stk_doc_tests/stk_io/addFileContentsToOutputDatabase.cpp

raw
1 // Copyright 2002 - 2008, 2010, 2011 National Technology Engineering
2 // Solutions of Sandia, LLC (NTESS). Under the terms of Contract
3 // DE-NA0003525 with NTESS, the U.S. Government retains certain rights
4 // in this software.
5 //
6 // Redistribution and use in source and binary forms, with or without
7 // modification, are permitted provided that the following conditions are
8 // met:
9 //
10 // * Redistributions of source code must retain the above copyright
11 // notice, this list of conditions and the following disclaimer.
12 //
13 // * Redistributions in binary form must reproduce the above
14 // copyright notice, this list of conditions and the following
15 // disclaimer in the documentation and/or other materials provided
16 // with the distribution.
17 //
18 // * Neither the name of NTESS nor the names of its contributors
19 // may be used to endorse or promote products derived from this
20 // software without specific prior written permission.
21 //
22 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
23 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
24 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
25 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
26 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
27 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
28 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
29 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
30 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
31 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
32 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 //
34
35 #include <gtest/gtest.h> // for AssertHelper, EXPECT_STREQ, etc
36 #include <stddef.h> // for size_t
37 #include <unistd.h> // for unlink
38 #include <ostream> // for basic_ostream, operator<<, etc
39 #include <stk_io/StkMeshIoBroker.hpp> // for StkMeshIoBroker
40 #include <string> // for string, basic_string, etc
41 #include <vector> // for vector
42 #include "Ioss_DBUsage.h" // for DatabaseUsage::READ_MODEL
43 #include "Ioss_Property.h" // for Property
44 #include "Ioss_Region.h" // for Region
45 #include "stk_io/DatabasePurpose.hpp" // for DatabasePurpose::READ_MESH, etc
46 #include "stk_util/parallel/Parallel.hpp"
47 namespace Ioss { class DatabaseIO; }
48
49 namespace {
50
51 TEST(StkMeshIoBrokerHowTo, addFileContentsToOutputDatabase)
52 {
53 std::string filename = "information_records.e";
54 MPI_Comm communicator = MPI_COMM_WORLD;
55 int numProcs = stk::parallel_machine_size(communicator);
56 if (numProcs != 1) {
57 return;
58 }
59
60 //-BEGIN
61 // ============================================================
62 //+ SETUP
63 std::string input_file = "application_input_file.i";
64 std::string info1("This is the first line of the input file.");
65 std::string info2("This is the second line of the input file. "
66 "It is longer than 80 characters, so it should be wrapped.");
67 std::string info3("This is the third line of the input file.");
68 std::string info4("This is the fourth and last line of the input file.");
69
70 std::string additional_info_record = "This is an info record added explicitly,"
71 " not from the input file.";
72 {
73 std::ofstream my_file(input_file.c_str());
74 my_file << info1 <<"\n" << info2 <<"\n" << info3 <<"\n" << info4 <<"\n";
75 }
76
77 {
78 // ============================================================
79 //+ EXAMPLE
80 stk::io::StkMeshIoBroker stkIo(communicator);
81 size_t ifh = stkIo.add_mesh_database("9x9x9|shell:xyzXYZ", "generated", stk::io::READ_MESH);
82 stkIo.set_active_mesh(ifh);
83 stkIo.create_input_mesh();
84 stkIo.populate_bulk_data();
85
86 // Output...
87 size_t fh = stkIo.create_output_mesh(filename,
88 stk::io::WRITE_RESULTS);
89 Ioss::Region *io_reg = stkIo.get_output_ioss_region(fh).get();
90
91 //+ Add the data from the file "application_input_file.i"
92 //+ as information records on this file.
93 io_reg->property_add(Ioss::Property("input_file_name",input_file)); /*@\label{io:info:file}*/
94
95 //+ Add the data from the "additional_info_record" vector as
96 //+ information records on this file.
97 io_reg->add_information_record(additional_info_record); /*@\label{io:info:vector}*/
98
99 stkIo.write_output_mesh(fh);
100 // ... Verification deleted
101 //-END
102 }
103
104 // ============================================================
105 // VERIFICATION
106
107 {
108 stk::io::StkMeshIoBroker stkIo(communicator);
109 // Verify output mesh contains the data in
110 // 'input_file' as information records... Note that
111 // the output mesh will contain all element blocks; however, the
112 // non-shell element block will have zero elements. This is due
113 // to the subset_selector subsetting the entities and not the
114 // parts...
115 size_t index = stkIo.add_mesh_database(filename, stk::io::READ_MESH);
116 stkIo.set_active_mesh(index);
117 stkIo.create_input_mesh();
118
119 const std::vector<std::string> &info_records = stkIo.get_info_records();
120 // First line of info records is host information (node name,
121 // os version) (2) Next record is the file name of the input file
122 // data that follows (1) File contains 4 records; 1 is longer than
123 // 80 characters, so it wraps (4+1) Next line is the
124 // "additional_info_record" added above (1)
125 // Last records are the IOSS configuration summary (35).
126 size_t expected_min_info_record_count = 1 + (4+1) + 1 + 1;
127 EXPECT_TRUE(expected_min_info_record_count <= info_records.size());
128
129 EXPECT_STREQ(input_file.c_str(), info_records[1].c_str());
130
131 EXPECT_STREQ(info1.c_str(), info_records[2].c_str());
132 EXPECT_STREQ(info2.substr(0,79).c_str(), info_records[3].substr(0,79).c_str());
133 EXPECT_STREQ(info2.substr(79).c_str(), info_records[4].c_str());
134 EXPECT_STREQ(info3.c_str(), info_records[5].c_str());
135 EXPECT_STREQ(info4.c_str(), info_records[6].c_str());
136 EXPECT_STREQ(additional_info_record.c_str(), info_records[7].c_str());
137
138 // unlink(filename.c_str());
139 unlink(input_file.c_str());
140 }
141 }
142
143 }
144 //TODO:
145 //. Check only on processor 0