Read .txt file , Truss optimization

2 visualizaciones (últimos 30 días)
Rita Akonobi
Rita Akonobi el 5 de Nov. de 2020
Comentada: Rita Akonobi el 5 de Nov. de 2020
Hi all, Please I have been stuck trying to figure out why my code does not read some part of my file. It doesn't read the NODE_COORDINATES. I have attached the .txt file and the code. NOTE: If I run other files with the code, it works, so i'm pretty sure the error is in the .txt file but I just can't find it
TITLE = test_22
ELEMENTS = 22
1 1 2 0.04 2.281e-4
2 2 3 0.04 2.281e-4
3 3 4 0.04 2.281e-4
4 4 5 0.04 2.281e-4
5 5 6 0.04 1.902e-4
6 6 7 0.04 1.902e-4
7 1 7 0.04 2.281e-4
8 2 6 0.04 2.281e-4
9 3 5 0.04 2.281e-4
10 2 7 0.04 2.281e-4
11 3 6 0.04 2.281e-4
12 1 6 0.04 2.281e-4
13 2 5 0.04 2.281e-4
14 1 8 0.04 1.902e-4
15 8 9 0.04 1.902e-4
16 9 10 0.04 2.281e-4
17 10 11 0.04 2.281e-4
18 11 12 0.04 2.281e-4
19 7 12 0.04 2.281e-4
20 8 12 0.04 2.281e-4
21 9 11 0.04 2.281e-4
22 7 8 0.04 2.281e-4
23 9 12 0.04 2.281e-4
24 1 12 0.04 1.902e-4
25 8 11 0.04 1.902e-4
NODE_COORDINATES = 12
1 0.00 0.00
2 -5.00 0.00
3 -10.00 0.00
4 -15.00 0.00
5 -10.00 3.33
6 -5.00 5.33
7 0.00 6.00
8 5.0 0.00
9 10.00 0.00
10 15.00 0.00
11 10.00 3.33
12 5.00 5.33
NODES_WITH_PRESCRIBED_DISPLACEMENTS = 2
4 01 0.000 0.000
10 11 0.000 0.000
YOUNG_MODULUS = 200000000000.0
YIELD_STRESS = 130000.0
DENSITY = 7860
THRESH_HOLD_FREQUENCY = 2.5
NODES_WITH_POINT_LOAD = 5
1 0 -300
2 0 -300
3 0 -300
8 0 -300
9 0 -300
PLOTTING_AMPLIFICATION_FACTOR = 50
NODES_WITH_VARIABLE_COORDINATES = 5
5 01 -15.0 1.0 -5.0 6.0
6 01 -10.0 2.0 0.0 10.0
7 01 -5.0 2.5 5.0 12.0
11 01 5.0 1.0 15.0 6.0
12 01 .2.50 2.0 10.0 10.0
DEFLECTION_LIMIT = 4
1 11 0.0508 0.0508
2 11 0.0508 0.0508
3 11 0.0508 0.0508
9 11 0.0508 0.0508
function [coorddofvar,fixeddata] = truss2D_readdata(filename)
%
% Set some basic variables
% ========================
% Number of degrees of freedom per node
ndofn=2;
% Number of nodes of the element
nnode=2;
%
% Read information from data file
% ===============================
%
fid = fopen(filename, 'r');
title = fscanf(fid, 'TITLE = %s',1);
%
% Total number of elements in the mesh
nelem = fscanf(fid, '\nELEMENTS = %d', 1);
%
%Read Table of connectivities
%
lnods = fscanf(fid, '\n%d %d %d %f %f', [nnode+3,nelem]);
lnods = lnods';
sortrows(lnods,1);
%...cross-sectional areas
csarea = lnods(:,4);
%...second moment of areas
sndmoa = lnods(:,5);
%...store only connectivities in lnods
lnods = lnods(:,2:nnode+1);
%...create table of element global degrees of freedom
eldofX = lnods*ndofn-1;
eldofY = lnods*ndofn;
eldofs = [ eldofX(:,1) eldofY(:,1) eldofX(:,2) eldofY(:,2) ]
%
% Read Nodal coordinates
%
npoin = fscanf(fid, '\nNODE_COORDINATES = %d', 1)
coord = fscanf(fid, '\n%d %f %f', [3, npoin])
coord = coord';sortrows(coord,1);nodnumbers=coord(:,1)
coord = coord(:,2:3);
%...create table of nodal degrees of freedom
nodofs = [ nodnumbers*ndofn-1 nodnumbers*ndofn ];
%
%
% Read Prescribed displacements
%
nnodefix = fscanf(fid,'\nNODES_WITH_PRESCRIBED_DISPLACEMENTS = %d',1);
ipresc = fscanf(fid, '\n%d %d %f %f', [2+ndofn, nnodefix]);
ipresc = ipresc';fixednodes=ipresc(:,1);
%...create tables of fixed dofs and corresponding prescribed values
ifdoffix = zeros(npoin*ndofn,1);
icount = 0;
for inodefix = 1:nnodefix
ipoin=ipresc(inodefix,1);
dofX=nodofs(ipoin,1);
dofY=nodofs(ipoin,2);
if ipresc(inodefix,2)==11
ifdoffix(dofX)=1;
ifdoffix(dofY)=1 ;
icount = icount+1;
valuedoffix(icount) = ipresc(inodefix,3);
fixeddoftable(icount) = dofX;
icount = icount+1;
valuedoffix(icount) = ipresc(inodefix,4);
fixeddoftable(icount) = dofY;
elseif ipresc(inodefix,2)==1
ifdoffix(dofY)=1;
icount = icount+1;
valuedoffix(icount) = ipresc(inodefix,4);
fixeddoftable(icount) = dofY;
elseif ipresc(inodefix,2)==10
ifdoffix(dofX)=1;
icount = icount+1;
valuedoffix(icount) = ipresc(inodefix,3);
fixeddoftable(icount) = dofX;
elseif ipresc(inodefix,2)==0
else
error('Wrong displacement prescription code in data file')
end
end
%...create table of free dofs by subtracting the set of fixed dofs
% from the set of all dofs
ngdof=npoin*ndofn
alldoftable=[1:ngdof]'
freedoftable = setxor(alldoftable,fixeddoftable);
%
% Read Material properties
%
matprop.young = fscanf(fid, '\nYOUNG_MODULUS = %f', 1);
matprop.yield = fscanf(fid, '\nYIELD _STRESS = %f', 1);
%added by Ritamatprop
matprop.density = fscanf(fid, '\nDENSITY = %f', 1);
matprop.frequency = fscanf(fid,'\nTHRESH_HOLD_FREQUENCY = %f', 1);
  2 comentarios
