/* Create the system admittance matrix from power system data. Note that this function is called transparently from GetPSFromFile once power system data has been populated (if the file read is successful). */ int CreateYBus(PS ps, int *ids){ int i, from, to, num_buses = ps->num_buses, num_branches = ps->num_branches, nnz = num_buses + 2*num_branches; double r, x, b; double _Complex y, *diags = malloc(num_buses*sizeof(double _Complex)); //allocate a ybus cs_ci *ybus = cs_ci_spalloc(num_buses, num_buses, nnz, 1, 1), *ybus_comp, *ybus_comp_t; //check memory if(!ybus) return 1; //loop through ybus branches for(i=0; i<num_branches; i++) { //get branch from/to bus indices from = ps->branches[i].from_index; to = ps->branches[i].to_index; //determine line admittance r = ps->branches[i].r; x = ps->branches[i].x; b = ps->branches[i].b; y = (1.0+0.0*I)/(r+x*I); //add off-diagonals to ybus and increment diagonal entries cs_ci_entry(ybus, from, to, -y); cs_ci_entry(ybus, to, from, -y); diags[from]+=(y+I*b/2.0); diags[to]+=(y+I*b/2.0); } //add diogonal entries to ybus. for(i=0; i<num_buses; i++) cs_ci_entry(ybus, i, i, diags[i]); //compress ybus ybus_comp=cs_ci_compress(ybus); //check that memory allocation was okay if(!ybus_comp){cs_ci_spfree(ybus); return 1;} //free the old ybus and sum duplicates in the compressed matrix (i.e., parallel branches) cs_ci_spfree(ybus); cs_ci_dupl(ybus_comp); //sort the columns of ybus using the transpose-transpose trick ybus_comp_t = cs_ci_transpose(ybus_comp, 1); if(!ybus_comp_t){free(ybus_comp); return 1;} free(ybus_comp); ybus_comp = cs_ci_transpose(ybus_comp_t, 1); if(!ybus_comp){free(ybus_comp_t); return 1;} free(ybus_comp_t); //assign the ps ybus pointer to the compressed, summed ybus matrix ps->ybus = ybus_comp; //free the diagonal workspace and return free(diags); return 0; }
/* read a problem from a file */ problem *get_problem (FILE *f, double tol) { cs_ci *T, *A, *C ; int sym, m, n, mn, nz1, nz2 ; problem *Prob ; Prob = cs_ci_calloc (1, sizeof (problem)) ; if (!Prob) return (NULL) ; T = cs_ci_load (f) ; /* load triplet matrix T from a file */ Prob->A = A = cs_ci_compress (T) ; /* A = compressed-column form of T */ cs_ci_spfree (T) ; /* clear T */ if (!cs_ci_dupl (A)) return (free_problem (Prob)) ; /* sum up duplicates */ Prob->sym = sym = is_sym (A) ; /* determine if A is symmetric */ m = A->m ; n = A->n ; mn = CS_MAX (m,n) ; nz1 = A->p [n] ; cs_ci_dropzeros (A) ; /* drop zero entries */ nz2 = A->p [n] ; if (tol > 0) cs_ci_droptol (A, tol) ; /* drop tiny entries (just to test) */ Prob->C = C = sym ? make_sym (A) : A ; /* C = A + triu(A,1)', or C=A */ if (!C) return (free_problem (Prob)) ; printf ("\n--- Matrix: %d-by-%d, nnz: %d (sym: %d: nnz %d), norm: %8.2e\n", m, n, A->p [n], sym, sym ? C->p [n] : 0, cs_ci_norm (C)) ; if (nz1 != nz2) printf ("zero entries dropped: %d\n", nz1 - nz2) ; if (nz2 != A->p [n]) printf ("tiny entries dropped: %d\n", nz2 - A->p [n]) ; Prob->b = cs_ci_malloc (mn, sizeof (cs_complex_t)) ; Prob->x = cs_ci_malloc (mn, sizeof (cs_complex_t)) ; Prob->resid = cs_ci_malloc (mn, sizeof (cs_complex_t)) ; return ((!Prob->b || !Prob->x || !Prob->resid) ? free_problem (Prob) : Prob) ; }