diff --git a/codegen/src/semantic.rs b/codegen/src/semantic.rs index c40e61a..7093f2f 100644 --- a/codegen/src/semantic.rs +++ b/codegen/src/semantic.rs @@ -488,22 +488,29 @@ fn compile_lookup( name: name.clone(), fields: fields.iter().map(|field| field.name.clone()).collect(), }; - let cases = lookup - .definition + let cases = enum_type .values .iter() - .map(|case| RecordLookupCase { - member: case.key.clone(), - values: fields + .map(|member| { + let case = lookup + .definition + .values .iter() - .map(|field| { - let value = case.value[&field.name]; - Number { - value, - lexeme: format!("{value:?}"), - } - }) - .collect(), + .find(|case| case.key == member.name) + .expect("validated lookup covers every enum member exactly once"); + RecordLookupCase { + member: member.name.clone(), + values: fields + .iter() + .map(|field| { + let value = case.value[&field.name]; + Number { + value, + lexeme: format!("{value:?}"), + } + }) + .collect(), + } }) .collect(); Ok(( @@ -729,12 +736,12 @@ mod tests { use crate::{ formula::parse, model::{ - RawExpression, RawFunction, RawInput, RawInputType, RawVariable, RawVariableValue, - SourceLocation, + EnumDefinition, LookupDefinition, Outputs, Parameter, RawExpression, RawFunction, + RawInput, RawInputType, RawLookup, RawVariable, RawVariableValue, SourceLocation, }, }; - use super::{BinaryOp, Expr, compile}; + use super::{BinaryOp, Expr, VariableValue, compile}; fn expression(path: &str, source: &str) -> RawExpression { RawExpression { @@ -798,6 +805,89 @@ mod tests { )); } + #[test] + fn lookup_cases_follow_enum_order() { + let mut enum_type: EnumDefinition = serde_yaml::from_str( + r#" + type: enum + description: Example enum. + values: + - name: first + value: first + - name: second + value: second + "#, + ) + .unwrap(); + enum_type.name = "ExampleEnum".into(); + + let mut lookup_definition: LookupDefinition = serde_yaml::from_str( + r##" + type: lookup + input: + $ref: "#/$defs/ExampleEnum" + output: + $ref: "#/$defs/ExampleRecord" + values: + - key: second + value: + value: 2.0 + - key: first + value: + value: 1.0 + "##, + ) + .unwrap(); + lookup_definition.name = "ExampleLookup".into(); + lookup_definition.input_type = Some(enum_type.clone()); + lookup_definition.output_type = Some(Outputs::Record { + name: "ExampleRecord".into(), + fields: vec![Parameter { + name: "value".into(), + unit: "1".into(), + domain: None, + description: "Example value.".into(), + }], + }); + + let raw = RawFunction { + specification_path: PathBuf::from("specs/functions/example.md"), + name: "example".into(), + inputs: vec![RawInput { + name: "kind".into(), + value_type: RawInputType::Enum(enum_type), + }], + variables: vec![RawVariable { + name: "row".into(), + value: RawVariableValue::Lookup(RawLookup { + implementation_path: "implementation.variables[0].lookup".into(), + key: "kind".into(), + definition: lookup_definition, + }), + }], + }; + let compiled = compile(&raw).unwrap(); + let VariableValue::RecordLookup(lookup) = &compiled.variables[0].value else { + panic!("expected record lookup"); + }; + assert_eq!( + lookup + .cases + .iter() + .map(|case| case.member.as_str()) + .collect::>(), + ["first", "second"] + ); + assert_eq!( + lookup + .cases + .iter() + .map(|case| case.values[0].value) + .collect::>(), + [1.0, 2.0] + ); + } + #[test] fn rejects_unknown_forward_and_self_references_with_expression_paths() { for (source, expected) in [ diff --git a/codegen/src/targets/native.rs b/codegen/src/targets/native.rs index 523210a..e97fd45 100644 --- a/codegen/src/targets/native.rs +++ b/codegen/src/targets/native.rs @@ -85,13 +85,10 @@ fn c_header(slug: &str, functions: &[&CompiledFunction]) -> Result { if requires_pow4(functions) { writer.write("#include \n"); } - if requires_record_literal(functions) { - writer.write("#include \n"); - } - if requires_math(functions) { + if requires_math(functions) || requires_lookup(functions) { writer.write("#include \n"); } - if requires_pow4(functions) || requires_math(functions) { + if requires_pow4(functions) || requires_math(functions) || requires_lookup(functions) { writer.blank_line(); } let first = functions @@ -147,7 +144,7 @@ fn c_header(slug: &str, functions: &[&CompiledFunction]) -> Result { fn cpp_module(slug: &str, functions: &[&CompiledFunction]) -> Result { let mut writer = Writer::new(); writer.write(format_args!("{HEADER}\n\n")); - if requires_pow4(functions) || requires_math(functions) || requires_cpp_unreachable(functions) { + if requires_pow4(functions) || requires_math(functions) { writer.write("module;\n"); if requires_pow4(functions) { writer.write("#include \n"); @@ -155,9 +152,6 @@ fn cpp_module(slug: &str, functions: &[&CompiledFunction]) -> Result { if requires_math(functions) { writer.write("#include \n"); } - if requires_cpp_unreachable(functions) { - writer.write("#include \n"); - } writer.blank_line(); } writer.write(format_args!("export module ptfkit.{slug};\n\n")); @@ -363,19 +357,10 @@ impl NativeFunction<'_> { } Output::Scalar => writer.line(format_args!("return {output_name};")), Output::Struct(fields) if matches!(self.dialect, NativeDialect::C) => { - writer.line("#ifdef __cplusplus"); - writer.write(format_args!("return {}{{", self.result)); + writer.write(format_args!("{} result = {{", self.result)); render_values(writer, fields); writer.line("};"); - writer.line("#else"); - writer.line(format_args!("return ({}) {{", self.result)); - writer.indented(|writer| { - for field in fields { - writer.line(format_args!(".{field} = {field},")); - } - }); - writer.line("};"); - writer.line("#endif"); + writer.line("return result;"); } Output::Struct(fields) => { writer.write(format_args!("return {}{{", self.result)); @@ -396,7 +381,7 @@ impl NativeFunction<'_> { }; writer.line(format_args!( "const {result} {name} = {}({key});", - record_lookup_function_name(lookup, self.dialect) + record_lookup_function_name(lookup) )); } } @@ -414,7 +399,7 @@ fn lookup_definitions<'a>(functions: &[&'a CompiledFunction]) -> Vec<&'a RecordL .collect() } -fn record_lookup_function_name(lookup: &RecordLookup, _dialect: NativeDialect) -> String { +fn record_lookup_function_name(lookup: &RecordLookup) -> String { format!( "{}_from_{}", c_result_name(&lookup.output.name), @@ -442,65 +427,56 @@ fn render_record_lookup_helper( } writer.line(format_args!( "{result} {}({enum_name} value) {{", - record_lookup_function_name(lookup, dialect) + record_lookup_function_name(lookup) )); writer.indented(|writer| { - writer.line("switch (value) {"); + match dialect { + NativeDialect::C => writer.line(format_args!("static const {result} table[] = {{")), + NativeDialect::Cpp => { + writer.line(format_args!("static constexpr {result} table[] = {{")) + } + } writer.indented(|writer| { for case in &lookup.cases { - let member = match dialect { - NativeDialect::C => c_enum_member(slug, &lookup.enum_name, &case.member), - NativeDialect::Cpp => format!( - "{}::{}", - lookup.enum_name, - case.member.to_case(Case::Pascal) - ), - }; - writer.line(format_args!("case {member}:")); + writer.write("{"); + render_lookup_values(writer, &case.values); + writer.line("},"); + } + }); + writer.line("};"); + match dialect { + NativeDialect::C => { + writer.line("const unsigned index = (unsigned)value;"); + writer.line("if (index < sizeof(table) / sizeof(table[0])) {"); writer.indented(|writer| { - writer.write("return "); - render_record_literal(writer, &result, &case.values, dialect); - writer.line(";"); + writer.line("return table[index];"); }); - } - writer.line("default:"); - writer.indented(|writer| match dialect { - NativeDialect::C => { - let nan_values = std::iter::repeat_n("NAN", lookup.output.fields.len()) - .collect::>() - .join(", "); - writer.line(format_args!( - "return PTFKIT_RECORD_LITERAL({result}, {nan_values});" - )); + writer.line("}"); + writer.write(format_args!("const {result} fallback = {{")); + for index in 0..lookup.output.fields.len() { + if index > 0 { + writer.write(", "); + } + writer.write("NAN"); } - NativeDialect::Cpp => writer.line("std::unreachable();"), - }); - }); - writer.line("}"); + writer.line("};"); + writer.line("return fallback;"); + } + NativeDialect::Cpp => { + writer.line("return table[static_cast(value)];"); + } + } }); writer.line("}"); } -fn render_record_literal( - writer: &mut Writer, - result: &str, - values: &[crate::semantic::Number], - dialect: NativeDialect, -) { - match dialect { - NativeDialect::C => writer.write(format_args!("PTFKIT_RECORD_LITERAL({result}, ")), - NativeDialect::Cpp => writer.write(format_args!("{result}{{")), - } +fn render_lookup_values(writer: &mut Writer, values: &[crate::semantic::Number]) { for (index, value) in values.iter().enumerate() { if index > 0 { writer.write(", "); } writer.write(c::float_literal(&value.lexeme)); } - writer.write(match dialect { - NativeDialect::C => ")", - NativeDialect::Cpp => "}", - }); } fn render_values(writer: &mut Writer, values: &[String]) { @@ -756,38 +732,32 @@ fn requires_math(functions: &[&CompiledFunction]) -> bool { .ir .variables .iter() - .any(|variable| match &variable.value { - VariableValue::Number(expression) => c::requires_math(expression), - VariableValue::RecordLookup(_) => true, - }) + .filter_map(|variable| variable.value.as_number()) + .any(c::requires_math) }) } -fn requires_pow4(functions: &[&CompiledFunction]) -> bool { +fn requires_lookup(functions: &[&CompiledFunction]) -> bool { functions.iter().any(|function| { function .ir .variables .iter() - .filter_map(|variable| variable.value.as_number()) - .any(c::requires_pow4) + .any(|variable| matches!(variable.value, VariableValue::RecordLookup(_))) }) } -fn requires_record_literal(functions: &[&CompiledFunction]) -> bool { +fn requires_pow4(functions: &[&CompiledFunction]) -> bool { functions.iter().any(|function| { function .ir .variables .iter() - .any(|variable| matches!(variable.value, VariableValue::RecordLookup(_))) + .filter_map(|variable| variable.value.as_number()) + .any(c::requires_pow4) }) } -fn requires_cpp_unreachable(functions: &[&CompiledFunction]) -> bool { - requires_record_literal(functions) -} - fn c_test(slug: &str, functions: &[&CompiledFunction]) -> Result { c_compatibility_test(slug, functions) } diff --git a/targets/ptfkit-native/cpp/clapp1978.cppm b/targets/ptfkit-native/cpp/clapp1978.cppm index d06f995..983ccaf 100644 --- a/targets/ptfkit-native/cpp/clapp1978.cppm +++ b/targets/ptfkit-native/cpp/clapp1978.cppm @@ -1,9 +1,5 @@ /* @generated by ptfkit-codegen; DO NOT EDIT. */ -module; -#include -#include - export module ptfkit.clapp1978; /** @@ -102,32 +98,15 @@ struct Clapp1978Parameters { [[nodiscard]] inline Clapp1978Parameters clapp1978_parameters_from_usda_texture_class(UsdaTextureClass value) { - switch (value) { - case UsdaTextureClass::Sand: - return Clapp1978Parameters{4.05, 3.5, 4.66, 0.395, 1.056, 1.52}; - case UsdaTextureClass::LoamySand: - return Clapp1978Parameters{4.38, 1.78, 2.38, 0.41, 0.938, 1.04}; - case UsdaTextureClass::SandyLoam: - return Clapp1978Parameters{4.9, 7.18, 9.52, 0.435, 0.208, 1.03}; - case UsdaTextureClass::SiltLoam: - return Clapp1978Parameters{5.3, 56.6, 75.3, 0.485, 0.0432, 1.26}; - case UsdaTextureClass::Loam: - return Clapp1978Parameters{5.39, 14.6, 20.0, 0.451, 0.0417, 0.693}; - case UsdaTextureClass::SandyClayLoam: - return Clapp1978Parameters{7.12, 8.63, 11.7, 0.42, 0.0378, 0.488}; - case UsdaTextureClass::SiltyClayLoam: - return Clapp1978Parameters{7.75, 14.6, 19.7, 0.477, 0.0102, 0.31}; - case UsdaTextureClass::ClayLoam: - return Clapp1978Parameters{8.52, 36.1, 48.1, 0.476, 0.0147, 0.537}; - case UsdaTextureClass::SandyClay: - return Clapp1978Parameters{10.4, 6.16, 8.18, 0.426, 0.013, 0.223}; - case UsdaTextureClass::SiltyClay: - return Clapp1978Parameters{10.4, 17.4, 23.0, 0.492, 0.0062, 0.242}; - case UsdaTextureClass::Clay: - return Clapp1978Parameters{11.4, 18.6, 24.3, 0.482, 0.0077, 0.268}; - default: - std::unreachable(); - } + static constexpr Clapp1978Parameters table[] = { + {4.05, 3.5, 4.66, 0.395, 1.056, 1.52}, {4.38, 1.78, 2.38, 0.41, 0.938, 1.04}, + {4.9, 7.18, 9.52, 0.435, 0.208, 1.03}, {5.3, 56.6, 75.3, 0.485, 0.0432, 1.26}, + {5.39, 14.6, 20.0, 0.451, 0.0417, 0.693}, {7.12, 8.63, 11.7, 0.42, 0.0378, 0.488}, + {7.75, 14.6, 19.7, 0.477, 0.0102, 0.31}, {8.52, 36.1, 48.1, 0.476, 0.0147, 0.537}, + {10.4, 6.16, 8.18, 0.426, 0.013, 0.223}, {10.4, 17.4, 23.0, 0.492, 0.0062, 0.242}, + {11.4, 18.6, 24.3, 0.482, 0.0077, 0.268}, + }; + return table[static_cast(value)]; } /** diff --git a/targets/ptfkit-native/include/ptfkit/beniaich2023.h b/targets/ptfkit-native/include/ptfkit/beniaich2023.h index 96b0336..b957532 100644 --- a/targets/ptfkit-native/include/ptfkit/beniaich2023.h +++ b/targets/ptfkit-native/include/ptfkit/beniaich2023.h @@ -55,15 +55,8 @@ static inline beniaich2023_ptf_result calc_ptf_beniaich2023_slr1(double clay) { const double water_saturation = (46.307 + 0.556 * clay) / 100.0; const double water_field_capacity = (10.277 + 0.365 * clay) / 100.0; const double water_wilting_point = (3.081 + 0.327 * clay) / 100.0; -#ifdef __cplusplus - return beniaich2023_ptf_result{water_saturation, water_field_capacity, water_wilting_point}; -#else - return (beniaich2023_ptf_result){ - .water_saturation = water_saturation, - .water_field_capacity = water_field_capacity, - .water_wilting_point = water_wilting_point, - }; -#endif + beniaich2023_ptf_result result = {water_saturation, water_field_capacity, water_wilting_point}; + return result; } /** @@ -85,15 +78,8 @@ static inline beniaich2023_ptf_result calc_ptf_beniaich2023_slr2(double silt) { const double water_saturation = (59.508 + 0.299 * silt) / 100.0; const double water_field_capacity = (16.178 + 0.290 * silt) / 100.0; const double water_wilting_point = (10.521 + 0.187 * silt) / 100.0; -#ifdef __cplusplus - return beniaich2023_ptf_result{water_saturation, water_field_capacity, water_wilting_point}; -#else - return (beniaich2023_ptf_result){ - .water_saturation = water_saturation, - .water_field_capacity = water_field_capacity, - .water_wilting_point = water_wilting_point, - }; -#endif + beniaich2023_ptf_result result = {water_saturation, water_field_capacity, water_wilting_point}; + return result; } /** @@ -115,15 +101,8 @@ static inline beniaich2023_ptf_result calc_ptf_beniaich2023_slr3(double sand) { const double water_saturation = (81.420 - 0.427 * sand) / 100.0; const double water_field_capacity = (34.680 - 0.324 * sand) / 100.0; const double water_wilting_point = (23.927 - 0.257 * sand) / 100.0; -#ifdef __cplusplus - return beniaich2023_ptf_result{water_saturation, water_field_capacity, water_wilting_point}; -#else - return (beniaich2023_ptf_result){ - .water_saturation = water_saturation, - .water_field_capacity = water_field_capacity, - .water_wilting_point = water_wilting_point, - }; -#endif + beniaich2023_ptf_result result = {water_saturation, water_field_capacity, water_wilting_point}; + return result; } /** @@ -147,15 +126,8 @@ static inline beniaich2023_ptf_result calc_ptf_beniaich2023_slr4(double clay, do const double water_saturation = (89.401 - 0.298 * clay_silt) / 100.0; const double water_field_capacity = (45.178 - 0.290 * clay_silt) / 100.0; const double water_wilting_point = (29.265 - 0.187 * clay_silt) / 100.0; -#ifdef __cplusplus - return beniaich2023_ptf_result{water_saturation, water_field_capacity, water_wilting_point}; -#else - return (beniaich2023_ptf_result){ - .water_saturation = water_saturation, - .water_field_capacity = water_field_capacity, - .water_wilting_point = water_wilting_point, - }; -#endif + beniaich2023_ptf_result result = {water_saturation, water_field_capacity, water_wilting_point}; + return result; } /** @@ -179,15 +151,8 @@ static inline beniaich2023_ptf_result calc_ptf_beniaich2023_slr5(double clay, do const double water_saturation = (68.851 - 0.546 * clay_silt_ratio) / 100.0; const double water_field_capacity = (23.278 + 1.819 * clay_silt_ratio) / 100.0; const double water_wilting_point = (16.298 - 0.244 * clay_silt_ratio) / 100.0; -#ifdef __cplusplus - return beniaich2023_ptf_result{water_saturation, water_field_capacity, water_wilting_point}; -#else - return (beniaich2023_ptf_result){ - .water_saturation = water_saturation, - .water_field_capacity = water_field_capacity, - .water_wilting_point = water_wilting_point, - }; -#endif + beniaich2023_ptf_result result = {water_saturation, water_field_capacity, water_wilting_point}; + return result; } /** @@ -209,15 +174,8 @@ static inline beniaich2023_ptf_result calc_ptf_beniaich2023_slr6(double soil_org const double water_saturation = (61.163 + 2.793 * soil_organic_matter) / 100.0; const double water_field_capacity = (21.331 + 1.339 * soil_organic_matter) / 100.0; const double water_wilting_point = (13.758 + 0.902 * soil_organic_matter) / 100.0; -#ifdef __cplusplus - return beniaich2023_ptf_result{water_saturation, water_field_capacity, water_wilting_point}; -#else - return (beniaich2023_ptf_result){ - .water_saturation = water_saturation, - .water_field_capacity = water_field_capacity, - .water_wilting_point = water_wilting_point, - }; -#endif + beniaich2023_ptf_result result = {water_saturation, water_field_capacity, water_wilting_point}; + return result; } /** @@ -245,15 +203,8 @@ static inline beniaich2023_ptf_result calc_ptf_beniaich2023_mlr1(double silt, do (35.844 - 0.085 * silt - 0.359 * sand + 0.947 * soil_organic_matter) / 100.0; const double water_wilting_point = (28.734 - 0.148 * silt - 0.324 * sand + 0.636 * soil_organic_matter) / 100.0; -#ifdef __cplusplus - return beniaich2023_ptf_result{water_saturation, water_field_capacity, water_wilting_point}; -#else - return (beniaich2023_ptf_result){ - .water_saturation = water_saturation, - .water_field_capacity = water_field_capacity, - .water_wilting_point = water_wilting_point, - }; -#endif + beniaich2023_ptf_result result = {water_saturation, water_field_capacity, water_wilting_point}; + return result; } /** @@ -279,15 +230,8 @@ static inline beniaich2023_ptf_result calc_ptf_beniaich2023_mlr2(double sand, (32.227 - 0.320 * sand + 0.899 * soil_organic_matter) / 100.0; const double water_wilting_point = (22.421 - 0.254 * sand + 0.552 * soil_organic_matter) / 100.0; -#ifdef __cplusplus - return beniaich2023_ptf_result{water_saturation, water_field_capacity, water_wilting_point}; -#else - return (beniaich2023_ptf_result){ - .water_saturation = water_saturation, - .water_field_capacity = water_field_capacity, - .water_wilting_point = water_wilting_point, - }; -#endif + beniaich2023_ptf_result result = {water_saturation, water_field_capacity, water_wilting_point}; + return result; } /** @@ -312,15 +256,8 @@ static inline beniaich2023_ptf_result calc_ptf_beniaich2023_mlr3(double silt, const double water_field_capacity = (13.847 + 0.281 * silt + 0.999 * soil_organic_matter) / 100.0; const double water_wilting_point = (8.929 + 0.182 * silt + 0.683 * soil_organic_matter) / 100.0; -#ifdef __cplusplus - return beniaich2023_ptf_result{water_saturation, water_field_capacity, water_wilting_point}; -#else - return (beniaich2023_ptf_result){ - .water_saturation = water_saturation, - .water_field_capacity = water_field_capacity, - .water_wilting_point = water_wilting_point, - }; -#endif + beniaich2023_ptf_result result = {water_saturation, water_field_capacity, water_wilting_point}; + return result; } /** @@ -345,15 +282,8 @@ static inline beniaich2023_ptf_result calc_ptf_beniaich2023_mlr4(double clay, const double water_field_capacity = (7.023 + 0.364 * clay + 1.278 * soil_organic_matter) / 100.0; const double water_wilting_point = (0.923 + 0.327 * clay + 0.847 * soil_organic_matter) / 100.0; -#ifdef __cplusplus - return beniaich2023_ptf_result{water_saturation, water_field_capacity, water_wilting_point}; -#else - return (beniaich2023_ptf_result){ - .water_saturation = water_saturation, - .water_field_capacity = water_field_capacity, - .water_wilting_point = water_wilting_point, - }; -#endif + beniaich2023_ptf_result result = {water_saturation, water_field_capacity, water_wilting_point}; + return result; } /** @@ -381,15 +311,8 @@ static inline beniaich2023_ptf_result calc_ptf_beniaich2023_mlr5(double clay, do (-0.094 + 0.359 * clay + 0.274 * silt + 0.947 * soil_organic_matter) / 100.0; const double water_wilting_point = (-3.623 + 0.324 * clay + 0.175 * silt + 0.636 * soil_organic_matter) / 100.0; -#ifdef __cplusplus - return beniaich2023_ptf_result{water_saturation, water_field_capacity, water_wilting_point}; -#else - return (beniaich2023_ptf_result){ - .water_saturation = water_saturation, - .water_field_capacity = water_field_capacity, - .water_wilting_point = water_wilting_point, - }; -#endif + beniaich2023_ptf_result result = {water_saturation, water_field_capacity, water_wilting_point}; + return result; } #endif diff --git a/targets/ptfkit-native/include/ptfkit/chakraborty2011.h b/targets/ptfkit-native/include/ptfkit/chakraborty2011.h index 6bc07fd..411811b 100644 --- a/targets/ptfkit-native/include/ptfkit/chakraborty2011.h +++ b/targets/ptfkit-native/include/ptfkit/chakraborty2011.h @@ -62,17 +62,9 @@ static inline chakraborty2011_ptf_result calc_ptf_chakraborty2011_eq1(double cla const double water_content_100 = (0.270 * clay + 0.379 * silt + 2.988) / 100.0; const double water_content_500 = (0.251 * clay + 0.185 * silt + 2.958) / 100.0; const double water_content_1500 = (0.184 * clay + 0.144 * silt + 3.702) / 100.0; -#ifdef __cplusplus - return chakraborty2011_ptf_result{water_content_33, water_content_100, water_content_500, - water_content_1500}; -#else - return (chakraborty2011_ptf_result){ - .water_content_33 = water_content_33, - .water_content_100 = water_content_100, - .water_content_500 = water_content_500, - .water_content_1500 = water_content_1500, - }; -#endif + chakraborty2011_ptf_result result = {water_content_33, water_content_100, water_content_500, + water_content_1500}; + return result; } /** @@ -99,17 +91,9 @@ static inline chakraborty2011_ptf_result calc_ptf_chakraborty2011_eq2(double san const double water_content_100 = (-0.330 * sand + 2.611 * bulk_density + 30.160) / 100.0; const double water_content_500 = (-0.244 * sand - 0.795 * bulk_density + 27.495) / 100.0; const double water_content_1500 = (-0.187 * sand + 1.241 * bulk_density + 19.320) / 100.0; -#ifdef __cplusplus - return chakraborty2011_ptf_result{water_content_33, water_content_100, water_content_500, - water_content_1500}; -#else - return (chakraborty2011_ptf_result){ - .water_content_33 = water_content_33, - .water_content_100 = water_content_100, - .water_content_500 = water_content_500, - .water_content_1500 = water_content_1500, - }; -#endif + chakraborty2011_ptf_result result = {water_content_33, water_content_100, water_content_500, + water_content_1500}; + return result; } /** @@ -141,17 +125,9 @@ static inline chakraborty2011_ptf_result calc_ptf_chakraborty2011_eq3(double cla (0.244 * clay + 0.187 * silt - 3.079 * bulk_density + 8.046) / 100.0; const double water_content_1500 = (0.183 * clay + 0.144 * silt - 0.689 * bulk_density + 4.840) / 100.0; -#ifdef __cplusplus - return chakraborty2011_ptf_result{water_content_33, water_content_100, water_content_500, - water_content_1500}; -#else - return (chakraborty2011_ptf_result){ - .water_content_33 = water_content_33, - .water_content_100 = water_content_100, - .water_content_500 = water_content_500, - .water_content_1500 = water_content_1500, - }; -#endif + chakraborty2011_ptf_result result = {water_content_33, water_content_100, water_content_500, + water_content_1500}; + return result; } /** @@ -179,17 +155,9 @@ static inline chakraborty2011_ptf_result calc_ptf_chakraborty2011_eq4(double cla const double water_content_100 = (0.049 * clay + 0.147 * silt - 0.242 * sand + 25.945) / 100.0; const double water_content_500 = (0.067 * clay - 0.008 * silt - 0.204 * sand + 22.216) / 100.0; const double water_content_1500 = (0.021 * clay - 0.028 * silt - 0.179 * sand + 20.695) / 100.0; -#ifdef __cplusplus - return chakraborty2011_ptf_result{water_content_33, water_content_100, water_content_500, - water_content_1500}; -#else - return (chakraborty2011_ptf_result){ - .water_content_33 = water_content_33, - .water_content_100 = water_content_100, - .water_content_500 = water_content_500, - .water_content_1500 = water_content_1500, - }; -#endif + chakraborty2011_ptf_result result = {water_content_33, water_content_100, water_content_500, + water_content_1500}; + return result; } /** @@ -222,17 +190,9 @@ calc_ptf_chakraborty2011_eq5(double clay, double silt, double sand, double bulk_ (0.067 * clay - 0.008 * silt - 0.203 * sand - 0.135 * bulk_density + 22.359) / 100.0; const double water_content_1500 = (0.014 * clay - 0.041 * silt - 0.192 * sand + 2.093 * bulk_density + 18.470) / 100.0; -#ifdef __cplusplus - return chakraborty2011_ptf_result{water_content_33, water_content_100, water_content_500, - water_content_1500}; -#else - return (chakraborty2011_ptf_result){ - .water_content_33 = water_content_33, - .water_content_100 = water_content_100, - .water_content_500 = water_content_500, - .water_content_1500 = water_content_1500, - }; -#endif + chakraborty2011_ptf_result result = {water_content_33, water_content_100, water_content_500, + water_content_1500}; + return result; } /** @@ -273,17 +233,9 @@ static inline chakraborty2011_ptf_result calc_ptf_chakraborty2011_eq6(double cla const double water_content_1500 = (0.017 * clay - 0.053 * silt - 0.184 * sand + 2.950 * organic_carbon + 2.327 * bulk_density + 16.802) / 100.0; -#ifdef __cplusplus - return chakraborty2011_ptf_result{water_content_33, water_content_100, water_content_500, - water_content_1500}; -#else - return (chakraborty2011_ptf_result){ - .water_content_33 = water_content_33, - .water_content_100 = water_content_100, - .water_content_500 = water_content_500, - .water_content_1500 = water_content_1500, - }; -#endif + chakraborty2011_ptf_result result = {water_content_33, water_content_100, water_content_500, + water_content_1500}; + return result; } #endif diff --git a/targets/ptfkit-native/include/ptfkit/clapp1978.h b/targets/ptfkit-native/include/ptfkit/clapp1978.h index 0d5f8a6..0962f53 100644 --- a/targets/ptfkit-native/include/ptfkit/clapp1978.h +++ b/targets/ptfkit-native/include/ptfkit/clapp1978.h @@ -3,7 +3,6 @@ #ifndef PTFKIT_CLAPP1978_H #define PTFKIT_CLAPP1978_H -#include #include /** @@ -100,32 +99,20 @@ typedef struct { static inline clapp1978_parameters clapp1978_parameters_from_usda_texture_class(clapp1978_usda_texture_class value) { - switch (value) { - case clapp1978_usda_texture_class_sand: - return PTFKIT_RECORD_LITERAL(clapp1978_parameters, 4.05, 3.5, 4.66, 0.395, 1.056, 1.52); - case clapp1978_usda_texture_class_loamy_sand: - return PTFKIT_RECORD_LITERAL(clapp1978_parameters, 4.38, 1.78, 2.38, 0.41, 0.938, 1.04); - case clapp1978_usda_texture_class_sandy_loam: - return PTFKIT_RECORD_LITERAL(clapp1978_parameters, 4.9, 7.18, 9.52, 0.435, 0.208, 1.03); - case clapp1978_usda_texture_class_silt_loam: - return PTFKIT_RECORD_LITERAL(clapp1978_parameters, 5.3, 56.6, 75.3, 0.485, 0.0432, 1.26); - case clapp1978_usda_texture_class_loam: - return PTFKIT_RECORD_LITERAL(clapp1978_parameters, 5.39, 14.6, 20.0, 0.451, 0.0417, 0.693); - case clapp1978_usda_texture_class_sandy_clay_loam: - return PTFKIT_RECORD_LITERAL(clapp1978_parameters, 7.12, 8.63, 11.7, 0.42, 0.0378, 0.488); - case clapp1978_usda_texture_class_silty_clay_loam: - return PTFKIT_RECORD_LITERAL(clapp1978_parameters, 7.75, 14.6, 19.7, 0.477, 0.0102, 0.31); - case clapp1978_usda_texture_class_clay_loam: - return PTFKIT_RECORD_LITERAL(clapp1978_parameters, 8.52, 36.1, 48.1, 0.476, 0.0147, 0.537); - case clapp1978_usda_texture_class_sandy_clay: - return PTFKIT_RECORD_LITERAL(clapp1978_parameters, 10.4, 6.16, 8.18, 0.426, 0.013, 0.223); - case clapp1978_usda_texture_class_silty_clay: - return PTFKIT_RECORD_LITERAL(clapp1978_parameters, 10.4, 17.4, 23.0, 0.492, 0.0062, 0.242); - case clapp1978_usda_texture_class_clay: - return PTFKIT_RECORD_LITERAL(clapp1978_parameters, 11.4, 18.6, 24.3, 0.482, 0.0077, 0.268); - default: - return PTFKIT_RECORD_LITERAL(clapp1978_parameters, NAN, NAN, NAN, NAN, NAN, NAN); + static const clapp1978_parameters table[] = { + {4.05, 3.5, 4.66, 0.395, 1.056, 1.52}, {4.38, 1.78, 2.38, 0.41, 0.938, 1.04}, + {4.9, 7.18, 9.52, 0.435, 0.208, 1.03}, {5.3, 56.6, 75.3, 0.485, 0.0432, 1.26}, + {5.39, 14.6, 20.0, 0.451, 0.0417, 0.693}, {7.12, 8.63, 11.7, 0.42, 0.0378, 0.488}, + {7.75, 14.6, 19.7, 0.477, 0.0102, 0.31}, {8.52, 36.1, 48.1, 0.476, 0.0147, 0.537}, + {10.4, 6.16, 8.18, 0.426, 0.013, 0.223}, {10.4, 17.4, 23.0, 0.492, 0.0062, 0.242}, + {11.4, 18.6, 24.3, 0.482, 0.0077, 0.268}, + }; + const unsigned index = (unsigned)value; + if (index < sizeof(table) / sizeof(table[0])) { + return table[index]; } + const clapp1978_parameters fallback = {NAN, NAN, NAN, NAN, NAN, NAN}; + return fallback; } /** diff --git a/targets/ptfkit-native/include/ptfkit/cosby1984.h b/targets/ptfkit-native/include/ptfkit/cosby1984.h index 41e6db9..a941ee6 100644 --- a/targets/ptfkit-native/include/ptfkit/cosby1984.h +++ b/targets/ptfkit-native/include/ptfkit/cosby1984.h @@ -85,20 +85,9 @@ calc_ptf_cosby1984_univariate(double sand, double silt, double clay) { const double sd_b = 1.34 + 0.0500 * clay; const double sd_log_k_sat = 0.459 + 0.00321 * silt; const double sd_theta_s = 7.73 - 0.0730 * clay; -#ifdef __cplusplus - return cosby1984_univariate_ptf_result{mean_b, mean_log_psi_s, mean_log_k_sat, mean_theta_s, - sd_b, sd_log_k_sat, sd_theta_s}; -#else - return (cosby1984_univariate_ptf_result){ - .mean_b = mean_b, - .mean_log_psi_s = mean_log_psi_s, - .mean_log_k_sat = mean_log_k_sat, - .mean_theta_s = mean_theta_s, - .sd_b = sd_b, - .sd_log_k_sat = sd_log_k_sat, - .sd_theta_s = sd_theta_s, - }; -#endif + cosby1984_univariate_ptf_result result = {mean_b, mean_log_psi_s, mean_log_k_sat, mean_theta_s, + sd_b, sd_log_k_sat, sd_theta_s}; + return result; } #endif diff --git a/targets/ptfkit-native/include/ptfkit/detail/record.h b/targets/ptfkit-native/include/ptfkit/detail/record.h deleted file mode 100644 index 5d9c861..0000000 --- a/targets/ptfkit-native/include/ptfkit/detail/record.h +++ /dev/null @@ -1,12 +0,0 @@ -#ifndef PTFKIT_DETAIL_RECORD_H -#define PTFKIT_DETAIL_RECORD_H - -#ifdef __cplusplus -#define PTFKIT_RECORD_LITERAL(type, ...) \ - type { __VA_ARGS__ } -#else -#define PTFKIT_RECORD_LITERAL(type, ...) \ - (type) { __VA_ARGS__ } -#endif - -#endif diff --git a/targets/ptfkit-native/include/ptfkit/dharumarajan2019.h b/targets/ptfkit-native/include/ptfkit/dharumarajan2019.h index a072d93..d52e6c3 100644 --- a/targets/ptfkit-native/include/ptfkit/dharumarajan2019.h +++ b/targets/ptfkit-native/include/ptfkit/dharumarajan2019.h @@ -61,14 +61,8 @@ calc_ptf_dharumarajan2019_nkp(double clay, double sand, double cation_exchange_c 13.82 + 0.205 * clay - 0.088 * sand + 0.316 * cation_exchange_capacity; const double permanent_wilting_point = -5.776 + 0.315 * clay + 0.050 * sand + 0.271 * cation_exchange_capacity; -#ifdef __cplusplus - return dharumarajan2019_water_retention_result{field_capacity, permanent_wilting_point}; -#else - return (dharumarajan2019_water_retention_result){ - .field_capacity = field_capacity, - .permanent_wilting_point = permanent_wilting_point, - }; -#endif + dharumarajan2019_water_retention_result result = {field_capacity, permanent_wilting_point}; + return result; } /** @@ -94,14 +88,8 @@ static inline dharumarajan2019_water_retention_result calc_ptf_dharumarajan2019_nkp_clay(double clay) { const double field_capacity = 4.968 + 0.586 * clay; const double permanent_wilting_point = -2.443 + 0.500 * clay; -#ifdef __cplusplus - return dharumarajan2019_water_retention_result{field_capacity, permanent_wilting_point}; -#else - return (dharumarajan2019_water_retention_result){ - .field_capacity = field_capacity, - .permanent_wilting_point = permanent_wilting_point, - }; -#endif + dharumarajan2019_water_retention_result result = {field_capacity, permanent_wilting_point}; + return result; } /** @@ -130,14 +118,8 @@ calc_ptf_dharumarajan2019_skp(double clay, double sand, double cation_exchange_c 39.179 - 0.041 * clay - 0.371 * sand + 0.257 * cation_exchange_capacity; const double permanent_wilting_point = 8.227 + 0.168 * clay - 0.101 * sand + 0.217 * cation_exchange_capacity; -#ifdef __cplusplus - return dharumarajan2019_water_retention_result{field_capacity, permanent_wilting_point}; -#else - return (dharumarajan2019_water_retention_result){ - .field_capacity = field_capacity, - .permanent_wilting_point = permanent_wilting_point, - }; -#endif + dharumarajan2019_water_retention_result result = {field_capacity, permanent_wilting_point}; + return result; } /** @@ -161,14 +143,8 @@ static inline dharumarajan2019_water_retention_result calc_ptf_dharumarajan2019_skp_clay(double clay) { const double field_capacity = 3.724 + 0.581 * clay; const double permanent_wilting_point = -1.979 + 0.428 * clay; -#ifdef __cplusplus - return dharumarajan2019_water_retention_result{field_capacity, permanent_wilting_point}; -#else - return (dharumarajan2019_water_retention_result){ - .field_capacity = field_capacity, - .permanent_wilting_point = permanent_wilting_point, - }; -#endif + dharumarajan2019_water_retention_result result = {field_capacity, permanent_wilting_point}; + return result; } /** diff --git a/targets/ptfkit-native/include/ptfkit/hodnett2002.h b/targets/ptfkit-native/include/ptfkit/hodnett2002.h index 29c779d..618e6de 100644 --- a/targets/ptfkit-native/include/ptfkit/hodnett2002.h +++ b/targets/ptfkit-native/include/ptfkit/hodnett2002.h @@ -98,16 +98,8 @@ calc_ptf_hodnett2002(double sand, double silt, double clay, double organic_carbo const double theta_r = (22.733 - 0.164 * sand + 0.235 * cation_exchange_capacity - 0.831 * ph + 0.0018 * clay_squared + 0.0026 * sand * clay) / 100.0; -#ifdef __cplusplus - return hodnett2002_ptf_result{alpha, n, theta_s, theta_r}; -#else - return (hodnett2002_ptf_result){ - .alpha = alpha, - .n = n, - .theta_s = theta_s, - .theta_r = theta_r, - }; -#endif + hodnett2002_ptf_result result = {alpha, n, theta_s, theta_r}; + return result; } #endif diff --git a/targets/ptfkit-native/include/ptfkit/li2007.h b/targets/ptfkit-native/include/ptfkit/li2007.h index acb7b32..77413df 100644 --- a/targets/ptfkit-native/include/ptfkit/li2007.h +++ b/targets/ptfkit-native/include/ptfkit/li2007.h @@ -78,16 +78,8 @@ static inline li2007_ptf_result calc_ptf_li2007(double sand, double silt, double exp(13.262 - 1.914 * sand_ln - 0.974 * silt_ln - 0.058 * clay - 1.709 * soil_organic_matter_ln + 2.885 * soil_organic_matter - 8.026 * bulk_density_ln); const double k_sat = k_sat_cm_per_day * (1.0 / 8640000.0); -#ifdef __cplusplus - return li2007_ptf_result{theta_s, a_vg, n_vg, k_sat}; -#else - return (li2007_ptf_result){ - .theta_s = theta_s, - .a_vg = a_vg, - .n_vg = n_vg, - .k_sat = k_sat, - }; -#endif + li2007_ptf_result result = {theta_s, a_vg, n_vg, k_sat}; + return result; } #endif diff --git a/targets/ptfkit-native/include/ptfkit/mayr1999.h b/targets/ptfkit-native/include/ptfkit/mayr1999.h index 5407799..d7260ae 100644 --- a/targets/ptfkit-native/include/ptfkit/mayr1999.h +++ b/targets/ptfkit-native/include/ptfkit/mayr1999.h @@ -84,15 +84,8 @@ static inline mayr1999_ptf_result calc_ptf_mayr1999(double sand, double silt, do const double theta_s = 0.2345971971 + 0.0046614221 * sand + 0.0088163314 * silt + 0.0064338641 * clay - 0.3028160229 * bulk_density + 1.79762e-5 * sand_squared - 3.134631e-5 * silt_squared; -#ifdef __cplusplus - return mayr1999_ptf_result{a_hc, b_hc, theta_s}; -#else - return (mayr1999_ptf_result){ - .a_hc = a_hc, - .b_hc = b_hc, - .theta_s = theta_s, - }; -#endif + mayr1999_ptf_result result = {a_hc, b_hc, theta_s}; + return result; } #endif diff --git a/targets/ptfkit-native/include/ptfkit/puckett1985.h b/targets/ptfkit-native/include/ptfkit/puckett1985.h index 9578de9..b81825b 100644 --- a/targets/ptfkit-native/include/ptfkit/puckett1985.h +++ b/targets/ptfkit-native/include/ptfkit/puckett1985.h @@ -106,24 +106,10 @@ static inline puckett1985_ptf_result calc_ptf_puckett1985(double sand, double fi const double theta_1000 = 0.0000197 * fine_sand - 0.000244 * sand + 0.000378 * clay + 0.264; const double theta_1500 = 0.0000254 * fine_sand - 0.000239 * sand + 0.000380 * clay + 0.239; const double k_sat = 4.36e-5 * exp(-0.1975 * clay); -#ifdef __cplusplus - return puckett1985_ptf_result{theta_0, theta_1, theta_5, theta_10, theta_30, theta_60, - theta_100, theta_500, theta_1000, theta_1500, k_sat}; -#else - return (puckett1985_ptf_result){ - .theta_0 = theta_0, - .theta_1 = theta_1, - .theta_5 = theta_5, - .theta_10 = theta_10, - .theta_30 = theta_30, - .theta_60 = theta_60, - .theta_100 = theta_100, - .theta_500 = theta_500, - .theta_1000 = theta_1000, - .theta_1500 = theta_1500, - .k_sat = k_sat, - }; -#endif + puckett1985_ptf_result result = {theta_0, theta_1, theta_5, theta_10, + theta_30, theta_60, theta_100, theta_500, + theta_1000, theta_1500, k_sat}; + return result; } #endif diff --git a/targets/ptfkit-native/include/ptfkit/rawls1982.h b/targets/ptfkit-native/include/ptfkit/rawls1982.h index 5379316..4ae25e5 100644 --- a/targets/ptfkit-native/include/ptfkit/rawls1982.h +++ b/targets/ptfkit-native/include/ptfkit/rawls1982.h @@ -144,25 +144,10 @@ static inline rawls1982_ptf_result calc_ptf_rawls1982_full_wrc(double sand, doub -0.0027 + 0.0024 * organic_matter + 0.16 * theta_33 + 0.86 * theta_1500; const double theta_1000 = -0.0019 + 0.0022 * organic_matter + 0.11 * theta_33 + 0.89 * theta_1500; -#ifdef __cplusplus - return rawls1982_ptf_result{theta_4, theta_7, theta_10, theta_20, theta_33, theta_60, - theta_100, theta_200, theta_400, theta_700, theta_1000, theta_1500}; -#else - return (rawls1982_ptf_result){ - .theta_4 = theta_4, - .theta_7 = theta_7, - .theta_10 = theta_10, - .theta_20 = theta_20, - .theta_33 = theta_33, - .theta_60 = theta_60, - .theta_100 = theta_100, - .theta_200 = theta_200, - .theta_400 = theta_400, - .theta_700 = theta_700, - .theta_1000 = theta_1000, - .theta_1500 = theta_1500, - }; -#endif + rawls1982_ptf_result result = {theta_4, theta_7, theta_10, theta_20, + theta_33, theta_60, theta_100, theta_200, + theta_400, theta_700, theta_1000, theta_1500}; + return result; } #endif diff --git a/targets/ptfkit-native/include/ptfkit/saxton2006.h b/targets/ptfkit-native/include/ptfkit/saxton2006.h index daf43b0..0a9c00a 100644 --- a/targets/ptfkit-native/include/ptfkit/saxton2006.h +++ b/targets/ptfkit-native/include/ptfkit/saxton2006.h @@ -178,31 +178,17 @@ static inline saxton2006_ptf_result calc_ptf_saxton2006(double sand, double clay const double saturated_conductivity = 1930.0 * pow(theta_s - theta_33, 3.0 - conductivity_lambda); const double normal_density = (1.0 - theta_s) * 2.65; -#ifdef __cplusplus - return saxton2006_ptf_result{theta_1500, - theta_33, - theta_s, - plant_available_water, - air_entry_tension, - retention_a, - retention_b, - conductivity_lambda, - saturated_conductivity, - normal_density}; -#else - return (saxton2006_ptf_result){ - .theta_1500 = theta_1500, - .theta_33 = theta_33, - .theta_s = theta_s, - .plant_available_water = plant_available_water, - .air_entry_tension = air_entry_tension, - .retention_a = retention_a, - .retention_b = retention_b, - .conductivity_lambda = conductivity_lambda, - .saturated_conductivity = saturated_conductivity, - .normal_density = normal_density, - }; -#endif + saxton2006_ptf_result result = {theta_1500, + theta_33, + theta_s, + plant_available_water, + air_entry_tension, + retention_a, + retention_b, + conductivity_lambda, + saturated_conductivity, + normal_density}; + return result; } /** @@ -230,17 +216,9 @@ static inline saxton2006_density_result calc_ptf_saxton2006_density(double norma const double adjusted_theta_s = 1.0 - adjusted_density / 2.65; const double adjusted_theta_33 = theta_33 - 0.2 * (theta_s - adjusted_theta_s); const double adjusted_theta_s_minus_33 = fmax(adjusted_theta_s - adjusted_theta_33, 0.005); -#ifdef __cplusplus - return saxton2006_density_result{adjusted_density, adjusted_theta_s, adjusted_theta_33, - adjusted_theta_s_minus_33}; -#else - return (saxton2006_density_result){ - .adjusted_density = adjusted_density, - .adjusted_theta_s = adjusted_theta_s, - .adjusted_theta_33 = adjusted_theta_33, - .adjusted_theta_s_minus_33 = adjusted_theta_s_minus_33, - }; -#endif + saxton2006_density_result result = {adjusted_density, adjusted_theta_s, adjusted_theta_33, + adjusted_theta_s_minus_33}; + return result; } /** @@ -334,17 +312,9 @@ static inline saxton2006_gravel_result calc_ptf_saxton2006_gravel(double gravel_ const double bulk_saturated_conductivity = saturated_conductivity * (1.0 - gravel_volume_fraction) / (1.0 - gravel_volume_fraction * (1.0 - 3.0 * density_ratio / 2.0)); -#ifdef __cplusplus - return saxton2006_gravel_result{gravel_volume_fraction, bulk_density, - bulk_plant_available_water, bulk_saturated_conductivity}; -#else - return (saxton2006_gravel_result){ - .gravel_volume_fraction = gravel_volume_fraction, - .bulk_density = bulk_density, - .bulk_plant_available_water = bulk_plant_available_water, - .bulk_saturated_conductivity = bulk_saturated_conductivity, - }; -#endif + saxton2006_gravel_result result = {gravel_volume_fraction, bulk_density, + bulk_plant_available_water, bulk_saturated_conductivity}; + return result; } /** @@ -367,14 +337,8 @@ static inline saxton2006_salinity_result calc_ptf_saxton2006_salinity(double electrical_conductivity, double theta, double theta_s) { const double saturated_osmotic_potential = 36.0 * electrical_conductivity; const double osmotic_potential = theta_s / theta * saturated_osmotic_potential; -#ifdef __cplusplus - return saxton2006_salinity_result{saturated_osmotic_potential, osmotic_potential}; -#else - return (saxton2006_salinity_result){ - .saturated_osmotic_potential = saturated_osmotic_potential, - .osmotic_potential = osmotic_potential, - }; -#endif + saxton2006_salinity_result result = {saturated_osmotic_potential, osmotic_potential}; + return result; } #endif diff --git a/targets/ptfkit-native/include/ptfkit/tiwary2014.h b/targets/ptfkit-native/include/ptfkit/tiwary2014.h index 3bfce6a..2953311 100644 --- a/targets/ptfkit-native/include/ptfkit/tiwary2014.h +++ b/targets/ptfkit-native/include/ptfkit/tiwary2014.h @@ -83,16 +83,8 @@ static inline tiwary2014_ptf_result calc_ptf_tiwary2014_bsr(double clay, double const double w_1500 = 0.541 + 0.306 * cation_exchange_capacity + 0.146 * esp + 0.058 * emp; const double k_sat_mm_per_hour = 120.637 - 13.094 * ph - 0.102 * clay + 1.151 * excm; const double k_sat = k_sat_mm_per_hour / 3600000.0; -#ifdef __cplusplus - return tiwary2014_ptf_result{w_33, w_100, w_1500, k_sat}; -#else - return (tiwary2014_ptf_result){ - .w_33 = w_33, - .w_100 = w_100, - .w_1500 = w_1500, - .k_sat = k_sat, - }; -#endif + tiwary2014_ptf_result result = {w_33, w_100, w_1500, k_sat}; + return result; } #endif diff --git a/targets/ptfkit-native/include/ptfkit/varallyai1982.h b/targets/ptfkit-native/include/ptfkit/varallyai1982.h index 5a7545e..2b5f45f 100644 --- a/targets/ptfkit-native/include/ptfkit/varallyai1982.h +++ b/targets/ptfkit-native/include/ptfkit/varallyai1982.h @@ -69,15 +69,8 @@ static inline varallyai1982_parameters calc_ptf_varallyai1982_meadow(double bulk 1.434 * fine_sand_fraction * fine_fraction + 0.156; const double pf_star = -1.702 * fine_sand_fraction + 1.103 * bulk_density * fine_fraction + 3.749; -#ifdef __cplusplus - return varallyai1982_parameters{theta_0, m, pf_star}; -#else - return (varallyai1982_parameters){ - .theta_0 = theta_0, - .m = m, - .pf_star = pf_star, - }; -#endif + varallyai1982_parameters result = {theta_0, m, pf_star}; + return result; } /** @@ -108,15 +101,8 @@ static inline varallyai1982_parameters calc_ptf_varallyai1982_chernozem_a(double const double theta_0 = -56.40 * bulk_density + 20.50 * fine_fraction + 123.79; const double m = 0.336 * bulk_density - 0.053; const double pf_star = 4.701 * bulk_density * fine_fraction + 1.513 * bulk_density - 0.417; -#ifdef __cplusplus - return varallyai1982_parameters{theta_0, m, pf_star}; -#else - return (varallyai1982_parameters){ - .theta_0 = theta_0, - .m = m, - .pf_star = pf_star, - }; -#endif + varallyai1982_parameters result = {theta_0, m, pf_star}; + return result; } /** @@ -147,15 +133,8 @@ static inline varallyai1982_parameters calc_ptf_varallyai1982_chernozem_b(double const double theta_0 = -62.20 * bulk_density - 49.14 * (fine_fraction * fine_fraction) + 140.70; const double m = 0.635 * bulk_density - 0.482; const double pf_star = 4.270 * bulk_density * fine_fraction + 3.509 * bulk_density - 3.075; -#ifdef __cplusplus - return varallyai1982_parameters{theta_0, m, pf_star}; -#else - return (varallyai1982_parameters){ - .theta_0 = theta_0, - .m = m, - .pf_star = pf_star, - }; -#endif + varallyai1982_parameters result = {theta_0, m, pf_star}; + return result; } /** @@ -186,15 +165,8 @@ static inline varallyai1982_parameters calc_ptf_varallyai1982_chernozem_c(double const double m = 0.439 * bulk_density * fine_fraction + 0.625; const double pf_star = 3.268 * bulk_density * fine_fraction + 0.865 * (bulk_density * bulk_density) + 0.301; -#ifdef __cplusplus - return varallyai1982_parameters{theta_0, m, pf_star}; -#else - return (varallyai1982_parameters){ - .theta_0 = theta_0, - .m = m, - .pf_star = pf_star, - }; -#endif + varallyai1982_parameters result = {theta_0, m, pf_star}; + return result; } #endif diff --git a/targets/ptfkit-native/include/ptfkit/vereecken1989.h b/targets/ptfkit-native/include/ptfkit/vereecken1989.h index 514ee49..917a860 100644 --- a/targets/ptfkit-native/include/ptfkit/vereecken1989.h +++ b/targets/ptfkit-native/include/ptfkit/vereecken1989.h @@ -93,16 +93,8 @@ static inline vereecken1989_ptf_result calc_ptf_vereecken1989(double sand, doubl const double alpha = exp(ln_alpha); const double ln_n = 0.053 - 0.009 * sand - 0.013 * clay + 0.00015 * sand * sand; const double n = exp(ln_n); -#ifdef __cplusplus - return vereecken1989_ptf_result{theta_r, theta_s, alpha, n}; -#else - return (vereecken1989_ptf_result){ - .theta_r = theta_r, - .theta_s = theta_s, - .alpha = alpha, - .n = n, - }; -#endif + vereecken1989_ptf_result result = {theta_r, theta_s, alpha, n}; + return result; } /** @@ -167,16 +159,8 @@ static inline vereecken1989_detailed_ptf_result calc_ptf_vereecken1989_detailed( 0.007 * particle_50_20 + 0.0300 * particle_20_10 - 0.0380 * particle_10_2 - 0.0042 * clay + 1.0322 * geometric_mean_particle_size - 0.0019 * geometric_standard_deviation; const double n = exp(ln_n); -#ifdef __cplusplus - return vereecken1989_detailed_ptf_result{theta_r, theta_s, alpha, n}; -#else - return (vereecken1989_detailed_ptf_result){ - .theta_r = theta_r, - .theta_s = theta_s, - .alpha = alpha, - .n = n, - }; -#endif + vereecken1989_detailed_ptf_result result = {theta_r, theta_s, alpha, n}; + return result; } #endif diff --git a/targets/ptfkit-native/include/ptfkit/wang2012.h b/targets/ptfkit-native/include/ptfkit/wang2012.h index 371d56d..1b7d1b8 100644 --- a/targets/ptfkit-native/include/ptfkit/wang2012.h +++ b/targets/ptfkit-native/include/ptfkit/wang2012.h @@ -85,15 +85,8 @@ static inline wang2012_ptf_result calc_ptf_wang2012(double sand, double silt, do const double theta_s = sswc_percent / 100.0; const double theta_fc = fc_percent / 100.0; const double k_sat = k_sat_cm_per_day / 8640000.0; -#ifdef __cplusplus - return wang2012_ptf_result{theta_s, theta_fc, k_sat}; -#else - return (wang2012_ptf_result){ - .theta_s = theta_s, - .theta_fc = theta_fc, - .k_sat = k_sat, - }; -#endif + wang2012_ptf_result result = {theta_s, theta_fc, k_sat}; + return result; } #endif diff --git a/targets/ptfkit-native/include/ptfkit/weber2020.h b/targets/ptfkit-native/include/ptfkit/weber2020.h index 9944ac6..ac0e450 100644 --- a/targets/ptfkit-native/include/ptfkit/weber2020.h +++ b/targets/ptfkit-native/include/ptfkit/weber2020.h @@ -96,20 +96,9 @@ static inline weber2020_ptf_result calc_ptf_weber2020(double theta_r_vgm, double const double tau_bw = 2.95e-2 + 1.833 * tau_vgm_constrained; const double k_sc_bw = pow(10.0, 1.16e-1 + 1.060 * log10(k_s_vgm)); const double k_snc_bw = pow(10.0, -1.72); -#ifdef __cplusplus - return weber2020_ptf_result{theta_snc_bw, theta_sc_bw, alpha_bw, n_bw, - tau_bw, k_sc_bw, k_snc_bw}; -#else - return (weber2020_ptf_result){ - .theta_snc_bw = theta_snc_bw, - .theta_sc_bw = theta_sc_bw, - .alpha_bw = alpha_bw, - .n_bw = n_bw, - .tau_bw = tau_bw, - .k_sc_bw = k_sc_bw, - .k_snc_bw = k_snc_bw, - }; -#endif + weber2020_ptf_result result = {theta_snc_bw, theta_sc_bw, alpha_bw, n_bw, + tau_bw, k_sc_bw, k_snc_bw}; + return result; } #endif diff --git a/targets/ptfkit-native/tests/CMakeLists.txt b/targets/ptfkit-native/tests/CMakeLists.txt index a161c57..3828ee8 100644 --- a/targets/ptfkit-native/tests/CMakeLists.txt +++ b/targets/ptfkit-native/tests/CMakeLists.txt @@ -3,7 +3,7 @@ function(ptfkit_add_native_tests case) PARSE_ARGV 1 arg "" "" - "SOURCES;LIBRARIES;DEFINITIONS;PROPERTIES" + "SOURCES;LIBRARIES;DEFINITIONS;PROPERTIES;COMPILE_FEATURES" ) foreach(source ${arg_SOURCES}) get_filename_component(name ${source} NAME_WE) @@ -16,6 +16,9 @@ function(ptfkit_add_native_tests case) if(arg_PROPERTIES) set_target_properties(${target} PROPERTIES ${arg_PROPERTIES}) endif() + if(arg_COMPILE_FEATURES) + target_compile_features(${target} PRIVATE ${arg_COMPILE_FEATURES}) + endif() add_test(NAME ${target} COMMAND ${target}) endforeach() endfunction() @@ -24,6 +27,13 @@ include_directories(${CMAKE_CURRENT_SOURCE_DIR}) add_subdirectory(c) +include(CheckLanguage) +check_language(CXX) +if(CMAKE_CXX_COMPILER) + enable_language(CXX) + add_subdirectory(cpp_fallback) +endif() + if(PTFKIT_BUILD_CPP_MODULES) add_subdirectory(cpp) endif() diff --git a/targets/ptfkit-native/tests/c/CMakeLists.txt b/targets/ptfkit-native/tests/c/CMakeLists.txt index 93c0118..521371b 100644 --- a/targets/ptfkit-native/tests/c/CMakeLists.txt +++ b/targets/ptfkit-native/tests/c/CMakeLists.txt @@ -2,5 +2,5 @@ file(GLOB sources CONFIGURE_DEPENDS *.c) ptfkit_add_native_tests(c SOURCES ${sources} - LIBRARIES ptfkit::c m + LIBRARIES ptfkit::c ) diff --git a/targets/ptfkit-native/tests/close_enough.h b/targets/ptfkit-native/tests/close_enough.h index 87c2ce7..d542ecb 100644 --- a/targets/ptfkit-native/tests/close_enough.h +++ b/targets/ptfkit-native/tests/close_enough.h @@ -35,19 +35,4 @@ static inline void _close_enough_impl(const char *file, int line, double actual, _close_enough_impl(__FILE__, __LINE__, (actual), (expected), (atol), (rtol)); \ } while (0) -// #define assert_close_enough(actual, expected, atol, rtol) \ -// do { \ -// if (!(close_enough(actual, expected, atol, rtol))) { \ -// fprintf( \ -// stderr, \ -// "asserion failed: %s:%d: %s ≈ %\n", \ -// __FILE__, \ -// __LINE__, \ -// #actual, \ -// #expected \ -// ); \ -// return 1; \ -// } \ -// } while (0) - -#endif +#endif // PTFKIT_TEST_CLOSE_ENOUGH_H diff --git a/targets/ptfkit-native/tests/cpp_fallback/CMakeLists.txt b/targets/ptfkit-native/tests/cpp_fallback/CMakeLists.txt new file mode 100644 index 0000000..b409785 --- /dev/null +++ b/targets/ptfkit-native/tests/cpp_fallback/CMakeLists.txt @@ -0,0 +1,12 @@ +file(GLOB sources CONFIGURE_DEPENDS ../c/*.c) + +set_source_files_properties( + ${sources} + PROPERTIES LANGUAGE CXX +) + +ptfkit_add_native_tests(cpp_fallback + SOURCES ${sources} + LIBRARIES ptfkit::c + COMPILE_FEATURES cxx_std_23 +)