| | 139 | |
|---|
| | 140 | PyArrayObject *array = (PyArrayObject*) pObj; |
|---|
| | 141 | |
|---|
| | 142 | |
|---|
| | 143 | // is it 1-D |
|---|
| | 144 | |
|---|
| | 145 | if (array->nd==1) { |
|---|
| | 146 | |
|---|
| | 147 | int i,size = array->dimensions[0]; |
|---|
| | 148 | double *p; |
|---|
| | 149 | |
|---|
| | 150 | Symbol* s = hoc_decl_name(name); |
|---|
| | 151 | if (s->type == UNDEF) { |
|---|
| | 152 | // type==UNDEF means the VAR was just newly defined |
|---|
| | 153 | // make it a VAR |
|---|
| | 154 | hoc_obvar_declare(s, VAR, 0); |
|---|
| | 155 | |
|---|
| | 156 | // it is not an array |
|---|
| | 157 | s->arayinfo = (Arrayinfo *)0; |
|---|
| | 158 | |
|---|
| | 159 | // get hoc to allocate array for data |
|---|
| | 160 | // sub[0] |
|---|
| | 161 | hoc_pushx(size); |
|---|
| | 162 | // symbol |
|---|
| | 163 | hoc_pushs(s); |
|---|
| | 164 | // nsub |
|---|
| | 165 | hoc_pushx(1.0); |
|---|
| | 166 | hoc_arayinstal(); |
|---|
| | 167 | |
|---|
| | 168 | // set VALs (memory already allocated) |
|---|
| | 169 | |
|---|
| | 170 | p = OPVAL(s); |
|---|
| | 171 | |
|---|
| | 172 | for (i=0;i<size;i++) { |
|---|
| | 173 | p[i] = *(double*)(array->data+i*array->strides[0]); |
|---|
| | 174 | } |
|---|
| | 175 | |
|---|
| | 176 | |
|---|
| | 177 | }else if (s->type == VAR) { |
|---|
| | 178 | // this below should work for previous array or non-array |
|---|
| | 179 | // sub[0] |
|---|
| | 180 | hoc_pushx(size); |
|---|
| | 181 | // symbol |
|---|
| | 182 | hoc_pushs(s); |
|---|
| | 183 | // nsub |
|---|
| | 184 | hoc_pushx(1.0); |
|---|
| | 185 | hoc_arayinstal(); |
|---|
| | 186 | |
|---|
| | 187 | // set VALs (memory already allocated) |
|---|
| 136 | | // is the destination an OBJREF or a double? |
|---|
| 137 | | |
|---|
| 138 | | printf("OBJREF unimplemented.\n"); |
|---|
| | 189 | |
|---|
| | 190 | p = OPVAL(s); |
|---|
| | 191 | |
|---|
| | 192 | for (i=0;i<size;i++) { |
|---|
| | 193 | p[i] = *(double*)(array->data+i*array->strides[0]); |
|---|
| | 194 | } |
|---|
| | 195 | |
|---|
| | 196 | |
|---|
| | 197 | } |
|---|
| | 198 | else if (s->type == OBJECTVAR) { |
|---|
| | 199 | // write 1-D numpy array as a Vector |
|---|
| | 200 | |
|---|
| | 201 | |
|---|
| | 202 | if (!is_obj_type(*OPOBJ(s),"Vector")) { |
|---|
| | 203 | |
|---|
| | 204 | // OBJECTVAR is not a vector so lets make it one |
|---|
| | 205 | |
|---|
| | 206 | // construct command to send to hoc to allocate Vector of desired size |
|---|
| | 207 | std::stringstream ss; |
|---|
| | 208 | ss << name << " = new Vector(" << size << ")"; |
|---|
| | 209 | |
|---|
| | 210 | |
|---|
| | 211 | if (!Oc::valid_stmt((const char*)ss.str().c_str(), 0)) { |
|---|
| | 212 | PyErr_SetString(PyExc_RuntimeError,"hoc.set error while allocating new Vector"); |
|---|
| | 213 | return NULL; |
|---|
| | 214 | |
|---|
| | 215 | } |
|---|
| | 216 | |
|---|
| | 217 | s = hoc_decl_name(name); |
|---|
| | 218 | |
|---|
| | 219 | if (!s->type==OBJECTVAR) { |
|---|
| | 220 | PyErr_SetString(PyExc_RuntimeError,"hoc.set error: newly allocated Vector corrupt."); |
|---|
| | 221 | return NULL; |
|---|
| | 222 | } |
|---|
| | 223 | |
|---|
| | 224 | } |
|---|
| | 225 | |
|---|
| | 226 | // fill vector with array data |
|---|
| | 227 | |
|---|
| | 228 | Vect* v = (Vect*)OPOBJ(s)[0]->u.this_pointer; |
|---|
| | 229 | v->resize(size); |
|---|
| | 230 | |
|---|
| | 231 | for (i=0;i<size;i++) { |
|---|
| | 232 | (*v)[i] = *(double*)(array->data + i*array->strides[0]) ; |
|---|
| | 233 | } |
|---|
| | 234 | |
|---|
| | 235 | |
|---|
| | 236 | |
|---|
| | 237 | } |
|---|
| | 238 | else { |
|---|
| | 239 | PyErr_SetString(PyExc_RuntimeError,"hoc.set var already defined and not a double or Vector."); |
|---|
| | 240 | return NULL; |
|---|
| | 241 | } |
|---|
| | 242 | |
|---|
| | 243 | |
|---|
| | 244 | |
|---|
| | 245 | } |
|---|
| | 246 | |
|---|
| | 247 | else { |
|---|
| | 248 | assert(array->nd>1); |
|---|
| | 249 | |
|---|
| | 250 | |
|---|
| | 251 | // is the destination an OBJREF or a double? |
|---|
| | 252 | |
|---|
| | 253 | printf("OBJREF unimplemented.\n"); |
|---|
| | 254 | |
|---|
| | 255 | |
|---|
| | 256 | } |
|---|