diff --git a/demos/struct/Exists_GetForced.geo b/demos/struct/Exists_GetForced.geo new file mode 100644 index 0000000000000000000000000000000000000000..1d586b5a82bad316cc2255aa73a47754e36d8e2c --- /dev/null +++ b/demos/struct/Exists_GetForced.geo @@ -0,0 +1,55 @@ +// +// Functions: Exists(.) and GetForced(.) +// (for both Gmsh and GetDP) +// + +val_real = 12; + +list_of_real() = {11 ,22, 33}; + +Struct namespace::struct_name [ Dim 2, Ref 5 ]; // See file struct.geo for Struct definitions + +// Look at the Current Workspace for checking the variables contents. + +// +// Function Exists(name_variable): +// returns 1 if variable 'name_variable' exists, 0 if not + +If (Exists(val_real)) + Printf("Variable 'val_real' exists"); +EndIf + +If (!Exists(other_val_real)) + Printf("Variable 'other_val_real' does not exist"); +EndIf + +If (Exists(list_of_real)) + Printf("List 'list_of_real()' exists"); +EndIf + +If (Exists(namespace::struct_name.Dim)) + Printf("Member 'Dim' of structure 'namespace::struct_name' exists"); +EndIf + +If (!Exists(namespace::struct_name.Thickness)) + Printf("Member 'Thickness' of structure 'namespace::struct_name' does not exist"); + Struct namespace::struct_name (Append) [ Thickness 0.01 ]; // This member is added to the structure + If (Exists(namespace::struct_name.Thickness)) + Printf("Member 'Thickness' of structure 'namespace::struct_name' now exists"); + EndIf +EndIf + +// +// GetForced(name_variable, default_value): +// returns value of 'name_variable' if it exists, default_value if not +// GetForced(name_variable): +// returns value of 'name_variable' if it exists, 0 (zero) if not + +get_forced_val_real = GetForced(val_real, 1); // Will be 12, the value of the existing variable + +get_forced_other_val_real_with_defined_default_value = GetForced(other_val_real, 1); // Will be 1 (defined default value) because variable does not exist +get_forced_other_val_real_with_zero_default_value = GetForced(other_val_real); // Will be 0 (default value) because variable does not exist + +get_forced_struct_member_with_default = GetForced(namespace::struct_name.InexistingMember, -1); // Will be the defined default value -1 + +// diff --git a/demos/struct/struct.geo b/demos/struct/struct.geo new file mode 100644 index 0000000000000000000000000000000000000000..9874471a4b121e267efe02cbf101d8ab308afa9c --- /dev/null +++ b/demos/struct/struct.geo @@ -0,0 +1,97 @@ +// +// DATA STRUCTURES (for both Gmsh and GetDP) +// + +// A data structure is a group of data elements grouped together under one name, +// the structure identifier. These data elements, defining the structure members, +// can be of different types: real, string, list of real. +// The syntax should be clear from the example below: + +Struct struct_identifier [ + struct_member_real_1 11., + struct_member_real_2 22., + struct_member_string_1 "string1", + struct_member_string_2 "string2", + struct_member_list_of_real_1 { 111., 222., 333. } +]; + +// Look at the Current Workspace for checking the structure content. +// You will see there that a structure is automatically given a (real) 'Tag' member, +// starting at 1 and incremented by 1 for each new structure. +// When given in an RHS expression, the Struct definition returns its 'Tag', +// that can also be explicitly given: + +Struct St1 [ Type 1 ]; // Tag will be 2 = 1 (the one of last Struct) + 1 +Struct St2 [ Type 2, Tag 10 ]; // Tag is forced to 10 +tag_of_struct_St3 = Struct St3 [ Type 3 ]; // Tag will be 11 = 10 + 1 + +// For clear classifications (contexts), structures can be defined in a +// given namespace (other than the global one used until now). +// Their names then start with the namespace name followed by the +// scope operator '::'. The 'Tag' numbering is proper to each namespace. E.g., + +Struct NS1::St1 [ Type 1 ]; // Tag will be 1 (for the 1st structure in namespace 'NS1') +Struct NS1::St2 [ Type 2 ]; +Struct NS1::St3 [ Type 3 ]; + +// To access the value of a member of a structure, use the dot '.' operator +// between the structure name (with possible namespace) and the member name: + +val_Type_of_Struct_St2 = St2.Type; +val_Type_of_Struct_St2_in_NS1 = NS1::St2.Type; + +an_element_from_a_list = struct_identifier.struct_member_list_of_real_1(1); + +// The function DimNameSpace(.) returns the number of structures in a given namespace: + +nb_struct_namespace_global = DimNameSpace(); +nb_struct_namespace_NS1 = DimNameSpace(NS1); + +// The function NameStruct(namespace::#index) +// (or NameStruct(#index) for the global namespace) +// returns the name (as a string) of the index-th structures in the given namespace: + +name_of_struct_2_in_namespace_global = NameStruct(#2); +name_of_struct_2_in_namespace_NS1 = NameStruct[NS1::#2]; + +// Thanks to these two functions, and the S2N[.] function ('StringToName'), +// one can make loops on structures of a given namespace and +// access their members values: + +For i In {1:DimNameSpace(NS1)} + id_NS1~{i} = NameStruct[NS1::#i]; // Gets the identifier of the i-th structure in NS1 + val_Type_of_NS1_struct~{i} = NS1::S2N[id_NS1~{i}].Type; // Gets the value of member Type + // Prints all that + Printf(Sprintf[StrCat["id of %g-th structure in NS1 = '", id_NS1~{i}, "'"], i]); + Printf("Value of member 'Type' of %g-th structure in NS1 = %g", + i, val_Type_of_NS1_struct~{i}); +EndFor + +// Some additional members can be appended to an existing structure, +// with '(Append)' following the structure name: + +Struct NS1::St2 (Append) [ AdditionalMember 222 ]; +Struct NS1::St3 (Append) [ AdditionalMember 333, HColor "Orange" ]; + +// A structure can be used to define an enumeration, to give automatically +// incremented values to the members (by default, starting at 0, or at any fixed value). +// This is useful, e.g., for defining constants to be used for types (hidding the values): + +Struct T::REGION_TYPE [ Enum, NONE, PHYS, SKIN, GATE, BC ]; +// Automatic values will be: NONE 0, PHYS 1, SKIN 2, GATE 3, BC 4 + +Struct T::REGION_TYPE_2 [ Enum, PHYS 10, SKIN, GATE 20, BC ]; +// Automatic values will be: PHYS 10, SKIN 11, GATE 20, BC 21 + +// Add members with Append: +Struct T::REGION_TYPE (Append) [ Enum, CUTBOX ]; // CUTBOX will be 5 + +// Using explicit name for constants: +myType = T::REGION_TYPE.PHYS; +If (myType == T::REGION_TYPE.PHYS) + // ... +EndIf + + +// You can now play with all that! +//