Newer
Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
test_that("to_node works for atomic values", {
my_string <- "Hello there"
str_node <- to_node(my_string)
str_xml <- xml2::as_xml_document(str_node)
expect_equal(xml2::xml_text(xml2::xml_find_first(str_xml,
"/my_string")),
"Hello there")
})
test_that("to_node works for vectors", {
my_vect = c(a = 1, b = 2, c = 3)
vect_node <- to_node(my_vect, "vect")
vect_xml <- xml2::as_xml_document(vect_node)
expect_equal(xml2::xml_text(xml2::xml_find_first(vect_xml,
"/vect/a")),
"1")
})
test_that("to_node works for attribute vectors", {
my_attr_list <- list(a = c(id = 0, name = "Alice"),
a = c(id = 3, name = "Bob"))
attr_names = c("a")
attr_node <- to_node(my_attr_list, NULL, c("a"))
attr_xml <- xml2::as_xml_document(attr_node)
expect_equal(xml2::xml_attrs(xml2::xml_find_first(attr_xml,
"/my_attr_list/a")),
c(id = "0", name = "Alice"))
})
test_that("to_node works reads parameter names implicitly if not given", {
test_class <- function(x){
structure(list(x = x,
y = "there"),
class = "test_class")
}
test_obj <- test_class("Hi")
test_node <- to_node(test_obj$x)
test_xml <- xml2::as_xml_document(test_node)
expect_equal(xml2::xml_text(xml2::xml_find_first(test_xml, "/x")), "Hi")
})
test_that("to_node works for simple classes", {
#Test for a single class element
parameter <- r2ogs6_parameter(name = "pressure0",
type = "Constant",
values = 1e5)
parameter_node <- to_node(parameter)
parameter_xml <- xml2::as_xml_document(parameter_node)
expect_equal(xml2::xml_text(xml2::xml_find_first(parameter_xml,
"/parameter/name")),
"pressure0")
expect_equal(xml2::xml_double(xml2::xml_find_first(parameter_xml,
"/parameter/values")),
1e5)
#Test for a wrapper list
parameter_2 <- r2ogs6_parameter(name = "pressure1",
type = "Constant",
values = c(0, 0))
para_wrapper <- list(parameter, parameter_2)
wrapper_node <- to_node(para_wrapper, "parameters")
wrapper_xml <- xml2::as_xml_document(wrapper_node)
expect_equal(length(
xml2::xml_find_all(wrapper_xml, "/parameters/parameter")), 2)
expect_equal(xml2::xml_text(
xml2::xml_find_all(wrapper_xml, "/parameters/parameter/values")[[2]]),
"0 0")
})
test_that("to_node works for classes that have lists as parameters", {
linear_solver <- r2ogs6_linear_solver(
name = "general_linear_solver",
eigen = list(
solver_type = "BiCGSTAB",
precon_type = "ILUT",
max_iteration_step = 10000,
error_tolerance = 1e-16
),
lis = "-i bicgstab -p ilu -tol 1e-16 -maxiter 10000"
)
linear_solver_node <- to_node(linear_solver)
linear_solver_xml <- xml2::as_xml_document(linear_solver_node)
expect_equal(length(xml2::xml_find_all(linear_solver_xml,
"/linear_solver/eigen/*")), 4)
expect_equal(xml2::xml_text(
xml2::xml_find_all(linear_solver_xml,
"/linear_solver/eigen/precon_type")),
"ILUT")
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
})
test_that("to_node works for classes that have subclasses", {
process_variable <- r2ogs6_process_variable(
name = "pressure",
components = 1,
order = 1,
initial_condition = "pressure0",
boundary_conditions = list(
r2ogs6_boundary_condition(
type = "Neumann",
parameter = "flux",
component = 0,
geometrical_set = "square_1x1_geometry",
geometry = "left"
)
)
)
process_variable_node <- to_node(process_variable)
process_variable_xml <- xml2::as_xml_document(process_variable_node)
expect_equal(length(xml2::xml_find_all(
process_variable_xml, "/process_variable/boundary_conditions/*")), 1)
expect_equal(xml2::xml_text(
xml2::xml_find_first(
process_variable_xml,
"/process_variable/boundary_conditions/boundary_condition/geometry"
)
),
"left")
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
})
test_that("to_node works for classes that have attributes", {
tl_process <- r2ogs6_tl_process(
ref = "HM",
nonlinear_solver = "basic_newton",
convergence_criterion = list(
type = "PerComponentDeltaX",
norm_type = "NORM2",
reltols = "5e-8 1e10 1e10"
),
time_discretization = list(type = "BackwardEuler"),
time_stepping = list(
type = "FixedTimeStepping",
t_initial = 0,
t_end = 100,
timesteps = list(pair = list(rep = 1,
delta_t = 0.1))
)
)
tl_process_node <- to_node(tl_process)
tl_process_xml <- xml2::as_xml_document(tl_process_node)
attrs <- xml2::xml_attrs(xml2::xml_find_first(tl_process_xml, "/process"))
expect_equal(attrs, c(ref = "HM"))
})
test_that("to_node works for r2ogs6_process class", {
process <- r2ogs6_process(
name = "HM",
type = "HYDRO_MECHANICS",
integration_order = 3,
dimension = 2,
constitutive_relation = c(
type = "LinearElasticIsotropic",
youngs_modulus = "E",
poissons_ratio = "nu"
),
process_variables = c(displacement = "displacement",
pressure = "pressure"),
secondary_variables = list(
c("sigma_xx", "sigma_xx"),
c("sigma_yy", "sigma_yy")
),
specific_body_force = c(0, 0)
)
process_node <- to_node(process)
process_xml <- xml2::as_xml_document(process_node)
attrs <- xml2::xml_attrs(
xml2::xml_find_first(process_xml,
"/process/secondary_variables/secondary_variable"))
expect_equal(attrs, c(internal_name = "sigma_xx", output_name = "sigma_xx"))
})