MOOCHO (Single Doxygen Collection)  Version of the Day
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
RTOp_TOp_force_in_bounds.c
Go to the documentation of this file.
1 /*
2 // @HEADER
3 // ***********************************************************************
4 //
5 // Moocho: Multi-functional Object-Oriented arCHitecture for Optimization
6 // Copyright (2003) Sandia Corporation
7 //
8 // Under terms of Contract DE-AC04-94AL85000, there is a non-exclusive
9 // license for use of this work by or on behalf of the U.S. Government.
10 //
11 // Redistribution and use in source and binary forms, with or without
12 // modification, are permitted provided that the following conditions are
13 // met:
14 //
15 // 1. Redistributions of source code must retain the above copyright
16 // notice, this list of conditions and the following disclaimer.
17 //
18 // 2. Redistributions in binary form must reproduce the above copyright
19 // notice, this list of conditions and the following disclaimer in the
20 // documentation and/or other materials provided with the distribution.
21 //
22 // 3. Neither the name of the Corporation nor the names of the
23 // contributors may be used to endorse or promote products derived from
24 // this software without specific prior written permission.
25 //
26 // THIS SOFTWARE IS PROVIDED BY SANDIA CORPORATION "AS IS" AND ANY
27 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
29 // PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SANDIA CORPORATION OR THE
30 // CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
31 // EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
32 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
33 // PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
34 // LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
35 // NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
36 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
37 //
38 // Questions? Contact Roscoe A. Bartlett (rabartl@sandia.gov)
39 //
40 // ***********************************************************************
41 // @HEADER
42 */
43 
45 #include "RTOp_obj_null_vtbl.h"
46 #include "RTOp_obj_value_value_vtbl.h" /* vtbl for operator object instance data */
47 
48 #define max(a,b) ( (a) > (b) ? (a) : (b) )
49 #define min(a,b) ( (a) < (b) ? (a) : (b) )
50 
51 /* Implementation functions for RTOp_RTOp TOp_force_in_bounds */
52 
54  const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data
55  , const int num_vecs, const struct RTOp_SubVector vecs[]
56  , const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[]
57  , RTOp_ReductTarget targ_obj )
58 {
59  RTOp_index_type sub_dim;
60  RTOp_value_type *x_val;
61  ptrdiff_t x_val_s;
62  const RTOp_value_type *xl_val;
63  ptrdiff_t xl_val_s;
64  const RTOp_value_type *xu_val;
65  ptrdiff_t xu_val_s;
66  register RTOp_index_type k;
67 
68  /* */
69  /* Validate the input */
70  /* */
71  if( num_vecs != 2 || vecs == NULL )
73  if( num_targ_vecs != 1 || targ_vecs == NULL )
75  if( targ_vecs[0].sub_dim != vecs[0].sub_dim
76  || targ_vecs[0].sub_dim != vecs[1].sub_dim )
78 
79  /* */
80  /* Get pointers to data */
81  /* */
82 
83  /* x */
84  sub_dim = targ_vecs[0].sub_dim;
85  x_val = targ_vecs[0].values;
86  x_val_s = targ_vecs[0].values_stride;
87  /* xl */
88  xl_val = vecs[0].values;
89  xl_val_s = vecs[0].values_stride;
90  /* xu */
91  xu_val = vecs[1].values;
92  xu_val_s = vecs[1].values_stride;
93 
94  /* */
95  /* Force in bounds */
96  /* */
97 
98  if( x_val_s == 1 && xl_val_s == 1 && xu_val_s == 1 ) {
99  /* Slightly faster loop for unit stride vectors */
100  for( k = 0; k < sub_dim; ++k, ++x_val, ++xl_val, ++xu_val ) {
101  if( *x_val < *xl_val )
102  *x_val = *xl_val;
103  else if( *x_val > *xu_val )
104  *x_val = *xu_val;
105  }
106  }
107  else {
108  /* More general implementation for non-unit strides */
109  for( k = 0; k < sub_dim; ++k, x_val+=x_val_s, xl_val+=xl_val_s, xu_val+=xu_val_s ) {
110  if( *x_val < *xl_val )
111  *x_val = *xl_val;
112  else if( *x_val > *xu_val )
113  *x_val = *xu_val;
114  }
115  }
116 
117  return 0; /* success? */
118 }
119 
120 /* Virtual function table */
122 {
123  &RTOp_obj_null_vtbl /* Use a null object for instance data */
124  ,&RTOp_obj_null_vtbl /* use null type for target object */
125  ,"TOp_force_in_bounds"
126  ,NULL /* use default from reduct_vtbl */
128  ,NULL
129  ,NULL
130 };
131 
132 /* Class specific functions */
133 
135 {
136  op->obj_data = NULL;
138  return 0; /* success? */
139 }
140 
142 {
143  op->obj_data = NULL;
144  op->vtbl = NULL;
145  return 0; /* success? */
146 }
147 
148 /* Implementation functions for RTOp_RTOp for TOp_force_in_bounds_buffer */
149 
151  const struct RTOp_RTOp_vtbl_t* vtbl, const void* obj_data
152  , const int num_vecs, const struct RTOp_SubVector vecs[]
153  , const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[]
154  , RTOp_ReductTarget reduct_obj )
155 {
156  /* */
157  /* Declare local variables */
158  /* */
159 
160  /* Access to the operator object instance data */
161  struct RTOp_value_value_type *data_cntr = (struct RTOp_value_value_type*)obj_data;
162  RTOp_value_type *rel_push = &data_cntr->value1;
163  RTOp_value_type *abs_push = &data_cntr->value2;
164  /* Vector data */
165  RTOp_index_type sub_dim;
166  /* z0 */
167  RTOp_value_type *z0_val;
168  ptrdiff_t z0_val_s;
169  /* v0 */
170  const RTOp_value_type *v0_val;
171  ptrdiff_t v0_val_s;
172  /* v1 */
173  const RTOp_value_type *v1_val;
174  ptrdiff_t v1_val_s;
175 
176  /* Automatic temporary variables */
177  register RTOp_index_type k;
178  /* User defined temporary variables */
179  RTOp_value_type xl_sb;
180  RTOp_value_type xu_sb;
181 
182  /* */
183  /* Validate the input */
184  /* */
185  if( num_vecs != 2 || ( num_vecs && vecs == NULL ) )
187  if( num_targ_vecs != 1 || ( num_targ_vecs && targ_vecs == NULL ) )
189  if( /* Validate sub_dim */
190  vecs[1].sub_dim != vecs[0].sub_dim
191  || targ_vecs[0].sub_dim != vecs[0].sub_dim
192  )
194  assert(obj_data);
195 
196 
197  /* */
198  /* Get pointers to data */
199  /* */
200  sub_dim = vecs[0].sub_dim;
201  /* z0 */
202  z0_val = targ_vecs[0].values;
203  z0_val_s = targ_vecs[0].values_stride;
204  /* v0 */
205  v0_val = vecs[0].values;
206  v0_val_s = vecs[0].values_stride;
207  /* v1 */
208  v1_val = vecs[1].values;
209  v1_val_s = vecs[1].values_stride;
210 
211 
212  /* */
213  /* Apply the operator: */
214  /* */
215  for( k = 0; k < sub_dim; ++k, v0_val += v0_val_s, v1_val += v1_val_s, z0_val += z0_val_s )
216  {
217  /* Element-wise transformation */
218  xl_sb = min((*v0_val) + (*rel_push)*((*v1_val)-(*v0_val)), (*v0_val) + (*abs_push));
219  xu_sb = max((*v1_val) - (*rel_push)*((*v1_val)-(*v0_val)), (*v1_val) - (*abs_push));
220  if (xl_sb >= xu_sb)
221  {
222  (*z0_val) = (*v0_val) + ((*v1_val)-(*v0_val))/2.0;
223  }
224  else if ((*z0_val) < xl_sb)
225  { (*z0_val) = xl_sb; }
226  else if ((*z0_val) > xu_sb)
227  { (*z0_val) = xu_sb; }
228  /* Otherwise, leave it */
229  }
230 
231  return 0; /* success? */
232 }
233 
234 /* Virtual function table */
236 {
239  ,"TOp_force_in_bounds_buffer"
240  ,NULL
242  ,NULL
243  ,NULL
244 };
245 
246 /* Class specific functions */
247 
249 {
250 #ifdef RTOp_DEBUG
251  assert(op);
252 #endif
253  op->obj_data = NULL;
255  op->vtbl->obj_data_vtbl->obj_create(NULL,NULL,&op->obj_data);
256  return RTOp_TOp_force_in_bounds_buffer_init(rel_push,abs_push,op);
257 }
258 
260 {
261  op->vtbl->obj_data_vtbl->obj_free(NULL,NULL,&op->obj_data);
262  op->obj_data = NULL;
263  op->vtbl = NULL;
264  return 0;
265 }
266 
268 {
269  struct RTOp_value_value_type *ptr_data_cntr = (struct RTOp_value_value_type*)op->obj_data;
270  RTOp_value_type *ptr_rel_push = &ptr_data_cntr->value1;
271  RTOp_value_type *ptr_abs_push = &ptr_data_cntr->value2;
272  *ptr_rel_push = rel_push;
273  *ptr_abs_push = abs_push;
274  return 0;
275 }
276 
static int RTOp_TOp_force_in_bounds_apply_op(const struct RTOp_RTOp_vtbl_t *vtbl, const void *obj_data, const int num_vecs, const struct RTOp_SubVector vecs[], const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[], RTOp_ReductTarget targ_obj)
ptrdiff_t values_stride
Definition: RTOp.h:305
double RTOp_value_type
Definition: RTOp.h:69
ptrdiff_t values_stride
Definition: RTOp.h:338
void * RTOp_ReductTarget
Definition: RTOp.h:191
int(* obj_create)(const struct RTOp_obj_type_vtbl_t *vtbl, const void *instance_data, void **obj)
Definition: RTOp.h:941
const struct RTOp_obj_type_vtbl_t RTOp_obj_value_value_vtbl
const struct RTOp_obj_type_vtbl_t * obj_data_vtbl
Definition: RTOp.h:819
int RTOp_TOp_force_in_bounds_buffer_init(RTOp_value_type rel_push, RTOp_value_type abs_push, struct RTOp_RTOp *op)
const struct RTOp_RTOp_vtbl_t RTOp_TOp_force_in_bounds_buffer_vtbl
#define RTOp_ERR_INCOMPATIBLE_VECS
Definition: RTOp.h:270
#define max(a, b)
const struct RTOp_RTOp_vtbl_t RTOp_TOp_force_in_bounds_vtbl
const RTOp_value_type * values
Definition: RTOp.h:303
RTOp_index_type sub_dim
Definition: RTOp.h:334
#define RTOp_ERR_INVALID_NUM_VECS
Definition: RTOp.h:266
int(* obj_free)(const struct RTOp_obj_type_vtbl_t *vtbl, const void *instance_data, void **obj)
Definition: RTOp.h:1019
int RTOp_TOp_force_in_bounds_destroy(struct RTOp_RTOp *op)
RTOp_value_type * values
Definition: RTOp.h:336
const struct RTOp_obj_type_vtbl_t RTOp_obj_null_vtbl
#define min(a, b)
void * obj_data
Definition: RTOp.h:800
const struct RTOp_RTOp_vtbl_t * vtbl
Definition: RTOp.h:802
static int RTOp_TOp_force_in_bounds_buffer_apply_op(const struct RTOp_RTOp_vtbl_t *vtbl, const void *obj_data, const int num_vecs, const struct RTOp_SubVector vecs[], const int num_targ_vecs, const struct RTOp_MutableSubVector targ_vecs[], RTOp_ReductTarget reduct_obj)
int RTOp_TOp_force_in_bounds_buffer_construct(RTOp_value_type rel_push, RTOp_value_type abs_push, struct RTOp_RTOp *op)
RTOp_index_type sub_dim
Definition: RTOp.h:301
#define RTOp_ERR_INVALID_NUM_TARG_VECS
Definition: RTOp.h:268
int RTOp_TOp_force_in_bounds_buffer_destroy(struct RTOp_RTOp *op)
int RTOp_TOp_force_in_bounds_construct(struct RTOp_RTOp *op)
Teuchos_Ordinal RTOp_index_type
Definition: RTOp.h:68