Naif Alsalem
Naif Alsalem el 5 de Nov. de 2020
It would be helpful if you just attached the .txt file so that we can try it out by your code.
Rita Akonobi
Rita Akonobi el 5 de Nov. de 2020
Hi, thank you for your response. I will attach the data file and the whole code here. I also noticed another part of the code not reading is the DEFLECTION_LIMIT, the last section of the data file
%***********************************************************************
function [coorddofvar,fixeddata] = truss2D_readdata(filename)
%
% Set some basic variables
% ========================
% Number of degrees of freedom per node
ndofn=2;
% Number of nodes of the element
nnode=2;
%
% Read information from data file
% ===============================
%
fid = fopen(filename, 'r');
title = fscanf(fid, 'TITLE = %s',1);
%
% Total number of elements in the mesh
nelem = fscanf(fid, '\nELEMENTS = %d', 1);
%
%Read Table of connectivities
%
lnods = fscanf(fid, '\n%d %d %d %f %f', [nnode+3,nelem]);
lnods = lnods';
sortrows(lnods,1);
%...cross-sectional areas
csarea = lnods(:,4);
%...second moment of areas
sndmoa = lnods(:,5);
%...store only connectivities in lnods
lnods = lnods(:,2:nnode+1);
%...create table of element global degrees of freedom
eldofX = lnods*ndofn-1;
eldofY = lnods*ndofn;
eldofs = [ eldofX(:,1) eldofY(:,1) eldofX(:,2) eldofY(:,2) ];
%
% Read Nodal coordinates
%
npoin = fscanf(fid, '\nNODE_COORDINATES = %d', 1);
coord = fscanf(fid, '\n%d %f %f', [3, npoin]);
coord = coord';sortrows(coord,1);nodnumbers=coord(:,1);
coord = coord(:,2:3);
%...create table of nodal degrees of freedom
nodofs = [ nodnumbers*ndofn-1 nodnumbers*ndofn ];
%
%
% Read Prescribed displacements
%
nnodefix = fscanf(fid,'\nNODES_WITH_PRESCRIBED_DISPLACEMENTS = %d',1);
ipresc = fscanf(fid, '\n%d %d %f %f', [2+ndofn, nnodefix]);
ipresc = ipresc';fixednodes=ipresc(:,1);
%...create tables of fixed dofs and corresponding prescribed values
ifdoffix = zeros(npoin*ndofn,1);
icount = 0;
for inodefix = 1:nnodefix
ipoin=ipresc(inodefix,1);
dofX=nodofs(ipoin,1);
dofY=nodofs(ipoin,2);
if ipresc(inodefix,2)==11
ifdoffix(dofX)=1;
ifdoffix(dofY)=1 ;
icount = icount+1;
valuedoffix(icount) = ipresc(inodefix,3);
fixeddoftable(icount) = dofX;
icount = icount+1;
valuedoffix(icount) = ipresc(inodefix,4);
fixeddoftable(icount) = dofY;
elseif ipresc(inodefix,2)==1
ifdoffix(dofY)=1;
icount = icount+1;
valuedoffix(icount) = ipresc(inodefix,4);
fixeddoftable(icount) = dofY;
elseif ipresc(inodefix,2)==10
ifdoffix(dofX)=1;
icount = icount+1;
valuedoffix(icount) = ipresc(inodefix,3);
fixeddoftable(icount) = dofX;
elseif ipresc(inodefix,2)==0
else
error('Wrong displacement prescription code in data file')
end
end
%...create table of free dofs by subtracting the set of fixed dofs
% from the set of all dofs
ngdof=npoin*ndofn;
alldoftable=[1:ngdof]';
freedoftable = setxor(alldoftable,fixeddoftable);
%
% Read Material properties
%
matprop.young = fscanf(fid, '\nYOUNG_MODULUS = %f', 1);
matprop.yield = fscanf(fid, '\nYIELD _STRESS = %f', 1);
%added by Ritamatprop
matprop.density = fscanf(fid, '\nDENSITY = %f', 1);
matprop.frequency = fscanf(fid,'\nTHRESH_HOLD_FREQUENCY = %f', 1);
%
%Read Load vector (point loads only)
%
npload = fscanf(fid,'\nNODES_WITH_POINT_LOAD = %d',1);
pload = fscanf(fid, '\n%d %f %f', [1+ndofn, npload]);
pload = pload';
%...add point loads to global load vector
F=zeros(ngdof,1);
loadednodes=pload(:,1);
loadeddofs=nodofs(loadednodes,:);
F(loadeddofs) = pload(:,2:3);
%
% Reads deformation amplification factor (for plotting only)
amplfact = fscanf(fid,'\nPLOTTING_AMPLIFICATION_FACTOR = %d',1);
%
% Reads upper and lower bounds of variable coordinates to be optimised
%
nnodevar = fscanf(fid, '\nNODES_WITH_VARIABLE_COORDINATES = %d', 1);
ivar = fscanf(fid, '\n%d %d %f %f %f %f', [2+2*ndofn, nnodevar]);
ivar = ivar';
varnodes=ivar(:,1);%...create tables of variable coordinates and corresponding prescribed
% upper and lower bounds
ifcoordvar = zeros(npoin*ndofn,1); icount = 0;
upperbound=[];
lowerbound=[];
dofcoordvar=[];
for inodevar = 1:nnodevar
ipoin=ivar(inodevar,1);
dofX=nodofs(ipoin,1);
dofY=nodofs(ipoin,2);
if ivar(inodevar,2)==11
% both x- and y-coordinates are variable
ifcoordvar(dofX)=1;
ifcoordvar(dofY)=1;
icount = icount+1;
lowerbound(icount) = ivar(inodevar,3);
upperbound(icount) = ivar(inodevar,5);
dofcoordvar(icount) = dofX;
icount = icount+1;
lowerbound(icount) = ivar(inodevar,4);
upperbound(icount) = ivar(inodevar,6);
dofcoordvar(icount) = dofY;
elseif ivar(inodevar,2)==1
% only y-coordinate is variable
ifcoordvar(dofY)=1;
icount = icount+1;
lowerbound(icount) = ivar(inodevar,4);
upperbound(icount) = ivar(inodevar,6);
dofcoordvar(icount) = dofY;
elseif ivar(inodevar,2)==10
% only x-coordinate is variable
ifcoordvar(dofX)=1;
icount = icount+1;
lowerbound(icount) = ivar(inodevar,3);
upperbound(icount) = ivar(inodevar,5);
dofcoordvar(icount) = dofX;
elseif ivar(inodevar,2)==0
disp('WARNING: Node listed with no variable coordinates for optimisation process')
else
error('Wrong variable coordinate prescription code in data file')
end
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% RITA CODE STARTS FROM HERE
%Read deflection limit from data file
nnodelimit = fscanf(fid,'\nDEFLECTION_LIMIT = %d',1);
ilimit = fscanf(fid, '\n%d %d %f %f', [2+ndofn, nnodelimit]);
ilimit = ilimit';limitednodes=ilimit(:,1);
%...create tables of fixed dofs and corresponding prescribed values
ifdoflimit = zeros(npoin*ndofn,1); iicount = 0;
for inodelimit = 1:nnodelimit
ipoin=ilimit(inodelimit,1);
dofX=nodofs(ipoin,1);
dofY=nodofs(ipoin,2);
if ilimit(inodelimit,2)==11
ifdoflimit(dofX)=1;
ifdoflimit(dofY)=1;
iicount = iicount+1;
valuedoflimit(iicount)= ilimit(inodelimit,3);
limiteddoftable(iicount) = dofX;
iicount = iicount+1;
valuedoflimit(iicount) = ilimit(inodelimit,4);
limiteddoftable(iicount)= dofY;
elseif ilimit(inodelimit,2)==1
ifdoflimit(dofY)=1;
iicount = iicount+1;
valuedoflimit(iicount)= ilimit(inodelimit,4);
limiteddoftable(iicount) = dofY;
elseif ilimit(inodefix,2)==10
ifdoflimit(dofX)=1;
iicount = iicount+1;
valuedoflimit(iicount) = ilimit(inodelimit,3);
limiteddoftable(iicount) = dofX;
elseif ilimit(inodelimit,2)==0
else error('Deflection limitaion value is not inserted')
end
end
%************************************************************************
%
% IMPORTANT
% (DEFINIFION OF DESIGN VARIABLES)
%
% set of dofs associated with variable coordinates
dofcoordfix=setxor(alldoftable,dofcoordvar);
if isempty(dofcoordvar)
disp('No variables in optimisation problem - all nodes have fixed coordinates')
error('No variables in optimisation problem - all nodes have fixed coordinates')
end
%
% arrange coordinates following dof ordering (in one-dimensional array, as
% in global displacement vector)
coorddof=zeros(ngdof,1);
for ipoin=1:npoin
dofx=ipoin*2-1;
dofy=ipoin*2;
coorddof(dofx)=coord(ipoin,1);
coorddof(dofy)=coord(ipoin,2);
end
% split coordinates array into variable and fixed coordinates
coorddofvar=coorddof(dofcoordvar);
coorddoffix=coorddof(dofcoordfix);
%AppRita
coorddoflimit=coorddof(alldoftable);
%x = coorddofvar
%************************************************************************
%
% Store fixed data in structure
fixeddata.amplfact=amplfact;
fixeddata.coorddoffix=coorddoffix;
fixeddata.csarea=csarea;
fixeddata.sndmoa=sndmoa;
fixeddata.dofcoordfix=dofcoordfix;
fixeddata.dofcoordvar=dofcoordvar;
fixeddata.eldofs=eldofs;
fixeddata.F=F;
fixeddata.fixeddoftable=fixeddoftable;
fixeddata.freedoftable=freedoftable;
fixeddata.lnods=lnods;
fixeddata.lowerbound=lowerbound;
fixeddata.matprop=matprop;
fixeddata.ndofn=ndofn;
fixeddata.nelem=nelem;
fixeddata.ngdof=ngdof;
fixeddata.npoin=npoin;
fixeddata.upperbound=upperbound;
fixeddata.valuedoffix=valuedoffix;
%fixeddata.coorddoflimit=coorddoflimit; %appended by Rita
%fixeddata.coorddoflimit=coorddoflimit;
fixeddata.ifdoflimit=ifdoflimit;
fixeddata.limiteddoftable=limiteddoftable;
fixeddata.valuedoflimit=valuedoflimit;
fixeddata.csarea=csarea;
fixeddata.sndmoa=sndmoa;
% Close file(s)
status = fclose(fid);
%
end

Iniciar sesión para comentar.

Respuesta aceptada

Sylvain
Sylvain el 5 de Nov. de 2020
The issue is not in your code, but in your text file, line 71:
12 01 .2.50 2.0 10.0 10.0
Must be converted to :
12 01 2.50 2.0 10.0 10.0
  1 comentario
Rita Akonobi
Rita Akonobi el 5 de Nov. de 2020
Thank you sylvian... just a typo error kept me up all night. I wonder how I couldn't find it. Thanks a lot

Iniciar sesión para comentar.

Más respuestas (0)

Categorías

Más información sobre Transportation Engineering en Help Center y File Exchange.

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by